config_json.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // +build json
  2. package socks
  3. import (
  4. "encoding/json"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/proxy/internal"
  8. "github.com/v2ray/v2ray-core/proxy/internal/config"
  9. )
  10. const (
  11. AuthMethodNoAuth = "noauth"
  12. AuthMethodUserPass = "password"
  13. )
  14. func init() {
  15. config.RegisterInboundConfig("socks",
  16. func(data []byte) (interface{}, error) {
  17. type SocksAccount struct {
  18. Username string `json:"user"`
  19. Password string `json:"pass"`
  20. }
  21. type SocksConfig struct {
  22. AuthMethod string `json:"auth"`
  23. Accounts []*SocksAccount `json:"accounts"`
  24. UDP bool `json:"udp"`
  25. Host *v2net.AddressJson `json:"ip"`
  26. }
  27. rawConfig := new(SocksConfig)
  28. if err := json.Unmarshal(data, rawConfig); err != nil {
  29. return nil, err
  30. }
  31. socksConfig := new(Config)
  32. if rawConfig.AuthMethod == AuthMethodNoAuth {
  33. socksConfig.AuthType = AuthTypeNoAuth
  34. } else if rawConfig.AuthMethod == AuthMethodUserPass {
  35. socksConfig.AuthType = AuthTypePassword
  36. } else {
  37. log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
  38. return nil, internal.ErrorBadConfiguration
  39. }
  40. if len(rawConfig.Accounts) > 0 {
  41. socksConfig.Accounts = make(map[string]string, len(rawConfig.Accounts))
  42. for _, account := range rawConfig.Accounts {
  43. socksConfig.Accounts[account.Username] = account.Password
  44. }
  45. }
  46. socksConfig.UDPEnabled = rawConfig.UDP
  47. if rawConfig.Host != nil {
  48. socksConfig.Address = rawConfig.Host.Address
  49. } else {
  50. socksConfig.Address = v2net.IPAddress([]byte{127, 0, 0, 1})
  51. }
  52. return socksConfig, nil
  53. })
  54. }