status.html 20 KB

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