status.html 26 KB

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