2016-07-26 04:23:02 -04:00
|
|
|
// +build json
|
|
|
|
|
2016-07-25 17:45:25 -04:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/proxy/registry"
|
2016-07-25 17:45:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (this *ClientConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
type ServerConfig struct {
|
2016-08-26 18:04:35 -04:00
|
|
|
Address *v2net.AddressPB `json:"address"`
|
|
|
|
Port v2net.Port `json:"port"`
|
|
|
|
Users []json.RawMessage `json:"users"`
|
2016-07-25 17:45:25 -04:00
|
|
|
}
|
|
|
|
type JsonConfig struct {
|
|
|
|
Servers []*ServerConfig `json:"servers"`
|
|
|
|
}
|
|
|
|
jsonConfig := new(JsonConfig)
|
|
|
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
|
|
|
return errors.New("Socks|Client: Failed to parse config: " + err.Error())
|
|
|
|
}
|
|
|
|
this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers))
|
|
|
|
for idx, serverConfig := range jsonConfig.Servers {
|
2016-08-26 18:04:35 -04:00
|
|
|
server := protocol.NewServerSpec(v2net.TCPDestination(serverConfig.Address.AsAddress(), serverConfig.Port), protocol.AlwaysValid())
|
2016-07-25 17:45:25 -04:00
|
|
|
for _, rawUser := range serverConfig.Users {
|
|
|
|
user := new(protocol.User)
|
|
|
|
if err := json.Unmarshal(rawUser, user); err != nil {
|
|
|
|
return errors.New("Socks|Client: Failed to parse user: " + err.Error())
|
|
|
|
}
|
|
|
|
account := new(Account)
|
|
|
|
if err := json.Unmarshal(rawUser, account); err != nil {
|
|
|
|
return errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
|
|
|
|
}
|
|
|
|
user.Account = account
|
|
|
|
server.AddUser(user)
|
|
|
|
}
|
|
|
|
this.Servers[idx] = server
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-08-17 17:30:15 -04:00
|
|
|
registry.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) })
|
2016-07-25 17:45:25 -04:00
|
|
|
}
|