2024-08-10 06:34:37 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-08-10 07:15:22 +00:00
|
|
|
"context"
|
2024-08-10 06:34:37 +00:00
|
|
|
"encoding/base64"
|
2024-08-12 22:37:20 +00:00
|
|
|
"errors"
|
2024-08-10 06:34:37 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2024-08-11 12:35:02 +00:00
|
|
|
"sync"
|
2024-08-10 06:34:37 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
type DOHClient struct {
|
2024-08-10 06:34:37 +00:00
|
|
|
upstream string
|
2024-08-12 22:37:20 +00:00
|
|
|
client *http.Client
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
var client *DOHClient
|
2024-08-11 12:35:02 +00:00
|
|
|
var clientOnce sync.Once
|
2024-08-10 06:34:37 +00:00
|
|
|
|
2024-08-12 22:53:37 +00:00
|
|
|
func getDOHClient(host string) *DOHClient {
|
2024-08-11 12:35:02 +00:00
|
|
|
clientOnce.Do(func() {
|
|
|
|
if client == nil {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2024-08-10 06:34:37 +00:00
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
client = &DOHClient{
|
2024-08-12 22:53:37 +00:00
|
|
|
upstream: "https://" + host + "/dns-query",
|
2024-08-12 22:37:20 +00:00
|
|
|
client: c,
|
2024-08-11 12:35:02 +00:00
|
|
|
}
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|
2024-08-11 12:35:02 +00:00
|
|
|
})
|
2024-08-10 06:34:37 +00:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2024-08-12 22:53:37 +00:00
|
|
|
func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
2024-08-10 06:34:37 +00:00
|
|
|
pack, err := msg.Pack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s?dns=%s", d.upstream, base64.RawStdEncoding.EncodeToString(pack))
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-10 07:15:22 +00:00
|
|
|
req = req.WithContext(ctx)
|
2024-08-10 06:34:37 +00:00
|
|
|
req.Header.Set("Accept", "application/dns-message")
|
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
resp, err := d.client.Do(req)
|
2024-08-10 06:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-08-12 22:37:20 +00:00
|
|
|
return nil, errors.New("doh status error")
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
2024-08-12 22:37:20 +00:00
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-10 06:34:37 +00:00
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
resultMsg := new(dns.Msg)
|
|
|
|
err = resultMsg.Unpack(buf.Bytes())
|
2024-08-10 06:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
return resultMsg, nil
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 22:53:37 +00:00
|
|
|
func (d *DOHClient) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
|
|
|
res, err := d.dohQuery(ctx, msg)
|
2024-08-12 22:37:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-10 07:02:50 +00:00
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
if res.Rcode != dns.RcodeSuccess {
|
|
|
|
return nil, errors.New("doh rcode wasn't successful")
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|
2024-08-10 07:02:50 +00:00
|
|
|
|
2024-08-12 22:37:20 +00:00
|
|
|
return res, nil
|
2024-08-10 06:34:37 +00:00
|
|
|
}
|