status.html 26 KB

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