status.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. return;
  274. }
  275. msgbox("Setting Updated");
  276. initRPStaste();
  277. });
  278. }
  279. function initHTTPtoHTTPSRedirectSetting(){
  280. $.get("/api/proxy/useHttpsRedirect", function(data){
  281. if (data == true){
  282. $("#redirect").checkbox("set checked");
  283. }
  284. //Initiate the input listener on the checkbox
  285. $("#redirect").find("input").on("change", function(){
  286. let thisValue = $("#redirect").checkbox("is checked");
  287. $.ajax({
  288. url: "/api/proxy/useHttpsRedirect",
  289. data: {set: thisValue},
  290. success: function(data){
  291. if (data.error != undefined){
  292. alert(data.error);
  293. }else{
  294. //Updated
  295. msgbox("Setting Updated");
  296. initRPStaste();
  297. }
  298. }
  299. })
  300. });
  301. });
  302. }
  303. initHTTPtoHTTPSRedirectSetting();
  304. function initTlsSetting(){
  305. $.get("/api/cert/tls", function(data){
  306. if (data == true){
  307. $("#tls").checkbox("set checked");
  308. }else{
  309. $("#redirect").addClass('disabled');
  310. }
  311. //Initiate the input listener on the checkbox
  312. $("#tls").find("input").on("change", function(){
  313. let thisValue = $("#tls").checkbox("is checked");
  314. if (thisValue){
  315. $("#redirect").removeClass('disabled');
  316. }else{
  317. $("#redirect").addClass('disabled');
  318. }
  319. $.ajax({
  320. url: "/api/cert/tls",
  321. data: {set: thisValue},
  322. success: function(data){
  323. if (data.error != undefined){
  324. alert(data.error);
  325. }else{
  326. //Updated
  327. msgbox("Setting Updated");
  328. initRPStaste();
  329. }
  330. }
  331. })
  332. });
  333. })
  334. }
  335. initTlsSetting();
  336. </script>
  337. <script>
  338. /*
  339. Render Network Activity Graph
  340. */
  341. /*
  342. Setup Graph
  343. */
  344. let rxValues = [];
  345. let txValues = [];
  346. let dataCount = 300;
  347. let timestamps = [];
  348. for(var i = 0; i < dataCount; i++){
  349. timestamps.push(parseInt(Date.now() / 1000) + i);
  350. }
  351. function fetchData() {
  352. $.ajax({
  353. url: '/api/stats/netstatgraph?array=true',
  354. success: function(data){
  355. if (rxValues.length == 0){
  356. rxValues = JSON.parse(JSON.stringify(data.Rx));
  357. }else{
  358. rxValues.push(data.Rx[dataCount-1]);
  359. rxValues.shift();
  360. }
  361. if (txValues.length == 0){
  362. txValues = JSON.parse(JSON.stringify(data.Tx));
  363. }else{
  364. txValues.push(data.Tx[dataCount-1]);
  365. txValues.shift();
  366. }
  367. timestamps.push(parseInt(Date.now() / 1000));
  368. timestamps.shift();
  369. updateChart();
  370. }
  371. })
  372. }
  373. function formatBandwidth(bps) {
  374. const KBPS = 1000;
  375. const MBPS = 1000 * KBPS;
  376. const GBPS = 1000 * MBPS;
  377. if (bps >= GBPS) {
  378. return (bps / GBPS).toFixed(2) + " Gbps";
  379. } else if (bps >= MBPS) {
  380. return (bps / MBPS).toFixed(2) + " Mbps";
  381. } else if (bps >= KBPS) {
  382. return (bps / KBPS).toFixed(2) + " Kbps";
  383. } else {
  384. return bps.toFixed(2) + " bps";
  385. }
  386. }
  387. var networkStatisticChart;
  388. function initChart(){
  389. $.get("/api/stats/netstat", function(data){
  390. networkStatisticChart = new Chart(
  391. document.getElementById('networkActivity'),
  392. {
  393. type: 'line',
  394. responsive: true,
  395. resizeDelay: 100,
  396. options: {
  397. animation: false,
  398. maintainAspectRatio: false,
  399. tooltips: {enabled: false},
  400. hover: {mode: null},
  401. //stepped: 'middle',
  402. plugins: {
  403. legend: {
  404. display: true,
  405. position: "right",
  406. },
  407. title: {
  408. display: false,
  409. text: 'Network Statistic'
  410. },
  411. },
  412. scales: {
  413. x: {
  414. display: false,
  415. },
  416. y: {
  417. display: true,
  418. scaleLabel: {
  419. display: true,
  420. labelString: 'Value'
  421. },
  422. ticks: {
  423. stepSize: 10000000,
  424. callback: function(label, index, labels) {
  425. return formatBandwidth(parseInt(label));
  426. }
  427. },
  428. gridLines: {
  429. display: true
  430. }
  431. }
  432. }
  433. },
  434. data: {
  435. labels: timestamps,
  436. datasets: [
  437. {
  438. label: 'Inbound',
  439. data: rxValues,
  440. borderColor: "#4d9dd9",
  441. borderWidth: 2,
  442. backgroundColor: 'rgba(77, 157, 217, 0.2)',
  443. fill: true,
  444. pointStyle: false,
  445. },
  446. {
  447. label: 'Outbound',
  448. data: txValues,
  449. borderColor: '#ffe32b',
  450. borderWidth: 2,
  451. backgroundColor: 'rgba(255, 227, 43, 0.2)',
  452. fill: true,
  453. pointStyle: false,
  454. }
  455. ]
  456. }
  457. }
  458. );
  459. });
  460. }
  461. function updateChart() {
  462. //networkStatisticChart.data.datasets[0].data = rxValues;
  463. //networkStatisticChart.data.datasets[1].data = txValues;
  464. if (networkStatisticChart != undefined){
  465. networkStatisticChart.update();
  466. }
  467. }
  468. function updateChartSize(){
  469. let newSize = $("#networkActWrapper").width() - 300;
  470. if (window.innerWidth > 750){
  471. newSize = window.innerWidth - $(".toolbar").width() - 500;
  472. }else{
  473. newSize = $("#networkActWrapper").width() - 500;
  474. }
  475. if (networkStatisticChart != undefined){
  476. networkStatisticChart.resize(newSize, 200);
  477. }
  478. }
  479. function handleChartAccumulateResize(){
  480. $("#networkActivity").hide();
  481. $("#networkActivityPlaceHolder").show();
  482. if (chartResizeTimeout != undefined){
  483. clearTimeout(chartResizeTimeout);
  484. }
  485. chartResizeTimeout = setTimeout(function(){
  486. chartResizeTimeout = undefined;
  487. $("#networkActivityPlaceHolder").hide();
  488. $("#networkActivity").show();
  489. updateChartSize();
  490. }, 300);
  491. }
  492. var chartResizeTimeout;
  493. window.addEventListener('resize', () => {
  494. handleChartAccumulateResize();
  495. });
  496. //Bind event to tab switch
  497. tabSwitchEventBind["status"] = function(){
  498. //On switch over to this page, resize the chart
  499. $("#networkActivityPlaceHolder").hide();
  500. $("#networkActivity").show().delay(100, function(){
  501. updateChartSize();
  502. });
  503. }
  504. window.addEventListener("focus", function(event){
  505. handleChartAccumulateResize();
  506. });
  507. document.addEventListener("visibilitychange", () => {
  508. // it could be either hidden or visible
  509. //handleChartAccumulateResize();
  510. });
  511. //Initialize chart data
  512. initChart();
  513. fetchData();
  514. setInterval(fetchData, 1000);
  515. </script>