forked from LUJUNQUAN/hap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pairings.go
153 lines (133 loc) · 3.36 KB
/
pairings.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
package hap
import (
"github.com/twxstar/hap/log"
"github.com/twxstar/hap/tlv8"
"net/http"
"reflect"
)
type pairingPayload struct {
Identifier string `tlv8:"1"`
PublicKey []byte `tlv8:"3"`
Permission byte `tlv8:"11"`
}
func (srv *Server) pairings(res http.ResponseWriter, req *http.Request) {
if !srv.IsAuthorized(req) {
log.Info.Printf("request from %s not authorized\n", req.RemoteAddr)
JsonError(res, JsonStatusInsufficientPrivileges)
return
}
ss, err := srv.getSession(req.RemoteAddr)
if err != nil {
log.Info.Println(err)
res.WriteHeader(http.StatusInternalServerError)
tlv8Error(res, M2, TlvErrorUnknown)
return
}
d := struct {
Method byte `tlv8:"0"`
Identifier string `tlv8:"1"`
PublicKey []byte `tlv8:"3,optional"`
Permission byte `tlv8:"11,optional"`
State byte `tlv8:"6"`
}{}
if err := tlv8.UnmarshalReader(req.Body, &d); err != nil {
log.Info.Println("tlv8:", err)
res.WriteHeader(http.StatusBadRequest)
tlv8Error(res, M2, TlvErrorUnknown)
return
}
switch d.Method {
case MethodAddPairing:
log.Debug.Println("add pairing", d.Identifier)
if ss.Pairing.Permission != PermissionAdmin {
log.Info.Println("operation not allowed for non-admin controllers")
tlv8Error(res, M2, TlvErrorAuthentication)
return
}
p, err := srv.st.Pairing(d.Identifier)
if err != nil {
p = Pairing{
Name: d.Identifier,
PublicKey: d.PublicKey,
Permission: d.Permission,
}
} else {
if !reflect.DeepEqual(p.PublicKey, d.PublicKey) {
log.Info.Println("invalid public key")
tlv8Error(res, M2, TlvErrorUnknown)
return
}
// Update permission
p.Permission = d.Permission
}
err = srv.savePairing(p)
if err != nil {
log.Info.Println(err)
tlv8Error(res, M2, TlvErrorUnknown)
return
}
resp := struct {
State byte `tlv8:"6"`
}{
State: M2,
}
tlv8OK(res, resp)
case MethodDeletePairing:
log.Debug.Println("delete pairing", d.Identifier)
if ss.Pairing.Permission != PermissionAdmin {
log.Info.Println("operation not allowed for non-admin controllers")
tlv8Error(res, M2, TlvErrorAuthentication)
return
}
p, err := srv.st.Pairing(d.Identifier)
if err != nil {
log.Info.Println(err)
tlv8Error(res, M2, TlvErrorUnknown)
return
}
if err = srv.deletePairing(p); err != nil {
log.Info.Println(err)
tlv8Error(res, M2, TlvErrorUnknown)
return
}
resp := struct {
State byte `tlv8:"6"`
}{
State: 2,
}
tlv8OK(res, resp)
// If no admin controller is paired anymore,
// close all connections and delete all pairings
if !srv.pairedWithAdmin() {
for addr, conn := range conns() {
log.Debug.Println("Closing connection to", addr)
conn.Close()
}
srv.deleteAllPairings()
}
// Close connection of deleted controller
for addr, conn := range conns() {
ss, err := srv.getSession(addr)
if err != nil {
log.Debug.Println("no session for", addr, err)
continue
}
if ss.Pairing.Name == p.Name {
log.Debug.Println("closing connection of removed controller", d.Identifier)
conn.Close()
}
}
case MethodListPairings:
log.Debug.Println("list pairings")
ps := srv.st.Pairings()
resp := make([]pairingPayload, len(ps))
for i, p := range ps {
resp[i] = pairingPayload{
Identifier: p.Name,
PublicKey: p.PublicKey,
Permission: p.Permission,
}
}
tlv8OK(res, resp)
}
}