socks.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package conf
  2. import (
  3. "encoding/json"
  4. "v2ray.com/core/common/errors"
  5. "v2ray.com/core/common/loader"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/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. }
  30. func (v *SocksServerConfig) Build() (*loader.TypedSettings, error) {
  31. config := new(socks.ServerConfig)
  32. if v.AuthMethod == AuthMethodNoAuth {
  33. config.AuthType = socks.AuthType_NO_AUTH
  34. } else if v.AuthMethod == AuthMethodUserPass {
  35. config.AuthType = socks.AuthType_PASSWORD
  36. } else {
  37. return nil, errors.New("Unknown socks auth method: " + v.AuthMethod)
  38. }
  39. if len(v.Accounts) > 0 {
  40. config.Accounts = make(map[string]string, len(v.Accounts))
  41. for _, account := range v.Accounts {
  42. config.Accounts[account.Username] = account.Password
  43. }
  44. }
  45. config.UdpEnabled = v.UDP
  46. if v.Host != nil {
  47. config.Address = v.Host.Build()
  48. }
  49. config.Timeout = v.Timeout
  50. return loader.NewTypedSettings(config), nil
  51. }
  52. type SocksRemoteConfig struct {
  53. Address *Address `json:"address"`
  54. Port uint16 `json:"port"`
  55. Users []json.RawMessage `json:"users"`
  56. }
  57. type SocksClientConfig struct {
  58. Servers []*SocksRemoteConfig `json:"servers"`
  59. }
  60. func (v *SocksClientConfig) Build() (*loader.TypedSettings, error) {
  61. config := new(socks.ClientConfig)
  62. config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
  63. for idx, serverConfig := range v.Servers {
  64. server := &protocol.ServerEndpoint{
  65. Address: serverConfig.Address.Build(),
  66. Port: uint32(serverConfig.Port),
  67. }
  68. for _, rawUser := range serverConfig.Users {
  69. user := new(protocol.User)
  70. if err := json.Unmarshal(rawUser, user); err != nil {
  71. return nil, errors.New("Socks|Client: Failed to parse user: " + err.Error())
  72. }
  73. account := new(SocksAccount)
  74. if err := json.Unmarshal(rawUser, account); err != nil {
  75. return nil, errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
  76. }
  77. user.Account = loader.NewTypedSettings(account.Build())
  78. server.User = append(server.User, user)
  79. }
  80. config.Server[idx] = server
  81. }
  82. return loader.NewTypedSettings(config), nil
  83. }