status.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. var countryName = getCountryName(key);
  203. if (countryName == ""){
  204. countryName = "LAN"
  205. }
  206. $("#countryCodetable").append(`<tr>
  207. <td>${key} (${countryName})</td>
  208. <td>${value}</td>
  209. </tr>`);
  210. }
  211. if (Object.keys(data).length == 0){
  212. $("#countryCodetable").append(`<tr>
  213. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  214. </tr>`);
  215. }
  216. });
  217. //Filter forward type
  218. function fft(ft){
  219. if (ft.indexOf("-") >= 0){
  220. ft = ft.replace("-", " (");
  221. ft = ft + ")";
  222. }
  223. ft = ft.charAt(0).toUpperCase() + ft.slice(1);
  224. return ft;
  225. }
  226. $.get("/api/stats/summary", function(data){
  227. data = sortObjectByValue(data.ForwardTypes);
  228. $("#forwardtype").html((Object.keys(data)[0])?fft(Object.keys(data)[0]) + ": " + abbreviateNumber(data[Object.keys(data)[0]]):"No Data");
  229. $("#forwardtypeList").html(`
  230. <div>
  231. ${(Object.keys(data)[1])?fft(Object.keys(data)[1]) + ": " + abbreviateNumber(data[Object.keys(data)[1]]):"-"}<br>
  232. ${(Object.keys(data)[2])?fft(Object.keys(data)[2]) + ": " + abbreviateNumber(data[Object.keys(data)[2]]):"-"}
  233. </div>
  234. `);
  235. $("#forwardTypeTable").html("");
  236. for (const [key, value] of Object.entries(data)) {
  237. $("#forwardTypeTable").append(`<tr>
  238. <td>${key}</td>
  239. <td>${value}</td>
  240. </tr>`);
  241. }
  242. if (Object.keys(data).length == 0){
  243. $("#forwardTypeTable").append(`<tr>
  244. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  245. </tr>`);
  246. }
  247. });
  248. }
  249. getDailySummaryDetails();
  250. function getDailySummary(){
  251. $.get("/api/stats/summary?fast=true", function(data){
  252. $("#summaryTotalCount").text(abbreviateNumber(data.TotalRequest));
  253. $("#summarySuccCount").text(abbreviateNumber(data.ValidRequest));
  254. $("#summaryErrCount").text(abbreviateNumber(data.ErrorRequest));
  255. });
  256. }
  257. setInterval(function(){
  258. getDailySummary();
  259. }, 10000);
  260. getDailySummary();
  261. //Start and stop service button
  262. function startService(){
  263. $.post("/api/proxy/enable", {enable: true}, function(data){
  264. if (data.error != undefined){
  265. msgbox(data.error, false, 5000);
  266. }
  267. initRPStaste();
  268. });
  269. }
  270. function stopService(){
  271. $.post("/api/proxy/enable", {enable: false}, function(data){
  272. if (data.error != undefined){
  273. msgbox(data.error, false, 5000);
  274. }
  275. initRPStaste();
  276. });
  277. }
  278. function handlePortChange(){
  279. var newPortValue = $("#incomingPort").val();
  280. if (isNaN(newPortValue - 1) || newPortValue < 1 || newPortValue > 65535){
  281. msgbox("Invalid incoming port value", false, 5000);
  282. return;
  283. }
  284. $.post("/api/proxy/setIncoming", {incoming: newPortValue}, function(data){
  285. if (data.error != undefined){
  286. msgbox(data.error, false, 5000);
  287. }
  288. msgbox("Setting Updated");
  289. initRPStaste();
  290. });
  291. }
  292. function initHTTPtoHTTPSRedirectSetting(){
  293. $.get("/api/proxy/useHttpsRedirect", function(data){
  294. if (data == true){
  295. $("#redirect").checkbox("set checked");
  296. }
  297. //Initiate the input listener on the checkbox
  298. $("#redirect").find("input").on("change", function(){
  299. let thisValue = $("#redirect").checkbox("is checked");
  300. $.ajax({
  301. url: "/api/proxy/useHttpsRedirect",
  302. data: {set: thisValue},
  303. success: function(data){
  304. if (data.error != undefined){
  305. alert(data.error);
  306. }else{
  307. //Updated
  308. msgbox("Setting Updated");
  309. initRPStaste();
  310. }
  311. }
  312. })
  313. });
  314. });
  315. }
  316. initHTTPtoHTTPSRedirectSetting();
  317. function initTlsSetting(){
  318. $.get("/api/cert/tls", function(data){
  319. if (data == true){
  320. $("#tls").checkbox("set checked");
  321. }else{
  322. $("#redirect").addClass('disabled');
  323. }
  324. //Initiate the input listener on the checkbox
  325. $("#tls").find("input").on("change", function(){
  326. let thisValue = $("#tls").checkbox("is checked");
  327. if (thisValue){
  328. $("#redirect").removeClass('disabled');
  329. }else{
  330. $("#redirect").addClass('disabled');
  331. }
  332. $.ajax({
  333. url: "/api/cert/tls",
  334. data: {set: thisValue},
  335. success: function(data){
  336. if (data.error != undefined){
  337. alert(data.error);
  338. }else{
  339. //Updated
  340. msgbox("Setting Updated");
  341. initRPStaste();
  342. }
  343. }
  344. })
  345. });
  346. })
  347. }
  348. initTlsSetting();
  349. </script>
  350. <script>
  351. /*
  352. Render Network Activity Graph
  353. */
  354. /*
  355. Setup Graph
  356. */
  357. let rxValues = [];
  358. let txValues = [];
  359. let lastRx = 0;
  360. let lastTx = 0;
  361. let timestamps = [];
  362. let netstatRecordTokeep = 60;
  363. function fetchData() {
  364. fetch('/api/stats/netstat')
  365. .then(response => response.json())
  366. .then(data => {
  367. let rx = data.RX;
  368. let tx = data.TX;
  369. // Calculate change from previous values
  370. if (lastRx == 0 && lastTx == 0){
  371. //inital value
  372. lastRx = rx;
  373. lastTx = tx;
  374. return;
  375. }
  376. let deltaRx = rx - lastRx;
  377. let deltaTx = tx - lastTx;
  378. if (rxValues.length < netstatRecordTokeep){
  379. //pad init into the array
  380. for(var i = 0;i<netstatRecordTokeep;i++){
  381. rxValues.push(deltaRx);
  382. txValues.push(deltaTx);
  383. timestamps.push(Date.now() - 1000 * (netstatRecordTokeep - i))
  384. }
  385. }
  386. // Add change to accumulated values
  387. rxValues.push(deltaRx);
  388. txValues.push(deltaTx);
  389. timestamps.push(Date.now());
  390. // Only keep last netstatRecordTokeep data points
  391. if (rxValues.length > netstatRecordTokeep) {
  392. rxValues.shift();
  393. txValues.shift();
  394. timestamps.shift();
  395. }
  396. lastRx = rx;
  397. lastTx = tx;
  398. updateChart();
  399. })
  400. .catch(error => {
  401. console.error('Failed to fetch data', error);
  402. });
  403. }
  404. function formatBandwidth(bps) {
  405. const KBPS = 1000;
  406. const MBPS = 1000 * KBPS;
  407. const GBPS = 1000 * MBPS;
  408. if (bps >= GBPS) {
  409. return (bps / GBPS).toFixed(2) + " Gbps";
  410. } else if (bps >= MBPS) {
  411. return (bps / MBPS).toFixed(2) + " Mbps";
  412. } else if (bps >= KBPS) {
  413. return (bps / KBPS).toFixed(2) + " Kbps";
  414. } else {
  415. return bps.toFixed(2) + " bps";
  416. }
  417. }
  418. var networkStatisticChart;
  419. function initChart(){
  420. $.get("/api/stats/netstat", function(data){
  421. networkStatisticChart = new Chart(
  422. document.getElementById('networkActivity'),
  423. {
  424. type: 'line',
  425. responsive: true,
  426. resizeDelay: 100,
  427. options: {
  428. animation: false,
  429. maintainAspectRatio: false,
  430. tooltips: {enabled: false},
  431. hover: {mode: null},
  432. //stepped: 'middle',
  433. plugins: {
  434. legend: {
  435. display: true,
  436. position: "right",
  437. },
  438. title: {
  439. display: false,
  440. text: 'Network Statistic'
  441. },
  442. },
  443. scales: {
  444. x: {
  445. display: false,
  446. },
  447. y: {
  448. display: true,
  449. scaleLabel: {
  450. display: true,
  451. labelString: 'Value'
  452. },
  453. ticks: {
  454. stepSize: 10000000,
  455. callback: function(label, index, labels) {
  456. return formatBandwidth(parseInt(label));
  457. }
  458. },
  459. gridLines: {
  460. display: true
  461. }
  462. }
  463. }
  464. },
  465. data: {
  466. labels: timestamps,
  467. datasets: [
  468. {
  469. label: 'Inbound',
  470. data: rxValues,
  471. borderColor: "#4d9dd9",
  472. borderWidth: 2,
  473. backgroundColor: 'rgba(77, 157, 217, 0.2)',
  474. fill: true,
  475. pointStyle: false,
  476. },
  477. {
  478. label: 'Outbound',
  479. data: txValues,
  480. borderColor: '#ffe32b',
  481. borderWidth: 2,
  482. backgroundColor: 'rgba(255, 227, 43, 0.2)',
  483. fill: true,
  484. pointStyle: false,
  485. }
  486. ]
  487. }
  488. }
  489. );
  490. });
  491. }
  492. function updateChart() {
  493. //networkStatisticChart.data.datasets[0].data = rxValues;
  494. //networkStatisticChart.data.datasets[1].data = txValues;
  495. if (networkStatisticChart != undefined){
  496. networkStatisticChart.update();
  497. }
  498. }
  499. function updateChartSize(){
  500. let newSize = $("#networkActWrapper").width() - 300;
  501. if (window.innerWidth > 750){
  502. newSize = window.innerWidth - $(".toolbar").width() - 500;
  503. }else{
  504. newSize = $("#networkActWrapper").width() - 500;
  505. }
  506. if (networkStatisticChart != undefined){
  507. networkStatisticChart.resize(newSize, 200);
  508. }
  509. }
  510. function handleChartAccumulateResize(){
  511. $("#networkActivity").hide();
  512. $("#networkActivityPlaceHolder").show();
  513. if (chartResizeTimeout != undefined){
  514. clearTimeout(chartResizeTimeout);
  515. }
  516. chartResizeTimeout = setTimeout(function(){
  517. chartResizeTimeout = undefined;
  518. $("#networkActivityPlaceHolder").hide();
  519. $("#networkActivity").show();
  520. updateChartSize();
  521. }, 300);
  522. }
  523. var chartResizeTimeout;
  524. window.addEventListener('resize', () => {
  525. handleChartAccumulateResize();
  526. });
  527. //Bind event to tab switch
  528. tabSwitchEventBind["status"] = function(){
  529. //On switch over to this page, resize the chart
  530. $("#networkActivityPlaceHolder").hide();
  531. $("#networkActivity").show().delay(100, function(){
  532. updateChartSize();
  533. });
  534. }
  535. document.addEventListener("visibilitychange", () => {
  536. // it could be either hidden or visible
  537. //handleChartAccumulateResize();
  538. });
  539. //Initialize chart data
  540. initChart();
  541. fetchData();
  542. setInterval(fetchData, 1000);
  543. </script>