Merge pull request #5 from xvzc/dns

Dns
This commit is contained in:
Kwanghoo Park 2022-01-10 23:53:36 +09:00 committed by GitHub
commit c06aeda497
4 changed files with 43 additions and 21 deletions

View File

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

36
doh/dns.go Normal file
View File

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

View File

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

View File

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