1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 03:57:42 -05:00
v2fly/proxy/socks/config.go

102 lines
2.4 KiB
Go
Raw Normal View History

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"
2016-09-25 16:54:18 -04:00
"v2ray.com/core/common/protocol"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/proxy/registry"
2016-09-25 16:54:18 -04:00
"github.com/golang/protobuf/ptypes"
google_protobuf "github.com/golang/protobuf/ptypes/any"
)
2016-09-25 16:54:18 -04:00
func (this *Account) Equals(another protocol.Account) bool {
if account, ok := another.(*Account); ok {
return this.Username == account.Username
}
return false
}
func (this *Account) AsAccount() (protocol.Account, error) {
return this, nil
}
func NewAccount() protocol.AsAccount {
return &Account{}
}
func (this *Account) AsAny() (*google_protobuf.Any, error) {
return ptypes.MarshalAny(this)
}
func (this *ServerConfig) HasAccount(username, password string) bool {
if this.Accounts == nil {
return false
}
storedPassed, found := this.Accounts[username]
if !found {
return false
}
return storedPassed == password
}
func (this *ServerConfig) GetNetAddress() v2net.Address {
if this.Address == nil {
return v2net.LocalHostIP
}
return this.Address.AsAddress()
}
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"`
2016-10-12 12:43:55 -04:00
Host *v2net.IPOrDomain `json:"ip"`
2016-08-26 18:04:35 -04:00
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-09-25 16:54:18 -04:00
this.AuthType = AuthType_NO_AUTH
2016-06-10 16:26:39 -04:00
} else if rawConfig.AuthMethod == AuthMethodUserPass {
2016-09-25 16:54:18 -04:00
this.AuthType = AuthType_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) })
}