-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
82 lines (72 loc) · 2.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
)
func main() {
host := flag.String("host", "localhost", "interface to listen on")
port := flag.Int("port", 5353, "dns port to listen on")
dohserver := flag.String("dohserver", "https://mozilla.cloudflare-dns.com/dns-query", "DNS Over HTTPS server address")
debug := flag.Bool("debug", false, "print debug logs")
flag.Parse()
if *debug {
log.SetFlags(log.Lshortfile)
} else {
log.SetFlags(0)
}
if err := newUDPServer(*host, *port, *dohserver); err != nil {
log.Fatalf("could not listen on %s:%d: %s", *host, *port, err)
}
}
func newUDPServer(host string, port int, dohserver string) error {
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP(host), Port: port})
if err != nil {
return err
}
for {
var raw [512]byte
n, addr, err := conn.ReadFromUDP(raw[:512])
if err != nil {
log.Printf("could not read: %s", err)
continue
}
log.Printf("new connection from %s:%d", addr.IP.String(), addr.Port)
go proxy(dohserver, conn, addr, raw[:n])
}
}
func proxy(dohserver string, conn *net.UDPConn, addr *net.UDPAddr, raw []byte) {
enc := base64.RawURLEncoding.EncodeToString(raw)
url := fmt.Sprintf("%s?dns=%s", dohserver, enc)
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Printf("could not create request: %s", err)
return
}
r.Header.Set("Content-Type", "application/dns-message")
r.Header.Set("Accept", "application/dns-message")
c := http.Client{}
resp, err := c.Do(r)
if err != nil {
log.Printf("could not perform request: %s", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("wrong response from DOH server got %s", http.StatusText(resp.StatusCode))
return
}
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read message from response: %s", err)
return
}
if _, err := conn.WriteToUDP(msg, addr); err != nil {
log.Printf("could not write to udp connection: %s", err)
return
}
}