1
0

status.html 21 KB

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