Skip to content

Commit

Permalink
move dns client to a separate library (#15)
Browse files Browse the repository at this point in the history
* mvp

* updated dependencies
  • Loading branch information
mosajjal authored Oct 25, 2022
1 parent 9fbce24 commit 2ae3cfb
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 247 deletions.
8 changes: 0 additions & 8 deletions certtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,3 @@ func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, a

return certBuffer.Bytes(), keyBuffer.Bytes(), nil
}

func ipsToStrings(ips []net.IP) []string {
ss := make([]string, 0, len(ips))
for _, ip := range ips {
ss = append(ss, ip.String())
}
return ss
}
52 changes: 20 additions & 32 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package main

import (
"bufio"
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/golang-collections/collections/tst"
doqclient "github.com/mosajjal/doqd/pkg/client"
"github.com/mosajjal/sniproxy/doh"
"github.com/mosajjal/dnsclient"
log "github.com/sirupsen/logrus"

"github.com/miekg/dns"
Expand Down Expand Up @@ -48,12 +47,6 @@ func inDomainList(fqdn string) bool {
return true
}

var dnsClient struct {
Doq doqclient.Client
Doh doh.Client
classicDNS *dns.Client
}

func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
Expand Down Expand Up @@ -126,30 +119,25 @@ func LoadDomainsCsv(Filename string) (prefix *tst.TernarySearchTree, suffix *tst
return prefix, suffix, all
}

func performExternalQuery(question dns.Question, server string) (*dns.Msg, time.Duration, error) {
dnsURL, err := url.Parse(server)
if err != nil {
log.Fatalf("[DNS] Invalid upstream DNS URL: %s", server)
func performExternalAQuery(fqdn string) ([]dns.RR, time.Duration, error) {
if !strings.HasSuffix(fqdn, ".") {
fqdn = fqdn + "."
}
msg := dns.Msg{
MsgHdr: dns.MsgHdr{
Id: dns.Id(),
RecursionDesired: true,
},
Question: []dns.Question{question},
}

if dnsURL.Scheme == "quic" {
rmsg, err := dnsClient.Doq.SendQuery(msg)
return &rmsg, 0, err

}
if dnsURL.Scheme == "https" {
rmsg, t, err := dnsClient.Doh.SendQuery(msg)
return &rmsg, t, err

msg := dns.Msg{}
msg.RecursionDesired = true
msg.SetQuestion(fqdn, dns.TypeA)
msg.SetEdns0(1232, true)
//TODO: context and timeout here
res, trr, err := c.dnsClient.Query(context.Background(), &msg)
if err != nil {
if err.Error() == "EOF" {
log.Infof("[DNS] reconnecting DNS...") //TODO: don't like logging here
c.dnsClient.Close()
c.dnsClient, err = dnsclient.New(c.UpstreamDNS, true)
}
}
return dnsClient.classicDNS.Exchange(&msg, dnsURL.Host)
return res, trr, err
}

func processQuestion(q dns.Question) ([]dns.RR, error) {
Expand All @@ -166,12 +154,12 @@ func processQuestion(q dns.Question) ([]dns.RR, error) {
}

// Otherwise do an upstream query and use that answer.
resp, rtt, err := performExternalQuery(q, c.UpstreamDNS)
resp, rtt, err := performExternalAQuery(q.Name)
if err != nil {
return nil, err
}

log.Infof("[DNS] returned origin address for domain: %s, rtt: %s", q.Name, rtt)

return resp.Answer, nil
return resp, nil
}
91 changes: 0 additions & 91 deletions doh/client.go

This file was deleted.

23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ go 1.18
require (
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/miekg/dns v1.1.50
github.com/mosajjal/dnsclient v0.1.0
github.com/mosajjal/doqd v0.0.0-20221017212049-9745a8eb6912
github.com/sirupsen/logrus v1.9.0
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/lucas-clemente/quic-go v0.29.2 // indirect
github.com/google/pprof v0.0.0-20221010195024-131d412537ea // indirect
github.com/lucas-clemente/quic-go v0.30.0 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.3 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/onsi/ginkgo/v2 v2.4.0 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/exp v0.0.0-20221025133541-111beb427cde // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/tools v0.1.12 // indirect
golang.org/x/tools v0.2.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
Loading

0 comments on commit 2ae3cfb

Please sign in to comment.