hds_scan.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. include '../auth.php';
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <meta charset="utf-8">
  9. <title>HDS Scanner (Experimental)</title>
  10. <script src="../script/jquery.min.js"></script>
  11. </head>
  12. <body style="background-color:black;color:white;">
  13. <h2>HDS Protocol IP Scanner (Experimental)</h2>
  14. This tool can only scan for NodeMCU / ESP8266 with HDS Protocol Enabled.<br>
  15. To test this script, please make sure there is content under ip_address/uuid with allow origin *<br>
  16. Your network IP is: <p id="list">Loading</p>
  17. <p id="debug"></p>
  18. <script>
  19. var HDSIPList = [];
  20. var startedConnections = 0;
  21. $(document).ready(function(){
  22. GrapIP();
  23. setTimeout(function() {
  24. StartWebWorker();
  25. }, 2000);
  26. });
  27. function StartWebWorker(){
  28. var ip = $("#list").html();
  29. var webWorkers = [];
  30. if (ip.includes("ifconfig")){
  31. $("#debug").append("This browser is not supported.<br>");
  32. return;
  33. }
  34. if (typeof(Worker) !== "undefined") {
  35. $("#debug").append("Web Worker Exists. IP Scanning Started.<br>");
  36. //The browser support everything and ready to start scanning
  37. var ipg = ip.split(".");
  38. var header = ipg[0] + "." + ipg[1] + "." + ipg[2] + "."; //a.b.c.
  39. for (var i=1; i < 255;i++){
  40. startedConnections++;
  41. GetWorkingOrNot(header + i)
  42. }
  43. WaitAllFinish();
  44. } else {
  45. $("#debug").html("Error. Web Worker not supported.");
  46. }
  47. }
  48. function WaitAllFinish(){
  49. $("html, body").animate({ scrollTop: $(document).height() }, 200);
  50. if (startedConnections <= 1){
  51. $("#debug").append("<br>DONE<br><br>");
  52. GenerateTable();
  53. }else{
  54. setTimeout(WaitAllFinish, 1000);
  55. }
  56. }
  57. function GetWorkingOrNot(ip){
  58. $.ajax({url: "http://" + ip + "/uuid",
  59. type: "HEAD",
  60. timeout:5000,
  61. statusCode: {
  62. 200: function (response) {
  63. $("#debug").append("[OK]" +ip + " -> OCL:" + startedConnections + "<br>");
  64. //$("#debug").append("<a href='http://" +ip + "/' target='_blank'>Click here to redirect</a><br>");
  65. HDSIPList.push(ip);
  66. startedConnections--;
  67. },
  68. 400: function (response) {
  69. $("#debug").append("[NOT FIND]" +ip + " -> OCL:" + startedConnections + "<br>");
  70. startedConnections--;
  71. },
  72. 0: function (response) {
  73. $("#debug").append("[NOT REPLY]" +ip + " -> OCL:" + startedConnections + "<br>");
  74. startedConnections--;
  75. },
  76. 401: function (response) {
  77. $("#debug").append("[NOT FIND]" +ip + " -> OCL:" + startedConnections + "<br>");
  78. startedConnections--;
  79. },
  80. 404: function (response) {
  81. $("#debug").append("[NOT FIND]" +ip + " -> OCL:" + startedConnections + "<br>");
  82. startedConnections--;
  83. }
  84. }
  85. });
  86. }
  87. function GrapIP(){
  88. // NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
  89. var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
  90. if (RTCPeerConnection) (function () {
  91. var rtc = new RTCPeerConnection({iceServers:[]});
  92. if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed
  93. rtc.createDataChannel('', {reliable:false});
  94. };
  95. rtc.onicecandidate = function (evt) {
  96. // convert the candidate to SDP so we can run it through our general parser
  97. // see https://twitter.com/lancestout/status/525796175425720320 for details
  98. if (evt.candidate) grepSDP("a="+evt.candidate.candidate);
  99. };
  100. rtc.createOffer(function (offerDesc) {
  101. grepSDP(offerDesc.sdp);
  102. rtc.setLocalDescription(offerDesc);
  103. }, function (e) { console.warn("offer failed", e); });
  104. var addrs = Object.create(null);
  105. addrs["0.0.0.0"] = false;
  106. function updateDisplay(newAddr) {
  107. if (newAddr in addrs) return;
  108. else addrs[newAddr] = true;
  109. var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
  110. document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";
  111. }
  112. function grepSDP(sdp) {
  113. var hosts = [];
  114. sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
  115. if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
  116. var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
  117. addr = parts[4],
  118. type = parts[7];
  119. if (type === 'host') updateDisplay(addr);
  120. } else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
  121. var parts = line.split(' '),
  122. addr = parts[2];
  123. updateDisplay(addr);
  124. }
  125. });
  126. }
  127. })(); else {
  128. document.getElementById('list').innerHTML = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
  129. document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
  130. //Callback to next function
  131. }
  132. }
  133. function GenerateTable(){
  134. //Request the HDS Host to generate the ip table for all in range HDS unit
  135. console.log(HDSIPList);
  136. $.post( "HDS_iptable.php", {ipTable: HDSIPList})
  137. .done(function( data ) {
  138. window.location = "index.php";
  139. });
  140. }
  141. </script>
  142. </body></html>