diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/dns_utils.go | 31 | ||||
| -rw-r--r-- | utils/other_utils.go | 68 |
2 files changed, 99 insertions, 0 deletions
diff --git a/utils/dns_utils.go b/utils/dns_utils.go new file mode 100644 index 0000000..60e3d62 --- /dev/null +++ b/utils/dns_utils.go @@ -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 +} diff --git a/utils/other_utils.go b/utils/other_utils.go new file mode 100644 index 0000000..a25dcdc --- /dev/null +++ b/utils/other_utils.go @@ -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 +} |
