github.com/eagleql/xray-core@v1.4.4/infra/conf/socks.go (about)

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