From a86b7b3d075d3808b8649e2969fc5979507d8554 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sat, 10 Aug 2024 13:34:37 +0700 Subject: [PATCH 1/5] fixed: move doh to internal --- dns/dns.go | 37 +++++------------- dns/doh.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 - go.sum | 4 -- 4 files changed, 117 insertions(+), 33 deletions(-) create mode 100644 dns/doh.go diff --git a/dns/dns.go b/dns/dns.go index 9ba0d3e..97d8485 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -6,10 +6,7 @@ import ( "net" "regexp" "strconv" - "time" - "github.com/likexian/doh" - dohDns "github.com/likexian/doh/dns" "github.com/miekg/dns" log "github.com/sirupsen/logrus" "github.com/xvzc/SpoofDPI/util" @@ -37,17 +34,17 @@ func (d *DnsResolver) Lookup(domain string, useSystemDns bool) (string, error) { } if useSystemDns { - log.Debug("[DNS] ", domain, " resolving with system dns") + log.Debug("[DNS] ", domain, " resolving with system dns") return systemLookup(domain) } if d.enableDoh { - log.Debug("[DNS] ", domain, " resolving with dns over https") + log.Debug("[DNS] ", domain, " resolving with dns over https") return dohLookup(domain) } - log.Debug("[DNS] ", domain, " resolving with custom dns") - return customLookup(d.host, d.port, domain) + log.Debug("[DNS] ", domain, " resolving with custom dns") + return customLookup(d.host, d.port, domain) } func customLookup(host string, port string, domain string) (string, error) { @@ -89,28 +86,14 @@ func systemLookup(domain string) (string, error) { } func dohLookup(domain string) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) - defer cancel() - c := doh.Use(doh.CloudflareProvider, doh.GoogleProvider) + log.Debug("[DoH] ", domain, " resolving with dns over https") - rsp, err := c.Query(ctx, dohDns.Domain(domain), dohDns.TypeA) + dnsUpstream := util.GetConfig().DnsAddr + client := GetDoHClient(*dnsUpstream) + resp, err := client.Resolve(domain, dns.TypeA) if err != nil { - return "", errors.New("could not resolve the domain(doh)") - } - // doh dns answer - answer := rsp.Answer - - // print all answer - for _, a := range answer { - if a.Type != 1 { // Type == 1 -> A Record - continue - } - - return a.Data, nil + return "", errors.New("couldn not resolve the domain(doh)") } - // close the client - c.Close() - - return "", errors.New("no record found(doh)") + return resp[0], nil } diff --git a/dns/doh.go b/dns/doh.go new file mode 100644 index 0000000..02ee557 --- /dev/null +++ b/dns/doh.go @@ -0,0 +1,107 @@ +package dns + +import ( + "bytes" + "encoding/base64" + "fmt" + "net" + "net/http" + "strings" + "time" + + "github.com/miekg/dns" + log "github.com/sirupsen/logrus" +) + +type DoHClient struct { + upstream string + c *http.Client +} + +var client *DoHClient + +func GetDoHClient(upstream string) *DoHClient { + if client == nil { + if !strings.HasPrefix(upstream, "https://") { + upstream = "https://" + upstream + } + + if !strings.HasSuffix(upstream, "/dns-query") { + upstream = upstream + "/dns-query" + } + + 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{ + upstream: upstream, + c: c, + } + } + + return client +} + +func (d *DoHClient) doGetRequest(msg *dns.Msg) (*dns.Msg, error) { + 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 + } + + req.Header.Set("Accept", "application/dns-message") + + resp, err := d.c.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Debug("[DoH] Error while resolving ", url, " : ", resp.Status) + } + + buf := bytes.Buffer{} + buf.ReadFrom(resp.Body) + + ret_msg := new(dns.Msg) + err = ret_msg.Unpack(buf.Bytes()) + if err != nil { + return nil, err + } + + return ret_msg, nil +} + +func (d *DoHClient) Resolve(domain string, dnsType uint16) ([]string, error) { + msg := new(dns.Msg) + msg.SetQuestion(dns.Fqdn(domain), dnsType) + + resp, err := d.doGetRequest(msg) + if err != nil { + return nil, err + } + + var ret []string + for _, ans := range resp.Answer { + if a, ok := ans.(*dns.A); ok { + ret = append(ret, a.A.String()) + } + } + return ret, nil +} diff --git a/go.mod b/go.mod index 4028aac..1b45c00 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.21 toolchain go1.21.5 require ( - github.com/likexian/doh v0.7.1 github.com/miekg/dns v1.1.61 github.com/pterm/pterm v0.12.79 github.com/sirupsen/logrus v1.9.3 @@ -17,7 +16,6 @@ require ( atomicgo.dev/schedule v0.1.0 // indirect github.com/containerd/console v1.0.3 // indirect github.com/gookit/color v1.5.4 // indirect - github.com/likexian/gokit v0.25.15 // indirect github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/go.sum b/go.sum index 0f0fe2d..c98a4e4 100644 --- a/go.sum +++ b/go.sum @@ -33,10 +33,6 @@ github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/likexian/doh v0.7.1 h1:0p75orMxXrfGNAOnOEtl2xZ5T9Ctqi++i/WhN6P6hs8= -github.com/likexian/doh v0.7.1/go.mod h1:aIEOK197o6nC82iRo5+5L5ws+X7xRCmuer3M4In91WA= -github.com/likexian/gokit v0.25.15 h1:QjospM1eXhdMMHwZRpMKKAHY/Wig9wgcREmLtf9NslY= -github.com/likexian/gokit v0.25.15/go.mod h1:S2QisdsxLEHWeD/XI0QMVeggp+jbxYqUxMvSBil7MRg= github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= From 714daeab99972e93fc738364f23f06360bbff3eb Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:02:50 +0700 Subject: [PATCH 2/5] feat: ipv6 joins the ride. --- dns/dns.go | 15 +++++++++++---- dns/doh.go | 35 +++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/dns/dns.go b/dns/dns.go index 97d8485..32b7989 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -90,10 +90,17 @@ func dohLookup(domain string) (string, error) { dnsUpstream := util.GetConfig().DnsAddr client := GetDoHClient(*dnsUpstream) - resp, err := client.Resolve(domain, dns.TypeA) - if err != nil { - return "", errors.New("couldn not resolve the domain(doh)") + // try up to 3 times + for i := 0; i < 3; i++ { + resp, err := client.Resolve(domain, []uint16{dns.TypeA, dns.TypeAAAA}) + if err == nil { + if len(resp) == 0 { // yes this happens + return "", errors.New("no record found(doh)") + } + + return resp[0], nil + } } - return resp[0], nil + return "", errors.New("could not resolve the domain(doh)") } diff --git a/dns/doh.go b/dns/doh.go index 02ee557..561cb00 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -88,20 +88,31 @@ func (d *DoHClient) doGetRequest(msg *dns.Msg) (*dns.Msg, error) { return ret_msg, nil } -func (d *DoHClient) Resolve(domain string, dnsType uint16) ([]string, error) { - msg := new(dns.Msg) - msg.SetQuestion(dns.Fqdn(domain), dnsType) - - resp, err := d.doGetRequest(msg) - if err != nil { - return nil, err - } - +func (d *DoHClient) Resolve(domain string, dnsTypes []uint16) ([]string, error) { var ret []string - for _, ans := range resp.Answer { - if a, ok := ans.(*dns.A); ok { - ret = append(ret, a.A.String()) + + for _, dnsType := range dnsTypes { + msg := new(dns.Msg) + msg.SetQuestion(dns.Fqdn(domain), dnsType) + + resp, err := d.doGetRequest(msg) + if err != nil { + return nil, err + } + + if resp.Rcode != dns.RcodeSuccess { + continue + } + + for _, answer := range resp.Answer { + if t, ok := answer.(*dns.A); ok { + ret = append(ret, t.A.String()) + } + if t, ok := answer.(*dns.AAAA); ok { + ret = append(ret, t.AAAA.String()) + } } } + return ret, nil } From c93ddd67e0ee4b789f5d87baedf5cc3109e4dc38 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:15:22 +0700 Subject: [PATCH 3/5] fix: re-implement cancellation context --- dns/dns.go | 5 ++++- dns/doh.go | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/dns/dns.go b/dns/dns.go index 32b7989..b579946 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -6,6 +6,7 @@ import ( "net" "regexp" "strconv" + "time" "github.com/miekg/dns" log "github.com/sirupsen/logrus" @@ -86,13 +87,15 @@ func systemLookup(domain string) (string, error) { } func dohLookup(domain string) (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() log.Debug("[DoH] ", domain, " resolving with dns over https") dnsUpstream := util.GetConfig().DnsAddr client := GetDoHClient(*dnsUpstream) // try up to 3 times for i := 0; i < 3; i++ { - resp, err := client.Resolve(domain, []uint16{dns.TypeA, dns.TypeAAAA}) + resp, err := client.Resolve(ctx, domain, []uint16{dns.TypeA, dns.TypeAAAA}) if err == nil { if len(resp) == 0 { // yes this happens return "", errors.New("no record found(doh)") diff --git a/dns/doh.go b/dns/doh.go index 561cb00..dd8c4cb 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -2,6 +2,7 @@ package dns import ( "bytes" + "context" "encoding/base64" "fmt" "net" @@ -52,7 +53,7 @@ func GetDoHClient(upstream string) *DoHClient { return client } -func (d *DoHClient) doGetRequest(msg *dns.Msg) (*dns.Msg, error) { +func (d *DoHClient) doGetRequest(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { pack, err := msg.Pack() if err != nil { return nil, err @@ -64,6 +65,7 @@ func (d *DoHClient) doGetRequest(msg *dns.Msg) (*dns.Msg, error) { return nil, err } + req = req.WithContext(ctx) req.Header.Set("Accept", "application/dns-message") resp, err := d.c.Do(req) @@ -88,14 +90,14 @@ func (d *DoHClient) doGetRequest(msg *dns.Msg) (*dns.Msg, error) { return ret_msg, nil } -func (d *DoHClient) Resolve(domain string, dnsTypes []uint16) ([]string, error) { +func (d *DoHClient) Resolve(ctx context.Context, domain string, dnsTypes []uint16) ([]string, error) { var ret []string for _, dnsType := range dnsTypes { msg := new(dns.Msg) msg.SetQuestion(dns.Fqdn(domain), dnsType) - resp, err := d.doGetRequest(msg) + resp, err := d.doGetRequest(ctx, msg) if err != nil { return nil, err } From e9de332163f212a8d5fc513cbcc4995ad314e663 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:35:02 +0700 Subject: [PATCH 4/5] fix: make doh truly singleton --- dns/doh.go | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/dns/doh.go b/dns/doh.go index dd8c4cb..43e52bc 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "strings" + "sync" "time" "github.com/miekg/dns" @@ -20,35 +21,38 @@ type DoHClient struct { } var client *DoHClient +var clientOnce sync.Once func GetDoHClient(upstream string) *DoHClient { - if client == nil { - if !strings.HasPrefix(upstream, "https://") { - upstream = "https://" + upstream - } + clientOnce.Do(func() { + if client == nil { + if !strings.HasPrefix(upstream, "https://") { + upstream = "https://" + upstream + } - if !strings.HasSuffix(upstream, "/dns-query") { - upstream = upstream + "/dns-query" - } + if !strings.HasSuffix(upstream, "/dns-query") { + upstream = upstream + "/dns-query" + } - 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, - }, - } + 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{ - upstream: upstream, - c: c, + client = &DoHClient{ + upstream: upstream, + c: c, + } } - } + }) return client } From c8c18a46510135cddd0189c9c2fd21a9c250afe4 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:33:31 +0700 Subject: [PATCH 5/5] fix: remove automatic retry when doh fails --- dns/dns.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/dns/dns.go b/dns/dns.go index b579946..53b2c2a 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -93,16 +93,13 @@ func dohLookup(domain string) (string, error) { dnsUpstream := util.GetConfig().DnsAddr client := GetDoHClient(*dnsUpstream) - // try up to 3 times - for i := 0; i < 3; i++ { - resp, err := client.Resolve(ctx, domain, []uint16{dns.TypeA, dns.TypeAAAA}) - if err == nil { - if len(resp) == 0 { // yes this happens - return "", errors.New("no record found(doh)") - } - - return resp[0], nil + resp, err := client.Resolve(ctx, domain, []uint16{dns.TypeA, dns.TypeAAAA}) + if err == nil { + if len(resp) == 0 { // yes this happens + return "", errors.New("no record found(doh)") } + + return resp[0], nil } return "", errors.New("could not resolve the domain(doh)")