socks.go 2.6 KB

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