2022-10-28 21:36:57 +00:00
|
|
|
package xip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Xip struct {
|
|
|
|
Server dns.Server
|
|
|
|
NameServers []*dns.NS
|
|
|
|
Zone string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
flyRegion = os.Getenv("FLY_REGION")
|
|
|
|
dottedIpV4Regex = regexp.MustCompile(`(?:^|(?:[\w\d])+\.)(((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4})($|[.-])`)
|
|
|
|
dashedIpV4Regex = regexp.MustCompile(`(?:^|(?:[\w\d])+\.)(((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\-?\b){4})($|[.-])`)
|
|
|
|
)
|
|
|
|
|
2022-10-28 22:27:58 +00:00
|
|
|
func (xip *Xip) handleA(question dns.Question, message *dns.Msg) {
|
2022-10-28 21:36:57 +00:00
|
|
|
fqdn := question.Name
|
|
|
|
|
|
|
|
for _, ipV4RE := range []*regexp.Regexp{dashedIpV4Regex, dottedIpV4Regex} {
|
|
|
|
if ipV4RE.MatchString(fqdn) {
|
|
|
|
match := ipV4RE.FindStringSubmatch(fqdn)[1]
|
|
|
|
match = strings.ReplaceAll(match, "-", ".")
|
|
|
|
ipV4Address := net.ParseIP(match).To4()
|
|
|
|
if ipV4Address == nil {
|
2022-10-28 22:27:58 +00:00
|
|
|
message.Rcode = dns.RcodeNameError
|
|
|
|
message.Ns = append(message.Ns, xip.SOARecord(question))
|
|
|
|
return
|
2022-10-28 21:36:57 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 22:27:58 +00:00
|
|
|
record := &dns.A{
|
2022-10-28 21:36:57 +00:00
|
|
|
Hdr: dns.RR_Header{
|
2022-10-28 22:07:09 +00:00
|
|
|
// Ttl: uint32((time.Hour * 24 * 7).Seconds()),
|
|
|
|
Ttl: uint32((time.Second * 10).Seconds()),
|
2022-10-28 21:36:57 +00:00
|
|
|
Name: fqdn,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
A: ipV4Address,
|
|
|
|
}
|
|
|
|
log.Printf("(%s) %s => %s\n", flyRegion, fqdn, ipV4Address)
|
2022-10-28 22:27:58 +00:00
|
|
|
message.Answer = append(message.Answer, record)
|
2022-10-28 21:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:07:09 +00:00
|
|
|
func (xip *Xip) SOARecord(question dns.Question) *dns.SOA {
|
2022-10-28 21:36:57 +00:00
|
|
|
soa := new(dns.SOA)
|
|
|
|
soa.Hdr = dns.RR_Header{
|
2022-10-28 22:07:09 +00:00
|
|
|
Name: question.Name,
|
|
|
|
Rrtype: dns.TypeSOA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
// Ttl: uint32((time.Hour * 24 * 7).Seconds()),
|
|
|
|
Ttl: uint32((time.Second * 10).Seconds()),
|
2022-10-28 21:36:57 +00:00
|
|
|
Rdlength: 0,
|
|
|
|
}
|
|
|
|
soa.Ns = "ns.local-ip.dev."
|
|
|
|
soa.Mbox = "admin.local-ip.dev."
|
|
|
|
soa.Serial = 2022102800
|
2022-10-28 22:07:09 +00:00
|
|
|
// soa.Refresh = uint32((time.Minute * 15).Seconds())
|
|
|
|
soa.Refresh = uint32((time.Second * 10).Seconds())
|
|
|
|
// soa.Retry = uint32((time.Minute * 15).Seconds())
|
|
|
|
soa.Retry = uint32((time.Second * 10).Seconds())
|
|
|
|
// soa.Expire = uint32((time.Minute * 30).Seconds())
|
|
|
|
soa.Expire = uint32((time.Second * 10).Seconds())
|
|
|
|
// soa.Minttl = uint32((time.Minute * 5).Seconds())
|
|
|
|
soa.Minttl = uint32((time.Second * 10).Seconds())
|
|
|
|
|
|
|
|
return soa
|
|
|
|
}
|
|
|
|
|
|
|
|
func (xip *Xip) handleQuery(message *dns.Msg) {
|
|
|
|
for _, question := range message.Question {
|
|
|
|
switch question.Qtype {
|
|
|
|
case dns.TypeA:
|
2022-10-28 22:27:58 +00:00
|
|
|
xip.handleA(question, message)
|
2022-10-28 22:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 21:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (xip *Xip) handleDnsRequest(response dns.ResponseWriter, request *dns.Msg) {
|
|
|
|
go func() {
|
|
|
|
message := new(dns.Msg)
|
|
|
|
message.SetReply(request)
|
|
|
|
message.Compress = true
|
|
|
|
message.Authoritative = true
|
|
|
|
message.RecursionAvailable = false
|
|
|
|
|
|
|
|
switch request.Opcode {
|
|
|
|
case dns.OpcodeQuery:
|
|
|
|
xip.handleQuery(message)
|
|
|
|
default:
|
2022-10-28 22:07:09 +00:00
|
|
|
message.MsgHdr.Rcode = dns.RcodeRefused
|
2022-10-28 21:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteMsg(message)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (xip *Xip) StartServer() {
|
|
|
|
log.Printf("Listening on %s\n", xip.Server.Addr)
|
|
|
|
err := xip.Server.ListenAndServe()
|
|
|
|
defer xip.Server.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start server: %s\n ", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXip(zone string, nameservers []string, port int) (xip *Xip) {
|
|
|
|
xip = &Xip{}
|
|
|
|
|
|
|
|
for _, ns := range nameservers {
|
|
|
|
xip.NameServers = append(xip.NameServers, &dns.NS{Ns: ns})
|
|
|
|
}
|
|
|
|
|
|
|
|
xip.Server = dns.Server{
|
|
|
|
Addr: ":" + strconv.Itoa(port),
|
|
|
|
Net: "udp",
|
|
|
|
}
|
|
|
|
|
|
|
|
dns.HandleFunc(zone, xip.handleDnsRequest)
|
|
|
|
|
|
|
|
return xip
|
|
|
|
}
|