mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2025-01-03 04:50:11 +00:00
rename client to resolver
This commit is contained in:
parent
67bee5a49a
commit
afb3f5a921
@ -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
|
|
||||||
}
|
|
10
dns/dns.go
10
dns/dns.go
@ -9,16 +9,16 @@ import (
|
|||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/xvzc/SpoofDPI/dns/client"
|
"github.com/xvzc/SpoofDPI/dns/resolver"
|
||||||
"github.com/xvzc/SpoofDPI/util"
|
"github.com/xvzc/SpoofDPI/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dns struct {
|
type Dns struct {
|
||||||
host string
|
host string
|
||||||
port string
|
port string
|
||||||
systemClient client.Client
|
systemClient client.Resolver
|
||||||
generalClient client.Client
|
generalClient client.Resolver
|
||||||
dohClient client.Client
|
dohClient client.Resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(config *util.Config) *Dns {
|
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)
|
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 {
|
if useSystemDns {
|
||||||
return d.systemClient
|
return d.systemClient
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,12 @@ import (
|
|||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DOHClient struct {
|
type DOHResolver struct {
|
||||||
upstream string
|
upstream string
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDOHClient(host string) *DOHClient {
|
func NewDOHClient(host string) *DOHResolver {
|
||||||
h := &http.Client{
|
h := &http.Client{
|
||||||
Timeout: 5 * time.Second,
|
Timeout: 5 * time.Second,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
@ -34,16 +34,15 @@ func NewDOHClient(host string) *DOHClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "")
|
host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "")
|
||||||
return &DOHClient{
|
return &DOHResolver{
|
||||||
upstream: "https://" + host + "/dns-query",
|
upstream: "https://" + host + "/dns-query",
|
||||||
httpClient: h,
|
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) {
|
sendMsg := func(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
||||||
clt := NewDOHClient(net.JoinHostPort(host, "443"))
|
return r.dohExchange(ctx, msg)
|
||||||
return clt.dohExchange(ctx, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resultCh := lookup(ctx, host, qTypes, sendMsg)
|
resultCh := lookup(ctx, host, qTypes, sendMsg)
|
||||||
@ -51,17 +50,17 @@ func (c *DOHClient) Resolve(ctx context.Context, host string, qTypes []uint16) (
|
|||||||
return addrs, err
|
return addrs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DOHClient) String() string {
|
func (r *DOHResolver) String() string {
|
||||||
return fmt.Sprintf("doh client(%s)", c.upstream)
|
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()
|
pack, err := msg.Pack()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 = req.WithContext(ctx)
|
||||||
req.Header.Set("Accept", "application/dns-message")
|
req.Header.Set("Accept", "application/dns-message")
|
||||||
|
|
||||||
resp, err := d.httpClient.Do(req)
|
resp, err := r.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -95,8 +94,8 @@ func (d *DOHClient) dohQuery(ctx context.Context, msg *dns.Msg) (*dns.Msg, error
|
|||||||
return resultMsg, nil
|
return resultMsg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DOHClient) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
func (r *DOHResolver) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
||||||
res, err := d.dohQuery(ctx, msg)
|
res, err := r.dohQuery(ctx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
@ -13,20 +13,21 @@ type DNSResult struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
type GeneralClient struct {
|
type GeneralResolver struct {
|
||||||
|
client *dns.Client
|
||||||
server string
|
server string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGeneralClient(server string) *GeneralClient {
|
func NewGeneralClient(server string) *GeneralResolver {
|
||||||
return &GeneralClient{
|
return &GeneralResolver{
|
||||||
|
client: &dns.Client{},
|
||||||
server: server,
|
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) {
|
sendMsg := func(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
||||||
clt := &dns.Client{}
|
resp, _, err := r.client.Exchange(msg, r.server)
|
||||||
resp, _, err := clt.Exchange(msg, c.server)
|
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,6 +36,6 @@ func (c *GeneralClient) Resolve(ctx context.Context, host string, qTypes []uint1
|
|||||||
return addrs, err
|
return addrs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GeneralClient) String() string {
|
func (c *GeneralResolver) String() string {
|
||||||
return fmt.Sprintf("custom client(%s)", c.server)
|
return fmt.Sprintf("custom client(%s)", c.server)
|
||||||
}
|
}
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/xvzc/SpoofDPI/dns/addrselect"
|
"github.com/xvzc/SpoofDPI/dns/addrselect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type Resolver interface {
|
||||||
Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error)
|
Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error)
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
28
dns/resolver/system.go
Normal file
28
dns/resolver/system.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user