fix: re-add handling leading https and trailing /dns-query

This commit is contained in:
xvzc 2024-08-13 13:30:59 +09:00
parent 464afe937d
commit 84ad7288ea
2 changed files with 27 additions and 23 deletions

View File

@ -106,5 +106,5 @@ func dohLookup(host string, domain string) (string, error) {
} }
} }
return "", errors.New("no record found(system)") return "", errors.New("no record found(doh)")
} }

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"regexp"
"sync" "sync"
"time" "time"
@ -16,36 +17,39 @@ import (
type DOHClient struct { type DOHClient struct {
upstream string upstream string
client *http.Client httpClient *http.Client
} }
var client *DOHClient var dohClient *DOHClient
var clientOnce sync.Once var clientOnce sync.Once
func getDOHClient(host string) *DOHClient { func getDOHClient(host string) *DOHClient {
clientOnce.Do(func() { if dohClient != nil {
if client == nil { return dohClient
c := &http.Client{ }
Timeout: 5 * time.Second,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
MaxIdleConnsPerHost: 100,
MaxIdleConns: 100,
},
}
client = &DOHClient{ clientOnce.Do(func() {
upstream: "https://" + host + "/dns-query", h := &http.Client{
client: c, Timeout: 5 * time.Second,
} Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
MaxIdleConnsPerHost: 100,
MaxIdleConns: 100,
},
}
host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "")
dohClient = &DOHClient{
upstream: "https://" + host + "/dns-query",
httpClient: h,
} }
}) })
return client return dohClient
} }
func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
@ -63,7 +67,7 @@ func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error
req = req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("Accept", "application/dns-message") req.Header.Set("Accept", "application/dns-message")
resp, err := d.client.Do(req) resp, err := d.httpClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }