socks.go 2.5 KB

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