1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 10:47:48 -05:00
v2fly/proxy/socks/config_json.go

69 lines
1.6 KiB
Go
Raw Normal View History

// +build json
package socks
import (
"encoding/json"
2016-06-11 16:52:37 -04:00
"errors"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/internal"
)
const (
AuthMethodNoAuth = "noauth"
AuthMethodUserPass = "password"
)
2016-06-10 16:26:39 -04:00
func (this *Config) UnmarshalJSON(data []byte) error {
type SocksAccount struct {
Username string `json:"user"`
Password string `json:"pass"`
}
type SocksConfig struct {
AuthMethod string `json:"auth"`
Accounts []*SocksAccount `json:"accounts"`
UDP bool `json:"udp"`
Host *v2net.AddressJson `json:"ip"`
Timeout int `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 {
this.AuthType = AuthTypeNoAuth
} else if rawConfig.AuthMethod == AuthMethodUserPass {
this.AuthType = AuthTypePassword
} else {
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
2016-06-27 02:53:35 -04:00
return internal.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
}
}
this.UDPEnabled = rawConfig.UDP
if rawConfig.Host != nil {
this.Address = rawConfig.Host.Address
} else {
this.Address = v2net.LocalHostIP
}
if rawConfig.Timeout >= 0 {
this.Timeout = rawConfig.Timeout
}
2016-06-10 16:26:39 -04:00
return nil
}
func init() {
2016-06-10 16:26:39 -04:00
internal.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
}