status.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <script src="script/chart.js"></script>
  2. <div class="ui stackable grid">
  3. <div class="ten wide column serverstatusWrapper">
  4. <div id="serverstatus" class="ui statustab inverted segment">
  5. <h1 class="ui header" style="margin-top: 1em; margin-left: 0.4em; padding-bottom: 1em;">
  6. <i id="rpStatusIcon" class="loading spinner icon"></i>
  7. <div class="content">
  8. <span id="statusTitle">Loading</span>
  9. <div class="sub header" id="statusText">Checking server status</div>
  10. </div>
  11. </h1>
  12. <div class="dot-container">
  13. <div class="dot"></div>
  14. <div class="dot"></div>
  15. <div class="dot"></div>
  16. <div class="dot"></div>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="six wide column statisticWrapper">
  21. <div class="ui greybackground statustab segment">
  22. <h5 class="ui header">
  23. <i class="exchange icon"></i>
  24. <div class="content">
  25. <span id="summaryTotalCount"></span> <small>Req. Today</small>
  26. <div class="sub header" style="margin-top: 0.4em;">
  27. <i class="green circle check icon"></i> <span id="summarySuccCount"></span>
  28. / <i class="red red exclamation circle icon"></i> <span id="summaryErrCount"></span>
  29. </div>
  30. </div>
  31. </h5>
  32. <div class="ui divider"></div>
  33. <h5 class="ui header">
  34. <i class="arrows alternate horizontal icon"></i>
  35. <div class="content">
  36. <span id="forwardtype"></span>
  37. <div class="sub header" id="forwardtypeList">
  38. </div>
  39. </div>
  40. </h5>
  41. <div class="ui divider"></div>
  42. <h5 class="ui header">
  43. <i class="map marker alternate icon"></i>
  44. <div class="content">
  45. <span id="country"></span>
  46. <div class="sub header" id="countryList">
  47. </div>
  48. </div>
  49. </h5>
  50. </div>
  51. </div>
  52. </div>
  53. <div id="networkActWrapper" class="standardContainer" style="position: relative; margin-top: 1em;">
  54. <canvas id="networkActivity"></canvas>
  55. </div>
  56. <div id="networkActivityPlaceHolder">
  57. <p style="opacity: 0.5;"><i class="ui pause icon"></i> Graph Render Paused</p>
  58. </div>
  59. <br>
  60. <div class="standardContainer">
  61. <h4>Basic Settings</h4>
  62. <p>Inbound Port (Port to be proxied)</p>
  63. <div class="ui action fluid notloopbackOnly input">
  64. <input type="text" id="incomingPort" placeholder="Incoming Port" value="80">
  65. <button class="ui basic green notloopbackOnly button" onclick="handlePortChange();">Apply</button>
  66. </div>
  67. <br>
  68. <div id="tls" class="ui toggle notloopbackOnly checkbox">
  69. <input type="checkbox">
  70. <label>Use TLS to serve proxy request</label>
  71. </div>
  72. <br>
  73. <div id="redirect" class="ui toggle notloopbackOnly checkbox" style="margin-top: 0.6em;">
  74. <input type="checkbox">
  75. <label>Force redirect HTTP request to HTTPS<br>
  76. <small>(Only apply when listening port is not 80)</small></label>
  77. </div>
  78. <br><br>
  79. <button id="startbtn" class="ui teal button" onclick="startService();">Start Service</button>
  80. <button id="stopbtn" class="ui red notloopbackOnly disabled button" onclick="stopService();">Stop Service</button>
  81. <div id="rploopbackWarning" class="ui segment" style="display:none;">
  82. <b><i class="yellow warning icon"></i> Loopback Routing Warning</b><br>
  83. <small>This management interface is a loopback proxied service. <br>If you want to shutdown the reverse proxy server, please remove the proxy rule for the management interface and refresh.</small>
  84. </div>
  85. <div class="ui divider"></div>
  86. <div class="">
  87. <h4>Statistic Overview</h4>
  88. <div class="ui two column stackable grid">
  89. <div class="column">
  90. <p>Visitor Counts</p>
  91. <table class="ui unstackable inverted celled table">
  92. <thead>
  93. <tr>
  94. <th>Country ISO Code</th>
  95. <th>Unique Visitors</th>
  96. </tr>
  97. </thead>
  98. <tbody id="countryCodetable">
  99. <tr>
  100. <td colspan="2">No Data</td>
  101. </tr>
  102. </tbody>
  103. </table>
  104. </div>
  105. <div class="column">
  106. <p>Proxy Request Types</p>
  107. <table class="ui unstackable inverted celled table">
  108. <thead>
  109. <tr>
  110. <th>Proxy Type</th>
  111. <th>Count</th>
  112. </tr>
  113. </thead>
  114. <tbody id="forwardTypeTable">
  115. <tr>
  116. <td colspan="2">No Data</td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. </div>
  121. </div>
  122. </div>
  123. <br>
  124. <button class="ui right floated basic button" onclick="getDailySummaryDetails();"><i class="green refresh icon"></i> Refresh</button>
  125. <br><br>
  126. </div>
  127. <script>
  128. let loopbackProxiedInterface = false;
  129. //Initial the start stop button if this is reverse proxied
  130. $.get("/api/proxy/requestIsProxied", function(data){
  131. if (data == true){
  132. //This management interface is reverse proxied by itself
  133. //do not allow turning off the proxy
  134. $(".notloopbackOnly").addClass("disabled");
  135. loopbackProxiedInterface = true;
  136. $("#rploopbackWarning").show();
  137. }
  138. });
  139. //Get the latest server status from proxy server
  140. function initRPStaste(){
  141. $.get("/api/proxy/status", function(data){
  142. if (data.Running == true){
  143. $("#startbtn").addClass("disabled");
  144. if (!loopbackProxiedInterface){
  145. $("#stopbtn").removeClass("disabled");
  146. }
  147. $("#serverstatus").addClass("green");
  148. $("#statusTitle").text("Online");
  149. $("#rpStatusIcon").attr("class", "green circle check icon");
  150. $("#statusText").text("Serving request on port: " + data.Option.Port);
  151. }else{
  152. $("#startbtn").removeClass("disabled");
  153. $("#stopbtn").addClass("disabled");
  154. $("#statusTitle").text("Offline");
  155. $("#rpStatusIcon").attr("class", "black circle times icon")
  156. $("#statusText").text("Reverse proxy server is offline");
  157. $("#serverstatus").removeClass("green");
  158. }
  159. $("#incomingPort").val(data.Option.Port);
  160. });
  161. }
  162. function abbreviateNumber(value) {
  163. var newValue = value;
  164. var suffixes = ["", "k", "m", "b", "t"];
  165. var suffixNum = 0;
  166. while (newValue >= 1000 && suffixNum < suffixes.length - 1) {
  167. newValue /= 1000;
  168. suffixNum++;
  169. }
  170. if (value > 1000){
  171. newValue = newValue.toFixed(2);
  172. }
  173. return newValue + suffixes[suffixNum];
  174. }
  175. function getDailySummaryDetails(){
  176. function sortObjectByValue(obj) {
  177. // Convert object to array of [key, value] pairs
  178. const entries = Object.entries(obj);
  179. // Sort array based on value of each pair
  180. entries.sort((a, b) => {
  181. return b[1] - a[1];
  182. });
  183. // Convert sorted array back to object
  184. const sortedObj = {};
  185. for (const [key, value] of entries) {
  186. sortedObj[key] = value;
  187. }
  188. return sortedObj;
  189. }
  190. $.get("/api/stats/countries", function(data){
  191. data = sortObjectByValue(data);
  192. $("#country").html((Object.keys(data)[0])?Object.keys(data)[0]:"No Data");
  193. $("#countryList").html(`
  194. <div>
  195. ${(Object.keys(data)[1])?Object.keys(data)[1]:"-"}<br>
  196. ${(Object.keys(data)[2])?Object.keys(data)[2]:"-"}
  197. </div>
  198. `);
  199. //populate the table
  200. $("#countryCodetable").html("");
  201. for (const [key, value] of Object.entries(data)) {
  202. $("#countryCodetable").append(`<tr>
  203. <td>${key}</td>
  204. <td>${value}</td>
  205. </tr>`);
  206. }
  207. if (Object.keys(data).length == 0){
  208. $("#countryCodetable").append(`<tr>
  209. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  210. </tr>`);
  211. }
  212. });
  213. //Filter forward type
  214. function fft(ft){
  215. if (ft.indexOf("-") >= 0){
  216. ft = ft.replace("-", " (");
  217. ft = ft + ")";
  218. }
  219. ft = ft.charAt(0).toUpperCase() + ft.slice(1);
  220. return ft;
  221. }
  222. $.get("/api/stats/summary", function(data){
  223. data = sortObjectByValue(data.ForwardTypes);
  224. $("#forwardtype").html((Object.keys(data)[0])?fft(Object.keys(data)[0]) + ": " + abbreviateNumber(data[Object.keys(data)[0]]):"No Data");
  225. $("#forwardtypeList").html(`
  226. <div>
  227. ${(Object.keys(data)[1])?fft(Object.keys(data)[1]) + ": " + abbreviateNumber(data[Object.keys(data)[1]]):"-"}<br>
  228. ${(Object.keys(data)[2])?fft(Object.keys(data)[2]) + ": " + abbreviateNumber(data[Object.keys(data)[2]]):"-"}
  229. </div>
  230. `);
  231. $("#forwardTypeTable").html("");
  232. for (const [key, value] of Object.entries(data)) {
  233. $("#forwardTypeTable").append(`<tr>
  234. <td>${key}</td>
  235. <td>${value}</td>
  236. </tr>`);
  237. }
  238. if (Object.keys(data).length == 0){
  239. $("#forwardTypeTable").append(`<tr>
  240. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  241. </tr>`);
  242. }
  243. });
  244. }
  245. getDailySummaryDetails();
  246. function getDailySummary(){
  247. $.get("/api/stats/summary?fast=true", function(data){
  248. console.log(data);
  249. $("#summaryTotalCount").text(abbreviateNumber(data.TotalRequest));
  250. $("#summarySuccCount").text(abbreviateNumber(data.ValidRequest));
  251. $("#summaryErrCount").text(abbreviateNumber(data.ErrorRequest));
  252. });
  253. }
  254. setInterval(function(){
  255. getDailySummary();
  256. }, 10000);
  257. getDailySummary();
  258. //Start and stop service button
  259. function startService(){
  260. $.post("/api/proxy/enable", {enable: true}, function(data){
  261. if (data.error != undefined){
  262. msgbox(data.error, false, 5000);
  263. }
  264. initRPStaste();
  265. });
  266. }
  267. function stopService(){
  268. $.post("/api/proxy/enable", {enable: false}, function(data){
  269. if (data.error != undefined){
  270. msgbox(data.error, false, 5000);
  271. }
  272. initRPStaste();
  273. });
  274. }
  275. function handlePortChange(){
  276. var newPortValue = $("#incomingPort").val();
  277. if (isNaN(newPortValue - 1) || newPortValue < 1 || newPortValue > 65535){
  278. msgbox("Invalid incoming port value", false, 5000);
  279. return;
  280. }
  281. $.post("/api/proxy/setIncoming", {incoming: newPortValue}, function(data){
  282. if (data.error != undefined){
  283. msgbox(data.error, false, 5000);
  284. }
  285. msgbox("Setting Updated");
  286. initRPStaste();
  287. });
  288. }
  289. function initHTTPtoHTTPSRedirectSetting(){
  290. $.get("/api/proxy/useHttpsRedirect", function(data){
  291. if (data == true){
  292. $("#redirect").checkbox("set checked");
  293. }
  294. //Initiate the input listener on the checkbox
  295. $("#redirect").find("input").on("change", function(){
  296. let thisValue = $("#redirect").checkbox("is checked");
  297. $.ajax({
  298. url: "/api/proxy/useHttpsRedirect",
  299. data: {set: thisValue},
  300. success: function(data){
  301. if (data.error != undefined){
  302. alert(data.error);
  303. }else{
  304. //Updated
  305. msgbox("Setting Updated");
  306. initRPStaste();
  307. }
  308. }
  309. })
  310. });
  311. });
  312. }
  313. initHTTPtoHTTPSRedirectSetting();
  314. function initTlsSetting(){
  315. $.get("/api/cert/tls", function(data){
  316. if (data == true){
  317. $("#tls").checkbox("set checked");
  318. }else{
  319. $("#redirect").addClass('disabled');
  320. }
  321. //Initiate the input listener on the checkbox
  322. $("#tls").find("input").on("change", function(){
  323. let thisValue = $("#tls").checkbox("is checked");
  324. if (thisValue){
  325. $("#redirect").removeClass('disabled');
  326. }else{
  327. $("#redirect").addClass('disabled');
  328. }
  329. $.ajax({
  330. url: "/api/cert/tls",
  331. data: {set: thisValue},
  332. success: function(data){
  333. if (data.error != undefined){
  334. alert(data.error);
  335. }else{
  336. //Updated
  337. msgbox("Setting Updated");
  338. initRPStaste();
  339. }
  340. }
  341. })
  342. });
  343. })
  344. }
  345. initTlsSetting();
  346. </script>
  347. <script>
  348. /*
  349. Render Network Activity Graph
  350. */
  351. /*
  352. Setup Graph
  353. */
  354. let rxValues = [];
  355. let txValues = [];
  356. let lastRx = 0;
  357. let lastTx = 0;
  358. let timestamps = [];
  359. let netstatRecordTokeep = 60;
  360. function fetchData() {
  361. fetch('/api/stats/netstat')
  362. .then(response => response.json())
  363. .then(data => {
  364. let rx = data.RX;
  365. let tx = data.TX;
  366. // Calculate change from previous values
  367. if (lastRx == 0 && lastTx == 0){
  368. //inital value
  369. lastRx = rx;
  370. lastTx = tx;
  371. return;
  372. }
  373. let deltaRx = rx - lastRx;
  374. let deltaTx = tx - lastTx;
  375. if (rxValues.length < netstatRecordTokeep){
  376. //pad init into the array
  377. for(var i = 0;i<netstatRecordTokeep;i++){
  378. rxValues.push(deltaRx);
  379. txValues.push(deltaTx);
  380. timestamps.push(Date.now() - 1000 * (netstatRecordTokeep - i))
  381. }
  382. }
  383. // Add change to accumulated values
  384. rxValues.push(deltaRx);
  385. txValues.push(deltaTx);
  386. timestamps.push(Date.now());
  387. // Only keep last netstatRecordTokeep data points
  388. if (rxValues.length > netstatRecordTokeep) {
  389. rxValues.shift();
  390. txValues.shift();
  391. timestamps.shift();
  392. }
  393. lastRx = rx;
  394. lastTx = tx;
  395. updateChart();
  396. })
  397. .catch(error => {
  398. console.error('Failed to fetch data', error);
  399. });
  400. }
  401. function formatBandwidth(bps) {
  402. const KBPS = 1000;
  403. const MBPS = 1000 * KBPS;
  404. const GBPS = 1000 * MBPS;
  405. if (bps >= GBPS) {
  406. return (bps / GBPS).toFixed(2) + " Gbps";
  407. } else if (bps >= MBPS) {
  408. return (bps / MBPS).toFixed(2) + " Mbps";
  409. } else if (bps >= KBPS) {
  410. return (bps / KBPS).toFixed(2) + " Kbps";
  411. } else {
  412. return bps.toFixed(2) + " bps";
  413. }
  414. }
  415. var networkStatisticChart;
  416. function initChart(){
  417. $.get("/api/stats/netstat", function(data){
  418. networkStatisticChart = new Chart(
  419. document.getElementById('networkActivity'),
  420. {
  421. type: 'line',
  422. responsive: true,
  423. resizeDelay: 100,
  424. options: {
  425. animation: false,
  426. maintainAspectRatio: false,
  427. tooltips: {enabled: false},
  428. hover: {mode: null},
  429. stepped: 'middle',
  430. plugins: {
  431. legend: {
  432. display: true,
  433. position: "right",
  434. },
  435. title: {
  436. display: false,
  437. text: 'Network Statistic'
  438. },
  439. },
  440. scales: {
  441. x: {
  442. display: false,
  443. },
  444. y: {
  445. display: true,
  446. scaleLabel: {
  447. display: true,
  448. labelString: 'Value'
  449. },
  450. ticks: {
  451. stepSize: 10000000,
  452. callback: function(label, index, labels) {
  453. return formatBandwidth(parseInt(label));
  454. }
  455. },
  456. gridLines: {
  457. display: true
  458. }
  459. }
  460. }
  461. },
  462. data: {
  463. labels: timestamps,
  464. datasets: [
  465. {
  466. label: 'Inbound',
  467. data: rxValues,
  468. borderColor: "#4d9dd9",
  469. backgroundColor: 'rgba(77, 157, 217, 0.2)',
  470. fill: true,
  471. pointStyle: false,
  472. },
  473. {
  474. label: 'Outbound',
  475. data: txValues,
  476. borderColor: '#3a9460',
  477. backgroundColor: 'rgba(58, 148, 96, 0.2)',
  478. fill: true,
  479. pointStyle: false,
  480. }
  481. ]
  482. }
  483. }
  484. );
  485. });
  486. }
  487. function updateChart() {
  488. //networkStatisticChart.data.datasets[0].data = rxValues;
  489. //networkStatisticChart.data.datasets[1].data = txValues;
  490. networkStatisticChart.update();
  491. }
  492. function updateChartSize(){
  493. let newSize = $("#networkActWrapper").width() - 300;
  494. if (window.innerWidth > 750){
  495. newSize = window.innerWidth - $(".toolbar").width() - 500;
  496. }else{
  497. newSize = $("#networkActWrapper").width() - 500;
  498. }
  499. networkStatisticChart.resize(newSize, 200);
  500. }
  501. var chartResizeTimeout;
  502. window.addEventListener('resize', () => {
  503. $("#networkActivity").hide();
  504. $("#networkActivityPlaceHolder").show();
  505. if (chartResizeTimeout != undefined){
  506. clearTimeout(chartResizeTimeout);
  507. }
  508. chartResizeTimeout = setTimeout(function(){
  509. chartResizeTimeout = undefined;
  510. $("#networkActivityPlaceHolder").hide();
  511. $("#networkActivity").show();
  512. updateChartSize();
  513. }, 300);
  514. });
  515. //Bind event to tab switch
  516. tabSwitchEventBind["status"] = function(){
  517. //On switch over to this page, resize the chart
  518. $("#networkActivity").hide();
  519. $("#networkActivityPlaceHolder").show();
  520. if (chartResizeTimeout != undefined){
  521. clearTimeout(chartResizeTimeout);
  522. }
  523. chartResizeTimeout = setTimeout(function(){
  524. chartResizeTimeout = undefined;
  525. $("#networkActivityPlaceHolder").hide();
  526. $("#networkActivity").show();
  527. updateChartSize();
  528. }, 300);
  529. }
  530. //Initialize chart data
  531. initChart();
  532. fetchData();
  533. setInterval(fetchData, 1000);
  534. </script>