2015-09-25 15:00:51 -04:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2015-10-13 18:04:49 -04:00
|
|
|
"net"
|
|
|
|
|
2015-10-30 17:42:24 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
2015-09-25 15:00:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AuthMethodNoAuth = "noauth"
|
|
|
|
AuthMethodUserPass = "password"
|
|
|
|
)
|
|
|
|
|
2015-10-10 09:51:35 -04:00
|
|
|
type SocksAccount struct {
|
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:00:51 -04:00
|
|
|
type SocksConfig struct {
|
2015-10-10 09:51:35 -04:00
|
|
|
AuthMethod string `json:"auth"`
|
|
|
|
Accounts []SocksAccount `json:"accounts"`
|
|
|
|
UDPEnabled bool `json:"udp"`
|
2015-10-13 18:04:49 -04:00
|
|
|
HostIP string `json:"ip"`
|
2015-10-10 09:51:35 -04:00
|
|
|
|
|
|
|
accountMap map[string]string
|
2015-10-13 18:04:49 -04:00
|
|
|
ip net.IP
|
2015-09-25 15:00:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:04:49 -04:00
|
|
|
func (sc *SocksConfig) Initialize() {
|
|
|
|
sc.accountMap = make(map[string]string)
|
|
|
|
for _, account := range sc.Accounts {
|
|
|
|
sc.accountMap[account.Username] = account.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sc.HostIP) > 0 {
|
|
|
|
sc.ip = net.ParseIP(sc.HostIP)
|
|
|
|
if sc.ip == nil {
|
|
|
|
sc.ip = net.IPv4(127, 0, 0, 1)
|
|
|
|
}
|
2015-10-10 09:51:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:04:49 -04:00
|
|
|
func (sc *SocksConfig) IsNoAuth() bool {
|
|
|
|
return sc.AuthMethod == AuthMethodNoAuth
|
2015-09-25 15:00:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:04:49 -04:00
|
|
|
func (sc *SocksConfig) IsPassword() bool {
|
|
|
|
return sc.AuthMethod == AuthMethodUserPass
|
2015-09-25 15:00:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:04:49 -04:00
|
|
|
func (sc *SocksConfig) HasAccount(user, pass string) bool {
|
|
|
|
if actualPass, found := sc.accountMap[user]; found {
|
2015-10-10 09:51:35 -04:00
|
|
|
return actualPass == pass
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:04:49 -04:00
|
|
|
func (sc *SocksConfig) IP() net.IP {
|
|
|
|
return sc.ip
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:11:08 -04:00
|
|
|
func init() {
|
2015-10-30 17:42:24 -04:00
|
|
|
json.RegisterInboundConnectionConfig("socks", func() interface{} {
|
2015-10-06 17:11:08 -04:00
|
|
|
return new(SocksConfig)
|
|
|
|
})
|
2015-09-25 15:00:51 -04:00
|
|
|
}
|