1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-05 00:47:51 -05:00
v2fly/proxy/socks/config/json/config.go

89 lines
1.8 KiB
Go
Raw Normal View History

package json
import (
2015-11-03 16:09:07 -05:00
"encoding/json"
"errors"
2015-10-13 18:04:49 -04:00
"net"
2015-11-03 16:09:07 -05:00
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
)
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-11-03 16:09:07 -05:00
type SocksAccountMap map[string]string
2015-10-10 09:51:35 -04:00
2015-11-03 16:09:07 -05: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-11-03 16:23:50 -05:00
func (this *SocksAccountMap) HasAccount(user, pass string) bool {
if actualPass, found := (*this)[user]; found {
return actualPass == pass
}
return false
}
2015-11-03 16:09:07 -05:00
type IPAddress net.IP
2015-10-13 18:04:49 -04:00
2015-11-03 16:09:07 -05:00
func (this *IPAddress) UnmarshalJSON(data []byte) error {
var ipStr string
err := json.Unmarshal(data, &ipStr)
if err != nil {
return err
2015-10-10 09:51:35 -04:00
}
2015-11-03 16:09:07 -05: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 09:51:35 -04:00
}
2015-10-13 18:04:49 -04:00
func (sc *SocksConfig) IsNoAuth() bool {
return sc.AuthMethod == AuthMethodNoAuth
}
2015-10-13 18:04:49 -04:00
func (sc *SocksConfig) IsPassword() bool {
return sc.AuthMethod == AuthMethodUserPass
}
2015-10-13 18:04:49 -04:00
func (sc *SocksConfig) HasAccount(user, pass string) bool {
2015-11-03 16:23:50 -05:00
return sc.Accounts.HasAccount(user, pass)
2015-10-10 09:51:35 -04:00
}
2015-10-13 18:04:49 -04:00
func (sc *SocksConfig) IP() net.IP {
2015-11-03 16:09:07 -05:00
return net.IP(sc.HostIP)
2015-10-13 18:04:49 -04:00
}
2015-10-06 17:11:08 -04:00
func init() {
2015-11-03 16:09:07 -05:00
jsonconfig.RegisterInboundConnectionConfig("socks", func() interface{} {
return &SocksConfig{
HostIP: IPAddress(net.IPv4(127, 0, 0, 1)),
}
2015-10-06 17:11:08 -04:00
})
}