refactor doh

This commit is contained in:
xvzc 2024-08-18 16:17:05 +09:00
parent afb3f5a921
commit c1f6dcef2a

View File

@ -15,12 +15,12 @@ import (
) )
type DOHResolver struct { type DOHResolver struct {
upstream string upstream string
httpClient *http.Client client *http.Client
} }
func NewDOHClient(host string) *DOHResolver { func NewDOHClient(host string) *DOHResolver {
h := &http.Client{ c := &http.Client{
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
@ -35,8 +35,8 @@ func NewDOHClient(host string) *DOHResolver {
host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "") host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "")
return &DOHResolver{ return &DOHResolver{
upstream: "https://" + host + "/dns-query", upstream: "https://" + host + "/dns-query",
httpClient: h, client: c,
} }
} }
@ -69,7 +69,7 @@ func (r *DOHResolver) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, err
req = req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("Accept", "application/dns-message") req.Header.Set("Accept", "application/dns-message")
resp, err := r.httpClient.Do(req) resp, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }