config_json.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Timeout int `json:"timeout"`
  25. }
  26. rawConfig := new(SocksConfig)
  27. if err := json.Unmarshal(data, rawConfig); err != nil {
  28. return errors.New("Socks: Failed to parse config: " + err.Error())
  29. }
  30. if rawConfig.AuthMethod == AuthMethodNoAuth {
  31. this.AuthType = AuthTypeNoAuth
  32. } else if rawConfig.AuthMethod == AuthMethodUserPass {
  33. this.AuthType = AuthTypePassword
  34. } else {
  35. log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
  36. return internal.ErrBadConfiguration
  37. }
  38. if len(rawConfig.Accounts) > 0 {
  39. this.Accounts = make(map[string]string, len(rawConfig.Accounts))
  40. for _, account := range rawConfig.Accounts {
  41. this.Accounts[account.Username] = account.Password
  42. }
  43. }
  44. this.UDPEnabled = rawConfig.UDP
  45. if rawConfig.Host != nil {
  46. this.Address = rawConfig.Host.Address
  47. } else {
  48. this.Address = v2net.LocalHostIP
  49. }
  50. if rawConfig.Timeout >= 0 {
  51. this.Timeout = rawConfig.Timeout
  52. }
  53. return nil
  54. }
  55. func init() {
  56. internal.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
  57. }