diff --git a/cmd/spoof-dpi/main.go b/cmd/spoof-dpi/main.go index 9026499..8194df5 100644 --- a/cmd/spoof-dpi/main.go +++ b/cmd/spoof-dpi/main.go @@ -7,6 +7,7 @@ import ( "runtime" "syscall" + "github.com/xvzc/SpoofDPI/doh" "github.com/xvzc/SpoofDPI/proxy" "github.com/xvzc/SpoofDPI/util" ) @@ -14,13 +15,15 @@ import ( func main() { port, dns, debug := util.ParseArgs() - p := proxy.New(port, dns, runtime.GOOS, debug) + p := proxy.New(port, runtime.GOOS, debug) p.PrintWelcome() if err := p.SetOsProxy(); err != nil { log.Fatal(err) } + doh.Init(dns) + go p.Start() // Handle signals diff --git a/doh/dns.go b/doh/dns.go new file mode 100644 index 0000000..02c944c --- /dev/null +++ b/doh/dns.go @@ -0,0 +1,36 @@ +package doh + +import ( + "sync" + + "github.com/babolivier/go-doh-client" +) + +var resolver *doh.Resolver +var once sync.Once + +func Init(dns string) { + getInstance().Host = dns +} + +func Lookup(domain string) (string, error) { + a, _, err := resolver.LookupA(domain) + if err != nil { + return "", err + } + + ip := a[0].IP4 + + return ip, nil +} + +func getInstance() *doh.Resolver { + once.Do(func() { + resolver = &doh.Resolver{ + Host: "", + Class: doh.IN, + } + }) + + return resolver +} diff --git a/proxy/doh.go b/proxy/doh.go deleted file mode 100644 index ef7a105..0000000 --- a/proxy/doh.go +++ /dev/null @@ -1,14 +0,0 @@ -package proxy - -func (p *Proxy) DnsLookupOverHttps(domain string) (string, error) { - // Perform a A lookup on example.com - - a, _, err := p.DNS.LookupA(domain) - if err != nil { - return "", err - } - - ip := a[0].IP4 - - return ip, nil -} diff --git a/proxy/proxy.go b/proxy/proxy.go index 8d41af1..93ea199 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,22 +6,20 @@ import ( "net" "os" - "github.com/babolivier/go-doh-client" "github.com/pterm/pterm" + "github.com/xvzc/SpoofDPI/doh" "github.com/xvzc/SpoofDPI/packet" ) type Proxy struct { Port string - DNS doh.Resolver OS string Debug bool } -func New(port string, dns string, os string, debug bool) *Proxy { +func New(port string, os string, debug bool) *Proxy { return &Proxy{ Port: port, - DNS: doh.Resolver{Host: dns, Class: doh.IN}, OS: os, Debug: debug, } @@ -34,7 +32,6 @@ func (p *Proxy) PrintWelcome() { pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ {Level: 0, Text: "PORT : " + p.Port}, - {Level: 0, Text: "DNS : " + p.DNS.Host}, {Level: 0, Text: "DEBUG : " + fmt.Sprint(p.Debug)}, }).Render() } @@ -76,7 +73,7 @@ func (p *Proxy) Start() { } // Dns lookup over https - ip, err := p.DnsLookupOverHttps(r.Domain) + ip, err := doh.Lookup(r.Domain) if err != nil { log.Println("Error looking up dns: "+r.Domain, err) return