socks.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package v4
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/v2fly/v2ray-core/v4/common/protocol"
  6. "github.com/v2fly/v2ray-core/v4/common/serial"
  7. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
  8. "github.com/v2fly/v2ray-core/v4/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 *cfgcommon.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 *cfgcommon.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. }
  65. func (v *SocksClientConfig) Build() (proto.Message, error) {
  66. config := new(socks.ClientConfig)
  67. config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
  68. for idx, serverConfig := range v.Servers {
  69. server := &protocol.ServerEndpoint{
  70. Address: serverConfig.Address.Build(),
  71. Port: uint32(serverConfig.Port),
  72. }
  73. for _, rawUser := range serverConfig.Users {
  74. user := new(protocol.User)
  75. if err := json.Unmarshal(rawUser, user); err != nil {
  76. return nil, newError("failed to parse Socks user").Base(err).AtError()
  77. }
  78. account := new(SocksAccount)
  79. if err := json.Unmarshal(rawUser, account); err != nil {
  80. return nil, newError("failed to parse socks account").Base(err).AtError()
  81. }
  82. user.Account = serial.ToTypedMessage(account.Build())
  83. server.User = append(server.User, user)
  84. }
  85. config.Server[idx] = server
  86. }
  87. return config, nil
  88. }