config_json.go 906 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // +build json
  2. package dns
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "net"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. func (this *Config) UnmarshalJSON(data []byte) error {
  10. type JsonConfig struct {
  11. Servers []v2net.AddressJson `json:"servers"`
  12. Hosts map[string]v2net.AddressJson `json:"hosts"`
  13. }
  14. jsonConfig := new(JsonConfig)
  15. if err := json.Unmarshal(data, jsonConfig); err != nil {
  16. return err
  17. }
  18. this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers))
  19. for idx, server := range jsonConfig.Servers {
  20. this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53))
  21. }
  22. if jsonConfig.Hosts != nil {
  23. this.Hosts = make(map[string]net.IP)
  24. for domain, ip := range jsonConfig.Hosts {
  25. if ip.Address.Family().IsDomain() {
  26. return errors.New(ip.Address.String() + " is not an IP.")
  27. }
  28. this.Hosts[domain] = ip.Address.IP()
  29. }
  30. }
  31. return nil
  32. }