v2ray-core/app/dns/config_json.go

40 lines
915 B
Go
Raw Normal View History

2016-01-15 14:23:12 +00:00
// +build json
2016-05-16 07:25:34 +00:00
package dns
2016-01-15 14:23:12 +00:00
import (
"encoding/json"
2016-05-22 20:30:08 +00:00
"errors"
"net"
2016-01-15 14:23:12 +00:00
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-01-15 14:23:12 +00:00
)
2016-05-16 06:09:28 +00:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-08-26 22:04:35 +00:00
Servers []v2net.AddressPB `json:"servers"`
Hosts map[string]v2net.AddressPB `json:"hosts"`
2016-01-15 14:23:12 +00:00
}
2016-05-16 06:09:28 +00:00
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
2016-01-15 14:23:12 +00:00
}
2016-05-16 06:09:28 +00:00
this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers))
for idx, server := range jsonConfig.Servers {
2016-08-26 22:04:35 +00:00
this.NameServers[idx] = v2net.UDPDestination(server.AsAddress(), v2net.Port(53))
2016-01-15 14:23:12 +00:00
}
2016-05-16 06:09:28 +00:00
2016-05-22 20:30:08 +00:00
if jsonConfig.Hosts != nil {
this.Hosts = make(map[string]net.IP)
2016-08-26 22:04:35 +00:00
for domain, ipOrDomain := range jsonConfig.Hosts {
ip := ipOrDomain.GetIp()
if ip == nil {
return errors.New(ipOrDomain.AsAddress().String() + " is not an IP.")
2016-05-22 20:30:08 +00:00
}
2016-08-26 22:04:35 +00:00
this.Hosts[domain] = net.IP(ip)
2016-05-22 20:30:08 +00:00
}
}
2016-01-15 14:23:12 +00:00
return nil
}