initial commit

This commit is contained in:
MDK
2024-02-21 15:28:24 +08:00
commit 66580aad74
6 changed files with 231 additions and 0 deletions

31
utils/dns_utils.go Normal file
View File

@@ -0,0 +1,31 @@
// dns utils
package utils
import (
"github.com/miekg/dns"
)
// build the question section of a dns packet
func QuestionMaker(domain string, qclass uint16, qtype uint16) *dns.Question {
return &dns.Question{Name: dns.Fqdn(domain), Qtype: qtype, Qclass: qclass}
}
// build a specific query message
func QueryMaker(domain string, rd bool, qclass uint16, qtype uint16, edns bool) *dns.Msg {
msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = rd
msg.Question = make([]dns.Question, 1)
msg.Question[0] = *QuestionMaker(domain, qclass, qtype)
if edns {
msg = msg.SetEdns0(4096, false)
}
return msg
}
// query and receive the response
func DNSQuery(addr string, domain string, rd bool, qclass uint16, qtype uint16, edns bool) (*dns.Msg, error) {
msg := QueryMaker(domain, rd, qclass, qtype, edns)
res, err := dns.Exchange(msg, addr)
return res, err
}

68
utils/other_utils.go Normal file
View File

@@ -0,0 +1,68 @@
package utils
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
"strings"
)
type ResolverBehavior struct {
Dnssec bool `json:"dnssec"`
QnameEncode bool `json:"0x20"`
QueryMerge int `json:"merge_dup"`
MaxNsDepth int `json:"max_ns_depth"`
MaxCnameDepth int `json:"max_cname_depth"`
RetryLimit int `json:"retry_limit"`
FetchLimit int `json:"fetch_limit"`
TimeoutStart int64 `json:"timeout_start"`
TimeoutEnd int64 `json:"timeout_end"`
}
func IsValidIP(ip string) bool {
res := net.ParseIP(ip)
return res != nil
}
func IPv4ToInt(ip string) (uint32, error) {
if !IsValidIP(ip) {
return 0, fmt.Errorf("invalid IP address: %s", ip)
}
labels := strings.Split(ip, ".")
var result uint32
for _, label := range labels {
label_val, _ := strconv.Atoi(label)
result = (result << 8) | uint32(label_val)
}
return result, nil
}
func GetResult(addr string, token int) (*ResolverBehavior, error) {
data := new(ResolverBehavior)
url := "http://" + addr + "/results/" + strconv.Itoa(token)
response, err := http.Get(url)
if err != nil {
return data, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return data, fmt.Errorf("wrong HTTP status code : %v", response.StatusCode)
}
err = json.NewDecoder(response.Body).Decode(data)
if err != nil {
return data, err
}
return data, nil
}
func OutputJson(data interface{}) (string, error) {
json_byte, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(json_byte), nil
}