2015-09-25 19:00:51 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2015-11-03 21:09:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-10-13 22:04:49 +00:00
|
|
|
"net"
|
|
|
|
|
2015-11-03 21:09:07 +00:00
|
|
|
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
2015-09-25 19:00:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AuthMethodNoAuth = "noauth"
|
|
|
|
AuthMethodUserPass = "password"
|
|
|
|
)
|
|
|
|
|
2015-10-10 13:51:35 +00:00
|
|
|
type SocksAccount struct {
|
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
|
|
|
}
|
|
|
|
|
2015-11-03 21:09:07 +00:00
|
|
|
type SocksAccountMap map[string]string
|
2015-10-10 13:51:35 +00:00
|
|
|
|
2015-11-03 21:09:07 +00:00
|
|
|
func (this *SocksAccountMap) UnmarshalJSON(data []byte) error {
|
|
|
|
var accounts []SocksAccount
|
|
|
|
err := json.Unmarshal(data, &accounts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*this = make(map[string]string)
|
|
|
|
for _, account := range accounts {
|
|
|
|
(*this)[account.Username] = account.Password
|
|
|
|
}
|
|
|
|
return nil
|
2015-09-25 19:00:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 21:09:07 +00:00
|
|
|
type IPAddress net.IP
|
2015-10-13 22:04:49 +00:00
|
|
|
|
2015-11-03 21:09:07 +00:00
|
|
|
func (this *IPAddress) UnmarshalJSON(data []byte) error {
|
|
|
|
var ipStr string
|
|
|
|
err := json.Unmarshal(data, &ipStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-10 13:51:35 +00:00
|
|
|
}
|
2015-11-03 21:09:07 +00:00
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
if ip == nil {
|
|
|
|
return errors.New("Unknown IP format: " + ipStr)
|
|
|
|
}
|
|
|
|
*this = IPAddress(ip)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SocksConfig struct {
|
|
|
|
AuthMethod string `json:"auth"`
|
|
|
|
Accounts SocksAccountMap `json:"accounts"`
|
|
|
|
UDPEnabled bool `json:"udp"`
|
|
|
|
HostIP IPAddress `json:"ip"`
|
2015-10-10 13:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 22:04:49 +00:00
|
|
|
func (sc *SocksConfig) IsNoAuth() bool {
|
|
|
|
return sc.AuthMethod == AuthMethodNoAuth
|
2015-09-25 19:00:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 22:04:49 +00:00
|
|
|
func (sc *SocksConfig) IsPassword() bool {
|
|
|
|
return sc.AuthMethod == AuthMethodUserPass
|
2015-09-25 19:00:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 22:04:49 +00:00
|
|
|
func (sc *SocksConfig) HasAccount(user, pass string) bool {
|
2015-11-03 21:09:07 +00:00
|
|
|
if actualPass, found := sc.Accounts[user]; found {
|
2015-10-10 13:51:35 +00:00
|
|
|
return actualPass == pass
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-13 22:04:49 +00:00
|
|
|
func (sc *SocksConfig) IP() net.IP {
|
2015-11-03 21:09:07 +00:00
|
|
|
return net.IP(sc.HostIP)
|
2015-10-13 22:04:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 21:11:08 +00:00
|
|
|
func init() {
|
2015-11-03 21:09:07 +00:00
|
|
|
jsonconfig.RegisterInboundConnectionConfig("socks", func() interface{} {
|
|
|
|
return &SocksConfig{
|
|
|
|
HostIP: IPAddress(net.IPv4(127, 0, 0, 1)),
|
|
|
|
}
|
2015-10-06 21:11:08 +00:00
|
|
|
})
|
2015-09-25 19:00:51 +00:00
|
|
|
}
|