-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
240 lines (227 loc) · 4.86 KB
/
conn.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
package miio
import (
"context"
"encoding/hex"
"encoding/json"
"github.com/uole/miio/cipher"
"github.com/uole/miio/packet"
"net"
"strconv"
"strings"
"sync"
"time"
)
type (
Conn struct {
conn net.Conn
address string //device address
sequence int //message sequence
deviceID uint32 //device id
deviceUptime int64 //device uptime
cipher *cipher.TokenCipher //device token cipher
mutex sync.Mutex
}
)
func (c *Conn) nextID() int {
c.mutex.Lock()
defer c.mutex.Unlock()
c.sequence++
if c.sequence >= 9999 {
c.sequence = 1
}
return c.sequence
}
func (c *Conn) nextStamp() uint32 {
return uint32(time.Now().Unix() - c.deviceUptime)
}
func (c *Conn) handshake(ctx context.Context) (err error) {
var (
in []byte
resp *packet.Frame
)
c.mutex.Lock()
in = packet.ApplyBytes(HeadLength)
defer func() {
packet.ReleaseBytes(in)
c.mutex.Unlock()
}()
frame := packet.NewHelloFrame()
frame.Reserved = 4294967295
if _, err = frame.WriteTo(c.conn); err != nil {
return
}
if resp, err = c.ReadMsg(ctx); err != nil {
return
}
c.deviceID = resp.DeviceID
c.deviceUptime = time.Now().Unix() - int64(resp.Stamp)
return
}
func (c *Conn) reconnect(ctx context.Context) (err error) {
var (
dialer net.Dialer
)
if c.conn != nil {
if err = c.conn.Close(); err != nil {
return
}
}
if c.conn, err = dialer.DialContext(ctx, "udp", c.address); err != nil {
return
}
return c.handshake(ctx)
}
func (c *Conn) Dial(ctx context.Context, address string, token string) (err error) {
var (
pos int
buf []byte
dialer net.Dialer
)
if buf, err = hex.DecodeString(token); err != nil {
return ErrorTokenInvalid
}
if pos = strings.IndexByte(address, ':'); pos == -1 {
address += ":" + strconv.Itoa(DefaultPort)
}
if c.conn, err = dialer.DialContext(ctx, "udp", address); err != nil {
return
}
c.address = address
c.cipher = cipher.NewTokenCipher(buf)
err = c.handshake(ctx)
return
}
func (c *Conn) Write(ctx context.Context, b []byte) (n int, err error) {
var (
m int64
ok bool
buf []byte
deadline time.Time
)
if buf, err = c.cipher.Encrypt(b); err != nil {
return
}
if c.deviceID == 0 {
if err = c.handshake(ctx); err != nil {
return
}
}
frame := packet.NewFrame(c.deviceID, c.nextStamp(), c.cipher.Token, buf)
if deadline, ok = ctx.Deadline(); ok {
if err = c.conn.SetWriteDeadline(deadline); err != nil {
return
}
defer func() {
_ = c.conn.SetDeadline(time.Time{})
}()
}
m, err = frame.WriteTo(c.conn)
return int(m), err
}
func (c *Conn) Read(ctx context.Context, b []byte) (n int, err error) {
var (
ok bool
buf []byte
frame *packet.Frame
deadline time.Time
)
if deadline, ok = ctx.Deadline(); ok {
if err = c.conn.SetWriteDeadline(deadline); err != nil {
return
}
defer func() {
_ = c.conn.SetDeadline(time.Time{})
}()
}
frame = &packet.Frame{}
if _, err = frame.ReadFrom(c.conn); err == nil {
if len(frame.Data) > 0 {
if buf, err = c.cipher.Decrypt(frame.Data); err == nil {
frame.Release()
frame.Data = buf
copy(b[:], frame.Data[:])
}
}
}
return
}
func (c *Conn) WriteMsg(ctx context.Context, msg any) (err error) {
var (
ok bool
buf []byte
deadline time.Time
)
if buf, err = json.Marshal(msg); err != nil {
return
}
if buf, err = c.cipher.Encrypt(buf); err != nil {
return
}
if c.deviceID == 0 {
if err = c.handshake(ctx); err != nil {
return
}
}
frame := packet.NewFrame(c.deviceID, c.nextStamp(), c.cipher.Token, buf)
if deadline, ok = ctx.Deadline(); ok {
if err = c.conn.SetWriteDeadline(deadline); err != nil {
return
}
defer func() {
_ = c.conn.SetDeadline(time.Time{})
}()
}
_, err = frame.WriteTo(c.conn)
return
}
func (c *Conn) ReadMsg(ctx context.Context) (frame *packet.Frame, err error) {
var (
ok bool
buf []byte
deadline time.Time
)
if deadline, ok = ctx.Deadline(); ok {
if err = c.conn.SetWriteDeadline(deadline); err != nil {
return
}
defer func() {
_ = c.conn.SetDeadline(time.Time{})
}()
}
frame = &packet.Frame{}
if _, err = frame.ReadFrom(c.conn); err == nil {
if len(frame.Data) > 0 {
if buf, err = c.cipher.Decrypt(frame.Data); err == nil {
frame.Release()
frame.Data = buf
}
}
}
return
}
func (c *Conn) Execute(ctx context.Context, req *CommandRequest) (res *CommandResponse, err error) {
var (
frame *packet.Frame
)
req.ID = c.nextID()
c.mutex.Lock()
defer func() {
c.mutex.Unlock()
}()
if err = c.WriteMsg(ctx, req); err != nil {
return
}
if frame, err = c.ReadMsg(ctx); err == nil {
res = &CommandResponse{Method: req.Method}
if err = json.Unmarshal(frame.Data, res); err == nil {
if res.Error != nil {
err = res.Error
}
}
}
return
}
func (c *Conn) Close() (err error) {
err = c.conn.Close()
return
}