Skip to content

Commit

Permalink
all integers now int64, dnsfilter accepts @ name
Browse files Browse the repository at this point in the history
  • Loading branch information
ejstreet committed Feb 19, 2023
1 parent f44fbd7 commit 83b58e6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 52 deletions.
80 changes: 58 additions & 22 deletions omglol/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
)

// Get a list of all of your DNS records for an address. See https://api.omg.lol/#token-get-dns-retrieve-dns-records-for-an-address
Expand Down Expand Up @@ -48,15 +49,19 @@ func (c *Client) ListDNSRecords(address string) (*[]DNSRecord, error) {
var d []DNSRecord

for _, record := range r.Response.DNS {
id, _ := strconv.Atoi(record.ID)
ttl, _ := strconv.Atoi(record.TTL)
id_int, _ := strconv.Atoi(record.ID)
id := int64(id_int)
ttl_int, _ := strconv.Atoi(record.TTL)
ttl := int64(ttl_int)

var p int
var p *int64
if record.Priority != nil {
p, _ = strconv.Atoi(*record.Priority)
p_int, _ := strconv.Atoi(*record.Priority)
p_int64 := int64(p_int)
p = &p_int64
}

x := newDNSRecord(id, ttl, record.Type, record.Name, record.Data, record.CreatedAt, record.UpdatedAt, &p)
x := newDNSRecord(id, ttl, record.Type, record.Name, record.Data, record.CreatedAt, record.UpdatedAt, p)

d = append(d, *x)
}
Expand All @@ -78,7 +83,17 @@ func (c *Client) FilterDNSRecord(address string, filterCriteria map[string]inter
for key, value := range filterCriteria {
switch key {
case "ID":
if *record.ID != value.(int) {
var id int64
switch v := value.(type) {
case int:
id = int64(v)
case int64:
id = v
default:
match = false
break
}
if id != *record.ID {
match = false
break
}
Expand All @@ -88,7 +103,10 @@ func (c *Client) FilterDNSRecord(address string, filterCriteria map[string]inter
break
}
case "Name":
if *record.Name != value.(string) {
if strings.Contains(*record.Name, ".") && *record.Name != value.(string) {
match = false
break
} else if !strings.Contains(*record.Name, ".") && "@" != value.(string) {
match = false
break
}
Expand All @@ -98,17 +116,38 @@ func (c *Client) FilterDNSRecord(address string, filterCriteria map[string]inter
break
}
case "Priority":
priority, ok := value.(*int)
if !ok {
switch v := value.(type) {
case int:
if record.Priority != nil && *record.Priority != int64(v) {
match = false
break
}
case int64:
if record.Priority != nil && *record.Priority != v {
match = false
break
}
case nil:
if record.Priority != nil {
match = false
break
}
default:
match = false
break
}
if record.Priority != priority {
case "TTL":
var ttl int64
switch v := value.(type) {
case int:
ttl = int64(v)
case int64:
ttl = v
default:
match = false
break
}
case "TTL":
if *record.TTL != value.(int) {
if ttl != *record.TTL {
match = false
break
}
Expand All @@ -125,9 +164,6 @@ func (c *Client) FilterDNSRecord(address string, filterCriteria map[string]inter
default:
return nil, fmt.Errorf("Invalid filter criteria key: %s", key)
}
// if !match {
// break
// }
}
if match {
matchedRecord = record
Expand All @@ -143,8 +179,8 @@ func (c *Client) FilterDNSRecord(address string, filterCriteria map[string]inter
return &matchedRecord, nil
}

func NewDNSEntry(Type, Name, Data string, TTL int, Priority ...int) *DNSEntry {
var priority *int
func NewDNSEntry(Type, Name, Data string, TTL int64, Priority ...int64) *DNSEntry {
var priority *int64
if len(Priority) > 0 {
p := Priority[0]
priority = &p
Expand All @@ -158,7 +194,7 @@ func NewDNSEntry(Type, Name, Data string, TTL int, Priority ...int) *DNSEntry {
}
}

func newDNSRecord(id, ttl int, typ, name, data, createdAt, updatedAt string, priority *int) *DNSRecord {
func newDNSRecord(id, ttl int64, typ, name, data, createdAt, updatedAt string, priority *int64) *DNSRecord {
return &DNSRecord{
ID: &id,
Type: &typ,
Expand Down Expand Up @@ -192,7 +228,7 @@ func (d *DNSRecord) ToString() string {

priority := "<nil>"
if d.Priority != nil {
priority = strconv.Itoa(*d.Priority)
priority = strconv.Itoa(int(*d.Priority))
}
return fmt.Sprintf("ID: %d, Type: %s, Name: %s, Data: %s, Priority: %s, TTL: %d, CreatedAt: %s, UpdatedAt: %s", *d.ID, *d.Type, *d.Name, *d.Data, priority, *d.TTL, *d.CreatedAt, *d.UpdatedAt)
}
Expand Down Expand Up @@ -224,7 +260,7 @@ func (c *Client) CreateDNSRecord(domain string, record DNSEntry) (*DNSRecord, er
// Update an existing DNS record. See https://api.omg.lol/#token-patch-dns-edit-an-existing-dns-record
// Note this method does not work at time of writing due to an API bug, see https://github.com/neatnik/omg.lol/issues/584
// Suggested workaround is to use the Replace function instead, which uses the same interface.
func (c *Client) UpdateDNSRecord(domain string, record DNSEntry, record_id int) (*DNSRecord, error) {
func (c *Client) UpdateDNSRecord(domain string, record DNSEntry, record_id int64) (*DNSRecord, error) {
jsonData, err := json.Marshal(record)

req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/address/%s/dns/%d", c.HostURL, domain, record_id), bytes.NewBuffer([]byte(jsonData)))
Expand All @@ -248,7 +284,7 @@ func (c *Client) UpdateDNSRecord(domain string, record DNSEntry, record_id int)
}

// Delete a DNS record. See https://api.omg.lol/#token-delete-dns-delete-a-dns-record
func (c *Client) DeleteDNSRecord(domain string, record_id int) error {
func (c *Client) DeleteDNSRecord(domain string, record_id int64) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/address/%s/dns/%d", c.HostURL, domain, record_id), nil)

if err != nil {
Expand All @@ -270,7 +306,7 @@ func (c *Client) DeleteDNSRecord(domain string, record_id int) error {
}

// Delete a record with a given ID, then Create a new one using the provided values.
func (c *Client) ReplaceDNSRecord(domain string, record DNSEntry, record_id int) (*DNSRecord, error) {
func (c *Client) ReplaceDNSRecord(domain string, record DNSEntry, record_id int64) (*DNSRecord, error) {
err := c.DeleteDNSRecord(domain, record_id)
if err != nil {
return nil, err
Expand Down
34 changes: 28 additions & 6 deletions omglol/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,41 @@ func TestFilterDNSRecords(t *testing.T) {
t.Errorf(err.Error())
}

criteria := map[string]interface{}{
"ID": 41923511,
"Type": "TXT",
"TTL": 300,
// TXT Record
criteria1 := map[string]any{
"ID": 41923511,
"Type": "TXT",
"TTL": 300,
"Priority": nil,
}

d, err := c.FilterDNSRecord(testOwnedDomain, criteria)
d1, err := c.FilterDNSRecord(testOwnedDomain, criteria1)

if err != nil {
t.Errorf(err.Error())
}

t.Logf(d.ToString())
t.Logf(d1.ToString())

// MX Record
criteria2 := map[string]any{
"ID": int64(42197707),
"Priority": int64(20),
}

d2, err := c.FilterDNSRecord(testOwnedDomain, criteria2)

t.Logf(d2.ToString())

// Apex Record
criteria3 := map[string]any{
"ID": 42203377,
"Name": "@",
}

d3, err := c.FilterDNSRecord(testOwnedDomain, criteria3)

t.Logf(d3.ToString())
}

// There is currently no test for the Update method, as it does not work at time of writing, see https://github.com/neatnik/omg.lol/issues/584
Expand Down
26 changes: 13 additions & 13 deletions omglol/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

type request struct {
StatusCode int `json:"status_code"`
Success bool `json:"success"`
StatusCode int64 `json:"status_code"`
Success bool `json:"success"`
}

type apiResponse struct {
Expand Down Expand Up @@ -101,29 +101,29 @@ type DNSEntry struct {
Type *string `json:"type"`
Name *string `json:"name"`
Data *string `json:"data"`
Priority *int `json:"priority"`
TTL *int `json:"ttl"`
Priority *int64 `json:"priority"`
TTL *int64 `json:"ttl"`
}

// Return type for DNS related methods
type DNSRecord struct {
ID *int `json:"id"`
ID *int64 `json:"id"`
Type *string `json:"type"`
Name *string `json:"name"`
Data *string `json:"data"`
Priority *int `json:"priority"`
TTL *int `json:"ttl"`
Priority *int64 `json:"priority"`
TTL *int64 `json:"ttl"`
CreatedAt *string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
}

type dnsRecordContent struct {
ID *int `json:"id"`
ID *int64 `json:"id"`
Type *string `json:"type"`
Name *string `json:"name"`
Content *string `json:"content"`
Priority *int `json:"priority"`
TTL *int `json:"ttl"`
Priority *int64 `json:"priority"`
TTL *int64 `json:"ttl"`
CreatedAt *string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
}
Expand All @@ -134,8 +134,8 @@ type dnsChangeResponse struct {
Message string `json:"message"`
DataSent struct {
Type string `json:"type"`
Priority *int `json:"priority"`
TTL *int `json:"ttl"`
Priority *int64 `json:"priority"`
TTL *int64 `json:"ttl"`
Name string `json:"name"`
Content string `json:"content"`
} `json:"data_sent"`
Expand All @@ -148,6 +148,6 @@ type dnsChangeResponse struct {
type PersistentURL struct {
Name string `json:"name"`
URL string `json:"url"`
Counter *int `json:"counter"`
Counter *int64 `json:"counter"`
Listed *bool `json:"listed"`
}
24 changes: 13 additions & 11 deletions omglol/purl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// Create a PersistentURL object
func NewPersistentURL(Name, URL string, listed bool, Counter ...*int) *PersistentURL {
var counter *int
func NewPersistentURL(Name, URL string, listed bool, Counter ...*int64) *PersistentURL {
var counter *int64
if len(Counter) > 0 {
c := Counter[0]
counter = c
Expand All @@ -28,7 +28,7 @@ func NewPersistentURL(Name, URL string, listed bool, Counter ...*int) *Persisten
func (p *PersistentURL) ToString() string {
counter := "<nil>"
if p.Counter != nil {
counter = strconv.Itoa(*p.Counter)
counter = strconv.Itoa(int(*p.Counter))
}
return fmt.Sprintf("Name: %s, URL: %s, Counter: %s", p.Name, p.URL, counter)
}
Expand Down Expand Up @@ -76,8 +76,8 @@ func (c *Client) GetPersistentURL(domain string, purlName string) (*PersistentUR

type getPURLResponse struct {
Request struct {
StatusCode int `json:"status_code"`
Success bool `json:"success"`
StatusCode int64 `json:"status_code"`
Success bool `json:"success"`
} `json:"request"`
Response struct {
Message string `json:"message"`
Expand All @@ -96,12 +96,13 @@ func (c *Client) GetPersistentURL(domain string, purlName string) (*PersistentUR
return nil, err
}

var counter int
var counter int64
if g.Response.PURL.Counter != nil {
counter, err = strconv.Atoi(*g.Response.PURL.Counter)
counter_int, err := strconv.Atoi(*g.Response.PURL.Counter)
if err != nil {
return nil, err
}
counter = int64(counter_int)
} else {
counter = 0
}
Expand Down Expand Up @@ -130,8 +131,8 @@ func (c *Client) ListPersistentURLs(address string) (*[]PersistentURL, error) {

type listPURLResponse struct {
Request struct {
StatusCode int `json:"status_code"`
Success bool `json:"success"`
StatusCode int64 `json:"status_code"`
Success bool `json:"success"`
} `json:"request"`
Response struct {
Message string `json:"message"`
Expand Down Expand Up @@ -160,8 +161,9 @@ func (c *Client) ListPersistentURLs(address string) (*[]PersistentURL, error) {
if purl.Counter == nil {
x.Counter = nil
} else {
p, _ := strconv.Atoi(*purl.Counter)
x.Counter = &p
counter_int, _ := strconv.Atoi(*purl.Counter)
counter := int64(counter_int)
x.Counter = &counter
}

p = append(p, x)
Expand Down

0 comments on commit 83b58e6

Please sign in to comment.