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