-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsesame.js
62 lines (56 loc) · 1.69 KB
/
sesame.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
// require("dotenv").config();
const axios = require('axios');
const aesCmac = require('node-aes-cmac').aesCmac;
const sesame_id = process.env.SESAME_UUID;
const sesame_api_key = process.env.SESAME_API_KEY;
const key_secret_hex = process.env.KEY_SECRET_HEX;
exports.get_status = async () => {
const result = await axios({
method: 'get',
url: `https://app.candyhouse.co/api/sesame2/${sesame_id}`,
headers: { 'x-api-key': sesame_api_key }
})
return result;
};
exports.lock_cmd = async () => {
const cmd = 82;
const history = "Locked via LINE bot";
const base64_history = Buffer.from(history).toString('base64');
const sign = generateRandomTag(key_secret_hex)
const result = await axios({
method: 'post',
url: `https://app.candyhouse.co/api/sesame2/${sesame_id}/cmd`,
headers: { 'x-api-key': sesame_api_key },
data: {
cmd: cmd,
history: base64_history,
sign: sign
}
})
return result;
};
exports.unlock_cmd = async () => {
const cmd = 83;
const history = "Unlocked via LINE bot";
const base64_history = Buffer.from(history).toString('base64');
const sign = generateRandomTag(key_secret_hex);
const result = await axios({
method: 'post',
url: `https://app.candyhouse.co/api/sesame2/${sesame_id}/cmd`,
headers: { 'x-api-key': sesame_api_key },
data: {
cmd: cmd,
history: base64_history,
sign: sign
}
})
return result;
};
function generateRandomTag(secret) {
const key = Buffer.from(secret, 'hex');
const date = Math.floor(Date.now() / 1000);
const dateDate = Buffer.allocUnsafe(4);
dateDate.writeUInt32LE(date);
const message = Buffer.from(dateDate.slice(1, 4));
return aesCmac(key, message);
}