forked from webadb/webadb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpip.html
108 lines (95 loc) · 2.79 KB
/
tcpip.html
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
<!DOCTYPE HTML>
<html>
<head>
<script src="./webadb.js"></script>
<script>
let adb;
let webusb;
let log = (...args) => {
if (args[0] instanceof Error) {
console.error.apply(console, args);
} else {
console.log.apply(console, args);
}
document.getElementById('log').innerText += args.join(' ') + '\n';
};
let init = async () => {
log('init');
webusb = await Adb.open("WebUSB");
};
let connect = async () => {
await init();
log('connect');
if (webusb.isAdb()) {
try {
adb = null;
adb = await webusb.connectAdb("host::", () => {
log("Please check the screen of your " + webusb.device.productName + ".");
});
} catch(error) {
log(error);
adb = null;
}
}
};
let disconnect = async () => {
log('disconnect');
webusb.close();
};
let get_ip = async () => {
try {
if (!adb) throw new Error('Not connected');
log('get_ip');
let shell = await adb.shell('ip addr show to 0.0.0.0/0 scope global');
let response = await shell.receive();
let decoder = new TextDecoder('utf-8');
let txt = decoder.decode(response.data);
log(txt);
} catch (error) {
log(error);
}
};
let tcpip = async () => {
try {
if (!adb) throw new Error('Not connected');
let port = document.getElementById('port').value;
log('requesting tcpip mode on port', port);
await adb.tcpip(port);
log('tcpip connection ready');
} catch (error) {
log(error);
}
};
let add_ui = () => {
// Adb.Opt.use_checksum = false;
Adb.Opt.debug = true;
// Adb.Opt.dump = true;
document.getElementById('connect').onclick = connect;
document.getElementById('get_ip').onclick = get_ip;
document.getElementById('disconnect').onclick = disconnect;
document.getElementById('tcpip').onclick = tcpip;
document.getElementById('clear').onclick = () => {
document.getElementById('log').innerText = '';
};
};
document.addEventListener('DOMContentLoaded', add_ui, false);
</script>
</head>
<body>
<button id="connect">Connect</button>
<br />
<br />
<button id="get_ip">Get IP addr</button>
<br />
<br />
<input type="text" id="port" value="5555" />
<button id="tcpip" value="tcpip">tcpip</button>
<br />
<br />
<button id="disconnect">Disconnect</button>
<br />
<br />
<h2>Messages <button id="clear">clear</button></h2>
<pre id="log"></pre>
</body>
</html>