-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwan.go
187 lines (167 loc) · 4.02 KB
/
wan.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
187
// SPDX-FileCopyrightText: 2019 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: MIT
package main
import (
"context"
"net"
"time"
"github.com/warthog618/config"
"github.com/warthog618/config/dict"
)
func init() {
RegisterModule("wan", newWAN)
}
func onlineString(online bool) string {
if online {
return "online"
}
return "offline"
}
type wan struct {
online bool
ip string
linkPoller *PolledSensor
ipPoller *PolledSensor
ps PubSub
}
func (w *wan) Publish() {
if w.linkPoller != nil {
w.ps.Publish("", onlineString(w.online))
}
if w.ipPoller != nil {
w.ps.Publish("/ip", w.ip)
}
}
func (w *wan) RefreshLink(forced bool) {
online := getLink()
if w.online != online || forced {
w.online = online
w.ps.Publish("", onlineString(w.online))
if w.ipPoller != nil {
w.ipPoller.poller.Refresh(false)
}
}
}
func (w *wan) RefreshIP(forced bool) {
ip := getIP()
if w.ip != ip || forced {
w.ip = ip
w.ps.Publish("/ip", w.ip)
}
}
func (w *wan) Close() {
w.linkPoller.Close()
w.ipPoller.Close()
}
func (w *wan) Sync(ps PubSub) {
w.ps = ps
w.linkPoller.Sync(ps)
w.ipPoller.Sync(ps)
}
func newWAN(cfg *config.Config) SyncCloser {
defCfg := dict.New()
defCfg.Set("link.period", "1m")
defCfg.Set("ip.period", "15m")
defCfg.Set("entities", []string{"link", "ip"})
cfg.Append(defCfg)
entities := map[string]bool{}
for _, e := range cfg.MustGet("entities").StringSlice() {
entities[e] = true
}
w := wan{
online: getLink(),
ps: StubPubSub{},
}
if entities["link"] {
w.linkPoller = &PolledSensor{
topic: "",
poller: NewPoller(cfg.MustGet("link.period").Duration(), w.RefreshLink),
ps: StubPubSub{},
}
}
if entities["ip"] {
w.ipPoller = &PolledSensor{
topic: "/ip",
poller: NewPoller(cfg.MustGet("ip.period").Duration(), w.RefreshIP),
ps: StubPubSub{},
}
}
return &w
}
func (w *wan) Config() []EntityConfig {
var config []EntityConfig
if w.linkPoller != nil {
cfg := map[string]interface{}{
"name": "WAN",
"state_topic": "~/wan",
"device_class": "connectivity",
"icon": "mdi:wan",
"payload_on": "online",
"payload_off": "offline",
}
config = append(config, EntityConfig{"link", "binary_sensor", cfg})
}
if w.ipPoller != nil {
cfg := map[string]interface{}{
"name": "WAN IP",
"state_topic": "~/wan/ip",
"icon": "mdi:ip",
}
config = append(config, EntityConfig{"ip", "sensor", cfg})
}
return config
}
type dialer func(ctx context.Context, network, address string) (net.Conn, error)
func lookupGoogle(d dialer) (addrs []string, err error) {
r := net.Resolver{
PreferGo: true,
Dial: d,
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return r.LookupHost(ctx, "www.google.com")
}
func getLink() bool {
dialers := []dialer{CloudFlareDNSDialer, GoogleDNSDialer, OpenDNSDialer}
for _, dialer := range dialers {
_, err := lookupGoogle(dialer)
if err == nil {
return true
}
}
return false
}
func getIP() string {
r := net.Resolver{
PreferGo: true,
Dial: OpenDNSDialer,
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
addr, err := r.LookupHost(ctx, "myip.opendns.com")
if err != nil {
return "unknown"
}
return addr[0]
}
// CloudFlareDNSDialer connects to a CloudFlare DNS server
func CloudFlareDNSDialer(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", "1.1.1.1:53")
}
// GoogleDNSDialer connects to a Google DNS server
func GoogleDNSDialer(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", "8.8.8.8:53")
}
// OpenDNSDialer connects to an OpenDNS DNS server
// Note that this assumes the default DNS lookup is functional.
func OpenDNSDialer(ctx context.Context, _, _ string) (net.Conn, error) {
addrs, err := net.LookupHost("resolver1.opendns.com")
if err != nil {
return nil, err
}
d := net.Dialer{}
return d.DialContext(ctx, "udp", addrs[0]+":53")
}