-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (75 loc) · 1.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Package noip provides a convinent way to embed a no-ip.com client in your go program.
package noip
import (
"bytes"
"fmt"
"net/http"
"net/url"
"time"
)
const (
baseurl = "https://dynupdate.no-ip.com/nic/update"
defaultUseragent = "unprofession-al/noip/v1.0 [email protected]"
)
// Client holds all info and logic to talk to the dynupdate.no-ip.com API
type Client struct {
user string
pass string
params url.Values
useragent string
myip string
}
// New creates a new no-ip.com client. If `myip` is an empty string the myip url param is omitted
// completely in the resulting API request url. If `useragent` is an empty string a default user agent
// will be written to the header in order to fulfill the API requirements.
func New(user string, pass string, hostname string, myip string, useragent string) *Client {
params := url.Values{}
params.Set("hostname", hostname)
if myip != "" {
params.Set("myip", myip)
}
c := &Client{
user: user,
pass: pass,
useragent: useragent,
params: params,
}
if c.useragent == "" {
c.useragent = defaultUseragent
}
return c
}
func (c *Client) update() (string, error) {
url := fmt.Sprintf("%s?%s", baseurl, c.params.Encode())
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(c.user, c.pass)
req.Header.Set("User-Agent", c.useragent)
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return "", err
}
body := new(bytes.Buffer)
body.ReadFrom(resp.Body)
return body.String(), nil
}
// Run starts a goroutine which calls the API in a given interval (seconds). If `log` is set to
// `true`, each result of an API call will be printed to STDOUT.
func (c *Client) Run(interval int, log bool) {
go func() {
for {
out, err := c.update()
if log {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("NO-IP is updated:", out)
}
}
time.Sleep(time.Duration(interval) * time.Second)
}
}()
}