forked from reisxd/TizenTube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
201 lines (186 loc) · 7.15 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
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
191
192
193
194
195
196
197
198
199
200
201
import { WebSocketServer } from 'ws';
import adbhost from 'adbhost';
import startDebugging from './debuggerController.js';
import Config from './config.json' assert { type: 'json' };
import { log, log_error } from './utils.js';
const sleep = ms => new Promise(r => setTimeout(r, ms));
let adb;
async function createAdbConnection(tv_ip, ws = null) {
if (adb?._stream) {
adb._stream.end();
await sleep(300);
adb._stream.removeAllListeners('connect');
adb._stream.removeAllListeners('error');
adb._stream.removeAllListeners('close');
} else
await sleep(300);
adb = adbhost.createConnection({ host: tv_ip, port: 26101 });
adb._stream.on('connect', () => {
log('ADB connection established');
switch (Config.launch_method) {
case 1: {
log("Launching TizenTube... [Using basic method]");
basic_method(adb, tv_ip, ws);
break;
}
case 2: {
log("Launching TizenTube... [Using kill method]");
kill_method(adb, tv_ip);
break;
}
case 3: {
log("Launching TizenTube... [Using retry method]");
retry_method(adb, tv_ip, ws);
break;
}
default: {
log("Launch method not found, defaulting to method 1.");
basic_method(adb, tv_ip, ws);
break;
}
}
});
adb._stream.on('error', (error) => {
log_error(`ADB connection error. (${error.message})`);
});
adb._stream.on('close', () => {
log('ADB connection closed.');
});
}
const wss = new WebSocketServer({ host: '0.0.0.0', port: Config?.serverPort ?? 3000 });
wss.on('listening', () => {
const address = wss.address();
log(`Server listening for the launcher on ${address.address}:${address.port}`);
});
wss.on('connection', ws => {
ws.on('message', message => {
let msg;
try {
msg = JSON.parse(message.toString());
} catch {
ws.send(JSON.stringify({
error: 'Invalid data'
}));
return;
}
switch (msg.e) {
case 'launch': {
ws.send(JSON.stringify({
ok: true
}));
createAdbConnection(ws._socket.remoteAddress, ws);
break;
}
case 'android': {
ws.send(JSON.stringify({
ok: true
}));
createAdbConnection(msg.tv_ip);
break;
}
default: {
ws.send(JSON.stringify({
error: 'Unknown event'
}));
break;
}
}
});
});
function basic_method(adb_conn, tv_ip, ws) {
// Launch TizenTube in debug mode
const shellCmd = adb_conn.createStream(`shell:0 debug Ad6NutHP8l.TizenTube${Config.isTizen3 ? ' 0' : ''}`);
shellCmd.on('data', data => {
const dataString = data.toString();
if (dataString.includes('debug')) {
log("TizenTube launched.");
if (ws) {
ws.send(JSON.stringify({
exit: true
}));
}
const port = dataString.substr(dataString.indexOf(':') + 1, 6).replace(' ', '');
startDebugging(port, adb_conn, tv_ip);
}
});
}
function kill_method(adb_conn, tv_ip) {
// Kill TizenTube and the Launcher
const kill_job = adb_conn.createStream(`shell:0 was_kill Ad6NutHP8l.TizenTube`);
kill_job.on('data', data1 => {
if (data1.toString().includes("spend time")) {
const kill_job2 = adb_conn.createStream(`shell:0 was_kill I80YHgsJe2.Launcher`);
kill_job2.on('data', data2 => {
if (data2.toString().includes("spend time")) {
// Wait 200ms to prevent issue
setTimeout(() => {
// Launch TizenTube in debug mode
const shellCmd = adb_conn.createStream(`shell:0 debug Ad6NutHP8l.TizenTube${Config.isTizen3 ? ' 0' : ''}`);
shellCmd.on('data', data => {
const dataString = data.toString();
if (dataString.includes('debug')) {
log("TizenTube launched.");
const port = dataString.substr(dataString.indexOf(':') + 1, 6).replace(' ', '');
startDebugging(port, adb_conn, tv_ip);
}
});
}, 200);
} else if (data2.toString().replace(/[\s\r\n]/g, '') != "")
log(data2.toString().replace(/[\r\n]/g, '')); //Log non empty data from kill command
});
} else if (data1.toString().replace(/[\s\r\n]/g, '') != "")
log(data1.toString().replace(/[\r\n]/g, '')); //Log non empty data from kill command
});
}
function retry_method(adb_conn, tv_ip, ws) {
//Launch TizenTube in debug mode
let shellCmd = adb_conn.createStream(`shell:0 debug Ad6NutHP8l.TizenTube${Config.isTizen3 ? ' 0' : ''}`);
let retry_count = 0;
//If we don't get any response, we try again (max 2 times)
let reconnectInterval = setInterval(() => {
shellCmd.removeAllListeners("data");
if (retry_count >= 2) {
clearInterval(reconnectInterval);
log("Failed to launch TizenTube.");
adb._stream.end();
return;
}
retry_count++;
//Re-Launch TizenTube in debug mode
shellCmd = adb_conn.createStream(`shell:0 debug Ad6NutHP8l.TizenTube${Config.isTizen3 ? ' 0' : ''}`);
log("Retry launching TizenTube...");
shellCmd.on('data', data => {
const dataString = data.toString();
if (dataString.includes('debug')) {
log("TizenTube launched.");
clearInterval(reconnectInterval);
//Tell launcher to close itself
if (ws) {
ws.send(JSON.stringify({
exit: true
}));
}
const port = dataString.substr(dataString.indexOf(':') + 1, 6).replace(' ', '');
startDebugging(port, adb, tv_ip);
}
});
}, 4000);
shellCmd.on('data', data => {
clearInterval(reconnectInterval); //Cancel retries if we got a response
const dataString = data.toString();
if (dataString.includes('debug')) {
log("TizenTube launched.");
if (ws) {
ws.send(JSON.stringify({
exit: true
}));
}
const port = dataString.substr(dataString.indexOf(':') + 1, 6).replace(' ', '');
startDebugging(port, adb_conn, tv_ip);
}
});
}
// If the server is running on Android and the CWD is /, change it (required for the Android app)
if (process.cwd() === '/' && process.platform === 'android') {
process.chdir('/data/user/0/io.gh.reisxd.tizentube/files/tizentube');
}