This repository has been archived by the owner on Feb 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyndnscheck.go
186 lines (163 loc) · 4.99 KB
/
dyndnscheck.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
182
183
184
185
186
// DynDNSCheck is a monitoring tool for your DynDNS host.
//
// Copyright (C) 2014 CODE2K:LABS
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/smtp"
"os"
"strconv"
"strings"
)
const (
VERSION string = "1.0.1"
)
type Config struct {
DynDNSHost string // your DynDNS host. e.g. myhost.duckdns.org
CurrentIPURL string // URL of an external IP service like http://ifconfig.me/ip
EMailFrom string
EMailTo string
EMailSubject string
EMailServer string // smtp.gmail.com
EMailPort int // 587
EMailPassword string
}
var (
config = &Config{}
)
func loadConfig(filename string) {
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("open config: ", err)
}
if err = json.Unmarshal(file, config); err != nil {
log.Fatalf("parse config: ", err)
}
}
func sendMail(subject string, message string) {
auth := smtp.PlainAuth(
"",
config.EMailFrom,
config.EMailPassword,
config.EMailServer)
err := smtp.SendMail(
config.EMailServer+":"+strconv.Itoa(config.EMailPort),
auth,
config.EMailFrom,
[]string{config.EMailTo},
[]byte("From: "+config.EMailFrom+
"\nTo: "+config.EMailTo+
"\nSubject: "+subject+
"\n\n"+message))
if err != nil {
log.Fatal("sendMail: %v", err)
}
}
// getDynIP returns the current IP of your DnyDNS host.
func getDynIP(host string) (ip string, err error) {
ips, err := net.LookupIP(host)
if err != nil {
log.Printf("getDynIP: %v", err)
return
}
ip = ips[0].String()
log.Printf("DynDns IP: %v", ip)
return
}
// getCurrentIP returns your external IP by using a web service
// like http://ifconfig.me/ip or http://icanhazip.com.
func getCurrentIP() (ip string, err error) {
res, err := http.Get(config.CurrentIPURL)
if err != nil {
log.Printf("getCurrentIP: %v", err)
return
}
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("Can't get IP from %s: %s", config.CurrentIPURL, res.Status)
return
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Printf("getCurrentIP: %v", err)
return
}
ip = strings.TrimSpace(string(body))
if net.ParseIP(ip) == nil {
log.Printf("Result is not an IP address: %s", ip)
ip = ""
err = fmt.Errorf("%s does not return an IP address!", config.CurrentIPURL)
}
log.Printf("Current IP: %v", ip)
return
}
func init() {
config := flag.String("config", "config.json", "location of the configuration file")
flag.Parse()
log.Print("DynDNSCheck " + VERSION)
log.Print("Configuration: " + *config)
loadConfig(*config)
}
func main() {
currentIP, cerr := getCurrentIP()
dynIP, derr := getDynIP(config.DynDNSHost)
if cerr != nil || derr != nil || currentIP != dynIP {
// Something is wrong. Generate alert email:
var message string
if cerr != nil {
message += fmt.Sprintf("Error getting current IP: %v\n", cerr)
}
if derr != nil {
message += fmt.Sprintf("Error getting DynDNS IP: %v\n", derr)
}
if currentIP != dynIP && len(message) == 0 {
message += fmt.Sprintf("IPs are different!\n\nDynDNS IP:%s\n", dynIP)
}
if cerr == nil {
// if available always append the current IP
message += fmt.Sprintf("Current IP: %s\n", currentIP)
}
message += fmt.Sprintf("\nChecked Host: %s\n", config.DynDNSHost)
hostname, err := os.Hostname()
if err != nil {
hostname = fmt.Sprintf("Error: %v", err)
}
message += "DynDNSCheck " + VERSION + " is running on " + hostname
log.Print("Check failed... sending alert")
sendMail(config.EMailSubject, message)
} else {
log.Print("Check OK")
}
}