/
index.js
164 lines (141 loc) · 4.57 KB
/
index.js
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
const crypto = require('crypto')
const noble = global.noble = require('noble')
let mac = '00:21:4d:03:20:1b'
const name = 'Smart Light'
const password = '234'
let colors = [
0xff,
0xff,
0xff,
0xff
]
let packetCount = Math.random() * 0xffff | 0
packetCount = 1
let light = null
let ch = null
const range = to => Array(to).fill().map((_, i) => i)
function encrypt (key, data) {
key = Buffer.from(key)
key.reverse()
data = Buffer.from(data)
data.reverse()
const cipher = crypto.createCipheriv('aes-128-ecb', key, Buffer.from([]))
const encryptedData = cipher.update(data).reverse()
return encryptedData
}
function generateSk (name, password, data1, data2) {
name = Buffer.from(name.padEnd(16, '\u0000'))
password = Buffer.from(password.padEnd(16, '\u0000'))
const key = []
name.forEach((byte, index) => {
key.push(byte ^ password[index])
})
const data = [...data1.slice(0, 8), ...data2.slice(0, 8)]
return encrypt(key, data)
}
function keyEncrypt (name, password, data) {
name = Buffer.from(name.padEnd(16, '\u0000'))
password = Buffer.from(password.padEnd(16, '\u0000'))
const key = []
;[].forEach.call(name, (byte, index) => {
key.push(byte ^ password[index])
})
return encrypt(data, key)
}
function encryptPacket (sk, mac, packet) {
let tmp = [mac[0], mac[1], mac[2], mac[3], 0x01, packet[0], packet[1], packet[2], 15, 0, 0, 0, 0, 0, 0, 0]
tmp = encrypt(sk, tmp)
let i = 0
range(15).forEach(i => {
tmp[i] = tmp[i] ^ packet[i + 5]
})
tmp = encrypt(sk, tmp)
range(2).forEach(i => {
packet[i + 3] = tmp[i]
})
tmp = [0, mac[0], mac[1], mac[2], mac[3], 0x01, packet[0], packet[1], packet[2], 0, 0, 0, 0, 0, 0, 0]
tmp2 = []
range(15).forEach(i => {
if (i === 0) {
tmp2 = encrypt(sk, tmp)
tmp[0] = tmp[0] + 1
}
packet[i + 5] ^= tmp2[i]
})
return Buffer.from(packet)
}
const nobleReady = new Promise(resolve =>
noble.on('stateChange', state => {
state === 'poweredOn' && resolve()
})
)
global.connect = async function connect () {
global.light = light = await new Promise(resolve => {
noble.on('discover', thing => {
console.log(thing.advertisement.localName, name)
if (thing.advertisement.localName === name) {
console.log('thinging')
mac = thing.address
resolve(thing)
}
})
noble.startScanning()
})
.then(light => new Promise(resolve => light.connect(() => resolve(light))))
await new Promise(resolve => light.discoverAllServicesAndCharacteristics(() => resolve()))
ch = light.services[1].characteristics[1]
pairch = light.services[1].characteristics[3]
const pairHandle = 0x001b
// light now has .services each with .characteristics
const data = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0, 0, 0, 0, 0, 0, 0, 0]
const encryptedData = keyEncrypt(name, password, data)
const packet = [0x0c]
.concat(data.slice(0, 8))
.concat(Array.from(encryptedData).slice(0, 8))
await new Promise(resolve => pairch.write(new Buffer(packet), true, resolve))
const received = await new Promise((resolve, reject) => pairch.read((error, data) =>
error ? reject(error) : resolve(data)
))
light.sk = generateSk(name, password, data.slice(0, 8), received.slice(1, 9))
return light
}
global.sendPacket = async function sendPacket (id, command, data) {
const packet = Array(20).fill(0)
packet[0] = packetCount & 0xff
packet[1] = packetCount >> 8 & 0xff
packet[5] = id & 0xff
packet[6] = id & 0xff | 0x80
packet[7] = command
packet[8] = 0x69
packet[9] = 0x69
packet[10] = data[0]
packet[11] = data[1]
packet[12] = data[2]
packet[13] = data[3]
const macArray = mac.split(':')
const macKey = Buffer.from([macArray[5], macArray[4], macArray[3], macArray[2], macArray[1], macArray[0]].map(n => parseInt(n, 16)))
const encryptedPacket = encryptPacket(light.sk, macKey, [...packet])
packetCount += 1
if (packetCount > 0xffff) packetCount = 1
return new Promise(resolve => ch.write(encryptedPacket, false, resolve))
}
global.setState = function setState (red, green, blue, brightness) {
colors = [red, green, blue, brightness]
return sendPacket(0xffff, 0xc1, colors)
}
function setDefaultState (...colors) {
return sendPacket(0xffff, 0xc4, colors)
}
/// brightness, speed, mode, loop
function setRainbow (...args) {
return sendPacket(0xffff, 0xca, args)
}
function setMosquito (brightness) {
return sendPacket(0xffff, 0xcb, [brightness, 0, 0, 0])
}
const byte = () => Math.random() * 0xff | 0
nobleReady.then(global.connect).then(() => {
setInterval(() => {
setState(byte(), byte(), byte(), byte())
}, 300)
})