129 lines
2.9 KiB
JavaScript
129 lines
2.9 KiB
JavaScript
import child_process from "child_process";
|
|
import fs from "fs";
|
|
import { Server } from "socket.io";
|
|
|
|
async function spawnPromise(program, args) {
|
|
return new Promise((resolve, _reject) => {
|
|
let output = "";
|
|
const process = child_process.spawn(program, args);
|
|
process.stdout.on("data", (data) => {
|
|
output += data;
|
|
});
|
|
process.stderr.on("data", (data) => {
|
|
output += data;
|
|
});
|
|
process.on("close", (code) => {
|
|
resolve({ output, code });
|
|
});
|
|
});
|
|
}
|
|
|
|
let io = new Server();
|
|
|
|
async function send_private_data() {
|
|
let adid = fs.readFileSync("/adid").toString();
|
|
let gps_coords = await spawnPromise("bash", ["/conf/get_location.sh"])
|
|
gps_coords = gps_coords.output;
|
|
gps_coords = gps_coords.trim().split(',');
|
|
io.emit("private_info", {adid, latitude: gps_coords[0], longitude: gps_coords[1]})
|
|
}
|
|
|
|
function send_notification(socket, is_ok, context, message) {
|
|
socket.emit("notification", {
|
|
is_ok,
|
|
context,
|
|
message,
|
|
});
|
|
}
|
|
|
|
let screenshot_in_flight = false;
|
|
//maybe check output of child processes and send errors in some way
|
|
io.on("connection", (socket) => {
|
|
socket.onAny((ev, ...args) => {
|
|
console.log("server got: ", ev, ...args);
|
|
});
|
|
socket.on("screenshot", async () => {
|
|
if (screenshot_in_flight)
|
|
return;
|
|
screenshot_in_flight = true;
|
|
let screen = await fetch("http://localhost:9987/v2/uiDevice/screenshot");
|
|
let body = await screen.bytes();
|
|
socket.emit("screenshot_data", body);
|
|
screenshot_in_flight = false;
|
|
});
|
|
|
|
socket.on("private_info_req", async () => {
|
|
await send_private_data();
|
|
})
|
|
|
|
socket.on("reset_adid", async () => {
|
|
await spawnPromise("bash", ["/conf/reset_adid.sh"]);
|
|
await send_private_data();
|
|
})
|
|
|
|
socket.on("back", async () => {
|
|
await spawnPromise("bash", ["/conf/back.sh"]);
|
|
});
|
|
|
|
socket.on("home", async () => {
|
|
await spawnPromise("bash", ["/conf/home.sh"]);
|
|
});
|
|
|
|
socket.on("install", async () => {
|
|
const res = await spawnPromise("bash", ["/conf/install.sh"]);
|
|
send_notification(
|
|
socket,
|
|
res.code === 0,
|
|
"Installing the application",
|
|
res.output
|
|
);
|
|
});
|
|
|
|
// drag handles both drag and click
|
|
socket.on("motionevent", async (data) => {
|
|
await spawnPromise("bash", [
|
|
"/conf/motionevent.sh",
|
|
data.motionType,
|
|
data.x + "",
|
|
data.y + ""
|
|
]);
|
|
});
|
|
|
|
// drag handles both drag and click
|
|
socket.on("drag", async (data) => {
|
|
await spawnPromise("bash", [
|
|
"/conf/drag.sh",
|
|
data.startX + "",
|
|
data.startY + "",
|
|
data.endX + "",
|
|
data.endY + "",
|
|
data.dragTime + "",
|
|
]);
|
|
});
|
|
|
|
socket.on("key", async (data) => {
|
|
await spawnPromise("bash", [
|
|
"/conf/press_key.sh",
|
|
data.key
|
|
]);
|
|
});
|
|
|
|
socket.on("setcoord", async (data) => {
|
|
const res = await spawnPromise("bash", [
|
|
"/conf/set_geo.sh",
|
|
data.lon + "",
|
|
data.lat + "",
|
|
]);
|
|
send_notification(
|
|
socket,
|
|
res.code === 0,
|
|
"Setting the moch location",
|
|
res.output
|
|
);
|
|
await send_private_data();
|
|
});
|
|
});
|
|
|
|
io.listen(3000);
|
|
console.log("listening on port 3000");
|