1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/app/dns/config_json.go

39 lines
906 B
Go
Raw Normal View History

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