rename client to resolver

This commit is contained in:
xvzc 2024-08-18 16:11:04 +09:00
parent 67bee5a49a
commit afb3f5a921
6 changed files with 54 additions and 54 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

28
dns/resolver/system.go Normal file
View File

@ -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
}