@@ -16,6 +16,21 @@ import (
16
16
// https://github.com/lima-vm/lima/issues/380
17
17
const truncateSize = 512
18
18
19
+ var defaultFallbackIPs = []string {"8.8.8.8" , "1.1.1.1" }
20
+
21
+ type HandlerOptions struct {
22
+ IPv6 bool
23
+ StaticHosts map [string ]string
24
+ UpstreamServers []string
25
+ }
26
+
27
+ type ServerOptions struct {
28
+ HandlerOptions
29
+ Address string
30
+ TCPPort int
31
+ UDPPort int
32
+ }
33
+
19
34
type Handler struct {
20
35
clientConfig * dns.ClientConfig
21
36
clients []* dns.Client
@@ -38,21 +53,20 @@ func (s *Server) Shutdown() {
38
53
}
39
54
}
40
55
41
- func newStaticClientConfig (ips []net. IP ) (* dns.ClientConfig , error ) {
56
+ func newStaticClientConfig (ips []string ) (* dns.ClientConfig , error ) {
42
57
s := ``
43
58
for _ , ip := range ips {
44
- s += fmt .Sprintf ("nameserver %s\n " , ip . String () )
59
+ s += fmt .Sprintf ("nameserver %s\n " , ip )
45
60
}
46
61
r := strings .NewReader (s )
47
62
return dns .ClientConfigFromReader (r )
48
63
}
49
64
50
- func newHandler ( IPv6 bool , hosts map [ string ] string ) (dns.Handler , error ) {
65
+ func NewHandler ( opts HandlerOptions ) (dns.Handler , error ) {
51
66
cc , err := dns .ClientConfigFromFile ("/etc/resolv.conf" )
52
67
if err != nil {
53
- fallbackIPs := []net.IP {net .ParseIP ("8.8.8.8" ), net .ParseIP ("1.1.1.1" )}
54
- logrus .WithError (err ).Warnf ("failed to detect system DNS, falling back to %v" , fallbackIPs )
55
- cc , err = newStaticClientConfig (fallbackIPs )
68
+ logrus .WithError (err ).Warnf ("failed to detect system DNS, falling back to %v" , defaultFallbackIPs )
69
+ cc , err = newStaticClientConfig (defaultFallbackIPs )
56
70
if err != nil {
57
71
return nil , err
58
72
}
@@ -64,11 +78,11 @@ func newHandler(IPv6 bool, hosts map[string]string) (dns.Handler, error) {
64
78
h := & Handler {
65
79
clientConfig : cc ,
66
80
clients : clients ,
67
- IPv6 : IPv6 ,
81
+ IPv6 : opts . IPv6 ,
68
82
cname : make (map [string ]string ),
69
83
ip : make (map [string ]net.IP ),
70
84
}
71
- for host , address := range hosts {
85
+ for host , address := range opts . StaticHosts {
72
86
if ip := net .ParseIP (address ); ip != nil {
73
87
h .ip [host ] = ip
74
88
} else {
@@ -268,14 +282,14 @@ func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
268
282
}
269
283
}
270
284
271
- func Start (udpLocalPort , tcpLocalPort int , IPv6 bool , hosts map [ string ] string ) (* Server , error ) {
272
- h , err := newHandler ( IPv6 , hosts )
285
+ func Start (opts ServerOptions ) (* Server , error ) {
286
+ h , err := NewHandler ( opts . HandlerOptions )
273
287
if err != nil {
274
288
return nil , err
275
289
}
276
290
server := & Server {}
277
- if udpLocalPort > 0 {
278
- addr := fmt .Sprintf ("127.0.0.1:%d" , udpLocalPort )
291
+ if opts . UDPPort > 0 {
292
+ addr := fmt .Sprintf ("127.0.0.1:%d" , opts . UDPPort )
279
293
s := & dns.Server {Net : "udp" , Addr : addr , Handler : h }
280
294
server .udp = s
281
295
go func () {
@@ -284,8 +298,8 @@ func Start(udpLocalPort, tcpLocalPort int, IPv6 bool, hosts map[string]string) (
284
298
}
285
299
}()
286
300
}
287
- if tcpLocalPort > 0 {
288
- addr := fmt .Sprintf ("127.0.0.1:%d" , tcpLocalPort )
301
+ if opts . TCPPort > 0 {
302
+ addr := fmt .Sprintf ("127.0.0.1:%d" , opts . TCPPort )
289
303
s := & dns.Server {Net : "tcp" , Addr : addr , Handler : h }
290
304
server .tcp = s
291
305
go func () {
0 commit comments