Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Jan 17 12:45:56 2021 +0000
Revision:
148:3d685db9fdd3
Parent:
147:ea6f647725a1
Net js modified to not show address when empty (it did show whatever had been there before)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 132:5b2df69a4f17 1 "//Net script\n"
andrewboyson 132:5b2df69a4f17 2 "'use strict';\n"
andrewboyson 132:5b2df69a4f17 3 "\n"
andrewboyson 132:5b2df69a4f17 4 "let mac = '';\n"
andrewboyson 147:ea6f647725a1 5 "let nr = '';\n"
andrewboyson 132:5b2df69a4f17 6 "let tcp = '';\n"
andrewboyson 143:cc2e148cb96a 7 "let dns = '';\n"
andrewboyson 146:6bc151bd7063 8 "let a4s = null;\n"
andrewboyson 146:6bc151bd7063 9 "let a6s = null;\n"
andrewboyson 132:5b2df69a4f17 10 "\n"
andrewboyson 132:5b2df69a4f17 11 "function parseTcpLine(line)\n"
andrewboyson 132:5b2df69a4f17 12 "{\n"
andrewboyson 132:5b2df69a4f17 13 " if (line.length == 0) return;\n"
andrewboyson 132:5b2df69a4f17 14 " let fields = line.split('\\t');\n"
andrewboyson 133:98c6bf14bc37 15 " let state = parseInt(fields[0], 16);\n"
andrewboyson 133:98c6bf14bc37 16 " let idleMs = parseInt(fields[1], 16);\n"
andrewboyson 133:98c6bf14bc37 17 " let ipType = parseInt(fields[2], 16);\n"
andrewboyson 133:98c6bf14bc37 18 " let arIndex = parseInt(fields[3], 16);\n"
andrewboyson 133:98c6bf14bc37 19 " let locPort = parseInt(fields[4], 16);\n"
andrewboyson 133:98c6bf14bc37 20 " let remPort = parseInt(fields[5], 16);\n"
andrewboyson 133:98c6bf14bc37 21 " let bytesRcvd = parseInt(fields[6], 16);\n"
andrewboyson 133:98c6bf14bc37 22 " let bytesSent = parseInt(fields[7], 16);\n"
andrewboyson 132:5b2df69a4f17 23 " \n"
andrewboyson 133:98c6bf14bc37 24 " switch (state)\n"
andrewboyson 133:98c6bf14bc37 25 " {\n"
andrewboyson 143:cc2e148cb96a 26 " case 1: tcp += \" Syn\"; break;\n"
andrewboyson 143:cc2e148cb96a 27 " case 2: tcp += \" Est\"; break;\n"
andrewboyson 143:cc2e148cb96a 28 " case 3: tcp += \" Wait\"; break;\n"
andrewboyson 133:98c6bf14bc37 29 " default: tcp += state.toString().padStart(5, ' '); break;\n"
andrewboyson 133:98c6bf14bc37 30 " }\n"
andrewboyson 132:5b2df69a4f17 31 " \n"
andrewboyson 133:98c6bf14bc37 32 " let idleMinutes = Math.floor(idleMs / 1000 / 60);\n"
andrewboyson 133:98c6bf14bc37 33 " tcp += idleMinutes.toString().padStart(4, ' ');\n"
andrewboyson 133:98c6bf14bc37 34 " tcp += \" \";\n"
andrewboyson 132:5b2df69a4f17 35 " \n"
andrewboyson 133:98c6bf14bc37 36 " if (ipType == 0x0800) tcp += \"IPv4\";\n"
andrewboyson 132:5b2df69a4f17 37 " else if (ipType == 0x86DD) tcp += \"IPv6\";\n"
andrewboyson 132:5b2df69a4f17 38 " else tcp += fields[1];\n"
andrewboyson 132:5b2df69a4f17 39 " \n"
andrewboyson 133:98c6bf14bc37 40 " tcp += arIndex.toString().padStart(4, ' ');\n"
andrewboyson 133:98c6bf14bc37 41 " tcp += locPort.toString().padStart(9, ' ');\n"
andrewboyson 133:98c6bf14bc37 42 " tcp += remPort.toString().padStart(9, ' ');\n"
andrewboyson 133:98c6bf14bc37 43 " tcp += bytesRcvd.toString().padStart(6, ' ');\n"
andrewboyson 133:98c6bf14bc37 44 " tcp += bytesSent.toString().padStart(6, ' ');\n"
andrewboyson 132:5b2df69a4f17 45 " tcp += '\\r\\n';\n"
andrewboyson 132:5b2df69a4f17 46 "}\n"
andrewboyson 143:cc2e148cb96a 47 "function parseDnsLine(line)\n"
andrewboyson 143:cc2e148cb96a 48 "{\n"
andrewboyson 143:cc2e148cb96a 49 " if (line.length == 0) return;\n"
andrewboyson 143:cc2e148cb96a 50 " let fields = line.split('\\t');\n"
andrewboyson 143:cc2e148cb96a 51 " \n"
andrewboyson 143:cc2e148cb96a 52 " dns += parseInt(fields[0], 16).toString().padStart(2, ' ');\n"
andrewboyson 143:cc2e148cb96a 53 " let minutes = parseInt(fields[1], 16) / 1000 / 60;\n"
andrewboyson 143:cc2e148cb96a 54 " dns += Math.floor(minutes).toString().padStart(4, ' ');\n"
andrewboyson 143:cc2e148cb96a 55 " dns += ' ';\n"
andrewboyson 147:ea6f647725a1 56 " let addrType = fields[2];\n"
andrewboyson 147:ea6f647725a1 57 " switch (addrType)\n"
andrewboyson 147:ea6f647725a1 58 " {\n"
andrewboyson 147:ea6f647725a1 59 " case '4': dns += ' A'; break;\n"
andrewboyson 147:ea6f647725a1 60 " case '6': dns += 'AAAA'; break;\n"
andrewboyson 147:ea6f647725a1 61 " default: dns += addrType; break;\n"
andrewboyson 147:ea6f647725a1 62 " }\n"
andrewboyson 147:ea6f647725a1 63 " dns += ' ';\n"
andrewboyson 146:6bc151bd7063 64 " let addr = '';\n"
andrewboyson 146:6bc151bd7063 65 " let mac = '';\n"
andrewboyson 147:ea6f647725a1 66 " switch (addrType)\n"
andrewboyson 145:d2bd78be00b2 67 " {\n"
andrewboyson 147:ea6f647725a1 68 " case '4':\n"
andrewboyson 148:3d685db9fdd3 69 " if (fields[3] === '00000000') break;\n"
andrewboyson 147:ea6f647725a1 70 " addr = Net.makeIp4(fields[3]);\n"
andrewboyson 147:ea6f647725a1 71 " if (a4s.has(addr)) mac = a4s.get(addr);\n"
andrewboyson 147:ea6f647725a1 72 " break;\n"
andrewboyson 147:ea6f647725a1 73 " case '6':\n"
andrewboyson 148:3d685db9fdd3 74 " if (fields[3].substr(0, 2) === '00') break;\n"
andrewboyson 147:ea6f647725a1 75 " addr = Net.makeIp6(fields[3]);\n"
andrewboyson 147:ea6f647725a1 76 " if (a6s.has(addr)) mac = a6s.get(addr);\n"
andrewboyson 147:ea6f647725a1 77 " break;\n"
andrewboyson 147:ea6f647725a1 78 " }\n"
andrewboyson 146:6bc151bd7063 79 " dns += addr.padEnd(40, ' ');\n"
andrewboyson 146:6bc151bd7063 80 " \n"
andrewboyson 146:6bc151bd7063 81 " dns += ' ';\n"
andrewboyson 147:ea6f647725a1 82 " dns += fields[4]; //DNS protocol used\n"
andrewboyson 143:cc2e148cb96a 83 " dns += ' ';\n"
andrewboyson 147:ea6f647725a1 84 " dns += fields[5].padEnd(32, ' '); //Resolved name\n"
andrewboyson 146:6bc151bd7063 85 " \n"
andrewboyson 146:6bc151bd7063 86 " if (mac != '')\n"
andrewboyson 146:6bc151bd7063 87 " {\n"
andrewboyson 146:6bc151bd7063 88 " dns += ' ';\n"
andrewboyson 146:6bc151bd7063 89 " dns += mac;\n"
andrewboyson 146:6bc151bd7063 90 " dns += ' ';\n"
andrewboyson 146:6bc151bd7063 91 " \n"
andrewboyson 146:6bc151bd7063 92 " let vendor = Net.getVendorFromLocalStorage(mac);\n"
andrewboyson 146:6bc151bd7063 93 " if (vendor)\n"
andrewboyson 146:6bc151bd7063 94 " {\n"
andrewboyson 146:6bc151bd7063 95 " dns += ' ';\n"
andrewboyson 146:6bc151bd7063 96 " dns += vendor;\n"
andrewboyson 146:6bc151bd7063 97 " }\n"
andrewboyson 146:6bc151bd7063 98 " else\n"
andrewboyson 146:6bc151bd7063 99 " {\n"
andrewboyson 146:6bc151bd7063 100 " Net.addVendorToLocalStorage(mac);\n"
andrewboyson 146:6bc151bd7063 101 " }\n"
andrewboyson 146:6bc151bd7063 102 " }\n"
andrewboyson 143:cc2e148cb96a 103 " dns += '\\r\\n';\n"
andrewboyson 143:cc2e148cb96a 104 "}\n"
andrewboyson 146:6bc151bd7063 105 "function parseAr4Line(line)\n"
andrewboyson 146:6bc151bd7063 106 "{\n"
andrewboyson 146:6bc151bd7063 107 " if (line.length == 0) return;\n"
andrewboyson 146:6bc151bd7063 108 " let fields = line.split('\\t');\n"
andrewboyson 146:6bc151bd7063 109 " \n"
andrewboyson 146:6bc151bd7063 110 " let index = parseInt(fields[0], 16);\n"
andrewboyson 146:6bc151bd7063 111 " let ms = parseInt(fields[1], 16);\n"
andrewboyson 146:6bc151bd7063 112 " let addr = Net.makeIp4(fields[2]);\n"
andrewboyson 146:6bc151bd7063 113 " let mac = Net.makeMac(fields[3]);\n"
andrewboyson 146:6bc151bd7063 114 " \n"
andrewboyson 146:6bc151bd7063 115 " a4s.set(addr, mac);\n"
andrewboyson 146:6bc151bd7063 116 "}\n"
andrewboyson 146:6bc151bd7063 117 "function parseAr6Line(line)\n"
andrewboyson 146:6bc151bd7063 118 "{\n"
andrewboyson 146:6bc151bd7063 119 " if (line.length == 0) return;\n"
andrewboyson 146:6bc151bd7063 120 " let fields = line.split('\\t');\n"
andrewboyson 146:6bc151bd7063 121 " \n"
andrewboyson 146:6bc151bd7063 122 " let index = parseInt(fields[0], 16);\n"
andrewboyson 146:6bc151bd7063 123 " let ms = parseInt(fields[1], 16);\n"
andrewboyson 146:6bc151bd7063 124 " let addr = Net.makeIp6(fields[2]);\n"
andrewboyson 146:6bc151bd7063 125 " let mac = Net.makeMac(fields[3]);\n"
andrewboyson 146:6bc151bd7063 126 " \n"
andrewboyson 146:6bc151bd7063 127 " a6s.set(addr, mac);\n"
andrewboyson 146:6bc151bd7063 128 "}\n"
andrewboyson 146:6bc151bd7063 129 "function parseGenLines(text)\n"
andrewboyson 146:6bc151bd7063 130 "{\n"
andrewboyson 146:6bc151bd7063 131 " let lines = text.split('\\n');\n"
andrewboyson 146:6bc151bd7063 132 " mac = Net.makeMac(lines[ 0]);\n"
andrewboyson 147:ea6f647725a1 133 " nr = lines[ 1] ;\n"
andrewboyson 146:6bc151bd7063 134 "}\n"
andrewboyson 146:6bc151bd7063 135 "function parseTcpLines(text)\n"
andrewboyson 146:6bc151bd7063 136 "{\n"
andrewboyson 146:6bc151bd7063 137 " tcp = \"State Idle Protocol ARI Port-Loc Port-Rem Rcvd Sent\\n\";\n"
andrewboyson 146:6bc151bd7063 138 " text.split('\\n').forEach(parseTcpLine);\n"
andrewboyson 146:6bc151bd7063 139 "}\n"
andrewboyson 144:7106252b7abf 140 "function parseDnsLines(text)\n"
andrewboyson 144:7106252b7abf 141 "{\n"
andrewboyson 144:7106252b7abf 142 " dns = '';\n"
andrewboyson 144:7106252b7abf 143 " text.split('\\n').forEach(parseDnsLine);\n"
andrewboyson 144:7106252b7abf 144 "}\n"
andrewboyson 146:6bc151bd7063 145 "function parseAr4Lines(text)\n"
andrewboyson 132:5b2df69a4f17 146 "{\n"
andrewboyson 146:6bc151bd7063 147 " a4s = new Map();\n"
andrewboyson 146:6bc151bd7063 148 " text.split('\\n').forEach(parseAr4Line);\n"
andrewboyson 146:6bc151bd7063 149 "}\n"
andrewboyson 146:6bc151bd7063 150 "function parseAr6Lines(text)\n"
andrewboyson 146:6bc151bd7063 151 "{\n"
andrewboyson 146:6bc151bd7063 152 " a6s = new Map();\n"
andrewboyson 146:6bc151bd7063 153 " text.split('\\n').forEach(parseAr6Line);\n"
andrewboyson 132:5b2df69a4f17 154 "}\n"
andrewboyson 132:5b2df69a4f17 155 "function parse()\n"
andrewboyson 132:5b2df69a4f17 156 "{\n"
andrewboyson 132:5b2df69a4f17 157 " let topics = Ajax.response.split('\\f');\n"
andrewboyson 132:5b2df69a4f17 158 " parseGenLines(topics[0]);\n"
andrewboyson 132:5b2df69a4f17 159 " parseTcpLines(topics[1]);\n"
andrewboyson 146:6bc151bd7063 160 " parseAr4Lines(topics[3]);\n"
andrewboyson 146:6bc151bd7063 161 " parseAr6Lines(topics[4]);\n"
andrewboyson 143:cc2e148cb96a 162 " parseDnsLines(topics[2]);\n"
andrewboyson 132:5b2df69a4f17 163 "}\n"
andrewboyson 132:5b2df69a4f17 164 "function display()\n"
andrewboyson 132:5b2df69a4f17 165 "{\n"
andrewboyson 132:5b2df69a4f17 166 " let elem;\n"
andrewboyson 132:5b2df69a4f17 167 "\n"
andrewboyson 132:5b2df69a4f17 168 " elem = Ajax.getElementOrNull('ajax-tcp' ); if (elem) elem.textContent = tcp;\n"
andrewboyson 132:5b2df69a4f17 169 " elem = Ajax.getElementOrNull('ajax-mac' ); if (elem) elem.textContent = mac;\n"
andrewboyson 147:ea6f647725a1 170 " elem = Ajax.getElementOrNull('ajax-nr' ); if (elem) elem.value = nr;\n"
andrewboyson 143:cc2e148cb96a 171 " elem = Ajax.getElementOrNull('ajax-dns' ); if (elem) elem.textContent = dns;\n"
andrewboyson 132:5b2df69a4f17 172 "}\n"
andrewboyson 132:5b2df69a4f17 173 "\n"
andrewboyson 132:5b2df69a4f17 174 "Ajax.server = '/net-ajax';\n"
andrewboyson 132:5b2df69a4f17 175 "Ajax.onResponse = function() { parse(); display(); };\n"
andrewboyson 132:5b2df69a4f17 176 "Ajax.init();\n"
andrewboyson 132:5b2df69a4f17 177 ""