server_config_json.go 1.5 KB

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