1
0

status.html 25 KB

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