mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 22:36:53 +00:00
chore: refactor doh
This commit is contained in:
parent
76b16da2e8
commit
464afe937d
@ -41,7 +41,7 @@ func (d *DnsResolver) Lookup(domain string, useSystemDns bool) (string, error) {
|
|||||||
|
|
||||||
if d.enableDoh {
|
if d.enableDoh {
|
||||||
log.Debug("[DNS] ", domain, " resolving with dns over https")
|
log.Debug("[DNS] ", domain, " resolving with dns over https")
|
||||||
return dohLookup(domain)
|
return dohLookup(d.host, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("[DNS] ", domain, " resolving with custom dns")
|
log.Debug("[DNS] ", domain, " resolving with custom dns")
|
||||||
@ -86,16 +86,16 @@ func systemLookup(domain string) (string, error) {
|
|||||||
return "", errors.New("no record found(system)")
|
return "", errors.New("no record found(system)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func dohLookup(domain string) (string, error) {
|
func dohLookup(host string, domain string) (string, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
client := GetDOHClient(*util.GetConfig().DnsAddr)
|
client := getDOHClient(host)
|
||||||
|
|
||||||
msg := new(dns.Msg)
|
msg := new(dns.Msg)
|
||||||
msg.SetQuestion(dns.Fqdn(domain), dns.TypeA)
|
msg.SetQuestion(dns.Fqdn(domain), dns.TypeA)
|
||||||
|
|
||||||
response, err := client.Exchange(ctx, domain, msg)
|
response, err := client.dohExchange(ctx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("could not resolve the domain(doh)")
|
return "", errors.New("could not resolve the domain(doh)")
|
||||||
}
|
}
|
||||||
|
19
dns/doh.go
19
dns/doh.go
@ -8,7 +8,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -23,17 +22,9 @@ type DOHClient struct {
|
|||||||
var client *DOHClient
|
var client *DOHClient
|
||||||
var clientOnce sync.Once
|
var clientOnce sync.Once
|
||||||
|
|
||||||
func GetDOHClient(upstream string) *DOHClient {
|
func getDOHClient(host string) *DOHClient {
|
||||||
clientOnce.Do(func() {
|
clientOnce.Do(func() {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
if !strings.HasPrefix(upstream, "https://") {
|
|
||||||
upstream = "https://" + upstream
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasSuffix(upstream, "/dns-query") {
|
|
||||||
upstream = upstream + "/dns-query"
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &http.Client{
|
c := &http.Client{
|
||||||
Timeout: 5 * time.Second,
|
Timeout: 5 * time.Second,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
@ -48,7 +39,7 @@ func GetDOHClient(upstream string) *DOHClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client = &DOHClient{
|
client = &DOHClient{
|
||||||
upstream: upstream,
|
upstream: "https://" + host + "/dns-query",
|
||||||
client: c,
|
client: c,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,7 +48,7 @@ func GetDOHClient(upstream string) *DOHClient {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DOHClient) query(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
func (d *DOHClient) 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
|
||||||
@ -97,8 +88,8 @@ func (d *DOHClient) query(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
|||||||
return resultMsg, nil
|
return resultMsg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DOHClient) Exchange(ctx context.Context, domain string, msg *dns.Msg) (*dns.Msg, error) {
|
func (d *DOHClient) dohExchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
||||||
res, err := d.query(ctx, msg)
|
res, err := d.dohQuery(ctx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user