-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Refactor doh client and segregate config options.
- Loading branch information
Showing
7 changed files
with
156 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
// DNSInCache is serialized and stored in the cache | ||
type DNSInCache struct { | ||
Msg *dns.Msg | ||
CreatedAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/miekg/dns" | ||
|
||
type DNSClient interface { | ||
GetDNSResponse(m *dns.Msg) (*dns.Msg, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
# Address for danse to listen on. | ||
bind_address = "127.0.0.1:5454" | ||
|
||
# Only used for resolving resolver url. No queries received by danse will be sent here. Default is 9.9.9.9:53 | ||
bootstrap_address = "1.1.1.1:53" | ||
|
||
# Urls for resolvers. | ||
[resolver] | ||
urls = ["https://dns.quad9.net/dns-query", "https://cloudflare-dns.com/dns-query"] | ||
|
||
|
||
[cache] | ||
# Should the answers be cached according to ttl. Default is true. | ||
cache = true | ||
|
||
# Maximum records to cache. | ||
max_items = 10000 | ||
|
||
# Config for logging | ||
[log] | ||
# Log level | ||
log_level = "info" | ||
|
||
# Logs all queries to stdout. Default is false. | ||
log_queries = true | ||
|
||
# Only used for resolving resolver url. No queries received by danse will be sent here. Default is 9.9.9.9:53 | ||
bootstrap_address = "1.1.1.1:53" | ||
|
||
# Urls for resolvers. | ||
[resolver] | ||
urls = ["https://dns.quad9.net/dns-query", "https://cloudflare-dns.com/dns-query"] | ||
log_queries = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
type DohClient struct { | ||
httpClient *http.Client | ||
|
||
// Slice of URLs | ||
urls []string | ||
|
||
// Last used index | ||
lIndex int | ||
|
||
logQueries bool | ||
|
||
sync.Mutex | ||
} | ||
|
||
func (c *DohClient) GetDNSResponse(msg *dns.Msg) (*dns.Msg, error) { | ||
b, err := msg.Pack() | ||
if err != nil { | ||
return &dns.Msg{}, err | ||
} | ||
|
||
c.Lock() | ||
|
||
url := c.urls[c.lIndex] | ||
|
||
// Increase last index | ||
c.lIndex++ | ||
|
||
if c.lIndex == len(c.urls) { | ||
c.lIndex = 0 | ||
} | ||
|
||
c.Unlock() | ||
|
||
if c.logQueries { | ||
log.Printf("Sending to %s for query: %s", url, msg.Question[0].String()) | ||
} | ||
|
||
resp, err := c.httpClient.Post(url, "application/dns-message", bytes.NewBuffer(b)) | ||
if err != nil { | ||
return &dns.Msg{}, err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
log.Printf("Response from DOH provider has status code: %d", resp.StatusCode) | ||
return &dns.Msg{}, errors.New("error from DOH provider") | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return &dns.Msg{}, nil | ||
} | ||
|
||
r := &dns.Msg{} | ||
err = r.Unpack(body) | ||
|
||
return r, err | ||
} | ||
|
||
func NewDOHClient(c *http.Client, urls []string, logQueries bool) (*DohClient, error) { | ||
return &DohClient{ | ||
httpClient: c, | ||
urls: urls, | ||
logQueries: logQueries, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters