From e5c13441d78e35ede9b88f9dd8c727e956a58786 Mon Sep 17 00:00:00 2001 From: uoosef Date: Sun, 10 Sep 2023 00:29:51 +0330 Subject: [PATCH] add smart doh when worker is enabled --- dialer/http.go | 2 +- dialer/http_test.go | 2 +- doh/doh.go | 18 ++++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/dialer/http.go b/dialer/http.go index d17f867..5088eae 100644 --- a/dialer/http.go +++ b/dialer/http.go @@ -10,7 +10,7 @@ import ( ) // MakeHTTPClient creates an HTTP client with custom dialing behavior. -func (d *Dialer) MakeHTTPClient(hostPort string, enableProxy bool) *http.Client { +func (d *Dialer) MakeHTTPClient(enableProxy bool) *http.Client { transport := &http.Transport{ ForceAttemptHTTP2: false, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { diff --git a/dialer/http_test.go b/dialer/http_test.go index 13658dd..a2f0ceb 100644 --- a/dialer/http_test.go +++ b/dialer/http_test.go @@ -21,7 +21,7 @@ func TestMakeHTTPClient(t *testing.T) { } // Create an HTTP client using the MakeHTTPClient method - client := d.MakeHTTPClient(testServer.Listener.Addr().String(), false) + client := d.MakeHTTPClient(false) // Make an HTTP request using the client resp, err := client.Get(testServer.URL) diff --git a/doh/doh.go b/doh/doh.go index 0544339..d1fc5be 100644 --- a/doh/doh.go +++ b/doh/doh.go @@ -2,13 +2,13 @@ package doh import ( + "bepass/config" "bepass/dialer" "bepass/resolve" "encoding/base64" "errors" "io" "net/http" - "net/url" "time" "github.com/miekg/dns" @@ -66,17 +66,7 @@ func NewClient(opts ...ClientOption) *Client { // HTTPClient performs an HTTP GET request to the given address using the configured client. func (c *Client) HTTPClient(address string) ([]byte, error) { - var client *http.Client - if c.opt.EnableDNSFragment { - client = c.opt.Dialer.MakeHTTPClient("", true) - } else { - u, err := url.Parse(address) - if err != nil { - return nil, err - } - dohIP := c.opt.LocalResolver.Resolve(u.Hostname()) - client = c.opt.Dialer.MakeHTTPClient(dohIP+":443", false) - } + client := c.opt.Dialer.MakeHTTPClient(config.G.WorkerEnabled) resp, err := client.Get(address) if err != nil { return nil, err @@ -112,6 +102,10 @@ func (c *Client) Exchange(req *dns.Msg, address string) (r *dns.Msg, rtt time.Du b64 = make([]byte, base64.RawURLEncoding.EncodedLen(len(buf))) base64.RawURLEncoding.Encode(b64, buf) + if config.G.WorkerEnabled { + address = "https://8.8.8.8/dns-query" + } + content, err := c.HTTPClient(address + "?dns=" + string(b64)) if err != nil { return