Add socks4/4a support

This commit is contained in:
世界 2021-09-16 14:42:12 +08:00
parent 238bd5d050
commit 77d0419aca
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 154 additions and 33 deletions

View file

@ -2,6 +2,7 @@ package conf
import (
"encoding/json"
"strings"
"github.com/golang/protobuf/proto"
@ -73,11 +74,22 @@ type SocksRemoteConfig struct {
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(),
@ -92,6 +104,9 @@ func (v *SocksClientConfig) Build() (proto.Message, error) {
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)
}