1
package doh
2

3
import (
4
	"strings"
5

6
	"github.com/miekg/dns"
7
)
8

9
// Question struct
10
type Question struct {
11
	Name   string `json:"name"`
12
	Qtype  uint16 `json:"type"`
13
	Qclass uint16 `json:"-"`
14
}
15

16
// RR struct
17
type RR struct {
18
	Name string `json:"name"`
19
	Type uint16 `json:"type"`
20
	TTL  uint32 `json:"TTL"`
21
	Data string `json:"data"`
22
}
23

24
// Msg struct
25
type Msg struct {
26
	Status    int
27
	TC        bool
28
	RD        bool
29
	RA        bool
30
	AD        bool
31
	CD        bool
32
	Question  []Question
33
	Answer    []RR `json:",omitempty"`
34
	Authority []RR `json:",omitempty"`
35
}
36

37
// NewMsg function
38
func NewMsg(m *dns.Msg) *Msg {
39 1
	if m == nil {
40 1
		return nil
41
	}
42

43 1
	msg := &Msg{
44 1
		Status:    m.Rcode,
45 1
		TC:        m.Truncated,
46 1
		RD:        m.RecursionDesired,
47 1
		RA:        m.RecursionAvailable,
48 1
		AD:        m.AuthenticatedData,
49 1
		CD:        m.CheckingDisabled,
50 1
		Question:  make([]Question, len(m.Question)),
51 1
		Answer:    make([]RR, len(m.Answer)),
52 1
		Authority: make([]RR, len(m.Ns)),
53
	}
54

55
	for i, q := range m.Question {
56 1
		msg.Question[i] = Question(q)
57
	}
58

59
	for i, a := range m.Answer {
60 1
		msg.Answer[i] = RR{
61 1
			Name: a.Header().Name,
62 1
			Type: a.Header().Rrtype,
63 1
			TTL:  a.Header().Ttl,
64 1
			Data: strings.TrimPrefix(a.String(), a.Header().String()),
65
		}
66
	}
67

68
	for i, a := range m.Ns {
69 1
		msg.Authority[i] = RR{
70 1
			Name: a.Header().Name,
71 1
			Type: a.Header().Rrtype,
72 1
			TTL:  a.Header().Ttl,
73 1
			Data: strings.TrimPrefix(a.String(), a.Header().String()),
74
		}
75
	}
76

77 1
	return msg
78
}

Read our documentation on viewing source code .

Loading