Parse port in dns address

This commit is contained in:
世界 2021-10-13 13:37:46 +08:00
parent 710b283204
commit 486f96838d
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4

View File

@ -3,6 +3,7 @@ package conf
import ( import (
"encoding/json" "encoding/json"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/xtls/xray-core/app/dns" "github.com/xtls/xray-core/app/dns"
@ -19,13 +20,11 @@ type NameServerConfig struct {
ExpectIPs StringList ExpectIPs StringList
} }
func (c *NameServerConfig) UnmarshalJSON(data []byte) error { func (c *NameServerConfig) UnmarshalJSON(data []byte) (err error) {
var address Address var address Address
if err := json.Unmarshal(data, &address); err == nil { if err = json.Unmarshal(data, &address); err == nil {
c.Address = &address c.Address = &address
return nil } else {
}
var advanced struct { var advanced struct {
Address *Address `json:"address"` Address *Address `json:"address"`
ClientIP *Address `json:"clientIp"` ClientIP *Address `json:"clientIp"`
@ -34,13 +33,26 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
Domains []string `json:"domains"` Domains []string `json:"domains"`
ExpectIPs StringList `json:"expectIps"` ExpectIPs StringList `json:"expectIps"`
} }
if err := json.Unmarshal(data, &advanced); err == nil { if err = json.Unmarshal(data, &advanced); err == nil {
c.Address = advanced.Address c.Address = advanced.Address
c.ClientIP = advanced.ClientIP c.ClientIP = advanced.ClientIP
c.Port = advanced.Port c.Port = advanced.Port
c.SkipFallback = advanced.SkipFallback c.SkipFallback = advanced.SkipFallback
c.Domains = advanced.Domains c.Domains = advanced.Domains
c.ExpectIPs = advanced.ExpectIPs c.ExpectIPs = advanced.ExpectIPs
}
}
if err == nil {
if c.Port == 0 && c.Address.Family().IsDomain() {
if host, port, err := net.SplitHostPort(c.Address.Domain()); err == nil {
port, err := strconv.Atoi(port)
if err == nil {
c.Address = &Address{Address: net.ParseAddress(host)}
c.Port = uint16(port)
}
}
}
return nil return nil
} }