1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-20 04:04:15 -04:00
v2fly/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 16:52:37 -04:00
"errors"
2016-08-20 14:55:45 -04: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 17:46:50 -04:00
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
2016-06-10 16:26:39 -04:00
type SocksConfig struct {
2016-08-26 18:04:35 -04: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 16:26:39 -04:00
}
rawConfig := new(SocksConfig)
if err := json.Unmarshal(data, rawConfig); err != nil {
2016-06-11 16:52:37 -04:00
return errors.New("Socks: Failed to parse config: " + err.Error())
2016-06-10 16:26:39 -04:00
}
if rawConfig.AuthMethod == AuthMethodNoAuth {
2016-08-28 17:46:50 -04:00
this.AuthType = ServerConfig_NO_AUTH
2016-06-10 16:26:39 -04:00
} else if rawConfig.AuthMethod == AuthMethodUserPass {
2016-08-28 17:46:50 -04:00
this.AuthType = ServerConfig_PASSWORD
2016-06-10 16:26:39 -04:00
} else {
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
2016-08-18 02:21:20 -04:00
return common.ErrBadConfiguration
2016-06-10 16:26:39 -04: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 17:46:50 -04:00
this.UdpEnabled = rawConfig.UDP
2016-06-10 16:26:39 -04:00
if rawConfig.Host != nil {
2016-08-28 17:46:50 -04:00
this.Address = rawConfig.Host
2016-06-10 16:26:39 -04:00
}
if rawConfig.Timeout >= 0 {
this.Timeout = rawConfig.Timeout
}
2016-06-10 16:26:39 -04:00
return nil
}
func init() {
2016-08-28 17:46:50 -04:00
registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) })
}