add resolver

This commit is contained in:
ginuerzh
2021-12-30 19:12:42 +08:00
parent 9b3d7e1110
commit a6671a468e
8 changed files with 343 additions and 50 deletions

View File

@ -1,6 +1,8 @@
package dns
import (
"net"
"strings"
"time"
mdata "github.com/go-gost/gost/pkg/metadata"
@ -11,10 +13,10 @@ type metadata struct {
retryCount int
ttl time.Duration
timeout time.Duration
prefer string
clientIP string
clientIP net.IP
// nameservers
servers []string
dns []string // compatible with v2
}
func (h *dnsHandler) parseMetadata(md mdata.Metadata) (err error) {
@ -23,9 +25,9 @@ func (h *dnsHandler) parseMetadata(md mdata.Metadata) (err error) {
retryCount = "retry"
ttl = "ttl"
timeout = "timeout"
prefer = "prefer"
clientIP = "clientIP"
servers = "servers"
dns = "dns"
)
h.md.readTimeout = mdata.GetDuration(md, readTimeout)
@ -35,9 +37,15 @@ func (h *dnsHandler) parseMetadata(md mdata.Metadata) (err error) {
if h.md.timeout <= 0 {
h.md.timeout = 5 * time.Second
}
h.md.prefer = mdata.GetString(md, prefer)
h.md.clientIP = mdata.GetString(md, clientIP)
sip := mdata.GetString(md, clientIP)
if sip != "" {
h.md.clientIP = net.ParseIP(sip)
}
h.md.servers = mdata.GetStrings(md, servers)
h.md.dns = strings.Split(mdata.GetString(md, dns), ",")
if len(h.md.dns) > 0 {
h.md.servers = append(h.md.servers, h.md.dns...)
}
return
}