1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/socks/server_config_json.go
2016-08-28 23:46:50 +02:00

63 lines
1.5 KiB
Go

// +build json
package socks
import (
"encoding/json"
"errors"
"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"
)
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
type SocksConfig struct {
AuthMethod string `json:"auth"`
Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"`
Host *v2net.AddressPB `json:"ip"`
Timeout uint32 `json:"timeout"`
}
rawConfig := new(SocksConfig)
if err := json.Unmarshal(data, rawConfig); err != nil {
return errors.New("Socks: Failed to parse config: " + err.Error())
}
if rawConfig.AuthMethod == AuthMethodNoAuth {
this.AuthType = ServerConfig_NO_AUTH
} else if rawConfig.AuthMethod == AuthMethodUserPass {
this.AuthType = ServerConfig_PASSWORD
} else {
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
return common.ErrBadConfiguration
}
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
}
}
this.UdpEnabled = rawConfig.UDP
if rawConfig.Host != nil {
this.Address = rawConfig.Host
}
if rawConfig.Timeout >= 0 {
this.Timeout = rawConfig.Timeout
}
return nil
}
func init() {
registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) })
}