github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/handler.go (about) 1 package fastdns 2 3 import ( 4 "net" 5 "net/netip" 6 ) 7 8 // Handler is implemented by any value that implements ServeDNS. 9 type Handler interface { 10 ServeDNS(rw ResponseWriter, req *Message) 11 } 12 13 // Error replies to the request with the specified Rcode. 14 func Error(rw ResponseWriter, req *Message, rcode Rcode) { 15 req.SetResponseHeader(rcode, 0) 16 _, _ = rw.Write(req.Raw) 17 } 18 19 // HOST1 replies to the request with the specified Host record. 20 func HOST1(rw ResponseWriter, req *Message, ttl uint32, ip netip.Addr) { 21 req.SetResponseHeader(RcodeNoError, 1) 22 req.Raw = AppendHOST1Record(req.Raw, req, ttl, ip) 23 _, _ = rw.Write(req.Raw) 24 } 25 26 // HOST replies to the request with the specified Host records. 27 func HOST(rw ResponseWriter, req *Message, ttl uint32, ips []netip.Addr) { 28 req.SetResponseHeader(RcodeNoError, uint16(len(ips))) 29 req.Raw = AppendHOSTRecord(req.Raw, req, ttl, ips) 30 _, _ = rw.Write(req.Raw) 31 } 32 33 // CNAME replies to the request with the specified CName and Host records. 34 func CNAME(rw ResponseWriter, req *Message, ttl uint32, cnames []string, ips []netip.Addr) { 35 req.SetResponseHeader(RcodeNoError, uint16(len(cnames)+len(ips))) 36 req.Raw = AppendCNAMERecord(req.Raw, req, ttl, cnames, ips) 37 _, _ = rw.Write(req.Raw) 38 } 39 40 // SRV replies to the request with the specified SRV records. 41 func SRV(rw ResponseWriter, req *Message, ttl uint32, srvs []net.SRV) { 42 req.SetResponseHeader(RcodeNoError, uint16(len(srvs))) 43 req.Raw = AppendSRVRecord(req.Raw, req, ttl, srvs) 44 _, _ = rw.Write(req.Raw) 45 } 46 47 // NS replies to the request with the specified CName and Host records. 48 func NS(rw ResponseWriter, req *Message, ttl uint32, nameservers []net.NS) { 49 req.SetResponseHeader(RcodeNoError, uint16(len(nameservers))) 50 req.Raw = AppendNSRecord(req.Raw, req, ttl, nameservers) 51 _, _ = rw.Write(req.Raw) 52 } 53 54 // SOA replies to the request with the specified SOA records. 55 func SOA(rw ResponseWriter, req *Message, ttl uint32, mname, rname net.NS, serial, refresh, retry, expire, minimum uint32) { 56 req.SetResponseHeader(RcodeNoError, 1) 57 req.Raw = AppendSOARecord(req.Raw, req, ttl, mname, rname, serial, refresh, retry, expire, minimum) 58 _, _ = rw.Write(req.Raw) 59 } 60 61 // MX replies to the request with the specified MX records. 62 func MX(rw ResponseWriter, req *Message, ttl uint32, mxs []net.MX) { 63 req.SetResponseHeader(RcodeNoError, uint16(len(mxs))) 64 req.Raw = AppendMXRecord(req.Raw, req, ttl, mxs) 65 _, _ = rw.Write(req.Raw) 66 } 67 68 // PTR replies to the request with the specified PTR records. 69 func PTR(rw ResponseWriter, req *Message, ttl uint32, ptr string) { 70 req.SetResponseHeader(RcodeNoError, 1) 71 req.Raw = AppendPTRRecord(req.Raw, req, ttl, ptr) 72 _, _ = rw.Write(req.Raw) 73 } 74 75 // TXT replies to the request with the specified TXT records. 76 func TXT(rw ResponseWriter, req *Message, ttl uint32, txt string) { 77 req.SetResponseHeader(RcodeNoError, 1) 78 req.Raw = AppendTXTRecord(req.Raw, req, ttl, txt) 79 _, _ = rw.Write(req.Raw) 80 }