diff --git a/dns/client/system.go b/dns/client/system.go deleted file mode 100644 index f1c6523..0000000 --- a/dns/client/system.go +++ /dev/null @@ -1,28 +0,0 @@ -package client - -import ( - "context" - "net" -) - -type SystemClient struct { - client *net.Resolver -} - -func NewSystemClient() *SystemClient { - return &SystemClient{ - client: &net.Resolver{PreferGo: true}, - } -} - -func (c *SystemClient) String() string { - return "system client" -} - -func (c *SystemClient) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { - addrs, err := c.client.LookupIPAddr(ctx, host) - if err != nil { - return []net.IPAddr{}, err - } - return addrs, nil -} diff --git a/dns/dns.go b/dns/dns.go index 2bb38b2..b6ed014 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -9,16 +9,16 @@ import ( "github.com/miekg/dns" log "github.com/sirupsen/logrus" - "github.com/xvzc/SpoofDPI/dns/client" + "github.com/xvzc/SpoofDPI/dns/resolver" "github.com/xvzc/SpoofDPI/util" ) type Dns struct { host string port string - systemClient client.Client - generalClient client.Client - dohClient client.Client + systemClient client.Resolver + generalClient client.Resolver + dohClient client.Resolver } func NewResolver(config *util.Config) *Dns { @@ -61,7 +61,7 @@ func (d *Dns) ResolveHost(host string, enableDoh bool, useSystemDns bool) (strin return "", fmt.Errorf("could not resolve %s using %s", host, clt) } -func (d *Dns) clientFactory(enableDoh bool, useSystemDns bool) client.Client { +func (d *Dns) clientFactory(enableDoh bool, useSystemDns bool) client.Resolver { if useSystemDns { return d.systemClient } diff --git a/dns/client/doh.go b/dns/resolver/doh.go similarity index 70% rename from dns/client/doh.go rename to dns/resolver/doh.go index 55edd9c..c1d59fe 100644 --- a/dns/client/doh.go +++ b/dns/resolver/doh.go @@ -14,12 +14,12 @@ import ( "github.com/miekg/dns" ) -type DOHClient struct { +type DOHResolver struct { upstream string httpClient *http.Client } -func NewDOHClient(host string) *DOHClient { +func NewDOHClient(host string) *DOHResolver { h := &http.Client{ Timeout: 5 * time.Second, Transport: &http.Transport{ @@ -34,16 +34,15 @@ func NewDOHClient(host string) *DOHClient { } host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "") - return &DOHClient{ + return &DOHResolver{ upstream: "https://" + host + "/dns-query", httpClient: h, } } -func (c *DOHClient) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { +func (r *DOHResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { sendMsg := func(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { - clt := NewDOHClient(net.JoinHostPort(host, "443")) - return clt.dohExchange(ctx, msg) + return r.dohExchange(ctx, msg) } resultCh := lookup(ctx, host, qTypes, sendMsg) @@ -51,17 +50,17 @@ func (c *DOHClient) Resolve(ctx context.Context, host string, qTypes []uint16) ( return addrs, err } -func (c *DOHClient) String() string { - return fmt.Sprintf("doh client(%s)", c.upstream) +func (r *DOHResolver) String() string { + return fmt.Sprintf("doh client(%s)", r.upstream) } -func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { +func (r *DOHResolver) dohQuery(ctx context.Context, 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)) + url := fmt.Sprintf("%s?dns=%s", r.upstream, base64.RawStdEncoding.EncodeToString(pack)) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err @@ -70,7 +69,7 @@ func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error req = req.WithContext(ctx) req.Header.Set("Accept", "application/dns-message") - resp, err := d.httpClient.Do(req) + resp, err := r.httpClient.Do(req) if err != nil { return nil, err } @@ -95,8 +94,8 @@ func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error return resultMsg, nil } -func (d *DOHClient) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { - res, err := d.dohQuery(ctx, msg) +func (r *DOHResolver) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { + res, err := r.dohQuery(ctx, msg) if err != nil { return nil, err } diff --git a/dns/client/general.go b/dns/resolver/general.go similarity index 54% rename from dns/client/general.go rename to dns/resolver/general.go index 93be489..d676c14 100644 --- a/dns/client/general.go +++ b/dns/resolver/general.go @@ -13,20 +13,21 @@ type DNSResult struct { err error } -type GeneralClient struct { +type GeneralResolver struct { + client *dns.Client server string } -func NewGeneralClient(server string) *GeneralClient { - return &GeneralClient{ +func NewGeneralClient(server string) *GeneralResolver { + return &GeneralResolver{ + client: &dns.Client{}, server: server, } } -func (c *GeneralClient) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { +func (r *GeneralResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { sendMsg := func(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { - clt := &dns.Client{} - resp, _, err := clt.Exchange(msg, c.server) + resp, _, err := r.client.Exchange(msg, r.server) return resp, err } @@ -35,6 +36,6 @@ func (c *GeneralClient) Resolve(ctx context.Context, host string, qTypes []uint1 return addrs, err } -func (c *GeneralClient) String() string { +func (c *GeneralResolver) String() string { return fmt.Sprintf("custom client(%s)", c.server) } diff --git a/dns/client/client.go b/dns/resolver/resolver.go similarity index 98% rename from dns/client/client.go rename to dns/resolver/resolver.go index d0ac688..f2033ac 100644 --- a/dns/client/client.go +++ b/dns/resolver/resolver.go @@ -12,7 +12,7 @@ import ( "github.com/xvzc/SpoofDPI/dns/addrselect" ) -type Client interface { +type Resolver interface { Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) String() string } diff --git a/dns/resolver/system.go b/dns/resolver/system.go new file mode 100644 index 0000000..9a3f712 --- /dev/null +++ b/dns/resolver/system.go @@ -0,0 +1,28 @@ +package client + +import ( + "context" + "net" +) + +type SystemResolver struct { + *net.Resolver +} + +func NewSystemClient() *SystemResolver { + return &SystemResolver{ + &net.Resolver{PreferGo: true}, + } +} + +func (r *SystemResolver) String() string { + return "system client" +} + +func (r *SystemResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) { + addrs, err := r.LookupIPAddr(ctx, host) + if err != nil { + return []net.IPAddr{}, err + } + return addrs, nil +}