Skip to content

Commit

Permalink
add smart doh when worker is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Sep 9, 2023
1 parent dc8363d commit e5c1344
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dialer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dialer/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 6 additions & 12 deletions doh/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e5c1344

Please sign in to comment.