-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnswait.go
More file actions
154 lines (123 loc) · 3.84 KB
/
dnswait.go
File metadata and controls
154 lines (123 loc) · 3.84 KB
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
package main
import (
"github.com/0xAX/notificator"
spin "github.com/briandowns/spinner"
"github.com/urfave/cli"
"fmt"
"net"
"net/url"
"os"
"time"
)
var Options = &struct {
Domain string
IpAddress string
Time int64
DisableNotification bool
}{}
func main() {
app := &cli.App{
Name: "dnswait",
Version: "1.0.0",
Author: "Stefan Ruzitschka",
Description: "Waits for given domain to resolve to a given IP.\n A notification will be sent, when the program ends or the domain successfully resolves to the IP.\n\n Supported notifications:\n - Linux: notify-send or kdialog \n - OSX: terminal-notifier or osascript \n - Windows: growlnotify",
UsageText: "dnswait --domain <domain> --ip <ipv4|ipv6> [--time <minutes>] [--disable-notification]\n\n dnswait --domain sprint.net --ip 2600::\n dnswait --domain reddit.com --ip 151.101.65.140",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Destination: &Options.Domain,
Usage: "domain to resolve",
Value: "",
},
&cli.StringFlag{
Name: "ip",
Destination: &Options.IpAddress,
Usage: "target ip (IPv4 | IPv6)",
},
&cli.Int64Flag{
Name: "time",
Destination: &Options.Time,
Usage: "time to wait in minutes",
Value: 20,
},
&cli.BoolFlag{
Name: "disable-notification",
Destination: &Options.DisableNotification,
Usage: "disables notification",
},
},
}
app.Action = func(context *cli.Context) error {
if !context.IsSet("domain") {
return cli.NewExitError("Please set a domain", 1)
}
if !context.IsSet("ip") {
return cli.NewExitError("Please set an IP", 1)
}
var ip = net.ParseIP(Options.IpAddress)
if ip == nil {
return cli.NewExitError(fmt.Sprintf("Cannot parse ip %s", Options.IpAddress), 1)
}
domain, err := url.Parse(Options.Domain)
if err != nil {
return cli.NewExitError(err, 1)
}
var duration = 20 * time.Minute
if Options.Time > 0 {
duration = time.Duration(Options.Time) * time.Minute
}
spinner := spin.New(spin.CharSets[25], 100*time.Millisecond)
spinner.Start()
spinner.Suffix = fmt.Sprintf(" Waiting %s for %s to point to %s ...", duration, domain, ip.String())
defer spinner.Stop()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
errorChannel := make(chan error)
successChannel := make(chan string)
defer close(errorChannel)
defer close(successChannel)
go func() error {
for _ = range ticker.C {
ips, err := net.LookupHost(domain.String())
if err != nil {
errorChannel <- err
}
for _, foundIp := range ips {
if foundIp == ip.String() {
successChannel <- fmt.Sprintf("%s resolves to %s!", domain, ip.String())
}
}
}
return nil
}()
select {
case err := <-errorChannel:
return cli.NewExitError(err, 1)
case success := <-successChannel:
if !Options.DisableNotification {
sendNotification(
"dnswait succeeded!",
success,
notificator.UR_NORMAL,
)
}
return nil
case <-time.After(duration):
if !Options.DisableNotification {
sendNotification("dnswait failed!",
fmt.Sprintf("%s does not resolve to %s!", domain, ip.String()),
notificator.UR_CRITICAL,
)
}
return cli.NewExitError("", 1)
}
return nil
}
app.Run(os.Args)
}
func sendNotification(title string, message string, urgency string) {
notify := notificator.New(notificator.Options{
AppName: "dnswait",
})
notify.Push(title, message, "", urgency)
}