SpoofDPI/dns/resolver/doh.go

109 lines
2.2 KiB
Go
Raw Normal View History

2024-08-18 06:55:47 +00:00
package client
2024-08-10 06:34:37 +00:00
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"
"regexp"
2024-08-10 06:34:37 +00:00
"time"
"github.com/miekg/dns"
)
2024-08-18 07:11:04 +00:00
type DOHResolver struct {
2024-08-18 07:17:05 +00:00
upstream string
client *http.Client
2024-08-10 06:34:37 +00:00
}
2024-08-18 07:11:04 +00:00
func NewDOHClient(host string) *DOHResolver {
2024-08-18 07:17:05 +00:00
c := &http.Client{
2024-08-18 06:55:47 +00:00
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, "")
2024-08-18 07:11:04 +00:00
return &DOHResolver{
2024-08-18 07:17:05 +00:00
upstream: "https://" + host + "/dns-query",
client: c,
2024-08-18 06:55:47 +00:00
}
}
2024-08-10 06:34:37 +00:00
2024-08-18 07:11:04 +00:00
func (r *DOHResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) {
2024-08-18 06:55:47 +00:00
sendMsg := func(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
2024-08-18 07:11:04 +00:00
return r.dohExchange(ctx, msg)
}
2024-08-18 06:55:47 +00:00
resultCh := lookup(ctx, host, qTypes, sendMsg)
addrs, err := processResults(ctx, resultCh)
return addrs, err
}
2024-08-18 07:11:04 +00:00
func (r *DOHResolver) String() string {
2024-08-18 07:33:02 +00:00
return fmt.Sprintf("doh resolver(%s)", r.upstream)
2024-08-10 06:34:37 +00:00
}
2024-08-18 07:11:04 +00:00
func (r *DOHResolver) 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
}
2024-08-18 07:11:04 +00:00
url := fmt.Sprintf("%s?dns=%s", r.upstream, base64.RawStdEncoding.EncodeToString(pack))
2024-08-10 06:34:37 +00:00
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-18 07:17:05 +00:00
resp, err := r.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-18 07:11:04 +00:00
func (r *DOHResolver) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
res, err := r.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
}