forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddclient.go
181 lines (146 loc) · 3.69 KB
/
ddclient.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package services
// Based on github.com/clayshek/google-ddns-updater
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"time"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/logging"
)
var (
ddns_service = "domains.google.com"
)
type DynDNSService struct {
Done chan bool
wg sync.WaitGroup
}
func (self *DynDNSService) updateIP(config_obj *api_proto.Config) {
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
logger.Info("Checking DNS")
externalIP, err := GetExternalIp()
if err != nil {
logger.Error("Unable to get external IP: %v", err)
return
}
ddns_hostname := config_obj.Frontend.DynDns.Hostname
hostnameIPs, err := GetCurrentDDNSIp(ddns_hostname)
if err != nil {
logger.Error("Unable to resolve DDNS hostname IP: %v", err)
return
}
for _, ip := range hostnameIPs {
if ip == externalIP {
return
}
}
logger.Info("DNS UPDATE REQUIRED. External IP=%v. %v=%v.",
externalIP, ddns_hostname, hostnameIPs)
reqstr := fmt.Sprintf(
"https://%v/nic/update?hostname=%v&myip=%v",
ddns_service,
ddns_hostname,
externalIP)
logger.Debug("Submitting update request to %v", reqstr)
err = UpdateDDNSRecord(
config_obj,
reqstr,
config_obj.Frontend.DynDns.DdnsUsername,
config_obj.Frontend.DynDns.DdnsPassword)
if err != nil {
logger.Error("Failed to update: %v", err)
return
}
}
func (self *DynDNSService) Start(config_obj *api_proto.Config) {
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
logger.Info("Starting the DynDNS service: Updating hostname %v",
config_obj.Frontend.DynDns.Hostname)
defer self.wg.Done()
min_update_wait := config_obj.Frontend.DynDns.Frequency
if min_update_wait == 0 {
min_update_wait = 60
}
// First time check immediately.
self.updateIP(config_obj)
for {
select {
case <-self.Done:
return
// Do not try to update sooner than this or we
// get banned. It takes a while for dns
// records to propagate.
case <-time.After(time.Duration(min_update_wait) * time.Second):
self.updateIP(config_obj)
}
}
}
func (self *DynDNSService) Close() {
close(self.Done)
self.wg.Wait()
}
func startDynDNSService(config_obj *api_proto.Config) (*DynDNSService, error) {
result := &DynDNSService{
Done: make(chan bool),
}
if config_obj.Frontend.DynDns == nil ||
config_obj.Frontend.DynDns.Hostname == "" {
return result, nil
}
result.wg.Add(1)
go result.Start(config_obj)
return result, nil
}
func GetExternalIp() (string, error) {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return "Unable to determine external IP: %v ", err
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
result := strings.TrimSpace(string(ip))
return result, nil
}
func GoogleDNSDialer(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", "8.8.8.8:53")
}
func GetCurrentDDNSIp(fqdn string) ([]string, error) {
r := net.Resolver{
PreferGo: true,
Dial: GoogleDNSDialer,
}
ctx := context.Background()
ips, err := r.LookupHost(ctx, fqdn)
if err != nil {
return nil, err
}
return ips, nil
}
func UpdateDDNSRecord(config_obj *api_proto.Config,
url, user, pw string) error {
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
client := &http.Client{
CheckRedirect: nil,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.SetBasicAuth(user, pw)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
logger.Debug("Update response: %v", string(body))
return nil
}