config_json.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. )
  9. const (
  10. AuthMethodNoAuth = "noauth"
  11. AuthMethodUserPass = "password"
  12. )
  13. func (this *Config) UnmarshalJSON(data []byte) error {
  14. type SocksAccount struct {
  15. Username string `json:"user"`
  16. Password string `json:"pass"`
  17. }
  18. type SocksConfig struct {
  19. AuthMethod string `json:"auth"`
  20. Accounts []*SocksAccount `json:"accounts"`
  21. UDP bool `json:"udp"`
  22. Host *v2net.AddressJson `json:"ip"`
  23. }
  24. rawConfig := new(SocksConfig)
  25. if err := json.Unmarshal(data, rawConfig); err != nil {
  26. return err
  27. }
  28. if rawConfig.AuthMethod == AuthMethodNoAuth {
  29. this.AuthType = AuthTypeNoAuth
  30. } else if rawConfig.AuthMethod == AuthMethodUserPass {
  31. this.AuthType = AuthTypePassword
  32. } else {
  33. log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
  34. return internal.ErrorBadConfiguration
  35. }
  36. if len(rawConfig.Accounts) > 0 {
  37. this.Accounts = make(map[string]string, len(rawConfig.Accounts))
  38. for _, account := range rawConfig.Accounts {
  39. this.Accounts[account.Username] = account.Password
  40. }
  41. }
  42. this.UDPEnabled = rawConfig.UDP
  43. if rawConfig.Host != nil {
  44. this.Address = rawConfig.Host.Address
  45. } else {
  46. this.Address = v2net.LocalHostIP
  47. }
  48. return nil
  49. }
  50. func init() {
  51. internal.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
  52. }