2016-01-15 06:43:06 -05:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-11 16:52:37 -04:00
|
|
|
"errors"
|
2016-01-15 06:43:06 -05:00
|
|
|
|
2016-08-18 02:21:20 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common"
|
2016-01-15 06:43:06 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-08-17 17:30:15 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/registry"
|
2016-01-15 06:43:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AuthMethodNoAuth = "noauth"
|
|
|
|
AuthMethodUserPass = "password"
|
|
|
|
)
|
|
|
|
|
2016-06-10 16:26:39 -04:00
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
|
|
|
type SocksConfig struct {
|
|
|
|
AuthMethod string `json:"auth"`
|
2016-07-25 17:45:25 -04:00
|
|
|
Accounts []*Account `json:"accounts"`
|
2016-06-10 16:26:39 -04:00
|
|
|
UDP bool `json:"udp"`
|
|
|
|
Host *v2net.AddressJson `json:"ip"`
|
2016-07-11 09:46:18 -04:00
|
|
|
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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UDPEnabled = rawConfig.UDP
|
|
|
|
if rawConfig.Host != nil {
|
|
|
|
this.Address = rawConfig.Host.Address
|
|
|
|
} else {
|
|
|
|
this.Address = v2net.LocalHostIP
|
|
|
|
}
|
2016-07-11 09:46:18 -04:00
|
|
|
|
|
|
|
if rawConfig.Timeout >= 0 {
|
|
|
|
this.Timeout = rawConfig.Timeout
|
|
|
|
}
|
2016-06-10 16:26:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
func init() {
|
2016-08-17 17:30:15 -04:00
|
|
|
registry.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|