status.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 dataCount = 300;
  360. let timestamps = [];
  361. for(var i = 0; i < dataCount; i++){
  362. timestamps.push(parseInt(Date.now() / 1000) + i);
  363. }
  364. function fetchData() {
  365. $.ajax({
  366. url: '/api/stats/netstatgraph?array=true',
  367. success: function(data){
  368. if (rxValues.length == 0){
  369. rxValues = JSON.parse(JSON.stringify(data.Rx));
  370. }else{
  371. rxValues.push(data.Rx[dataCount-1]);
  372. rxValues.shift();
  373. }
  374. if (txValues.length == 0){
  375. txValues = JSON.parse(JSON.stringify(data.Tx));
  376. }else{
  377. txValues.push(data.Tx[dataCount-1]);
  378. txValues.shift();
  379. }
  380. timestamps.push(parseInt(Date.now() / 1000));
  381. timestamps.shift();
  382. updateChart();
  383. }
  384. })
  385. }
  386. function formatBandwidth(bps) {
  387. const KBPS = 1000;
  388. const MBPS = 1000 * KBPS;
  389. const GBPS = 1000 * MBPS;
  390. if (bps >= GBPS) {
  391. return (bps / GBPS).toFixed(2) + " Gbps";
  392. } else if (bps >= MBPS) {
  393. return (bps / MBPS).toFixed(2) + " Mbps";
  394. } else if (bps >= KBPS) {
  395. return (bps / KBPS).toFixed(2) + " Kbps";
  396. } else {
  397. return bps.toFixed(2) + " bps";
  398. }
  399. }
  400. var networkStatisticChart;
  401. function initChart(){
  402. $.get("/api/stats/netstat", function(data){
  403. networkStatisticChart = new Chart(
  404. document.getElementById('networkActivity'),
  405. {
  406. type: 'line',
  407. responsive: true,
  408. resizeDelay: 100,
  409. options: {
  410. animation: false,
  411. maintainAspectRatio: false,
  412. tooltips: {enabled: false},
  413. hover: {mode: null},
  414. //stepped: 'middle',
  415. plugins: {
  416. legend: {
  417. display: true,
  418. position: "right",
  419. },
  420. title: {
  421. display: false,
  422. text: 'Network Statistic'
  423. },
  424. },
  425. scales: {
  426. x: {
  427. display: false,
  428. },
  429. y: {
  430. display: true,
  431. scaleLabel: {
  432. display: true,
  433. labelString: 'Value'
  434. },
  435. ticks: {
  436. stepSize: 10000000,
  437. callback: function(label, index, labels) {
  438. return formatBandwidth(parseInt(label));
  439. }
  440. },
  441. gridLines: {
  442. display: true
  443. }
  444. }
  445. }
  446. },
  447. data: {
  448. labels: timestamps,
  449. datasets: [
  450. {
  451. label: 'Inbound',
  452. data: rxValues,
  453. borderColor: "#4d9dd9",
  454. borderWidth: 2,
  455. backgroundColor: 'rgba(77, 157, 217, 0.2)',
  456. fill: true,
  457. pointStyle: false,
  458. },
  459. {
  460. label: 'Outbound',
  461. data: txValues,
  462. borderColor: '#ffe32b',
  463. borderWidth: 2,
  464. backgroundColor: 'rgba(255, 227, 43, 0.2)',
  465. fill: true,
  466. pointStyle: false,
  467. }
  468. ]
  469. }
  470. }
  471. );
  472. });
  473. }
  474. function updateChart() {
  475. //networkStatisticChart.data.datasets[0].data = rxValues;
  476. //networkStatisticChart.data.datasets[1].data = txValues;
  477. if (networkStatisticChart != undefined){
  478. networkStatisticChart.update();
  479. }
  480. }
  481. function updateChartSize(){
  482. let newSize = $("#networkActWrapper").width() - 300;
  483. if (window.innerWidth > 750){
  484. newSize = window.innerWidth - $(".toolbar").width() - 500;
  485. }else{
  486. newSize = $("#networkActWrapper").width() - 500;
  487. }
  488. if (networkStatisticChart != undefined){
  489. networkStatisticChart.resize(newSize, 200);
  490. }
  491. }
  492. function handleChartAccumulateResize(){
  493. $("#networkActivity").hide();
  494. $("#networkActivityPlaceHolder").show();
  495. if (chartResizeTimeout != undefined){
  496. clearTimeout(chartResizeTimeout);
  497. }
  498. chartResizeTimeout = setTimeout(function(){
  499. chartResizeTimeout = undefined;
  500. $("#networkActivityPlaceHolder").hide();
  501. $("#networkActivity").show();
  502. updateChartSize();
  503. }, 300);
  504. }
  505. var chartResizeTimeout;
  506. window.addEventListener('resize', () => {
  507. handleChartAccumulateResize();
  508. });
  509. //Bind event to tab switch
  510. tabSwitchEventBind["status"] = function(){
  511. //On switch over to this page, resize the chart
  512. $("#networkActivityPlaceHolder").hide();
  513. $("#networkActivity").show().delay(100, function(){
  514. updateChartSize();
  515. });
  516. }
  517. window.addEventListener("focus", function(event){
  518. handleChartAccumulateResize();
  519. });
  520. document.addEventListener("visibilitychange", () => {
  521. // it could be either hidden or visible
  522. //handleChartAccumulateResize();
  523. });
  524. //Initialize chart data
  525. initChart();
  526. fetchData();
  527. setInterval(fetchData, 1000);
  528. </script>