/
main.go
201 lines (179 loc) · 4.91 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"math/rand"
"net"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/chenhw2/https-dns/gdns"
"github.com/golang/glog"
"github.com/miekg/dns"
"github.com/urfave/cli"
)
var (
version = "MISSING build version [git hash]"
gdnsOPT gdns.GDNSOptions
gdnsEndPT string
listenAddress string
listenProtocols []string
)
func serve(net, addr string) {
glog.V(LINFO).Infof("starting %s service on %s", net, addr)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
server := &dns.Server{Addr: addr, Net: net, TsigSecret: nil}
go func() {
if err := server.ListenAndServe(); err != nil {
glog.Errorf("Failed to setup the %s server: %s\n", net, err.Error())
sig <- syscall.SIGTERM
}
}()
// serve until exit
<-sig
glog.V(LINFO).Infof("shutting down %s on interrupt\n", net)
if err := server.Shutdown(); err != nil {
glog.V(LERROR).Infof("got unexpected error %s", err.Error())
}
}
func init() {
// seed the global random number generator, used in some utilities and the
// google provider
rand.Seed(time.Now().UTC().UnixNano())
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
app := cli.NewApp()
app.Name = "https-dns"
app.Usage = "A DNS-protocol proxy for Google's DNS-over-HTTPS service."
app.Version = fmt.Sprintf("Git:[%s] (%s)", strings.ToUpper(version), runtime.Version())
// app.HideVersion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "listen, l",
Value: ":5300",
Usage: "Serve address",
},
cli.StringFlag{
Name: "proxy, p",
Usage: "Proxy (SOCKS or SHADOWSOCKS) Server for HTTP GET",
},
cli.StringFlag{
Name: "endpoint, ep",
Value: "Google",
Usage: "Google or Cloudflare",
},
cli.StringFlag{
Name: "endpoint-uri, epuri",
Usage: "DNS-over-HTTPS endpoint url",
},
cli.StringSliceFlag{
Name: "endpoint-ip, epip",
Usage: "IPs of the DNS-over-HTTPS endpoint; if provided, endpoint lookup skip",
},
cli.StringSliceFlag{
Name: "dns-servers, d",
Usage: "DNS Servers used to look up the endpoint; system default is used if absent.",
},
cli.StringFlag{
Name: "edns, e",
Usage: "Extension mechanisms for DNS (EDNS) is parameters of the Domain Name System (DNS) protocol.",
},
cli.BoolFlag{
Name: "no-pad, N",
Usage: "Disable padding of Google DNS-over-HTTPS requests to identical length",
},
cli.BoolFlag{
Name: "insecure, I",
Usage: "Disable SSL/TLS Certificate check (for some OS without ca-certificates)",
},
cli.BoolFlag{
Name: "udp, U",
Usage: "Listen on UDP",
},
cli.BoolFlag{
Name: "tcp, T",
Usage: "Listen on TCP",
},
}
app.Action = func(c *cli.Context) error {
glogGangstaShim(c)
listenAddress = c.String("listen")
if c.Bool("tcp") {
listenProtocols = append(listenProtocols, "tcp")
}
if c.Bool("udp") {
listenProtocols = append(listenProtocols, "udp")
}
if 0 == len(listenProtocols) {
cli.ShowAppHelp(c)
os.Exit(0)
}
if !strings.HasPrefix(version, "MISSING") {
fmt.Fprintf(os.Stderr, "%s %s\n", strings.ToUpper(c.App.Name), c.App.Version)
}
gdnsOPT.PROXY = c.String("proxy")
gdnsOPT.EDNS = c.String("edns")
gdnsOPT.Pad = !c.Bool("no-pad")
gdnsOPT.Secure = !c.Bool("insecure")
for _, eip := range c.StringSlice("endpoint-ip") {
if ip := net.ParseIP(eip); ip == nil {
glog.V(LERROR).Infof("%+v", fmt.Errorf("unable to parse IP from string %s", eip))
} else {
gdnsOPT.EndpointIPs = append(gdnsOPT.EndpointIPs, ip)
}
}
glog.V(LDEBUG).Infof("EndpointIPs%+v", gdnsOPT.EndpointIPs)
for _, dns := range c.StringSlice("dns-servers") {
if d, err := gdns.ParseEndpoint(dns, 53); err != nil {
glog.V(LERROR).Infof("%+v", err)
} else {
gdnsOPT.DNSServers = append(gdnsOPT.DNSServers, d)
}
}
glog.V(LDEBUG).Infof("DNSServers%+v", gdnsOPT.DNSServers)
gdnsEndPT = c.String("endpoint-uri")
if 0 == len(gdnsEndPT) {
switch strings.ToUpper(c.String("endpoint")) {
default:
fallthrough
case "GOOGLE":
gdnsEndPT = `https://dns.google.com/resolve`
case "CLOUDFLARE":
gdnsEndPT = `https://cloudflare-dns.com/dns-query?ct=application/dns-json`
if 0 == len(gdnsOPT.EndpointIPs) {
gdnsOPT.EndpointIPs = []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("1.0.0.1")}
}
}
}
return nil
}
app.Flags = append(app.Flags, glogGangstaFlags...)
app.Run(os.Args)
defer glog.Flush()
provider, err := gdns.NewGDNSProvider(gdnsEndPT, &gdnsOPT)
if err != nil {
glog.Exitln(err)
}
// options := &gdns.HandlerOptions{}
handler := gdns.NewHandler(provider, new(gdns.HandlerOptions))
dns.HandleFunc(".", handler.Handle)
// start the servers
servers := make(chan bool)
for _, protocol := range listenProtocols {
go func(protocol string) {
serve(protocol, listenAddress)
servers <- true
}(protocol)
}
// wait for servers to exit
for range listenProtocols {
<-servers
}
glog.V(LINFO).Infoln("servers exited, stopping")
}