1(function() { 2 3/* 4 * We display untrusted stuff in html context... reject anything 5 * that has HTML stuff in it 6 */ 7 8function san(s) 9{ 10 if (s.search("<") !== -1) 11 return "invalid string"; 12 13 return s; 14} 15 16function humanize(s) 17{ 18 var i = parseInt(s, 10); 19 20 if (i >= (1024 * 1024 * 1024)) 21 return (i / (1024 * 1024 * 1024)).toFixed(3) + "Gi"; 22 23 if (i >= (1024 * 1024)) 24 return (i / (1024 * 1024)).toFixed(3) + "Mi"; 25 26 if (i > 1024) 27 return (i / 1024).toFixed(3) + "Ki"; 28 29 return s; 30} 31 32function get_appropriate_ws_url() 33{ 34 var pcol; 35 var u = document.URL; 36 37 /* 38 * We open the websocket encrypted if this page came on an 39 * https:// url itself, otherwise unencrypted 40 */ 41 42 if (u.substring(0, 5) === "https") { 43 pcol = "wss://"; 44 u = u.substr(8); 45 } else { 46 pcol = "ws://"; 47 if (u.substring(0, 4) === "http") 48 u = u.substr(7); 49 } 50 51 u = u.split("/"); 52 53 /* + "/xxx" bit is for IE10 workaround */ 54 55 return pcol + u[0] + "/xxx"; 56} 57 58 59 var socket_status, jso, s; 60 61function ws_open_server_status() 62{ 63 socket_status = new WebSocket(get_appropriate_ws_url(), 64 "lws-server-status"); 65 66 try { 67 socket_status.onopen = function() { 68 document.getElementById("title").innerHTML = "Server Status (Active)"; 69 lws_gray_out(false); 70 }; 71 72 socket_status.onmessage =function got_packet(msg) { 73 var u, ci, n; 74 //document.getElementById("json").innerHTML = "<pre>"+msg.data+"</pre>"; 75 if (msg.data.length < 100) 76 return; 77 jso = JSON.parse(msg.data); 78 u = parseInt(san(jso.i.uptime), 10); 79 80 if (parseInt(jso.i.contexts[0].deprecated, 10) === 0) 81 s = "<table><tr><td></td><td class=\"c0\">"; 82 else 83 s = "<table><tr><td></td><td class=\"dc0\">"; 84 s += 85 "Server</td><td>" + 86 "<span class=\"sn\">Server Version:</span> <span class=\"v\">" + 87 san(jso.i.version) + "</span><br>" + 88 "<span class=\"sn\">Host Uptime:</span> <span class=\"v\">" + 89 ((u / (24 * 3600)) | 0) + "d " + 90 (((u % (24 * 3600)) / 3600) | 0) + "h " + 91 (((u % 3600) / 60) | 0) + "m</span>"; 92 if (jso.i.l1) 93 s = s + ", <span class=\"sn\">Host Load:</span> <span class=\"v\">" + san(jso.i.l1) + " "; 94 if (jso.i.l2) 95 s = s + san(jso.i.l2) + " "; 96 if (jso.i.l3) 97 s = s + san(jso.i.l3); 98 if (jso.i.l1) 99 s =s + "</span>"; 100 101 if (jso.i.statm) { 102 var sm = jso.i.statm.split(" "); 103 s += ", <span class=\"sn\">Virt stack + heap Usage:</span> <span class=\"v\">" + 104 humanize(parseInt(sm[5], 10) * 4096) + "B</span>"; 105 } 106 s += ", <span class=\"sn\">lws heap usage:</span> <span class=\"v\">" + 107 humanize(jso.i.heap) + "B</span>"; 108 109 110 for (n = 0; n < jso.files.length; n++) { 111 s += "<br><span class=n>" + san(jso.files[n].path) + ":</span><br> " + san(jso.files[n].val); 112 } 113 s += "</td></tr>"; 114 115 for (ci = 0; ci < jso.i.contexts.length; ci++) { 116 117 if (parseInt(jso.i.contexts[ci].deprecated, 10) === 0) 118 s += "<tr><td></td><td class=\"c\">" + 119 "Active Context</td><td>"; 120 else 121 s += "<tr><td></td><td class=\"c1\">" + 122 "Deprecated Context " + ci + "</td><td>"; 123 124 u = parseInt(san(jso.i.contexts[ci].context_uptime), 10); 125 s += "<span class=n>Server Uptime:</span> <span class=v>" + 126 ((u / (24 * 3600)) | 0) + "d " + 127 (((u % (24 * 3600)) / 3600) | 0) + "h " + 128 (((u % 3600) / 60) | 0) + "m</span>"; 129 130 s = s + 131 "<br>" + 132 "<span class=n>Listening wsi:</span> <span class=v>" + san(jso.i.contexts[ci].listen_wsi) + "</span>, " + 133 "<span class=n>Current wsi alive:</span> <span class=v>" + (parseInt(san(jso.i.contexts[ci].wsi_alive), 10) - 134 parseInt(san(jso.i.contexts[ci].listen_wsi), 10)) + "</span><br>" + 135 "<span class=n>Total Rx:</span> <span class=v>" + humanize(san(jso.i.contexts[ci].rx)) +"B</span>, " + 136 "<span class=n>Total Tx:</span> <span class=v>" + humanize(san(jso.i.contexts[ci].tx)) +"B</span><br>" + 137 138 "<span class=n>CONNECTIONS: HTTP/1.x:</span> <span class=v>" + san(jso.i.contexts[ci].h1_conn) +"</span>, " + 139 "<span class=n>Websocket:</span> <span class=v>" + san(jso.i.contexts[ci].ws_upg) +"</span>, " + 140 "<span class=n>H2 upgrade:</span> <span class=v>" + san(jso.i.contexts[ci].h2_upg) +"</span>, " + 141 "<span class=n>H2 ALPN:</span> <span class=v>" + san(jso.i.contexts[ci].h2_alpn) +"</span>, " + 142 "<span class=n>Rejected:</span> <span class=v>" + san(jso.i.contexts[ci].rejected) +"</span><br>" + 143 144 "<span class=n>TRANSACTIONS: HTTP/1.x:</span> <span class=v>" + san(jso.i.contexts[ci].h1_trans) + "</span>, " + 145 "<span class=n>H2:</span> <span class=v>" + san(jso.i.contexts[ci].h2_trans) +"</span>, " + 146 "<span class=n>Total H2 substreams:</span> <span class=v>" + san(jso.i.contexts[ci].h2_subs) +"</span><br>" + 147 148 "<span class=n>CGI: alive:</span> <span class=v>" + san(jso.i.contexts[ci].cgi_alive) + "</span>, " + 149 "<span class=n>spawned:</span> <span class=v>" + san(jso.i.contexts[ci].cgi_spawned) + 150 "</span><table>"; 151 152 for (n = 0; n < jso.i.contexts[ci].pt.length; n++) { 153 154 if (parseInt(jso.i.contexts[ci].deprecated, 10) === 0) 155 s += "<tr><td> </td><td class=\"l\">service thread " + (n + 1); 156 else 157 s += "<tr><td> </td><td class=\"dl\">service thread " + (n + 1); 158 s += "</td><td>" + 159 "<span class=n>fds:</span> <span class=v>" + san(jso.i.contexts[ci].pt[n].fds_count) + " / " + 160 san(jso.i.contexts[ci].pt_fd_max) + "</span>, "; 161 s = s + "<span class=n>ah pool:</span> <span class=v>" + san(jso.i.contexts[ci].pt[n].ah_pool_inuse) + " / " + 162 san(jso.i.contexts[ci].ah_pool_max) + "</span>, " + 163 "<span class=n>ah waiting list:</span> <span class=v>" + san(jso.i.contexts[ci].pt[n].ah_wait_list); 164 165 s = s + "</span></td></tr>"; 166 167 } 168 for (n = 0; n < jso.i.contexts[ci].vhosts.length; n++) { 169 if (parseInt(jso.i.contexts[ci].deprecated, 10) === 0) 170 s += "<tr><td> </td><td class=\"l\">vhost " + (n + 1); 171 else 172 s += "<tr><td> </td><td class=\"dl\">vhost " + (n + 1); 173 s += "</td><td><span class=\"mountname\">"; 174 if (jso.i.contexts[ci].vhosts[n].use_ssl === "1") 175 s = s + "https://"; 176 else 177 s = s + "http://"; 178 s = s + san(jso.i.contexts[ci].vhosts[n].name) + ":" + 179 san(jso.i.contexts[ci].vhosts[n].port) + "</span>"; 180 if (jso.i.contexts[ci].vhosts[n].sts === "1") 181 s = s + " (STS)"; 182 s = s +"<br>" + 183 184 "<span class=n>Total Rx:</span> <span class=v>" + humanize(san(jso.i.contexts[ci].vhosts[n].rx)) +"B</span>, " + 185 "<span class=n>Total Tx:</span> <span class=v>" + humanize(san(jso.i.contexts[ci].vhosts[n].tx)) +"B</span><br>" + 186 187 "<span class=n>CONNECTIONS: HTTP/1.x:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h1_conn) +"</span>, " + 188 "<span class=n>Websocket:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].ws_upg) +"</span>, " + 189 "<span class=n>H2 upgrade:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h2_upg) +"</span>, " + 190 "<span class=n>H2 ALPN:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h2_alpn) +"</span>, " + 191 "<span class=n>Rejected:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].rejected) +"</span><br>" + 192 193 "<span class=n>TRANSACTIONS: HTTP/1.x:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h1_trans) + "</span>, " + 194 "<span class=n>H2:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h2_trans) +"</span>, " + 195 "<span class=n>Total H2 substreams:</span> <span class=v>" + san(jso.i.contexts[ci].vhosts[n].h2_subs) +"</span><br>"; 196 197 if (jso.i.contexts[ci].vhosts[n].mounts) { 198 s = s + "<table><tr><td class=t>Mountpoint</td><td class=t>Origin</td><td class=t>Cache Policy</td></tr>"; 199 200 var m; 201 for (m = 0; m < jso.i.contexts[ci].vhosts[n].mounts.length; m++) { 202 s = s + "<tr><td>"; 203 s = s + "<span class=\"m1\">" + san(jso.i.contexts[ci].vhosts[n].mounts[m].mountpoint) + 204 "</span></td><td><span class=\"m2\">" + 205 san(jso.i.contexts[ci].vhosts[n].mounts[m].origin) + 206 "</span></td><td>"; 207 if (parseInt(san(jso.i.contexts[ci].vhosts[n].mounts[m].cache_max_age), 10)) 208 s = s + "<span class=n>max-age:</span> <span class=v>" + 209 san(jso.i.contexts[ci].vhosts[n].mounts[m].cache_max_age) + 210 "</span>, <span class=n>reuse:</span> <span class=v>" + 211 san(jso.i.contexts[ci].vhosts[n].mounts[m].cache_reuse) + 212 "</span>, <span class=n>reval:</span> <span class=v>" + 213 san(jso.i.contexts[ci].vhosts[n].mounts[m].cache_revalidate) + 214 "</span>, <span class=n>inter:</span> <span class=v>" + 215 san(jso.i.contexts[ci].vhosts[n].mounts[m].cache_intermediaries); 216 s = s + "</span></td></tr>"; 217 } 218 s = s + "</table>"; 219 } 220 s = s + "</td></tr>"; 221 } 222 223 s += "</table></td></tr>"; 224 225 } // context 226 s = s + "</table>"; 227 228 document.getElementById("conninfo").innerHTML = s; 229 }; 230 231 socket_status.onclose = function(){ 232 document.getElementById("title").innerHTML = "Server Status (Disconnected)"; 233 lws_gray_out(true,{"zindex":"499"}); 234 }; 235 } catch(exception) { 236 alert("<p>Error" + exception); 237 } 238} 239 240/* stuff that has to be delayed until all the page assets are loaded */ 241 242window.addEventListener("load", function() { 243 244 lws_gray_out(true,{"zindex":"499"}); 245 246 ws_open_server_status(); 247 248}, false); 249 250}()); 251 252