config_json.go 715 B

12345678910111213141516171819202122232425262728293031323334
  1. // +build json
  2. package dns
  3. import (
  4. "encoding/json"
  5. v2net "v2ray.com/core/common/net"
  6. )
  7. func (this *Config) UnmarshalJSON(data []byte) error {
  8. type JsonConfig struct {
  9. Servers []*v2net.AddressPB `json:"servers"`
  10. Hosts map[string]*v2net.AddressPB `json:"hosts"`
  11. }
  12. jsonConfig := new(JsonConfig)
  13. if err := json.Unmarshal(data, jsonConfig); err != nil {
  14. return err
  15. }
  16. this.NameServers = make([]*v2net.DestinationPB, len(jsonConfig.Servers))
  17. for idx, server := range jsonConfig.Servers {
  18. this.NameServers[idx] = &v2net.DestinationPB{
  19. Network: v2net.Network_UDP,
  20. Address: server,
  21. Port: 53,
  22. }
  23. }
  24. if jsonConfig.Hosts != nil {
  25. this.Hosts = jsonConfig.Hosts
  26. }
  27. return nil
  28. }