initial commit

This commit is contained in:
puffaboo 2022-07-31 12:57:34 +01:00
commit 482958a85e
3 changed files with 118 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
nc.conf
ncddns

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module ncddns
go 1.18

113
main.go Normal file
View File

@ -0,0 +1,113 @@
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
const (
baseURL = "https://dynamicdns.park-your-domain.com/update?"
)
type DomainTuple struct {
Domain string
Password string
}
func Config(path string) ([]DomainTuple, error) {
conf, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read conf: %w", err)
}
tuples := []DomainTuple{}
for _, line := range strings.Split(string(conf), "\n") {
line := strings.TrimSpace(line)
if !strings.Contains(line, "=") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
tuples = append(tuples, DomainTuple{
Domain: parts[0],
Password: parts[1],
})
}
return tuples, nil
}
func fatal(name string, err error) {
if err != nil {
fmt.Fprintln(os.Stderr, name, err.Error())
os.Exit(1)
}
}
func main() {
cfgPath := flag.String("config", "nc.conf", "config file path")
flag.Parse()
ip, err := IP()
if err != nil {
fatal("ip", err)
}
cfg, err := Config(*cfgPath)
if err != nil {
fatal("config", err)
}
for _, cfgEntry := range cfg {
fmt.Println(cfgEntry.Domain)
if err := Set(ip, cfgEntry.Domain, cfgEntry.Password, "*"); err != nil {
fatal(cfgEntry.Domain, err)
}
if err := Set(ip, cfgEntry.Domain, cfgEntry.Password, "@"); err != nil {
fatal(cfgEntry.Domain, err)
}
}
}
func IP() (string, error) {
resp, err := http.Get("https://ipv4.icanhazip.com")
if err != nil {
return "", fmt.Errorf("get ip: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("read body: %w", err)
}
return string(body), nil
}
func Set(ip, domain, password, host string) error {
params := url.Values{}
params.Add("host", host)
params.Add("domain", domain)
params.Add("password", password)
params.Add("ip", ip)
resp, err := http.Get(fmt.Sprintf("%s%s", baseURL, params.Encode()))
if err != nil {
return fmt.Errorf("request: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "got status [%d]: %s\n", resp.StatusCode, string(data))
}
return nil
}