config_json.go 863 B

1234567891011121314151617181920212223242526272829303132
  1. // +build json
  2. package dokodemo
  3. import (
  4. "encoding/json"
  5. "errors"
  6. v2net "v2ray.com/core/common/net"
  7. )
  8. func (this *Config) UnmarshalJSON(data []byte) error {
  9. type DokodemoConfig struct {
  10. Host *v2net.IPOrDomain `json:"address"`
  11. PortValue v2net.Port `json:"port"`
  12. NetworkList *v2net.NetworkList `json:"network"`
  13. TimeoutValue uint32 `json:"timeout"`
  14. Redirect bool `json:"followRedirect"`
  15. }
  16. rawConfig := new(DokodemoConfig)
  17. if err := json.Unmarshal(data, rawConfig); err != nil {
  18. return errors.New("Dokodemo: Failed to parse config: " + err.Error())
  19. }
  20. if rawConfig.Host != nil {
  21. this.Address = rawConfig.Host
  22. }
  23. this.Port = uint32(rawConfig.PortValue)
  24. this.NetworkList = rawConfig.NetworkList
  25. this.Timeout = rawConfig.TimeoutValue
  26. this.FollowRedirect = rawConfig.Redirect
  27. return nil
  28. }