Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package v4
 
import (
    "encoding/json"
    "strings"
 
    "github.com/golang/protobuf/proto"
 
    "github.com/v2fly/v2ray-core/v5/common/protocol"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
    "github.com/v2fly/v2ray-core/v5/proxy/socks"
)
 
type SocksAccount struct {
    Username string `json:"user"`
    Password string `json:"pass"`
}
 
func (v *SocksAccount) Build() *socks.Account {
    return &socks.Account{
        Username: v.Username,
        Password: v.Password,
    }
}
 
const (
    AuthMethodNoAuth   = "noauth"
    AuthMethodUserPass = "password"
)
 
type SocksServerConfig struct {
    AuthMethod string             `json:"auth"`
    Accounts   []*SocksAccount    `json:"accounts"`
    UDP        bool               `json:"udp"`
    Host       *cfgcommon.Address `json:"ip"`
    Timeout    uint32             `json:"timeout"`
    UserLevel  uint32             `json:"userLevel"`
}
 
func (v *SocksServerConfig) Build() (proto.Message, error) {
    config := new(socks.ServerConfig)
    switch v.AuthMethod {
    case AuthMethodNoAuth:
        config.AuthType = socks.AuthType_NO_AUTH
    case AuthMethodUserPass:
        config.AuthType = socks.AuthType_PASSWORD
    default:
        // newError("unknown socks auth method: ", v.AuthMethod, ". Default to noauth.").AtWarning().WriteToLog()
        config.AuthType = socks.AuthType_NO_AUTH
    }
 
    if len(v.Accounts) > 0 {
        config.Accounts = make(map[string]string, len(v.Accounts))
        for _, account := range v.Accounts {
            config.Accounts[account.Username] = account.Password
        }
    }
 
    config.UdpEnabled = v.UDP
    if v.Host != nil {
        config.Address = v.Host.Build()
    }
 
    config.Timeout = v.Timeout
    config.UserLevel = v.UserLevel
    return config, nil
}
 
type SocksRemoteConfig struct {
    Address *cfgcommon.Address `json:"address"`
    Port    uint16             `json:"port"`
    Users   []json.RawMessage  `json:"users"`
}
 
type SocksClientConfig struct {
    Servers []*SocksRemoteConfig `json:"servers"`
    Version string               `json:"version"`
}
 
func (v *SocksClientConfig) Build() (proto.Message, error) {
    config := new(socks.ClientConfig)
    config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
    switch strings.ToLower(v.Version) {
    case "4":
        config.Version = socks.Version_SOCKS4
    case "4a":
        config.Version = socks.Version_SOCKS4A
    case "", "5":
        config.Version = socks.Version_SOCKS5
    default:
        return nil, newError("failed to parse socks server version: ", v.Version).AtError()
    }
    for idx, serverConfig := range v.Servers {
        server := &protocol.ServerEndpoint{
            Address: serverConfig.Address.Build(),
            Port:    uint32(serverConfig.Port),
        }
        for _, rawUser := range serverConfig.Users {
            user := new(protocol.User)
            if err := json.Unmarshal(rawUser, user); err != nil {
                return nil, newError("failed to parse Socks user").Base(err).AtError()
            }
            account := new(SocksAccount)
            if err := json.Unmarshal(rawUser, account); err != nil {
                return nil, newError("failed to parse socks account").Base(err).AtError()
            }
            if config.Version != socks.Version_SOCKS5 && account.Password != "" {
                return nil, newError("password is only supported in socks5").AtError()
            }
            user.Account = serial.ToTypedMessage(account.Build())
            server.User = append(server.User, user)
        }
        config.Server[idx] = server
    }
    return config, nil
}