socks.go 3.2 KB

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