| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"><head><meta charset="utf-8"> |
| 3 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 4 | <title>netcheck</title> |
| 5 | <!--TOKENS--> |
| 6 | <style> |
| 7 | *{box-sizing:border-box} |
| 8 | html,body{margin:0;height:100%} |
| 9 | body{background:var(--bg);color:var(--text);font-family:var(--font); |
| 10 | font-size:14px;line-height:1.5;-webkit-font-smoothing:antialiased} |
| 11 | .wrap{max-width:1080px;margin:0 auto;padding:16px 16px 32px} |
| 12 | .top{display:flex;align-items:center;gap:10px;margin-bottom:14px;flex-wrap:wrap} |
| 13 | .title{font-size:19px;font-weight:700;margin:0;letter-spacing:-.01em} |
| 14 | .pill{font-size:11px;font-weight:600;padding:2px 9px;border-radius:999px; |
| 15 | background:var(--surface-2);color:var(--text-2);border:1px solid var(--line); |
| 16 | white-space:nowrap} |
| 17 | .sp{flex:1} |
| 18 | .icon{display:inline-flex;align-items:center;justify-content:center;gap:6px; |
| 19 | background:var(--surface-2);border:1px solid var(--line);color:var(--text); |
| 20 | border-radius:9px;width:30px;height:30px;padding:0;cursor:pointer;font-family:inherit; |
| 21 | font-size:12px;font-weight:600} |
| 22 | .icon:hover{background:var(--surface-hover);border-color:var(--line-2)} |
| 23 | .icon svg{width:15px;height:15px;display:block;fill:none;stroke:currentColor; |
| 24 | stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round} |
| 25 | .btn{background:var(--blue);color:#fff;border:0;border-radius:980px;padding:8px 16px; |
| 26 | font-weight:600;font-size:13px;cursor:pointer;font-family:inherit} |
| 27 | .btn:disabled{opacity:.55;cursor:default} |
| 28 | .btn.gh{background:transparent;color:var(--text);border:1px solid var(--line)} |
| 29 | .card{background:var(--bg-card);border:1px solid var(--line);border-radius:14px; |
| 30 | padding:16px;margin-bottom:14px;box-shadow:var(--shadow)} |
| 31 | .card h2{margin:0 0 10px;font-size:11.5px;letter-spacing:.07em;text-transform:uppercase; |
| 32 | color:var(--text-2);font-weight:600} |
| 33 | .hint{color:var(--text-2);font-size:12px;margin:0 0 10px} |
| 34 | .grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:10px} |
| 35 | .kpi{background:var(--surface-2);border:1px solid var(--line);border-radius:10px; |
| 36 | padding:10px 12px} |
| 37 | .kpi .k{font-size:10.5px;color:var(--text-2);text-transform:uppercase;letter-spacing:.05em} |
| 38 | .kpi .v{font-size:15px;font-weight:700;font-family:var(--mono);margin-top:3px; |
| 39 | word-break:break-word} |
| 40 | .tb{width:100%;overflow-x:auto} |
| 41 | table{width:100%;border-collapse:collapse;font-size:13px} |
| 42 | th{text-align:left;font-size:10.5px;text-transform:uppercase;letter-spacing:.05em; |
| 43 | color:var(--text-2);font-weight:600;padding:6px 8px;border-bottom:1px solid var(--line); |
| 44 | white-space:nowrap} |
| 45 | td{padding:7px 8px;border-bottom:1px solid var(--line);vertical-align:middle; |
| 46 | word-break:break-word} |
| 47 | tr:last-child td{border-bottom:0} |
| 48 | .mo{font-family:var(--mono);font-size:12.5px} |
| 49 | .tag{font-size:10px;font-weight:700;padding:2px 7px;border-radius:6px;white-space:nowrap; |
| 50 | background:var(--surface-2);color:var(--text-2);border:1px solid var(--line)} |
| 51 | .tag.ok{background:rgba(48,209,88,.16);color:var(--green)} |
| 52 | .tag.warn{background:rgba(255,159,10,.16);color:var(--orange)} |
| 53 | .tag.bad{background:rgba(255,69,58,.16);color:var(--red)} |
| 54 | .tag.blue{background:rgba(10,132,255,.16);color:var(--blue)} |
| 55 | .tabs{display:flex;gap:6px;margin-bottom:10px;flex-wrap:wrap} |
| 56 | .tab{background:var(--surface-2);border:1px solid var(--line);color:var(--text-2); |
| 57 | border-radius:980px;padding:6px 14px;font-size:12.5px;font-weight:600;cursor:pointer; |
| 58 | font-family:inherit} |
| 59 | .tab.on{background:var(--blue);color:#fff;border-color:var(--blue)} |
| 60 | .inp{flex:1 1 240px;min-width:0;background:var(--input-bg);border:1px solid var(--line); |
| 61 | border-radius:10px;padding:10px 12px;color:var(--text);font-size:13.5px; |
| 62 | font-family:inherit} |
| 63 | .inp:focus{outline:2px solid var(--blue);outline-offset:-1px} |
| 64 | .inp.mo{font-family:var(--mono)} |
| 65 | .row{display:flex;gap:8px;align-items:center;flex-wrap:wrap} |
| 66 | .res{border-top:1px solid var(--line);padding-top:12px;margin-top:12px} |
| 67 | .res .hd{display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:8px} |
| 68 | .res .big{font-size:16px;font-weight:700;font-family:var(--mono)} |
| 69 | .kv{display:grid;grid-template-columns:auto 1fr;gap:4px 12px;font-size:12.5px} |
| 70 | .kv .k{color:var(--text-2)} |
| 71 | .kv .v{font-family:var(--mono);word-break:break-all} |
| 72 | ul.hist{list-style:none;margin:0;padding:0} |
| 73 | ul.hist li{display:flex;align-items:center;gap:8px;padding:7px 2px; |
| 74 | border-bottom:1px solid var(--line);font-size:12.5px;flex-wrap:wrap} |
| 75 | ul.hist li:last-child{border-bottom:0} |
| 76 | ul.hist .t{font-family:var(--mono);flex:1 1 200px;min-width:0;word-break:break-all} |
| 77 | ul.hist .ms{font-family:var(--mono);color:var(--text-2);font-size:11.5px} |
| 78 | .spark{width:100%;height:56px;display:block;margin-top:10px} |
| 79 | .spark[hidden]{display:none} |
| 80 | .spark .ln{fill:none;stroke:var(--blue);stroke-width:1.6; |
| 81 | stroke-linecap:round;stroke-linejoin:round} |
| 82 | .spark .ax{stroke:var(--line);stroke-width:1} |
| 83 | .spark .dot{fill:var(--blue)} |
| 84 | .spark .dot.bad{fill:var(--red)} |
| 85 | .empty{color:var(--text-2);font-size:12.5px;padding:10px 0} |
| 86 | [hidden]{display:none!important} |
| 87 | .err{color:var(--red);font-size:12.5px} |
| 88 | .note{color:var(--text-2);font-size:12px;margin-top:8px} |
| 89 | .foot{color:var(--text-2);font-size:11.5px;margin-top:18px;line-height:1.6} |
| 90 | .foot code{font-family:var(--mono);font-size:11px;background:var(--surface-2); |
| 91 | padding:1px 5px;border-radius:5px;border:1px solid var(--line)} |
| 92 | @media(max-width:700px){.wrap{padding:10px 10px 24px}.card{padding:12px;border-radius:12px} |
| 93 | .title{font-size:17px}} |
| 94 | </style></head><body><div class="wrap"> |
| 95 | |
| 96 | <div class="top"> |
| 97 | <a href="/" class="embed-hide" style="color:var(--blue);text-decoration:none;font-weight:600">←</a> |
| 98 | <h1 class="title">netcheck</h1> |
| 99 | <span class="pill" id="srcPill">this node</span> |
| 100 | <span class="sp"></span> |
| 101 | <button class="icon embed-hide" id="thBtn" title="Toggle appearance" aria-label="Toggle appearance"> |
| 102 | <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="4.5"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M19.1 4.9l-1.4 1.4M6.3 17.7l-1.4 1.4"/></svg> |
| 103 | </button> |
| 104 | <button class="icon" id="rfBtn" title="Refresh interfaces" aria-label="Refresh interfaces"> |
| 105 | <svg viewBox="0 0 24 24"><path d="M20 11a8 8 0 1 0-2.3 5.7"/><path d="M20 5v6h-6"/></svg> |
| 106 | </button> |
| 107 | </div> |
| 108 | |
| 109 | <div class="card"> |
| 110 | <h2>Interfaces on this node</h2> |
| 111 | <p class="hint">Addresses read from the node's own interface table. Nothing leaves the machine for this view.</p> |
| 112 | <div class="grid" id="kpis"></div> |
| 113 | <div class="tb" style="margin-top:12px"> |
| 114 | <table> |
| 115 | <thead><tr><th>Interface</th><th>Address</th><th>Netmask</th><th>State</th></tr></thead> |
| 116 | <tbody id="ifrows"></tbody> |
| 117 | </table> |
| 118 | </div> |
| 119 | <div class="empty" id="ifEmpty" hidden>No interfaces found.</div> |
| 120 | <div class="res"> |
| 121 | <h2 style="margin-top:0">Configured DNS servers</h2> |
| 122 | <div class="row" id="nsList"></div> |
| 123 | </div> |
| 124 | </div> |
| 125 | |
| 126 | <div class="card"> |
| 127 | <h2>Run a check</h2> |
| 128 | <div class="tabs" role="tablist"> |
| 129 | <button class="tab on" id="tabDns" role="tab" data-tab="dns">DNS</button> |
| 130 | <button class="tab" id="tabTcp" role="tab" data-tab="tcp">TCP</button> |
| 131 | <button class="tab" id="tabHttp" role="tab" data-tab="http">HTTP</button> |
| 132 | </div> |
| 133 | <div class="row"> |
| 134 | <input id="inp" class="inp mo" placeholder="example.com" autocomplete="off" spellcheck="false" aria-label="Target"> |
| 135 | <input id="port" class="inp mo" type="number" min="1" max="65535" step="1" |
| 136 | value="443" style="width:110px;display:none" placeholder="port" aria-label="Port"> |
| 137 | <button class="btn" id="goBtn">Check</button> |
| 138 | </div> |
| 139 | <p class="note" id="tabNote">Resolves a name to its addresses. Uses the node's resolver with a 3 second deadline.</p> |
| 140 | <div class="res" id="resBox"> |
| 141 | <div class="hd"><span class="big" id="resState">idle</span><span id="resTag"></span></div> |
| 142 | <div class="kv" id="resKv"></div> |
| 143 | <div class="note" id="resNote"></div> |
| 144 | <svg class="spark" id="spark" viewBox="0 0 320 56" preserveAspectRatio="none" |
| 145 | aria-label="TCP latency history" hidden></svg> |
| 146 | </div> |
| 147 | </div> |
| 148 | |
| 149 | <div class="card"> |
| 150 | <h2>History <span class="pill" id="histCount">0</span></h2> |
| 151 | <p class="hint">Kept in this page only — nothing is written to disk.</p> |
| 152 | <ul class="hist" id="hist"><li class="empty">No checks yet this session.</li></ul> |
| 153 | </div> |
| 154 | |
| 155 | <p class="foot"> |
| 156 | netcheck runs on the node and makes only the outbound connections you ask for: a DNS lookup, |
| 157 | a TCP connect, or one <code>HEAD</code> request over plain HTTP. There is no TLS library in the |
| 158 | binary, so an <code>https://</code> target is checked as a TCP connect to port 443 and reported |
| 159 | as <code>tls: not_checked</code>. Response bodies are never read, and redirects are never followed. |
| 160 | Every action sits behind node authentication. |
| 161 | </p> |
| 162 | </div> |
| 163 | <script> |
| 164 | (function(){ |
| 165 | "use strict"; |
| 166 | var q=new URLSearchParams(location.search); |
| 167 | var node=q.get('node')||''; |
| 168 | var sid=q.get('id')||''; |
| 169 | var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))|| |
| 170 | localStorage.getItem('bot_active_pwd')||''; |
| 171 | var HUB=q.get('plugin')||''; |
| 172 | |
| 173 | /* Two call routes, and getting this wrong is the most common plugin bug: |
| 174 | * page served through the hub (bot.scorpiox.net/plugins?plugin=plugin_netcheck&node=..) |
| 175 | * -> call the hub proxy back: /plugins?plugin=plugin_netcheck&action=.. |
| 176 | * opened directly on the node |
| 177 | * -> the plugin itself is the route: /plugin_netcheck?action=.. |
| 178 | * Calling /plugins?plugin= on the node returns the discovery list, not us. */ |
| 179 | function api(action,params){ |
| 180 | var u=(HUB?'/plugins?plugin='+encodeURIComponent(HUB):'/plugin_netcheck') |
| 181 | +'?action='+encodeURIComponent(action) |
| 182 | +(params||'') |
| 183 | +(node?'&node='+encodeURIComponent(node):'') |
| 184 | +(sid?'&id='+encodeURIComponent(sid):'') |
| 185 | +(pwd?'&pwd='+encodeURIComponent(pwd):''); |
| 186 | return fetch(u,{headers:pwd?{'X-Bot-Password':pwd}:{}}) |
| 187 | .then(function(r){return r.json().then(function(d){if(!r.ok)throw new Error('HTTP '+r.status);return d;});}); |
| 188 | } |
| 189 | |
| 190 | function esc(v){return String(v==null?'':v).replace(/[&<>"']/g,function(c){ |
| 191 | return({'&':'&','<':'<','>':'>','"':'"',"'":'''})[c];});} |
| 192 | function txt(id,v){var e=document.getElementById(id);if(e)e.textContent=v;} |
| 193 | function $(id){return document.getElementById(id);} |
| 194 | |
| 195 | var TAB='dns'; |
| 196 | var HIST=[]; /* last 20 checks, in-page only */ |
| 197 | var TCPMS=[]; /* tcp latency samples for the sparkline */ |
| 198 | |
| 199 | var NOTE={ |
| 200 | dns:'Resolves a name to its addresses. Uses the node\'s resolver with a 3 second deadline.', |
| 201 | tcp:'Measures a TCP connect. Three samples, 3 second deadline each, reported as min/avg/max.', |
| 202 | http:'Sends one HEAD request over plain TCP. Status line and headers only, body never read, no redirects.' |
| 203 | }; |
| 204 | var PH={dns:'example.com',tcp:'example.com',http:'http://example.com'}; |
| 205 | |
| 206 | function setTab(t){ |
| 207 | TAB=t; |
| 208 | ['dns','tcp','http'].forEach(function(k){ |
| 209 | var b=$('tab'+k.charAt(0).toUpperCase()+k.slice(1)); |
| 210 | if(b)b.className='tab'+(k===t?' on':''); |
| 211 | }); |
| 212 | $('port').style.display=(t==='tcp')?'':'none'; |
| 213 | $('inp').placeholder=PH[t]; |
| 214 | txt('tabNote',NOTE[t]); |
| 215 | } |
| 216 | |
| 217 | /* ---------- interfaces ---------- */ |
| 218 | function renderIfaces(d){ |
| 219 | var rows=$('ifrows'),kp=$('kpis'),ns=$('nsList'); |
| 220 | var list=(d&&d.interfaces)||[]; |
| 221 | var upn=0,glob=0,lo=0,i; |
| 222 | var h=''; |
| 223 | for(i=0;i<list.length;i++){ |
| 224 | var f=list[i]; |
| 225 | if(f.up)upn++; |
| 226 | if(!f.loopback&&(f.family===4||f.addr!=='::1'))glob++; |
| 227 | if(f.loopback)lo++; |
| 228 | h+='<tr><td class="mo">'+esc(f.name)+'</td>' |
| 229 | +'<td class="mo">'+esc(f.address)+'</td>' |
| 230 | +'<td class="mo">'+esc(f.netmask||'-')+'</td>' |
| 231 | +'<td>'+(f.up?'<span class="tag ok">up</span>':'<span class="tag bad">down</span>') |
| 232 | +(f.loopback?' <span class="tag">loopback</span>':'') |
| 233 | +(f.family===6?' <span class="tag">ipv6</span>':'')+'</td></tr>'; |
| 234 | } |
| 235 | rows.innerHTML=h; |
| 236 | /* show the empty message only when there really are no rows */ |
| 237 | if(list.length){$('ifEmpty').setAttribute('hidden','');} |
| 238 | else{$('ifEmpty').removeAttribute('hidden');} |
| 239 | var nsr=(d&&d.nameservers)||[]; |
| 240 | var kh=''; |
| 241 | kh+='<div class="kpi"><div class="k">Interfaces</div><div class="v">'+list.length+'</div></div>'; |
| 242 | kh+='<div class="kpi"><div class="k">Up</div><div class="v">'+upn+'</div></div>'; |
| 243 | kh+='<div class="kpi"><div class="k">Loopback</div><div class="v">'+lo+'</div></div>'; |
| 244 | kh+='<div class="kpi"><div class="k">DNS servers</div><div class="v">'+nsr.length+'</div></div>'; |
| 245 | kp.innerHTML=kh; |
| 246 | if(!nsr.length){ns.innerHTML='<span class="empty">None configured.</span>';} |
| 247 | else{var nh='';for(i=0;i<nsr.length;i++)nh+='<span class="tag blue">'+esc(nsr[i])+'</span>';ns.innerHTML=nh;} |
| 248 | txt('srcPill',(d&&d.count!=null)?(list.length+' iface'+(list.length===1?'':'s')):'this node'); |
| 249 | } |
| 250 | |
| 251 | function loadIfaces(){ |
| 252 | api('ifaces','').then(renderIfaces).catch(function(e){ |
| 253 | $('ifrows').innerHTML=''; |
| 254 | $('kpis').innerHTML='<div class="kpi"><div class="k">Error</div><div class="v">'+esc(String(e.message||e))+'</div></div>'; |
| 255 | $('ifEmpty').removeAttribute('hidden'); |
| 256 | }); |
| 257 | } |
| 258 | |
| 259 | /* ---------- sparkline ---------- */ |
| 260 | function drawSpark(){ |
| 261 | var svg=$('spark'); |
| 262 | /* `hidden` is an HTMLElement property, not an SVGElement one, so it has to |
| 263 | * be set as an attribute or the browser keeps the one from the markup. */ |
| 264 | if(TAB!=='tcp'||TCPMS.length<2){svg.setAttribute('hidden','');return;} |
| 265 | svg.removeAttribute('hidden'); |
| 266 | var W=320,H=56,PAD=6; |
| 267 | var vals=TCPMS.slice(-20); |
| 268 | var good=vals.filter(function(v){return v>=0;}); |
| 269 | var mx=good.length?Math.max.apply(null,good):0; |
| 270 | var mn=good.length?Math.min.apply(null,good):0; |
| 271 | /* A flat series has no range to normalise against. Plot it on the middle |
| 272 | * line instead of the floor, otherwise "every connect was instant" reads |
| 273 | * like "every connect hit the deadline". */ |
| 274 | var flat=(mx-mn)<1; |
| 275 | var scale=flat?1:(mx-mn); |
| 276 | var n=vals.length; |
| 277 | var pts=[],i,x,y; |
| 278 | for(i=0;i<n;i++){ |
| 279 | x=PAD+(i*(W-2*PAD))/(n-1); |
| 280 | var v=vals[i]; |
| 281 | if(v<0){y=H-PAD;} |
| 282 | else if(flat){y=(H-2*PAD)/2+PAD;} |
| 283 | else{y=H-PAD-((H-2*PAD)*((v-mn)/scale));} |
| 284 | pts.push([x,y,v]); |
| 285 | } |
| 286 | var out='<line class="ax" x1="0" y1="'+(H-PAD)+'" x2="'+W+'" y2="'+(H-PAD)+'"/>'; |
| 287 | var d=''; |
| 288 | for(i=0;i<pts.length;i++){ |
| 289 | if(pts[i][2]<0)continue; |
| 290 | d+=(d?'L':'M')+pts[i][0].toFixed(1)+' '+pts[i][1].toFixed(1); |
| 291 | } |
| 292 | if(d)out+='<path class="ln" d="'+d+'"/>'; |
| 293 | for(i=0;i<pts.length;i++){ |
| 294 | out+='<circle class="dot'+(pts[i][2]<0?' bad':'')+'" cx="'+pts[i][0].toFixed(1) |
| 295 | +'" cy="'+pts[i][1].toFixed(1)+'" r="2.4"/>'; |
| 296 | } |
| 297 | svg.innerHTML=out; |
| 298 | } |
| 299 | |
| 300 | /* ---------- history ---------- */ |
| 301 | function pushHist(item){ |
| 302 | HIST.unshift(item); |
| 303 | if(HIST.length>20)HIST.pop(); |
| 304 | renderHist(); |
| 305 | } |
| 306 | function renderHist(){ |
| 307 | var ul=$('hist'); |
| 308 | txt('histCount',String(HIST.length)); |
| 309 | if(!HIST.length){ul.innerHTML='<li class="empty">No checks yet this session.</li>';return;} |
| 310 | var h=''; |
| 311 | for(var i=0;i<HIST.length;i++){ |
| 312 | var it=HIST[i]; |
| 313 | h+='<li><span class="tag '+(it.ok?'ok':'bad')+'">'+esc(it.kind)+'</span>' |
| 314 | +'<span class="t">'+esc(it.label)+'</span>' |
| 315 | +'<span class="ms">'+esc(it.detail)+'</span>' |
| 316 | +'<span class="ms">'+esc(it.at)+'</span></li>'; |
| 317 | } |
| 318 | ul.innerHTML=h; |
| 319 | } |
| 320 | |
| 321 | /* ---------- result rendering ---------- */ |
| 322 | function kv(pairs){ |
| 323 | var h=''; |
| 324 | for(var i=0;i<pairs.length;i++){ |
| 325 | h+='<div class="k"><span class="k">'+esc(pairs[i][0])+'</span></div>' |
| 326 | +'<div class="v">'+esc(pairs[i][1]==null?'-':String(pairs[i][1]))+'</div>'; |
| 327 | } |
| 328 | $('resKv').innerHTML=h; |
| 329 | } |
| 330 | |
| 331 | function showDns(d,target){ |
| 332 | var ok=d.resolved===true; |
| 333 | txt('resState',ok?'resolved':'not resolved'); |
| 334 | $('resTag').innerHTML=ok?'<span class="tag ok">ok</span>':'<span class="tag bad">'+esc(d.timeout?'timeout':'fail')+'</span>'; |
| 335 | var pairs=[['host',d.host],['latency',(d.latency_ms==null?'-':d.latency_ms+' ms')], |
| 336 | ['answers',d.count],['deadline',d.timeout_ms+' ms']]; |
| 337 | if(d.results&&d.results.length){ |
| 338 | var addrs=[]; |
| 339 | for(var i=0;i<d.results.length;i++)addrs.push(d.results[i].address); |
| 340 | pairs.push(['addresses',addrs.join(', ')]); |
| 341 | } |
| 342 | kv(pairs); |
| 343 | txt('resNote',ok?'':'The resolver returned no record for this name.'); |
| 344 | pushHist({kind:'dns',label:target,ok:ok,detail:(d.count||0)+' addr',at:clock()}); |
| 345 | } |
| 346 | |
| 347 | function showTcp(d,target){ |
| 348 | var open=d.open===true; |
| 349 | var last=(d.samples&&d.samples.length)?d.samples[d.samples.length-1]:null; |
| 350 | TCPMS.push(typeof last==='number'?last:-1); |
| 351 | if(TCPMS.length>20)TCPMS.shift(); |
| 352 | txt('resState',open?'open':(d.status==='timeout'?'timeout':'closed')); |
| 353 | $('resTag').innerHTML=open?'<span class="tag ok">reachable</span>' |
| 354 | :'<span class="tag bad">'+esc(d.status||'closed')+'</span>'; |
| 355 | var pairs=[['target',d.host+':'+d.port],['resolved to',d.address], |
| 356 | ['avg',(d.avg_ms==null?'-':d.avg_ms+' ms')], |
| 357 | ['min',(d.min_ms==null?'-':d.min_ms+' ms')], |
| 358 | ['max',(d.max_ms==null?'-':d.max_ms+' ms')], |
| 359 | ['samples',(d.samples||[]).map(function(s){return s==null?'timeout':s+' ms';}).join(', ')]]; |
| 360 | kv(pairs); |
| 361 | txt('resNote',open?'Three connects succeeded against the first resolved address.' |
| 362 | :'The connect did not complete inside the deadline.'); |
| 363 | drawSpark(); |
| 364 | pushHist({kind:'tcp',label:d.host+':'+d.port,ok:open, |
| 365 | detail:open?((d.avg_ms==null?'-':d.avg_ms+' ms avg')):d.status,at:clock()}); |
| 366 | } |
| 367 | |
| 368 | function showHttp(d,target){ |
| 369 | var ok=d.reachable===true&&d.status!=null&&d.status<400; |
| 370 | txt('resState',d.status==null?(d.reachable?'connected':'unreachable'):('HTTP '+d.status)); |
| 371 | $('resTag').innerHTML=d.status==null |
| 372 | ?(d.reachable?'<span class="tag warn">no status line</span>':'<span class="tag bad">unreachable</span>') |
| 373 | :(ok?'<span class="tag ok">reachable</span>':'<span class="tag bad">status '+esc(d.status)+'</span>'); |
| 374 | var pairs=[['url',d.url],['host',d.host+':'+d.port],['path',d.path], |
| 375 | ['connect',(d.connect_ms==null?'-':d.connect_ms+' ms')], |
| 376 | ['status',(d.status==null?'-':d.status+' '+(d.status_text||''))], |
| 377 | ['headers',d.header_lines],['body','0 bytes (HEAD)'], |
| 378 | ['tls',d.tls]]; |
| 379 | kv(pairs); |
| 380 | txt('resNote',d.note||''); |
| 381 | pushHist({kind:'http',label:d.url,ok:ok,detail:(d.status==null?'no status':String(d.status)),at:clock()}); |
| 382 | } |
| 383 | |
| 384 | function clock(){ |
| 385 | var d=new Date(); |
| 386 | return d.toLocaleTimeString([],{hour:'2-digit',minute:'2-digit',second:'2-digit'}); |
| 387 | } |
| 388 | |
| 389 | function run(){ |
| 390 | var v=$('inp').value.trim(); |
| 391 | if(!v){txt('resState','enter a target');return;} |
| 392 | txt('resState','running...'); |
| 393 | $('resKv').innerHTML=''; |
| 394 | $('resNote').textContent=''; |
| 395 | $('goBtn').disabled=true; |
| 396 | var p=''; |
| 397 | var call; |
| 398 | if(TAB==='dns'){call=api('dns','&host='+encodeURIComponent(v));} |
| 399 | else if(TAB==='tcp'){ |
| 400 | var pt=$('port').value.trim(); |
| 401 | if(!pt){$('goBtn').disabled=false;txt('resState','port required');return;} |
| 402 | call=api('tcp','&host='+encodeURIComponent(v)+'&port='+encodeURIComponent(pt)); |
| 403 | } else { |
| 404 | var uv=v; |
| 405 | if(!/^https?:\/\//i.test(uv))uv='http://'+uv; |
| 406 | call=api('http','&url='+encodeURIComponent(uv)); |
| 407 | } |
| 408 | call.then(function(d){ |
| 409 | $('goBtn').disabled=false; |
| 410 | if(!d||d.ok!==true){ |
| 411 | txt('resState','error'); |
| 412 | kv([['error',(d&&d.error)||'request failed']]); |
| 413 | pushHist({kind:TAB,label:v,ok:false,detail:(d&&d.error)||'error',at:clock()}); |
| 414 | return; |
| 415 | } |
| 416 | if(TAB==='dns')showDns(d,v); |
| 417 | else if(TAB==='tcp')showTcp(d,v); |
| 418 | else showHttp(d,v); |
| 419 | }).catch(function(e){ |
| 420 | $('goBtn').disabled=false; |
| 421 | txt('resState','error'); |
| 422 | kv([['error',String(e.message||e)]]); |
| 423 | pushHist({kind:TAB,label:v,ok:false,detail:String(e.message||e),at:clock()}); |
| 424 | }); |
| 425 | } |
| 426 | |
| 427 | /* ---------- theme toggle (standalone only) ---------- */ |
| 428 | function toggleTheme(){ |
| 429 | var cur=(document.documentElement.getAttribute('data-theme')||'dark')==='light'?'dark':'light'; |
| 430 | document.documentElement.setAttribute('data-theme',cur); |
| 431 | try{localStorage.setItem('bot_theme',cur);}catch(e){} |
| 432 | try{window.dispatchEvent(new CustomEvent('bot-theme',{detail:cur}));}catch(e){} |
| 433 | } |
| 434 | |
| 435 | $('goBtn').addEventListener('click',run); |
| 436 | $('inp').addEventListener('keydown',function(e){if(e.key==='Enter'){e.preventDefault();run();}}); |
| 437 | $('rfBtn').addEventListener('click',loadIfaces); |
| 438 | $('thBtn').addEventListener('click',toggleTheme); |
| 439 | $('tabDns').addEventListener('click',function(){setTab('dns');}); |
| 440 | $('tabTcp').addEventListener('click',function(){setTab('tcp');}); |
| 441 | $('tabHttp').addEventListener('click',function(){setTab('http');}); |
| 442 | |
| 443 | setTab('dns'); |
| 444 | loadIfaces(); |
| 445 | })(); |
| 446 | </script></body></html> |