-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmd-api.js
190 lines (168 loc) · 6.7 KB
/
fmd-api.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
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
const axios = require('axios');
const { hashPasswordForLogin, unwrapPrivateKey, decryptPacketModern } = require('./crypto');
// This whole file is vodo magic
const defaultServer = "https://fmd.nulide.de:1008/"
/**
* @typedef {Object} apiConfig
* @property {String} apiConfig.url URL of server (Defaults to: https://fmd.nulide.de:1008/)
*/
/**
* @typedef {Object} loginData
* @property {Object} loginData.accessToken Used for server auth
* @property {CryptoKey} loginData.privateKey Uses to decrypt data returned
*/
/**
* @typedef {Object} LocationData
* @property {'fused' | 'gps' | 'network' | 'opencell'} LocationData.provider What provider provided the lat and lon data
* @property {Number} LocationData.date Date the loction was saved (Number)
* @property {String} LocationData.bat Battery level of device
* @property {String} LocationData.lon Device Longitude
* @property {String} LocationData.lat Device Latitude
* @property {String} LocationData.time Date the loction was saved (String)
*/
class FMD_API {
/**
* FMD API!
* @param {String} deviceID The Device ID
* @param {String} password Your password
* @param {apiConfig} config Config
*/
constructor(deviceID, password, config = {}) {
this.deviceID = deviceID;
this.password = password;
config ??= {}
config.url ??= defaultServer
this.config = config
this.url = config.url
this.commands = {
locate: () => this.sendToPhone("locate"),
locate_gps: () => this.sendToPhone("locate gps"),
locate_cell: () => this.sendToPhone("locate cell"),
locate_last: () => this.sendToPhone("locate last"),
ring: () => this.sendToPhone("ring"),
lock: () => this.sendToPhone("lock"),
camera_front: () => this.sendToPhone("camera front"),
camera_back: () => this.sendToPhone("camera back")
}
}
/**
* Login to FMD server
* @return {Promise<loginData>} The login data containing accessToken and privateKey
*/
async login() {
try {
let saltResponse = await axios.put(`${this.url}/salt`, { IDT: this.deviceID, Data: "unused" })
let saltJSON = saltResponse.data
let salt = saltJSON.Data
let res = await hashPasswordForLogin(this.password, salt)
// console.log("res", res)
let accessResponse = await axios.put(`${this.url}/requestAccess`, { IDT: this.deviceID, Data: res })
let AccessData = accessResponse.data
this.accessToken = AccessData.Data
let keyResponse = await axios.put(`${this.url}/key`, { IDT: this.accessToken, Data: "unused" })
let keyData = keyResponse.data
// console.log("keyData",keyData)
this.privateKey = await unwrapPrivateKey(this.password, keyData.Data)
// console.log("privateKey", this.privateKey)
return {
accessToken: this.accessToken,
privateKey: this.privateKey
}
} catch (error) {
console.error("Login failed:", error);
throw new Error("Unable to login to FMD server.");
}
}
/**
*
* @returns {Promise<Number>}
*/
async getLocationCount() {
try {
let responseDataSize = await axios.put(`${this.url}/locationDataSize`, { IDT: this.accessToken, Data: "unused" })
let newestLocationDataIndex = parseInt(responseDataSize.data.Data, 10) - 1;
return newestLocationDataIndex;
} catch (error) {
console.error("getLocationCount failed:", error);
throw new Error("Unable to get location count.");
}
}
/**
* Get location
* @property {Number} requestedIndex Location index
* @returns {Promise<LocationData>}
*/
async locate(requestedIndex) {
try {
let newestLocationDataIndex = await this.getLocationCount()
if (requestedIndex == -1) {
requestedIndex = newestLocationDataIndex;
}
let responseLocation = await axios.put(`${this.url}/location`, {
IDT: this.accessToken,
Data: requestedIndex.toString()
});
// console.log("responseLocation", responseLocation.data)
let rawLocation = await decryptPacketModern(this.privateKey, responseLocation.data.Data)
let location = JSON.parse(rawLocation)
return location
} catch (error) {
console.error("Location get failed:", error);
throw new Error("Unable to get location.");
}
}
/**
* Send command to device
* @property {String} command Location index
* @returns {Promise<Boolean>} Weather or not it was successful (this doesn't mean the command has been ran on the phone yet!)
*/
async sendToPhone(command) {
try {
let res = await axios.post(`${this.url}/command`, { IDT: this.accessToken, Data: command })
return res.status == 200
} catch (error) {
console.error("SendToPhone failed:", error);
throw new Error("Unable to send command to FMD server.");
}
}
/**
* Send command to device
* @deprecated Couldn't get this to work
* @returns {null} Doesn't work!!
*/
async getOldCommands() {
return null
let res = await axios.post(`${this.url}/commandLogs`, { IDT: this.accessToken, Data: "" })
// console.log(res)
}
/**
* Get amount of pictures the server has
* @returns {Promise<Number>} Count of picturs on the server
*/
async getPictureCount() {
try {
let resPictureSize = await axios.put(`${this.url}/pictureSize`, { IDT: this.accessToken, Data: "" })
return resPictureSize.data.Data
} catch (error) {
console.error("getPictureCount failed:", error);
throw new Error("Unable to get picture count.");
}
}
/**
* Get picture the server has at the given index
* @property {Number} pictureIndex Index of picture
* @returns {Promise<Buffer>} Picture buffer
*/
async getPicture(pictureIndex) {
try {
const resPicture = await axios.put(`${this.url}/picture`, { IDT: this.accessToken, Data: pictureIndex.toString() })
let pictureDataRaw = resPicture.data
let pictureData = await decryptPacketModern(this.privateKey, pictureDataRaw)
return Buffer.from(pictureData, "base64")
} catch (error) {
console.error("getPicture failed:", error);
throw new Error("Unable to get picture from FMD server.");
}
}
}
module.exports = FMD_API