-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (56 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/spf13/pflag"
)
const ver = "1.0.0"
func main() {
domains := pflag.StringSliceP("domain", "d", []string{}, "The domain(s) to be updated")
ip := pflag.StringP("ip", "i", "", "The IP address, auto-detect when empty")
token := pflag.StringP("token", "t", "", "The token")
verbose := pflag.BoolP("verbose", "v", false, "Print verbose information")
version := pflag.Bool("version", false, "Show version")
pflag.Parse()
if *version {
fmt.Printf("duckdnsup-go version %s\n", ver)
os.Exit(0)
}
if len(*domains) == 0 {
fmt.Fprintln(os.Stderr, "Error: domains not specified")
os.Exit(1)
}
if *token == "" {
fmt.Fprintln(os.Stderr, "Error: token not specified")
os.Exit(1)
}
url := fmt.Sprintf("https://www.duckdns.org/update?domains=%s&token=%s&ip=%s&verbose=%t", strings.Join(*domains, ","), *token, *ip, *verbose)
if *verbose {
fmt.Printf("Making HTTP request: %s\n", url)
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Error making request: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err)
os.Exit(1)
}
if bytes.HasPrefix(body, []byte("OK")) {
fmt.Println("Update successful.")
} else if bytes.HasPrefix(body, []byte("KO")) {
fmt.Println("Update failed.")
} else {
fmt.Println("Unexpected response.")
}
if *verbose {
fmt.Printf("Response from Duck DNS:\n%s\n", string(body))
}
}