v2ray-core/proxy/socks/server_config_json.go

63 lines
1.5 KiB
Go
Raw Normal View History

// +build json
package socks
import (
"encoding/json"
2016-06-11 20:52:37 +00:00
"errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy/registry"
)
const (
AuthMethodNoAuth = "noauth"
AuthMethodUserPass = "password"
)
2016-08-28 21:46:50 +00:00
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
2016-06-10 20:26:39 +00:00
type SocksConfig struct {
2016-08-26 22:04:35 +00:00
AuthMethod string `json:"auth"`
Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"`
Host *v2net.AddressPB `json:"ip"`
Timeout uint32 `json:"timeout"`
2016-06-10 20:26:39 +00:00
}
rawConfig := new(SocksConfig)
if err := json.Unmarshal(data, rawConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Socks: Failed to parse config: " + err.Error())
2016-06-10 20:26:39 +00:00
}
if rawConfig.AuthMethod == AuthMethodNoAuth {
2016-08-28 21:46:50 +00:00
this.AuthType = ServerConfig_NO_AUTH
2016-06-10 20:26:39 +00:00
} else if rawConfig.AuthMethod == AuthMethodUserPass {
2016-08-28 21:46:50 +00:00
this.AuthType = ServerConfig_PASSWORD
2016-06-10 20:26:39 +00:00
} else {
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
2016-06-10 20:26:39 +00:00
}
if len(rawConfig.Accounts) > 0 {
this.Accounts = make(map[string]string, len(rawConfig.Accounts))
for _, account := range rawConfig.Accounts {
this.Accounts[account.Username] = account.Password
}
}
2016-08-28 21:46:50 +00:00
this.UdpEnabled = rawConfig.UDP
2016-06-10 20:26:39 +00:00
if rawConfig.Host != nil {
2016-08-28 21:46:50 +00:00
this.Address = rawConfig.Host
2016-06-10 20:26:39 +00:00
}
if rawConfig.Timeout >= 0 {
this.Timeout = rawConfig.Timeout
}
2016-06-10 20:26:39 +00:00
return nil
}
func init() {
2016-08-28 21:46:50 +00:00
registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) })
}