package request
import (
"net"
"strings"
"github.com/coredns/coredns/plugin/pkg/edns"
"github.com/miekg/dns"
)
type Request struct {
Req *dns.Msg
W dns.ResponseWriter
Zone string
size uint16
do bool
family int8
name string
ip string
port string
localPort string
localIP string
}
func (r *Request) NewWithQuestion(name string, typ uint16) Request {
req1 := Request{W: r.W, Req: r.Req.Copy()}
req1.Req.Question[0] = dns.Question{Name: dns.Fqdn(name), Qclass: dns.ClassINET, Qtype: typ}
return req1
}
func (r *Request) IP() string {
if r.ip != "" {
return r.ip
}
ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
if err != nil {
r.ip = r.W.RemoteAddr().String()
return r.ip
}
r.ip = ip
return r.ip
}
func (r *Request) LocalIP() string {
if r.localIP != "" {
return r.localIP
}
ip, _, err := net.SplitHostPort(r.W.LocalAddr().String())
if err != nil {
r.localIP = r.W.LocalAddr().String()
return r.localIP
}
r.localIP = ip
return r.localIP
}
func (r *Request) Port() string {
if r.port != "" {
return r.port
}
_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
if err != nil {
r.port = "0"
return r.port
}
r.port = port
return r.port
}
func (r *Request) LocalPort() string {
if r.localPort != "" {
return r.localPort
}
_, port, err := net.SplitHostPort(r.W.LocalAddr().String())
if err != nil {
r.localPort = "0"
return r.localPort
}
r.localPort = port
return r.localPort
}
func (r *Request) RemoteAddr() string { return r.W.RemoteAddr().String() }
func (r *Request) LocalAddr() string { return r.W.LocalAddr().String() }
func (r *Request) Proto() string {
if _, ok := r.W.RemoteAddr().(*net.UDPAddr); ok {
return "udp"
}
if _, ok := r.W.RemoteAddr().(*net.TCPAddr); ok {
return "tcp"
}
return "udp"
}
func (r *Request) Family() int {
if r.family != 0 {
return int(r.family)
}
var a net.IP
ip := r.W.RemoteAddr()
if i, ok := ip.(*net.UDPAddr); ok {
a = i.IP
}
if i, ok := ip.(*net.TCPAddr); ok {
a = i.IP
}
if a.To4() != nil {
r.family = 1
return 1
}
r.family = 2
return 2
}
func (r *Request) Do() bool {
if r.size != 0 {
return r.do
}
r.Size()
return r.do
}
func (r *Request) Len() int { return r.Req.Len() }
func (r *Request) Size() int {
if r.size != 0 {
return int(r.size)
}
size := uint16(0)
if o := r.Req.IsEdns0(); o != nil {
r.do = o.Do()
size = o.UDPSize()
}
size = edns.Size(r.Proto(), size)
r.size = size
return int(size)
}
func (r *Request) SizeAndDo(m *dns.Msg) bool {
o := r.Req.IsEdns0()
if o == nil {
return false
}
if mo := m.IsEdns0(); mo != nil {
mo.Hdr.Name = "."
mo.Hdr.Rrtype = dns.TypeOPT
mo.SetVersion(0)
mo.SetUDPSize(o.UDPSize())
mo.Hdr.Ttl &= 0xff00
if o.Do() {
mo.SetDo()
}
return true
}
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.SetVersion(0)
o.Hdr.Ttl &= 0xff00
if len(o.Option) > 0 {
o.Option = supportedOptions(o.Option)
}
m.Extra = append(m.Extra, o)
return true
}
func (r *Request) Scrub(reply *dns.Msg) *dns.Msg {
reply.Truncate(r.Size())
if reply.Compress {
return reply
}
if r.Proto() == "udp" {
rl := reply.Len()
if rl > 1480 && r.Family() == 1 {
reply.Compress = true
}
if rl > 1220 && r.Family() == 2 {
reply.Compress = true
}
}
return reply
}
func (r *Request) Type() string {
if r.Req == nil {
return ""
}
if len(r.Req.Question) == 0 {
return ""
}
return dns.Type(r.Req.Question[0].Qtype).String()
}
func (r *Request) QType() uint16 {
if r.Req == nil {
return 0
}
if len(r.Req.Question) == 0 {
return 0
}
return r.Req.Question[0].Qtype
}
func (r *Request) Name() string {
if r.name != "" {
return r.name
}
if r.Req == nil {
r.name = "."
return "."
}
if len(r.Req.Question) == 0 {
r.name = "."
return "."
}
r.name = strings.ToLower(dns.Name(r.Req.Question[0].Name).String())
return r.name
}
func (r *Request) QName() string {
if r.Req == nil {
return "."
}
if len(r.Req.Question) == 0 {
return "."
}
return dns.Name(r.Req.Question[0].Name).String()
}
func (r *Request) Class() string {
if r.Req == nil {
return ""
}
if len(r.Req.Question) == 0 {
return ""
}
return dns.Class(r.Req.Question[0].Qclass).String()
}
func (r *Request) QClass() uint16 {
if r.Req == nil {
return 0
}
if len(r.Req.Question) == 0 {
return 0
}
return r.Req.Question[0].Qclass
}
func (r *Request) Clear() {
r.name = ""
r.ip = ""
r.localIP = ""
r.port = ""
r.localPort = ""
r.family = 0
r.size = 0
r.do = false
}
func (r *Request) Match(reply *dns.Msg) bool {
if len(reply.Question) != 1 {
return false
}
if !reply.Response {
return false
}
if strings.ToLower(reply.Question[0].Name) != r.Name() {
return false
}
if reply.Question[0].Qtype != r.QType() {
return false
}
return true
}