-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceController.js
More file actions
107 lines (88 loc) · 2.87 KB
/
deviceController.js
File metadata and controls
107 lines (88 loc) · 2.87 KB
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
const { execSync, spawn } = require('child_process');
const { promisify } = require('util');
const Client = require('ssh2').Client;
const execute = promisify(require('child_process').exec);
let deviceList = [];
let iproxyList = {};
let clientSshPort = 2222;
const getConnectedDevices = async () => {
try {
const { stdout, stderr } = await execute('idevice_id -l');
if (stderr) {
throw new Error(stderr);
}
let devices = stdout.trim().split('\n');
if (devices.length === 1 && devices[0] === '') {
devices = [];
} else {
devices = await Promise.all(devices.map(async (id) => ({
id,
status: 'uninitialized',
isJailbroken: await isJailbroken(id),
ssh: await getSsh(id)
})));
}
return devices;
} catch (error) {
console.error(`Error executing command: ${error}`);
throw error;
}
};
const reloadDevices = async () => {
deviceList = await getConnectedDevices();
await routineDeviceCheck();
};
const getDevices = () => deviceList;
const isJailbroken = async (id) => {
try {
const output = execSync(`ideviceinstaller -u ${id} -l -o list_system`);
return output.includes('Cydia');
} catch {
return false;
}
};
const getSsh = async (id) => {
if (!iproxyList[id]) await setupSsh(id);
return iproxyList[id];
};
const setupSsh = async (id) => {
const port = clientSshPort++;
const iproxy = spawn('iproxy', [port, 22, '-u', id]); // if you're having problems with iproxy, try omitting '-u'
iproxyList[id] = { iproxy_pid: iproxy.pid, port };
};
const routineDeviceCheck = () => {
for (const device of deviceList) {
const conn = new Client();
const port = device.ssh.port;
const connectionParams = {
host: '127.0.0.1',
port,
username: process.env.SSH_OVERRIDE_USERNAME || 'root',
password: process.env.SSH_OVERRIDE_PASSWORD || 'alpine',
};
conn.on('ready', () => {
device.status = 'ready';
device.isJailbroken = true;
conn.end();
});
conn.on('error', () => {
if (device.status != 'offline') {
device.status = 'offline';
sendWebhook(`[${device.id}]: Device is offline or not jailbroken! Go to http://${process.env.HOST}/device/${device.id}/start-jailbreak when your device is in DFU mode.`);
}
});
conn.connect(connectionParams);
}
};
const performJailbreak = async(id) => {
// TODO: Implement jailbreaking logic
return Promise.resolve();
};
const enterRecovery = async(id) => await execute(`ideviceenterrecovery ${id}`);
module.exports = {
getDevices,
reloadDevices,
routineDeviceCheck,
performJailbreak,
enterRecovery
};