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 ¶
MarshalDomainTo marshals domain string struct to []byte and write to w.
func UnmarshalHeader ¶
UnmarshalHeader unmarshals []bytes to Header.
Types ¶
type AnswerHandler ¶
AnswerHandler function handles the dns TypeA or TypeAAAA answer.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a dns client struct.
func (*Client) AddHandler ¶
func (c *Client) AddHandler(h AnswerHandler)
AddHandler adds a custom handler to handle the resolved result (A and AAAA).
func (*Client) AddRecord ¶
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 ¶
Exchange handles request message and returns response message. TODO: optimize it
func (*Client) SetServers ¶
SetServers sets upstream dns servers for the given domain.
type Config ¶
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 ¶
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) SetMsgType ¶
SetMsgType sets the message type.
func (*Header) SetQdcount ¶
SetQdcount sets query count, most dns servers only support 1 query per request.
type LruCache ¶
type LruCache struct {
// contains filtered or unexported fields
}
LruCache is the struct of LruCache.
func (*LruCache) Set ¶
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 ¶
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 ¶
MakeResponse makes a dns response message for the given domain and ip address. Note: you should make sure ttl > 0.
func NewMessage ¶
NewMessage returns a new message.
func UnmarshalMessage ¶
UnmarshalMessage unmarshals []bytes to Message.
func (*Message) SetQuestion ¶
SetQuestion sets a question to dns message.
func (*Message) UnmarshalDomainPointTo ¶
UnmarshalDomainPointTo gets domain from offset point to string builder.
func (*Message) UnmarshalDomainTo ¶
UnmarshalDomainTo gets domain from bytes to string builder.
func (*Message) UnmarshalQuestion ¶
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 ¶
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 ¶
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 ¶
ListenAndServeTCP listen and serves on tcp port.
func (*Server) ListenAndServeUDP ¶
ListenAndServeUDP listen and serves on udp port.
func (*Server) ServePacket ¶
ServePacket serves dns packet conn.