2016-07-26 08:23:02 +00:00
|
|
|
// +build json
|
|
|
|
|
2016-07-25 21:45:25 +00:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/proxy/registry"
|
2016-07-25 21:45:25 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 22:41:21 +00:00
|
|
|
func (this *Account) UnmarshalJSON(data []byte) error {
|
|
|
|
type JsonConfig struct {
|
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
|
|
|
}
|
|
|
|
jsonConfig := new(JsonConfig)
|
|
|
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
|
|
|
return errors.New("Socks: Failed to parse account: " + err.Error())
|
|
|
|
}
|
|
|
|
this.Username = jsonConfig.Username
|
|
|
|
this.Password = jsonConfig.Password
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-25 21:45:25 +00:00
|
|
|
func (this *ClientConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
type ServerConfig struct {
|
2016-08-26 22:04:35 +00:00
|
|
|
Address *v2net.AddressPB `json:"address"`
|
|
|
|
Port v2net.Port `json:"port"`
|
|
|
|
Users []json.RawMessage `json:"users"`
|
2016-07-25 21:45:25 +00: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())
|
|
|
|
}
|
2016-09-25 20:54:18 +00:00
|
|
|
this.Server = make([]*protocol.ServerSpecPB, len(jsonConfig.Servers))
|
2016-07-25 21:45:25 +00:00
|
|
|
for idx, serverConfig := range jsonConfig.Servers {
|
2016-09-25 20:54:18 +00:00
|
|
|
server := &protocol.ServerSpecPB{
|
|
|
|
Address: serverConfig.Address,
|
|
|
|
Port: uint32(serverConfig.Port),
|
|
|
|
}
|
2016-07-25 21:45:25 +00: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())
|
|
|
|
}
|
2016-09-17 22:41:21 +00:00
|
|
|
anyAccount, err := account.AsAny()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Account = anyAccount
|
2016-09-25 20:54:18 +00:00
|
|
|
server.Users = append(server.Users, user)
|
2016-07-25 21:45:25 +00:00
|
|
|
}
|
2016-09-25 20:54:18 +00:00
|
|
|
this.Server[idx] = server
|
2016-07-25 21:45:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-08-17 21:30:15 +00:00
|
|
|
registry.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) })
|
2016-07-25 21:45:25 +00:00
|
|
|
}
|