-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package dns | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/containers/gvisor-tap-vsock/pkg/types" | ||
"github.com/miekg/dns" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type dnsHandler struct { | ||
zones []types.Zone | ||
} | ||
|
||
func (h *dnsHandler) handle(w dns.ResponseWriter, r *dns.Msg) { | ||
m := new(dns.Msg) | ||
m.SetReply(r) | ||
m.RecursionAvailable = true | ||
h.addAnswers(m) | ||
if err := w.WriteMsg(m); err != nil { | ||
log.Error(err) | ||
} | ||
} | ||
|
||
func (h *dnsHandler) addAnswers(m *dns.Msg) { | ||
for _, q := range m.Question { | ||
for _, zone := range h.zones { | ||
zoneSuffix := fmt.Sprintf(".%s", zone.Name) | ||
if strings.HasSuffix(q.Name, zoneSuffix) { | ||
if q.Qtype != dns.TypeA { | ||
return | ||
} | ||
for _, record := range zone.Records { | ||
withoutZone := strings.TrimSuffix(q.Name, zoneSuffix) | ||
if (record.Name != "" && record.Name == withoutZone) || | ||
(record.Regexp != nil && record.Regexp.MatchString(withoutZone)) { | ||
m.Answer = append(m.Answer, &dns.A{ | ||
Hdr: dns.RR_Header{ | ||
Name: q.Name, | ||
Rrtype: dns.TypeA, | ||
Class: dns.ClassINET, | ||
Ttl: 0, | ||
}, | ||
A: record.IP, | ||
}) | ||
return | ||
} | ||
} | ||
if !zone.DefaultIP.Equal(net.IP("")) { | ||
m.Answer = append(m.Answer, &dns.A{ | ||
Hdr: dns.RR_Header{ | ||
Name: q.Name, | ||
Rrtype: dns.TypeA, | ||
Class: dns.ClassINET, | ||
Ttl: 0, | ||
}, | ||
A: zone.DefaultIP, | ||
}) | ||
return | ||
} | ||
m.Rcode = dns.RcodeNameError | ||
return | ||
} | ||
} | ||
|
||
resolver := net.Resolver{ | ||
PreferGo: false, | ||
} | ||
switch q.Qtype { | ||
case dns.TypeNS: | ||
records, err := resolver.LookupNS(context.TODO(), q.Name) | ||
if err != nil { | ||
m.Rcode = dns.RcodeNameError | ||
return | ||
} | ||
for _, ns := range records { | ||
m.Answer = append(m.Answer, &dns.NS{ | ||
Hdr: dns.RR_Header{ | ||
Name: q.Name, | ||
Rrtype: dns.TypeNS, | ||
Class: dns.ClassINET, | ||
Ttl: 0, | ||
}, | ||
Ns: ns.Host, | ||
}) | ||
} | ||
case dns.TypeA: | ||
ips, err := resolver.LookupIPAddr(context.TODO(), q.Name) | ||
if err != nil { | ||
m.Rcode = dns.RcodeNameError | ||
return | ||
} | ||
for _, ip := range ips { | ||
if len(ip.IP.To4()) != net.IPv4len { | ||
continue | ||
} | ||
m.Answer = append(m.Answer, &dns.A{ | ||
Hdr: dns.RR_Header{ | ||
Name: q.Name, | ||
Rrtype: dns.TypeA, | ||
Class: dns.ClassINET, | ||
Ttl: 0, | ||
}, | ||
A: ip.IP.To4(), | ||
}) | ||
} | ||
} | ||
} | ||
} |
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,19 @@ | ||
package dns | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/containers/gvisor-tap-vsock/pkg/types" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
func ServeListener(ln net.Listener, zones []types.Zone) error { | ||
mux := dns.NewServeMux() | ||
handler := &dnsHandler{zones: zones} | ||
mux.HandleFunc(".", handler.handle) | ||
srv := &dns.Server{ | ||
Listener: ln, | ||
Handler: mux, | ||
} | ||
return srv.ActivateAndServe() | ||
} |
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