status.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 tlsEnabledOnly 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. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  78. <div class="ui accordion advanceSettings">
  79. <div class="title">
  80. <i class="dropdown icon"></i>
  81. Advance Settings
  82. </div>
  83. <div class="content">
  84. <p>If you have no idea what are these, you can leave them as default :)</p>
  85. <div id="tlsMinVer" class="ui toggle notloopbackOnly tlsEnabledOnly checkbox" style="margin-top: 0.6em;">
  86. <input type="checkbox">
  87. <label>Force TLS v1.2 or above<br>
  88. <small>(Enhance security, but not compatible with legacy browsers)</small></label>
  89. </div>
  90. <br>
  91. </div>
  92. </div>
  93. </div>
  94. <br><br>
  95. <button id="startbtn" class="ui teal button" onclick="startService();">Start Service</button>
  96. <button id="stopbtn" class="ui red notloopbackOnly disabled button" onclick="stopService();">Stop Service</button>
  97. <div id="rploopbackWarning" class="ui segment" style="display:none;">
  98. <b><i class="yellow warning icon"></i> Loopback Routing Warning</b><br>
  99. <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>
  100. </div>
  101. <div class="ui divider"></div>
  102. <div class="">
  103. <h4>Statistic Overview</h4>
  104. <div class="ui two column stackable grid">
  105. <div class="column">
  106. <p>Visitor Counts</p>
  107. <table class="ui unstackable inverted celled table">
  108. <thead>
  109. <tr>
  110. <th>Country ISO Code</th>
  111. <th>Unique Visitors</th>
  112. </tr>
  113. </thead>
  114. <tbody id="countryCodetable">
  115. <tr>
  116. <td colspan="2">No Data</td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. </div>
  121. <div class="column">
  122. <p>Proxy Request Types</p>
  123. <table class="ui unstackable inverted celled table">
  124. <thead>
  125. <tr>
  126. <th>Proxy Type</th>
  127. <th>Count</th>
  128. </tr>
  129. </thead>
  130. <tbody id="forwardTypeTable">
  131. <tr>
  132. <td colspan="2">No Data</td>
  133. </tr>
  134. </tbody>
  135. </table>
  136. </div>
  137. </div>
  138. </div>
  139. <br>
  140. <button class="ui right floated basic button" onclick="getDailySummaryDetails();"><i class="green refresh icon"></i> Refresh</button>
  141. <br><br>
  142. </div>
  143. <script>
  144. let loopbackProxiedInterface = false;
  145. $(".advanceSettings").accordion();
  146. //Initial the start stop button if this is reverse proxied
  147. $.get("/api/proxy/requestIsProxied", function(data){
  148. if (data == true){
  149. //This management interface is reverse proxied by itself
  150. //do not allow turning off the proxy
  151. $(".notloopbackOnly").addClass("disabled");
  152. loopbackProxiedInterface = true;
  153. $("#rploopbackWarning").show();
  154. }
  155. });
  156. //Get the latest server status from proxy server
  157. function initRPStaste(){
  158. $.get("/api/proxy/status", function(data){
  159. if (data.Running == true){
  160. $("#startbtn").addClass("disabled");
  161. if (!loopbackProxiedInterface){
  162. $("#stopbtn").removeClass("disabled");
  163. }
  164. $("#serverstatus").addClass("green");
  165. $("#statusTitle").text("Online");
  166. $("#rpStatusIcon").attr("class", "green circle check icon");
  167. $("#statusText").text("Serving request on port: " + data.Option.Port);
  168. }else{
  169. $("#startbtn").removeClass("disabled");
  170. $("#stopbtn").addClass("disabled");
  171. $("#statusTitle").text("Offline");
  172. $("#rpStatusIcon").attr("class", "black circle times icon")
  173. $("#statusText").text("Reverse proxy server is offline");
  174. $("#serverstatus").removeClass("green");
  175. }
  176. $("#incomingPort").val(data.Option.Port);
  177. });
  178. }
  179. function getDailySummaryDetails(){
  180. function sortObjectByValue(obj) {
  181. // Convert object to array of [key, value] pairs
  182. const entries = Object.entries(obj);
  183. // Sort array based on value of each pair
  184. entries.sort((a, b) => {
  185. return b[1] - a[1];
  186. });
  187. // Convert sorted array back to object
  188. const sortedObj = {};
  189. for (const [key, value] of entries) {
  190. sortedObj[key] = value;
  191. }
  192. return sortedObj;
  193. }
  194. $.get("/api/stats/countries", function(data){
  195. data = sortObjectByValue(data);
  196. $("#country").html((Object.keys(data)[0])?Object.keys(data)[0]:"No Data");
  197. $("#countryList").html(`
  198. <div>
  199. ${(Object.keys(data)[1])?Object.keys(data)[1]:"-"}<br>
  200. ${(Object.keys(data)[2])?Object.keys(data)[2]:"-"}
  201. </div>
  202. `);
  203. //populate the table
  204. $("#countryCodetable").html("");
  205. for (const [key, value] of Object.entries(data)) {
  206. var countryName = getCountryName(key);
  207. if (countryName == ""){
  208. countryName = "LAN"
  209. }
  210. $("#countryCodetable").append(`<tr>
  211. <td>${key} (${countryName})</td>
  212. <td>${value}</td>
  213. </tr>`);
  214. }
  215. if (Object.keys(data).length == 0){
  216. $("#countryCodetable").append(`<tr>
  217. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  218. </tr>`);
  219. }
  220. });
  221. //Filter forward type
  222. function fft(ft){
  223. if (ft.indexOf("-") >= 0){
  224. ft = ft.replace("-", " (");
  225. ft = ft + ")";
  226. }
  227. ft = ft.charAt(0).toUpperCase() + ft.slice(1);
  228. return ft;
  229. }
  230. $.get("/api/stats/summary", function(data){
  231. data = sortObjectByValue(data.ForwardTypes);
  232. $("#forwardtype").html((Object.keys(data)[0])?fft(Object.keys(data)[0]) + ": " + abbreviateNumber(data[Object.keys(data)[0]]):"No Data");
  233. $("#forwardtypeList").html(`
  234. <div>
  235. ${(Object.keys(data)[1])?fft(Object.keys(data)[1]) + ": " + abbreviateNumber(data[Object.keys(data)[1]]):"-"}<br>
  236. ${(Object.keys(data)[2])?fft(Object.keys(data)[2]) + ": " + abbreviateNumber(data[Object.keys(data)[2]]):"-"}
  237. </div>
  238. `);
  239. $("#forwardTypeTable").html("");
  240. for (const [key, value] of Object.entries(data)) {
  241. $("#forwardTypeTable").append(`<tr>
  242. <td>${key}</td>
  243. <td>${value}</td>
  244. </tr>`);
  245. }
  246. if (Object.keys(data).length == 0){
  247. $("#forwardTypeTable").append(`<tr>
  248. <td colspan="2"><i class="ui green circle check icon"></i> No Data</td>
  249. </tr>`);
  250. }
  251. });
  252. }
  253. getDailySummaryDetails();
  254. function getDailySummary(){
  255. $.get("/api/stats/summary?fast=true", function(data){
  256. $("#summaryTotalCount").text(abbreviateNumber(data.TotalRequest));
  257. $("#summarySuccCount").text(abbreviateNumber(data.ValidRequest));
  258. $("#summaryErrCount").text(abbreviateNumber(data.ErrorRequest));
  259. });
  260. }
  261. setInterval(function(){
  262. getDailySummary();
  263. }, 10000);
  264. getDailySummary();
  265. //Start and stop service button
  266. function startService(){
  267. $.post("/api/proxy/enable", {enable: true}, function(data){
  268. if (data.error != undefined){
  269. msgbox(data.error, false, 5000);
  270. }
  271. initRPStaste();
  272. });
  273. }
  274. function stopService(){
  275. $.post("/api/proxy/enable", {enable: false}, function(data){
  276. if (data.error != undefined){
  277. msgbox(data.error, false, 5000);
  278. }
  279. initRPStaste();
  280. });
  281. }
  282. function handlePortChange(){
  283. var newPortValue = $("#incomingPort").val();
  284. if (isNaN(newPortValue - 1) || newPortValue < 1 || newPortValue > 65535){
  285. msgbox("Invalid incoming port value", false, 5000);
  286. return;
  287. }
  288. $.post("/api/proxy/setIncoming", {incoming: newPortValue}, function(data){
  289. if (data.error != undefined){
  290. msgbox(data.error, false, 5000);
  291. return;
  292. }
  293. msgbox("Setting Updated");
  294. initRPStaste();
  295. });
  296. }
  297. function initHTTPtoHTTPSRedirectSetting(){
  298. $.get("/api/proxy/useHttpsRedirect", function(data){
  299. if (data == true){
  300. $("#redirect").checkbox("set checked");
  301. }
  302. //Initiate the input listener on the checkbox
  303. $("#redirect").find("input").on("change", function(){
  304. let thisValue = $("#redirect").checkbox("is checked");
  305. $.ajax({
  306. url: "/api/proxy/useHttpsRedirect",
  307. data: {set: thisValue},
  308. success: function(data){
  309. if (data.error != undefined){
  310. msgbox(data.error, false, 8000);
  311. //Restore backend value to make sure the UI is always in sync
  312. $.get("/api/proxy/useHttpsRedirect", function(data){
  313. if (data == true){
  314. $("#redirect").checkbox("set checked");
  315. }else{
  316. $("#redirect").checkbox("set unchecked");
  317. }
  318. });
  319. }else{
  320. //Updated
  321. msgbox("Setting Updated");
  322. initRPStaste();
  323. }
  324. }
  325. })
  326. });
  327. });
  328. }
  329. initHTTPtoHTTPSRedirectSetting();
  330. function initTlsVersionSetting(){
  331. $.get("/api/cert/tlsRequireLatest", function(data){
  332. if (data == true){
  333. $("#tlsMinVer").checkbox("set checked");
  334. }else{
  335. $("#tlsMinVer").checkbox("set unchecked");
  336. }
  337. //Bind events to the checkbox
  338. $("#tlsMinVer").find("input").on("change", function(){
  339. let thisValue = $("#tlsMinVer").checkbox("is checked");
  340. $.ajax({
  341. url: "/api/cert/tlsRequireLatest",
  342. data: {"set": thisValue},
  343. success: function(data){
  344. if (data.error != undefined){
  345. msgbox(data.error, false, 5000);
  346. }else{
  347. msgbox("TLS Version Setting Updated");
  348. }
  349. }
  350. })
  351. });
  352. });
  353. }
  354. initTlsVersionSetting();
  355. function initTlsSetting(){
  356. $.get("/api/cert/tls", function(data){
  357. if (data == true){
  358. $("#tls").checkbox("set checked");
  359. }else{
  360. $(".tlsEnabledOnly").addClass('disabled');
  361. $(".tlsEnabledOnly").addClass('disabled');
  362. }
  363. //Initiate the input listener on the checkbox
  364. $("#tls").find("input").on("change", function(){
  365. let thisValue = $("#tls").checkbox("is checked");
  366. if (thisValue){
  367. $(".tlsEnabledOnly").removeClass('disabled');
  368. }else{
  369. $(".tlsEnabledOnly").addClass('disabled');
  370. }
  371. $.ajax({
  372. url: "/api/cert/tls",
  373. data: {set: thisValue},
  374. success: function(data){
  375. if (data.error != undefined){
  376. alert(data.error);
  377. }else{
  378. //Updated
  379. //Check for case if the port is invalid default ports
  380. if ($("#incomingPort").val() == "80" && thisValue == true){
  381. confirmBox("Change listen port to :443?", function(choice){
  382. if (choice == true){
  383. $("#incomingPort").val("443");
  384. handlePortChange();
  385. }
  386. });
  387. }else if ($("#incomingPort").val() == "443" && thisValue == false){
  388. confirmBox("Change listen port to :80?", function(choice){
  389. if (choice == true){
  390. $("#incomingPort").val("80");
  391. handlePortChange();
  392. }
  393. });
  394. }else{
  395. msgbox("Setting Updated");
  396. }
  397. initRPStaste();
  398. }
  399. }
  400. })
  401. });
  402. })
  403. }
  404. initTlsSetting();
  405. </script>
  406. <script>
  407. /*
  408. Render Network Activity Graph
  409. */
  410. /*
  411. Setup Graph
  412. */
  413. let rxValues = [];
  414. let txValues = [];
  415. let dataCount = 300;
  416. let timestamps = [];
  417. for(var i = 0; i < dataCount; i++){
  418. timestamps.push(parseInt(Date.now() / 1000) + i);
  419. }
  420. function fetchData() {
  421. $.ajax({
  422. url: '/api/stats/netstatgraph?array=true',
  423. success: function(data){
  424. if (rxValues.length == 0){
  425. rxValues = JSON.parse(JSON.stringify(data.Rx));
  426. }else{
  427. rxValues.push(data.Rx[dataCount-1]);
  428. rxValues.shift();
  429. }
  430. if (txValues.length == 0){
  431. txValues = JSON.parse(JSON.stringify(data.Tx));
  432. }else{
  433. txValues.push(data.Tx[dataCount-1]);
  434. txValues.shift();
  435. }
  436. timestamps.push(parseInt(Date.now() / 1000));
  437. timestamps.shift();
  438. updateChart();
  439. }
  440. })
  441. }
  442. function formatBandwidth(bps) {
  443. const KBPS = 1000;
  444. const MBPS = 1000 * KBPS;
  445. const GBPS = 1000 * MBPS;
  446. if (bps >= GBPS) {
  447. return (bps / GBPS).toFixed(2) + " Gbps";
  448. } else if (bps >= MBPS) {
  449. return (bps / MBPS).toFixed(2) + " Mbps";
  450. } else if (bps >= KBPS) {
  451. return (bps / KBPS).toFixed(2) + " Kbps";
  452. } else {
  453. return bps.toFixed(2) + " bps";
  454. }
  455. }
  456. var networkStatisticChart;
  457. function initChart(){
  458. $.get("/api/stats/netstat", function(data){
  459. networkStatisticChart = new Chart(
  460. document.getElementById('networkActivity'),
  461. {
  462. type: 'line',
  463. responsive: true,
  464. resizeDelay: 100,
  465. options: {
  466. animation: false,
  467. maintainAspectRatio: false,
  468. tooltips: {enabled: false},
  469. hover: {mode: null},
  470. //stepped: 'middle',
  471. plugins: {
  472. legend: {
  473. display: true,
  474. position: "right",
  475. },
  476. title: {
  477. display: false,
  478. text: 'Network Statistic'
  479. },
  480. },
  481. scales: {
  482. x: {
  483. display: false,
  484. },
  485. y: {
  486. display: true,
  487. scaleLabel: {
  488. display: true,
  489. labelString: 'Value'
  490. },
  491. ticks: {
  492. stepSize: 10000000,
  493. callback: function(label, index, labels) {
  494. return formatBandwidth(parseInt(label));
  495. }
  496. },
  497. gridLines: {
  498. display: true
  499. }
  500. }
  501. }
  502. },
  503. data: {
  504. labels: timestamps,
  505. datasets: [
  506. {
  507. label: 'Inbound',
  508. data: rxValues,
  509. borderColor: "#4d9dd9",
  510. borderWidth: 2,
  511. backgroundColor: 'rgba(77, 157, 217, 0.2)',
  512. fill: true,
  513. pointStyle: false,
  514. },
  515. {
  516. label: 'Outbound',
  517. data: txValues,
  518. borderColor: '#ffe32b',
  519. borderWidth: 2,
  520. backgroundColor: 'rgba(255, 227, 43, 0.2)',
  521. fill: true,
  522. pointStyle: false,
  523. }
  524. ]
  525. }
  526. }
  527. );
  528. });
  529. }
  530. function updateChart() {
  531. //networkStatisticChart.data.datasets[0].data = rxValues;
  532. //networkStatisticChart.data.datasets[1].data = txValues;
  533. if (networkStatisticChart != undefined){
  534. networkStatisticChart.update();
  535. }
  536. }
  537. function updateChartSize(){
  538. let newSize = $("#networkActWrapper").width() - 300;
  539. if (window.innerWidth > 750){
  540. newSize = window.innerWidth - $(".toolbar").width() - 500;
  541. }else{
  542. newSize = $("#networkActWrapper").width() - 500;
  543. }
  544. if (networkStatisticChart != undefined){
  545. networkStatisticChart.resize(newSize, 200);
  546. }
  547. }
  548. function handleChartAccumulateResize(){
  549. $("#networkActivity").hide();
  550. $("#networkActivityPlaceHolder").show();
  551. if (chartResizeTimeout != undefined){
  552. clearTimeout(chartResizeTimeout);
  553. }
  554. chartResizeTimeout = setTimeout(function(){
  555. chartResizeTimeout = undefined;
  556. $("#networkActivityPlaceHolder").hide();
  557. $("#networkActivity").show();
  558. updateChartSize();
  559. }, 300);
  560. }
  561. var chartResizeTimeout;
  562. window.addEventListener('resize', () => {
  563. handleChartAccumulateResize();
  564. });
  565. //Bind event to tab switch
  566. tabSwitchEventBind["status"] = function(){
  567. //On switch over to this page, resize the chart
  568. handleChartAccumulateResize();
  569. }
  570. window.addEventListener("focus", function(event){
  571. handleChartAccumulateResize();
  572. });
  573. //Initialize chart data
  574. initChart();
  575. fetchData();
  576. setInterval(fetchData, 1000);
  577. </script>