-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (152 loc) · 3.59 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
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
188
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
hclog "github.com/brutella/hc/log"
)
const (
statusEndpoint = "/control/status"
dnsConfigEndpoint = "/control/dns_config"
)
type Config struct {
// Storage path for information about the HomeKit accessory.
// Defaults to ~/.homecontrol
StoragePath string `json:"storage_path"`
// HomeKit PIN. Defaults to 00102003
HomekitPIN string `json:"homekit_pin"`
// AdGuard Home URL
URL string `json:"url"`
// AdGuard Home username
Username string `json:"username"`
// AdGuard Home password
Password string `json:"password"`
}
func main() {
var configFile string
flag.StringVar(&configFile, "config", "config.json", "config file")
flag.Parse()
// Default values
cfg := Config{
StoragePath: filepath.Join(os.Getenv("HOME"), ".homecontrol"),
HomekitPIN: "00102003",
}
f, err := os.Open(configFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&cfg); err != nil {
log.Fatal(err)
}
if cfg.URL == "" {
log.Fatal("missing URL")
}
if cfg.Username == "" {
log.Fatal("missing username")
}
if cfg.Password == "" {
log.Fatal("missing password")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if x := os.Getenv("HC_DEBUG"); x != "" {
hclog.Debug.Enable()
}
enabled, err := ProtectionEnabled(ctx, &cfg)
if err != nil {
log.Fatal(err)
}
log.Printf("Connected to AdGuard Home, protection enabled: %t", enabled)
info := accessory.Info{
Name: "AdGuard Home",
}
sw := accessory.NewSwitch(info)
sw.Switch.On.OnValueRemoteUpdate(func(on bool) {
SetProtectionEnabled(ctx, &cfg, on)
})
hcConfig := hc.Config{
Pin: cfg.HomekitPIN,
StoragePath: cfg.StoragePath,
}
t, err := hc.NewIPTransport(hcConfig, sw.Accessory)
if err != nil {
log.Fatal(err)
}
hc.OnTermination(func() {
cancel()
<-t.Stop()
})
go func() {
t := time.NewTicker(15 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
enabled, err := ProtectionEnabled(ctx, &cfg)
if err != nil {
log.Printf("error checking protection enabled: %v", err)
continue
}
sw.Switch.On.SetValue(enabled)
}
}
}()
log.Println("Starting transport...")
t.Start()
}
func ProtectionEnabled(ctx context.Context, cfg *Config) (bool, error) {
req, err := http.NewRequest("GET", cfg.URL+statusEndpoint, nil)
if err != nil {
return false, err
}
req.SetBasicAuth(cfg.Username, cfg.Password)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var status struct {
ProtectionEnabled bool `json:"protection_enabled"`
}
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return false, err
}
return status.ProtectionEnabled, nil
}
func SetProtectionEnabled(ctx context.Context, cfg *Config, enabled bool) error {
payload := fmt.Sprintf(`{"protection_enabled": %t}`, enabled)
req, err := http.NewRequest(
"POST",
cfg.URL+dnsConfigEndpoint,
strings.NewReader(payload),
)
if err != nil {
return err
}
req.SetBasicAuth(cfg.Username, cfg.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}