-
Notifications
You must be signed in to change notification settings - Fork 190
/
ip_transport.go
335 lines (279 loc) · 8.64 KB
/
ip_transport.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package hc
import (
"bytes"
"context"
"image"
"io/ioutil"
"net"
"strings"
"sync"
"github.com/brutella/dnssd"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/db"
"github.com/brutella/hc/event"
"github.com/brutella/hc/hap"
"github.com/brutella/hc/hap/endpoint"
"github.com/brutella/hc/hap/http"
"github.com/brutella/hc/log"
"github.com/brutella/hc/util"
"github.com/xiam/to"
)
type ipTransport struct {
CameraSnapshotReq func(width, height uint) (*image.Image, error)
config *Config
context hap.Context
server *http.Server
mutex *sync.Mutex
storage util.Storage
database db.Database
device hap.SecuredDevice
container *accessory.Container
// Used to communicate between different parts of the program (e.g. successful pairing with HomeKit)
emitter event.Emitter
ctx context.Context
cancel context.CancelFunc
responder dnssd.Responder
handle dnssd.ServiceHandle
stopped chan struct{}
}
// NewIPTransport creates a transport to provide accessories over IP.
//
// The IP transports stores the crypto keys inside a database, which
// is by default inside a folder at the current working directory.
// The folder is named exactly as the accessory name.
//
// The transports can contain more than one accessory. If this is the
// case, the first accessory acts as the HomeKit bridge.
//
// *Important:* Changing the name of the accessory, or letting multiple
// transports store the data inside the same database lead to
// unexpected behavior – don't do that.
//
// The transport is secured with an 8-digit pin, which must be entered
// by an iOS client to successfully pair with the accessory. If the
// provided transport config does not specify any pin, 00102003 is used.
func NewIPTransport(config Config, a *accessory.Accessory, as ...*accessory.Accessory) (*ipTransport, error) {
// Find transport name which is visible in mDNS
name := a.Info.Name.GetValue()
if len(name) == 0 {
log.Info.Panic("Invalid empty name for first accessory")
}
cfg := defaultConfig(name)
cfg.merge(config)
storage, err := util.NewFileStorage(cfg.StoragePath)
if err != nil {
return nil, err
}
database := db.NewDatabaseWithStorage(storage)
hap_pin, err := ValidatePin(cfg.Pin)
if err != nil {
return nil, err
}
cfg.load(storage)
device, err := hap.NewSecuredDevice(cfg.id, hap_pin, database)
if err != nil {
return nil, err
}
responder, err := dnssd.NewResponder()
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
t := &ipTransport{
storage: storage,
database: database,
device: device,
config: cfg,
container: accessory.NewContainer(),
mutex: &sync.Mutex{},
context: hap.NewContextForSecuredDevice(device),
emitter: event.NewEmitter(),
responder: responder,
ctx: ctx,
cancel: cancel,
stopped: make(chan struct{}),
}
t.addAccessory(a)
for _, a := range as {
t.addAccessory(a)
}
// Users can only pair discoverable accessories
if t.isPaired() {
cfg.discoverable = false
}
cfg.categoryId = uint8(t.container.AccessoryType())
cfg.updateConfigHash(t.container.ContentHash())
cfg.save(storage)
// Listen for events to update mDNS txt records
t.emitter.AddListener(t)
return t, err
}
func (t *ipTransport) Start() {
// Create server which handles incoming tcp connections
config := http.Config{
Port: t.config.Port,
Context: t.context,
Database: t.database,
Container: t.container,
Device: t.device,
Mutex: t.mutex,
Emitter: t.emitter,
}
s := http.NewServer(config)
t.server = s
if t.CameraSnapshotReq != nil {
t.server.Mux.Handle("/resource", endpoint.NewResource(t.context, t.CameraSnapshotReq))
}
// Publish server port which might be different then `t.config.Port`
t.config.servePort = int(to.Int64(s.Port()))
service := newService(t.config)
t.handle, _ = t.responder.Add(service)
mdnsCtx, mdnsCancel := context.WithCancel(t.ctx)
defer mdnsCancel()
mdnsStop := make(chan struct{})
go func() {
t.responder.Respond(mdnsCtx)
log.Debug.Println("mdns responder stopped")
mdnsStop <- struct{}{}
}()
// keepAliveCtx, keepAliveCancel := context.WithCancel(t.ctx)
// defer keepAliveCancel()
//
// // Send keep alive notifications to all connected clients every 10 minutes
// keepAlive := hap.NewKeepAlive(10*time.Minute, t.context)
// go func() {
// keepAlive.Start(keepAliveCtx)
// log.Info.Println("Keep alive stopped")
// }()
// Publish accessory ip
log.Info.Printf("Listening on port %s\n", s.Port())
serverCtx, serverCancel := context.WithCancel(t.ctx)
defer serverCancel()
serverStop := make(chan struct{})
go func() {
s.ListenAndServe(serverCtx)
log.Debug.Println("server stopped")
serverStop <- struct{}{}
}()
// Wait until mdns responder and server stopped
<-mdnsStop
<-serverStop
t.stopped <- struct{}{}
}
// Stop stops the ip transport by stopping the http server and unpublishing the mDNS service.
func (t *ipTransport) Stop() <-chan struct{} {
t.cancel()
return t.stopped
}
// XHMURI returns a X-HM styled uri to easily add the accessory to HomeKit.
// To print a QR code to the console, use the follow code snippet.
// ```
// import "github.com/mdp/qrterminal/v3"
//
// uri, _ := transport.XHMURI()
// qrterminal.Generate(uri, qrterminal.L, os.Stdout)
// ```
func (t *ipTransport) XHMURI() (string, error) {
return t.config.XHMURI(util.SetupFlagIP)
}
// isPaired returns true when the transport is already paired
func (t *ipTransport) isPaired() bool {
// If more than one entity is stored in the database, we are paired with a device.
// The transport itself is a device and is stored in the database, therefore
// we have to check for more than one entity.
if es, err := t.database.Entities(); err == nil && len(es) > 1 {
return true
}
return false
}
func (t *ipTransport) updateMDNSReachability() {
t.config.discoverable = t.isPaired() == false
if t.handle != nil {
t.handle.UpdateText(t.config.txtRecords(), t.responder)
}
}
func (t *ipTransport) addAccessory(a *accessory.Accessory) {
t.container.AddAccessory(a)
for _, s := range a.Services {
for _, c := range s.Characteristics {
// When a characteristic value changes and events are enabled for this characteristic
// all listeners are notified. Since we don't track which client is interested in
// which characteristic change event, we send them to all active connections.
onConnChange := func(conn net.Conn, c *characteristic.Characteristic, new, old interface{}) {
t.notifyListener(a, c, conn)
}
c.OnValueUpdateFromConn(onConnChange)
onChange := func(c *characteristic.Characteristic, new, old interface{}) {
t.notifyListener(a, c, nil)
}
c.OnValueUpdate(onChange)
}
}
}
func (t *ipTransport) notifyListener(a *accessory.Accessory, c *characteristic.Characteristic, except net.Conn) {
conns := t.context.ActiveConnections()
for _, conn := range conns {
if conn == except {
continue
}
sess := t.context.GetSessionForConnection(conn)
if sess == nil {
continue
}
if !sess.IsSubscribedTo(c) {
continue
}
resp, err := hap.NewCharacteristicNotification(a, c)
if err != nil {
log.Info.Panic(err)
}
// Write response into buffer to replace HTTP protocol
// specifier with EVENT as required by HAP
var buffer = new(bytes.Buffer)
resp.Write(buffer)
bytes, err := ioutil.ReadAll(buffer)
bytes = hap.FixProtocolSpecifier(bytes)
log.Debug.Printf("%s <- %s", conn.RemoteAddr(), string(bytes))
conn.Write(bytes)
}
}
// Handles event which are sent when pairing with a device is added or removed
func (t *ipTransport) Handle(ev interface{}) {
switch ev.(type) {
case event.DevicePaired:
log.Debug.Printf("Event: paired with device")
t.updateMDNSReachability()
case event.DeviceUnpaired:
log.Debug.Printf("Event: unpaired with device")
t.updateMDNSReachability()
default:
break
}
}
func newService(config *Config) dnssd.Service {
// 2016-03-14(brutella): Replace whitespaces (" ") from service name
// with underscores ("_")to fix invalid http host header field value
// produces by iOS.
//
// [Radar] http://openradar.appspot.com/radar?id=4931940373233664
stripped := strings.Replace(config.name, " ", "_", -1)
var ips []net.IP
if ip := net.ParseIP(config.IP); ip != nil {
ips = append(ips, ip)
}
dnsCfg := dnssd.Config{
Name: util.RemoveAccentsFromString(stripped),
Type: "_hap._tcp",
Domain: "local",
Host: strings.Replace(config.id, ":", "", -1), // use the id (without the colons) to get unique hostnames
Text: config.txtRecords(),
IPs: ips,
Port: config.servePort,
}
service, err := dnssd.NewService(dnsCfg)
if err != nil {
log.Info.Fatal(err)
}
return service
}