socks.go 3.1 KB

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