tcpip.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script src="./webadb.js"></script>
  5. <script>
  6. let adb;
  7. let webusb;
  8. let log = (...args) => {
  9. if (args[0] instanceof Error) {
  10. console.error.apply(console, args);
  11. } else {
  12. console.log.apply(console, args);
  13. }
  14. document.getElementById('log').innerText += args.join(' ') + '\n';
  15. };
  16. let init = async () => {
  17. log('init');
  18. webusb = await Adb.open("WebUSB");
  19. };
  20. let connect = async () => {
  21. await init();
  22. log('connect');
  23. if (webusb.isAdb()) {
  24. try {
  25. adb = null;
  26. // console.log(webusb)
  27. adb = await webusb.connectAdb("host::", () => {
  28. log("Please check the screen of your " + webusb.device.productName + ".");
  29. });
  30. } catch(error) {
  31. console.log('error',error)
  32. log(error);
  33. adb = null;
  34. }
  35. }
  36. };
  37. let disconnect = async () => {
  38. log('disconnect');
  39. webusb.close();
  40. };
  41. let get_ip = async () => {
  42. try {
  43. if (!adb) throw new Error('Not connected');
  44. log('get_ip');
  45. let shell = await adb.shell('ip addr show to 0.0.0.0/0 scope global');
  46. let response = await shell.receive();
  47. let decoder = new TextDecoder('utf-8');
  48. if(response && response.data){
  49. let txt = decoder.decode(response.data);
  50. log(txt);
  51. }else{
  52. log('no response')
  53. }
  54. } catch (error) {
  55. log(error);
  56. }
  57. };
  58. let tcpip = async () => {
  59. try {
  60. if (!adb) throw new Error('Not connected');
  61. let port = document.getElementById('port').value;
  62. log('requesting tcpip mode on port', port);
  63. await adb.tcpip(port);
  64. log('tcpip connection ready');
  65. } catch (error) {
  66. log(error);
  67. }
  68. };
  69. let send = async ()=>{
  70. try{
  71. if(!adb) throw new Error('Not connected')
  72. let a = await adb.send('push ./output.mp4 /root/')
  73. // log(a)
  74. }catch(error){
  75. log(error)
  76. }
  77. }
  78. let add_ui = () => {
  79. // Adb.Opt.use_checksum = false;
  80. Adb.Opt.debug = true;
  81. // Adb.Opt.dump = true;
  82. document.getElementById('connect').onclick = connect;
  83. document.getElementById('get_ip').onclick = get_ip;
  84. document.getElementById('disconnect').onclick = disconnect;
  85. document.getElementById('tcpip').onclick = tcpip;
  86. document.getElementById('send').onclick = send;
  87. document.getElementById('clear').onclick = () => {
  88. document.getElementById('log').innerText = '';
  89. };
  90. };
  91. document.addEventListener('DOMContentLoaded', add_ui, false);
  92. </script>
  93. </head>
  94. <body>
  95. <button id="connect">Connect</button>
  96. <br />
  97. <br />
  98. <button id="send">send</button>
  99. <button id="get_ip">Get IP addr</button>
  100. <br />
  101. <br />
  102. <input type="text" id="port" value="5037" />
  103. <button id="tcpip" value="tcpip">tcpip</button>
  104. <br />
  105. <br />
  106. <button id="disconnect">Disconnect</button>
  107. <br />
  108. <br />
  109. <h2>Messages <button id="clear">clear</button></h2>
  110. <pre id="log"></pre>
  111. </body>
  112. </html>