config_json.go 1.5 KB

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