This commit is contained in:
Cat /dev/Nulo 2023-01-10 00:23:32 -03:00
parent ed458c7db8
commit c28859eb84
3 changed files with 86 additions and 0 deletions

17
main.go
View file

@ -1,5 +1,22 @@
package main package main
import (
"log"
"nulo.in/ddnser/nameservers"
)
func main() { func main() {
njalla := nameservers.Njalla{Key: "yourkey"}
record, err := njalla.SetRecord("estoesprueba.nulo.in", "")
if err != nil {
log.Fatal(err)
}
log.Println(record)
record, err = njalla.SetRecord("estoesprueba.nulo.in", "1.1.1.1")
if err != nil {
log.Fatal(err)
}
log.Println(record)
} }

View file

@ -0,0 +1,5 @@
package nameservers
type NameServer interface {
SetRecord(domain string, overrideIp string) (string, error)
}

64
nameservers/njalla.go Normal file
View file

@ -0,0 +1,64 @@
package nameservers
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"net/url"
"strconv"
)
type Njalla struct {
httpClient http.Client
Key string
}
type njallaValue struct {
A string `json:"A"`
}
type njallaResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Value njallaValue `json:"value"`
}
func (n *Njalla) SetRecord(domain string, overrideIp string) (string, error) {
u, _ := url.Parse("https://njal.la/update/")
values := url.Values{
"h": {domain},
"k": {n.Key},
}
if len(overrideIp) > 0 {
values["a"] = []string{overrideIp}
} else {
values["auto"] = []string{""}
}
u.RawQuery = values.Encode()
resp, err := n.httpClient.Get(u.String())
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
decoded := njallaResponse{}
err = json.Unmarshal(body, &decoded)
if err != nil {
return "", err
}
log.Println(string(body))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return "", errors.New("Not nice status code: " + strconv.Itoa(resp.StatusCode) + " with body: " + string(body))
}
if decoded.Status < 200 || decoded.Status > 299 {
return "", errors.New("Not nice JSON status: " + strconv.Itoa(decoded.Status) + " with body: " + string(body))
}
return decoded.Value.A, nil
}