status.html 21 KB

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