socks.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package conf
  2. import (
  3. "encoding/json"
  4. "github.com/xtls/xray-core/common/errors"
  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. "google.golang.org/protobuf/proto"
  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. 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. // errors.New("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.UserLevel = v.UserLevel
  53. return config, nil
  54. }
  55. type SocksRemoteConfig struct {
  56. Address *Address `json:"address"`
  57. Port uint16 `json:"port"`
  58. Users []json.RawMessage `json:"users"`
  59. }
  60. type SocksClientConfig struct {
  61. Servers []*SocksRemoteConfig `json:"servers"`
  62. }
  63. func (v *SocksClientConfig) Build() (proto.Message, error) {
  64. config := new(socks.ClientConfig)
  65. config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
  66. for idx, serverConfig := range v.Servers {
  67. server := &protocol.ServerEndpoint{
  68. Address: serverConfig.Address.Build(),
  69. Port: uint32(serverConfig.Port),
  70. }
  71. for _, rawUser := range serverConfig.Users {
  72. user := new(protocol.User)
  73. if err := json.Unmarshal(rawUser, user); err != nil {
  74. return nil, errors.New("failed to parse Socks user").Base(err).AtError()
  75. }
  76. account := new(SocksAccount)
  77. if err := json.Unmarshal(rawUser, account); err != nil {
  78. return nil, errors.New("failed to parse socks account").Base(err).AtError()
  79. }
  80. user.Account = serial.ToTypedMessage(account.Build())
  81. server.User = append(server.User, user)
  82. }
  83. config.Server[idx] = server
  84. }
  85. return config, nil
  86. }