Documentation ¶
Index ¶
- Constants
- func MarshalDomainTo(w io.Writer, domain string) (n int, err error)
- func UnmarshalHeader(b []byte, h *Header) error
- type AnswerHandler
- type Client
- type Config
- type Header
- type LruCache
- type Message
- func (m *Message) AddAnswer(rr *RR) error
- func (m *Message) Marshal() ([]byte, error)
- func (m *Message) MarshalTo(w io.Writer) (n int, err error)
- func (m *Message) SetQuestion(q *Question) error
- func (m *Message) UnmarshalDomainPointTo(sb *strings.Builder, offset int) error
- func (m *Message) UnmarshalDomainTo(sb *strings.Builder, b []byte) (int, error)
- func (m *Message) UnmarshalQuestion(b []byte, q *Question) (n int, err error)
- func (m *Message) UnmarshalRR(start int, rr *RR) (n int, err error)
- type MsgType
- type Question
- type RR
- type Server
- type UPStream
Constants ¶
const ( QTypeA uint16 = 1 //ipv4 QTypeAAAA uint16 = 28 ///ipv6 )
Query types.
const ClassINET uint16 = 1
ClassINET .
const HeaderLen = 12
HeaderLen is the length of dns msg header.
const UDPMaxLen = 1232
UDPMaxLen is the max size of udp dns request. https://www.dnsflagday.net/2020/
Variables ¶
This section is empty.
Functions ¶
func MarshalDomainTo ¶ added in v0.10.0
MarshalDomainTo marshals domain string struct to []byte and write to w.
func UnmarshalHeader ¶ added in v0.6.5
UnmarshalHeader unmarshals []bytes to Header.
Types ¶
type AnswerHandler ¶
AnswerHandler function handles the dns TypeA or TypeAAAA answer.
type Client ¶ added in v0.6.5
type Client struct {
// contains filtered or unexported fields
}
Client is a dns client struct.
func (*Client) AddHandler ¶ added in v0.6.5
func (c *Client) AddHandler(h AnswerHandler)
AddHandler adds a custom handler to handle the resolved result (A and AAAA).
func (*Client) AddRecord ¶ added in v0.6.5
AddRecord adds custom record to dns cache, format: www.example.com/1.2.3.4 or www.example.com/2606:2800:220:1:248:1893:25c8:1946
func (*Client) Exchange ¶ added in v0.6.5
Exchange handles request message and returns response message. TODO: optimize it
func (*Client) SetServers ¶ added in v0.6.7
SetServers sets upstream dns servers for the given domain.
type Config ¶ added in v0.6.6
type Config struct { Servers []string Timeout int MaxTTL int MinTTL int Records []string AlwaysTCP bool CacheSize int CacheLog bool NoAAAA bool }
Config for dns.
type Header ¶ added in v0.6.5
type Header struct { ID uint16 Bits uint16 QDCOUNT uint16 ANCOUNT uint16 NSCOUNT uint16 ARCOUNT uint16 }
Header format: https://www.rfc-editor.org/rfc/rfc1035#section-4.1.1 The header contains the following fields:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
func (*Header) MarshalTo ¶ added in v0.10.0
MarshalTo marshals header struct to []byte and write to w.
func (*Header) SetAncount ¶ added in v0.6.5
SetAncount sets answers count.
func (*Header) SetMsgType ¶ added in v0.6.5
SetMsgType sets the message type.
func (*Header) SetQdcount ¶ added in v0.6.5
SetQdcount sets query count, most dns servers only support 1 query per request.
type LruCache ¶ added in v0.12.0
type LruCache struct {
// contains filtered or unexported fields
}
LruCache is the struct of LruCache.
func NewLruCache ¶ added in v0.12.0
NewLruCache returns a new LruCache.
func (*LruCache) Set ¶ added in v0.12.0
Set sets an item with key, value, and ttl(seconds). if the ttl is zero, this item will be set and never be deleted. if the key exists, update it with value and exp and move it to head. if the key does not exist, put a new item to the cache's head. finally, remove the tail if the cache is full.
type Message ¶ added in v0.6.5
type Message struct { Header // most dns implementation only support 1 question Question *Question Answers []*RR Authority []*RR Additional []*RR // contains filtered or unexported fields }
Message format: https://www.rfc-editor.org/rfc/rfc1035#section-4.1 All communications inside of the domain protocol are carried in a single format called a message. The top level format of message is divided into 5 sections (some of which are empty in certain cases) shown below:
+---------------------+ | Header | +---------------------+ | Question | the question for the name server +---------------------+ | Answer | RRs answering the question +---------------------+ | Authority | RRs pointing toward an authority +---------------------+ | Additional | RRs holding additional information
func MakeResponse ¶ added in v0.16.0
MakeResponse makes a dns response message for the given domain and ip address. Note: you should make sure ttl > 0.
func NewMessage ¶ added in v0.6.5
NewMessage returns a new message.
func UnmarshalMessage ¶ added in v0.6.5
UnmarshalMessage unmarshals []bytes to Message.
func (*Message) MarshalTo ¶ added in v0.10.3
MarshalTo marshals message struct to []byte and write to w.
func (*Message) SetQuestion ¶ added in v0.6.5
SetQuestion sets a question to dns message.
func (*Message) UnmarshalDomainPointTo ¶ added in v0.10.3
UnmarshalDomainPointTo gets domain from offset point to string builder.
func (*Message) UnmarshalDomainTo ¶ added in v0.10.3
UnmarshalDomainTo gets domain from bytes to string builder.
func (*Message) UnmarshalQuestion ¶ added in v0.6.5
UnmarshalQuestion unmarshals []bytes to Question.
type Question ¶
Question format: https://www.rfc-editor.org/rfc/rfc1035#section-4.1.2 The question section is used to carry the "question" in most queries, i.e., the parameters that define what is being asked. The section contains QDCOUNT (usually 1) entries, each of the following format:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / QNAME / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QTYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QCLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
func NewQuestion ¶ added in v0.6.5
NewQuestion returns a new dns question.
type RR ¶
type RR struct { NAME string TYPE uint16 CLASS uint16 TTL uint32 RDLENGTH uint16 RDATA []byte IP netip.Addr }
RR format: https://www.rfc-editor.org/rfc/rfc1035#section-3.2.1 https://www.rfc-editor.org/rfc/rfc1035#section-4.1.3 The answer, authority, and additional sections all share the same format: a variable number of resource records, where the number of records is specified in the corresponding count field in the header. Each resource record has the following format:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / / / NAME / | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | CLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TTL | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | RDLENGTH | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| / RDATA / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
type Server ¶ added in v0.6.5
type Server struct { // Client is used to communicate with upstream dns servers *Client // contains filtered or unexported fields }
Server is a dns server struct.
func (*Server) ListenAndServeTCP ¶ added in v0.6.5
ListenAndServeTCP listen and serves on tcp port.
func (*Server) ListenAndServeUDP ¶ added in v0.6.5
ListenAndServeUDP listen and serves on udp port.
func (*Server) ServePacket ¶ added in v0.10.3
ServePacket serves dns packet conn.
type UPStream ¶ added in v0.10.0
type UPStream struct {
// contains filtered or unexported fields
}
UPStream is a dns upstream.
func NewUPStream ¶ added in v0.10.0
NewUPStream returns a new UpStream.