-
Notifications
You must be signed in to change notification settings - Fork 4
/
device.go
200 lines (167 loc) · 4.21 KB
/
device.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
package broadlink
import (
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"math/rand"
"net"
)
type Device interface{}
type BaseDevice struct {
con *net.UDPConn
count uint16
Host string
MAC []byte
key []byte
iv []byte
id []byte
}
func newBaseDevice(con *net.UDPConn, host string, mac []byte) (dev *BaseDevice) {
dev = &BaseDevice{
Host: host,
MAC: mac,
con: con,
key: []byte{0x09, 0x76, 0x28, 0x34, 0x3f, 0xe9, 0x9e, 0x23, 0x76, 0x5c, 0x15, 0x13, 0xac, 0xcf, 0x8b, 0x02},
iv: []byte{0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58},
id: []byte{0, 0, 0, 0},
count: uint16(rand.Float64() * float64(0xffff)),
}
return
}
func (bd *BaseDevice) newDevice(devtype uint16) (dev Device) {
fmt.Printf("devtype:%x host:%s mac:%x\n", devtype, bd.Host, bd.MAC)
switch devtype {
// RM Mini
case 0x2737:
dev = newRM(bd)
default:
}
return
}
func (bd *BaseDevice) Auth() error {
payload := make([]byte, 0x50)
payload[0x04] = 0x31
payload[0x05] = 0x31
payload[0x06] = 0x31
payload[0x07] = 0x31
payload[0x08] = 0x31
payload[0x09] = 0x31
payload[0x0a] = 0x31
payload[0x0b] = 0x31
payload[0x0c] = 0x31
payload[0x0d] = 0x31
payload[0x0e] = 0x31
payload[0x0f] = 0x31
payload[0x10] = 0x31
payload[0x11] = 0x31
payload[0x12] = 0x31
payload[0x1e] = 0x01
payload[0x2d] = 0x01
payload[0x30] = 'T'
payload[0x31] = 'e'
payload[0x32] = 's'
payload[0x33] = 't'
payload[0x34] = ' '
payload[0x35] = ' '
payload[0x36] = '1'
response, err := bd.SendPacket(0x65, payload)
if err != nil {
return err
}
fmt.Printf("auth resp:%v\n", response)
//decode
enc_payload := response[0x38:]
if len(enc_payload) == 0 {
return errors.New("auth failed!!!")
}
fmt.Printf("auth resp data:%v\n", response[:0x38])
//aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
//payload = aes.decrypt(bytes(enc_payload))
block, aes_err := aes.NewCipher(bd.key)
if aes_err != nil {
return aes_err
}
mode := cipher.NewCBCDecrypter(block, bd.iv)
respdata := make([]byte, len(enc_payload))
mode.CryptBlocks(respdata, enc_payload)
fmt.Printf("auth resp data:%v\n", respdata)
bd.id = respdata[0x00:0x04]
bd.key = respdata[0x04:0x14]
return nil
}
func (bd *BaseDevice) SendPacket(command byte, payload []byte) (resp []byte, err error) {
resp = make([]byte, 1024)
bd.count = (bd.count + 1) & 0xffff
packet := make([]byte, 0x38)
packet[0x00] = 0x5a
packet[0x01] = 0xa5
packet[0x02] = 0xaa
packet[0x03] = 0x55
packet[0x04] = 0x5a
packet[0x05] = 0xa5
packet[0x06] = 0xaa
packet[0x07] = 0x55
packet[0x24] = 0x2a
packet[0x25] = 0x27
packet[0x26] = command
packet[0x28] = byte(bd.count & 0xff)
packet[0x29] = byte(bd.count >> 8)
packet[0x2a] = bd.MAC[0]
packet[0x2b] = bd.MAC[1]
packet[0x2c] = bd.MAC[2]
packet[0x2d] = bd.MAC[3]
packet[0x2e] = bd.MAC[4]
packet[0x2f] = bd.MAC[5]
packet[0x30] = bd.id[0]
packet[0x31] = bd.id[1]
packet[0x32] = bd.id[2]
packet[0x33] = bd.id[3]
//checksum
checksum := getCheckSum(packet)
fmt.Printf("%v\n", checksum)
packet[0x34] = byte(checksum & 0xff)
packet[0x35] = byte(checksum >> 8)
fmt.Printf("%v = %v %v\n", checksum, checksum&0xff, checksum>>8)
//aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
//payload = aes.encrypt(bytes(payload))
block, aes_err := aes.NewCipher(bd.key)
if aes_err != nil {
err = aes_err
return
}
ciphertext := make([]byte, len(payload))
mode := cipher.NewCBCEncrypter(block, bd.iv)
mode.CryptBlocks(ciphertext, payload)
//payload := fmt.Sprintf("%X", ciphertext)
packet = append(packet, ciphertext...)
checksum = getCheckSum(packet)
packet[0x20] = byte(checksum & 0xff)
packet[0x21] = byte(checksum >> 8)
rudp, addr_err := net.ResolveUDPAddr("udp", bd.Host)
if addr_err != nil {
err = addr_err
return
}
fmt.Printf("send packet: %v\n", packet)
_, werr := bd.con.WriteToUDP(packet, rudp)
if werr != nil {
err = werr
return
}
size, raddr, rerr := bd.con.ReadFrom(resp)
if rerr != nil {
err = rerr
return
}
fmt.Printf("get %d bytes from %s\n", size, raddr.String())
return resp[0:size], nil
}
func getCheckSum(packet []byte) (checksum uint16) {
checksum = 0xbeaf
for _, b := range packet {
checksum += uint16(b)
}
checksum = checksum & 0xffff
return
}