Added simple error handling
This commit is contained in:
@ -3,7 +3,7 @@ package dnsexit_manager
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
@ -14,19 +14,19 @@ import (
|
||||
const API_BASE_URL = "https://api.dnsexit.com/dns/"
|
||||
|
||||
type Config struct {
|
||||
DOMAIN_NAME string
|
||||
API_KEY string
|
||||
API_URL string
|
||||
DomainName string
|
||||
API_Key string
|
||||
API_URL string
|
||||
}
|
||||
|
||||
type client struct {
|
||||
Locker sync.Mutex
|
||||
API_KEY string
|
||||
API_Key string
|
||||
API_URL string
|
||||
Domain string
|
||||
}
|
||||
|
||||
type dnsexitDTO struct {
|
||||
type dnsexitRequestDTO struct {
|
||||
ApiKey string `json:"apikey"`
|
||||
Domain string `json:"domain"`
|
||||
Add *dns.Record `json:"add,omitempty"`
|
||||
@ -34,12 +34,31 @@ type dnsexitDTO struct {
|
||||
Delete *dns.Record `json:"delete,omitempty"`
|
||||
}
|
||||
|
||||
type dnsexitResponseDTO struct {
|
||||
Code int
|
||||
Message string
|
||||
}
|
||||
|
||||
func parseError(body *[]byte) error {
|
||||
var response dnsexitResponseDTO
|
||||
|
||||
if err := json.Unmarshal(*body, &response); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.Code != 0 {
|
||||
return errors.New(response.Message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *client) AddRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
c.Locker.Lock()
|
||||
defer c.Locker.Unlock()
|
||||
|
||||
request_body, err := json.Marshal(&dnsexitDTO{
|
||||
ApiKey: c.API_KEY,
|
||||
request_body, err := json.Marshal(&dnsexitRequestDTO{
|
||||
ApiKey: c.API_Key,
|
||||
Domain: c.Domain,
|
||||
Add: rec,
|
||||
})
|
||||
@ -48,9 +67,7 @@ func (c *client) AddRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(string(request_body))
|
||||
|
||||
resp, err := http.Post(
|
||||
response, err := http.Post(
|
||||
c.API_URL,
|
||||
"application/json",
|
||||
bytes.NewBuffer(request_body),
|
||||
@ -60,12 +77,16 @@ func (c *client) AddRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
defer response.Body.Close()
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = parseError(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dns.Response{Message: string(body)}, nil
|
||||
}
|
||||
|
||||
@ -73,11 +94,12 @@ func (c *client) DeleteRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
c.Locker.Lock()
|
||||
defer c.Locker.Unlock()
|
||||
|
||||
request_body, err := json.Marshal(&dnsexitDTO{
|
||||
ApiKey: c.API_KEY,
|
||||
request_body, err := json.Marshal(&dnsexitRequestDTO{
|
||||
ApiKey: c.API_Key,
|
||||
Domain: c.Domain,
|
||||
Delete: rec,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -98,6 +120,10 @@ func (c *client) DeleteRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = parseError(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dns.Response{Message: string(body)}, nil
|
||||
}
|
||||
|
||||
@ -105,8 +131,8 @@ func (c *client) UpdateRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
c.Locker.Lock()
|
||||
defer c.Locker.Unlock()
|
||||
|
||||
request_body, err := json.Marshal(&dnsexitDTO{
|
||||
ApiKey: c.API_KEY,
|
||||
request_body, err := json.Marshal(&dnsexitRequestDTO{
|
||||
ApiKey: c.API_Key,
|
||||
Domain: c.Domain,
|
||||
Update: rec,
|
||||
})
|
||||
@ -131,6 +157,10 @@ func (c *client) UpdateRecord(rec *dns.Record) (*dns.Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = parseError(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dns.Response{Message: string(body)}, nil
|
||||
}
|
||||
|
||||
@ -141,7 +171,7 @@ func (c *client) GetRecords() (*[]dns.Record, error) {
|
||||
result_chan := make(chan []*dns.Record)
|
||||
err_chan := make(chan error)
|
||||
|
||||
err, nameservers := c.getNSRecods()
|
||||
nameservers, err := c.getNSRecods()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -151,7 +181,7 @@ func (c *client) GetRecords() (*[]dns.Record, error) {
|
||||
|
||||
go func(ns string) {
|
||||
defer wg.Done()
|
||||
err, records := find_dns_records(c.Domain, ns)
|
||||
records, err := find_dns_records(c.Domain, ns)
|
||||
|
||||
if err != nil {
|
||||
err_chan <- err
|
||||
@ -185,7 +215,7 @@ func (c *client) GetRecords() (*[]dns.Record, error) {
|
||||
}
|
||||
|
||||
func (conf Config) New() (dns.Actions, error) {
|
||||
var api_url string = API_BASE_URL
|
||||
var api_url = API_BASE_URL
|
||||
|
||||
if len(conf.API_URL) > 5 {
|
||||
api_url = conf.API_URL
|
||||
@ -193,8 +223,8 @@ func (conf Config) New() (dns.Actions, error) {
|
||||
|
||||
return &client{
|
||||
Locker: sync.Mutex{},
|
||||
API_KEY: conf.API_KEY,
|
||||
API_Key: conf.API_Key,
|
||||
API_URL: api_url,
|
||||
Domain: conf.DOMAIN_NAME,
|
||||
Domain: conf.DomainName,
|
||||
}, nil
|
||||
}
|
||||
|
36
utilities.go
36
utilities.go
@ -10,46 +10,46 @@ import (
|
||||
dns_req "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func (c *client) getNSRecods() (error, *[]string) {
|
||||
func (c *client) getNSRecods() (*[]string, error) {
|
||||
var nameservers []string
|
||||
|
||||
ns, err := net.LookupNS(c.Domain)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, v := range ns {
|
||||
nameservers = append(nameservers, v.Host[:len(v.Host)-1]+":53")
|
||||
}
|
||||
|
||||
return nil, &nameservers
|
||||
return &nameservers, nil
|
||||
}
|
||||
|
||||
func parse_record(record dns_req.RR) (error, *dns.Record) {
|
||||
splitted := strings.SplitN(record.String(), "\t", -1)
|
||||
func parse_record(record dns_req.RR) (*dns.Record, error) {
|
||||
split := strings.SplitN(record.String(), "\t", -1)
|
||||
|
||||
if len(splitted) == 5 {
|
||||
if len(split) == 5 {
|
||||
var parsed dns.Record
|
||||
rec_type := dns.RecordType(splitted[3])
|
||||
rec_type := dns.RecordType(split[3])
|
||||
|
||||
if !rec_type.Check() {
|
||||
return errors.New("provided DNS type is not exist in the main library"), nil
|
||||
return nil, errors.New("provided DNS type is not exist in the main library")
|
||||
}
|
||||
|
||||
parsed.Type = rec_type
|
||||
ttl, _ := strconv.ParseUint(splitted[1], 10, 64)
|
||||
ttl, _ := strconv.ParseUint(split[1], 10, 64)
|
||||
|
||||
parsed.Name = splitted[0]
|
||||
parsed.Content = splitted[4]
|
||||
parsed.Name = split[0]
|
||||
parsed.Content = split[4]
|
||||
parsed.TTL = uint(ttl)
|
||||
|
||||
return nil, &parsed
|
||||
return &parsed, nil
|
||||
}
|
||||
|
||||
return errors.New("no meaningful record provided"), nil
|
||||
return nil, errors.New("no meaningful record provided")
|
||||
}
|
||||
|
||||
func find_dns_records(domain string, nameserver string) (error, []*dns.Record) {
|
||||
func find_dns_records(domain string, nameserver string) ([]*dns.Record, error) {
|
||||
var subdomains []*dns.Record
|
||||
|
||||
tr := new(dns_req.Transfer)
|
||||
@ -58,21 +58,21 @@ func find_dns_records(domain string, nameserver string) (error, []*dns.Record) {
|
||||
|
||||
client, err := tr.In(m, nameserver)
|
||||
if err != nil {
|
||||
return errors.New("failed to zone transfer in " + err.Error()), nil
|
||||
return nil, errors.New("failed to zone transfer in " + err.Error())
|
||||
}
|
||||
|
||||
for msg := range client {
|
||||
if msg.Error != nil {
|
||||
return msg.Error, nil
|
||||
return nil, msg.Error
|
||||
}
|
||||
|
||||
for _, r := range msg.RR {
|
||||
err, record := parse_record(r)
|
||||
record, err := parse_record(r)
|
||||
if err == nil {
|
||||
subdomains = append(subdomains, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, subdomains
|
||||
return subdomains, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user