rules.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <div class="ui stackable grid">
  2. <div class="ten wide column">
  3. <div class="standardContainer">
  4. <div class="ui basic segment" style="margin-top: 1em;">
  5. <h2>New Proxy Rule</h2>
  6. <p>You can create a proxy endpoing by subdomain or virtual directories</p>
  7. <div class="ui form">
  8. <div class="field">
  9. <label>Proxy Type</label>
  10. <div class="ui selection dropdown">
  11. <input type="hidden" id="ptype" value="subd">
  12. <i class="dropdown icon"></i>
  13. <div class="default text">Proxy Type</div>
  14. <div class="menu">
  15. <div class="item" data-value="subd">Sub-domain</div>
  16. <div class="item" data-value="vdir">Virtual Directory</div>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="field">
  21. <label>Subdomain Matching Keyword / Virtual Directory Name</label>
  22. <input type="text" id="rootname" placeholder="s1.mydomain.com">
  23. </div>
  24. <div class="field">
  25. <label>IP Address or Domain Name with port</label>
  26. <input type="text" id="proxyDomain" onchange="autoCheckTls(this.value);">
  27. <small>E.g. 192.168.0.101:8000 or example.com</small>
  28. </div>
  29. <div class="field">
  30. <div class="ui checkbox">
  31. <input type="checkbox" id="reqTls">
  32. <label>Proxy Target require TLS Connection <br><small>(i.e. Your proxy target starts with https://)</small></label>
  33. </div>
  34. </div>
  35. <!-- Advance configs -->
  36. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  37. <div id="advanceProxyRules" class="ui fluid accordion">
  38. <div class="title">
  39. <i class="dropdown icon"></i>
  40. Advance Settings
  41. </div>
  42. <div class="content">
  43. <p></p>
  44. <div class="field">
  45. <div class="ui checkbox">
  46. <input type="checkbox" id="skipTLSValidation">
  47. <label>Ignore TLS/SSL Verification Error<br><small>E.g. self-signed, expired certificate (Not Recommended)</small></label>
  48. </div>
  49. </div>
  50. <div class="field">
  51. <div class="ui checkbox">
  52. <input type="checkbox" id="requireBasicAuth">
  53. <label>Require Basic Auth<br><small>Require client to login in order to view the page</small></label>
  54. </div>
  55. </div>
  56. <div id="basicAuthCredentials" class="field">
  57. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  58. <table class="ui very basic celled table">
  59. <thead>
  60. <tr>
  61. <th>Username</th>
  62. <th>Password</th>
  63. <th>Remove</th>
  64. </tr></thead>
  65. <tbody id="basicAuthCredentialTable">
  66. <tr>
  67. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  68. </tr>
  69. </tbody>
  70. </table>
  71. <div class="three small fields credentialEntry">
  72. <div class="field">
  73. <input id="basicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  74. </div>
  75. <div class="field">
  76. <input id="basicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  77. </div>
  78. <div class="field">
  79. <button class="ui basic button" onclick="addCredentials();"><i class="blue add icon"></i> Add Credential</button>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. <br>
  87. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="blue add icon"></i> Create Endpoint</button>
  88. <br><br>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="six wide column">
  94. <div class="ui basic segment" style="height: 100%; background-color: var(--theme_grey); color: var(--theme_lgrey);">
  95. <br>
  96. <span style="font-size: 1.2em; font-weight: 300;">Subdomain</span><br>
  97. Example of subdomain matching keyword:<br>
  98. <code>s1.arozos.com</code> <br>(Any access starting with s1.arozos.com will be proxy to the IP address below)<br>
  99. <div class="ui divider"></div>
  100. <span style="font-size: 1.2em; font-weight: 300;">Virtual Directory</span><br>
  101. Example of virtual directory name: <br>
  102. <code>/s1/home/</code> <br>(Any access to {this_server}/s1/home/ will be proxy to the IP address below)<br>
  103. You can also ignore the tailing slash for wildcard like usage.<br>
  104. <code>/s1/room-</code> <br>Any access to {this_server}/s1/classroom_* will be proxied, for example: <br>
  105. <div class="ui list">
  106. <div class="item"><code>/s1/room-101</code></div>
  107. <div class="item"><code>/s1/room-102/</code></div>
  108. <div class="item"><code>/s1/room-103/map.txt</code></div>
  109. </div><br>
  110. <br>
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. <script>
  116. $("#advanceProxyRules").accordion();
  117. //New Proxy Endpoint
  118. function newProxyEndpoint(){
  119. var type = $("#ptype").val();
  120. var rootname = $("#rootname").val();
  121. var proxyDomain = $("#proxyDomain").val();
  122. var useTLS = $("#reqTls")[0].checked;
  123. var skipTLSValidation = $("#skipTLSValidation")[0].checked;
  124. var requireBasicAuth = $("#requireBasicAuth")[0].checked;
  125. if (type === "vdir") {
  126. if (!rootname.startsWith("/")) {
  127. rootname = "/" + rootname
  128. $("#rootname").val(rootname);
  129. }
  130. }else{
  131. if (!isSubdomainDomain(rootname)){
  132. //This doesn't seems like a subdomain
  133. if (!confirm(rootname + " does not looks like a subdomain. Continue anyway?")){
  134. return;
  135. }
  136. }
  137. }
  138. if (rootname.trim() == ""){
  139. $("#rootname").parent().addClass("error");
  140. return
  141. }else{
  142. $("#rootname").parent().removeClass("error");
  143. }
  144. if (proxyDomain.trim() == ""){
  145. $("#proxyDomain").parent().addClass("error");
  146. return
  147. }else{
  148. $("#proxyDomain").parent().removeClass("error");
  149. }
  150. //Create the endpoint by calling add
  151. $.ajax({
  152. url: "/api/proxy/add",
  153. data: {
  154. type: type,
  155. rootname: rootname,
  156. tls: useTLS,
  157. ep: proxyDomain,
  158. tlsval: skipTLSValidation,
  159. bauth: requireBasicAuth,
  160. cred: JSON.stringify(credentials),
  161. },
  162. success: function(data){
  163. if (data.error != undefined){
  164. msgbox(data.error, false, 5000);
  165. }else{
  166. //OK
  167. listVdirs();
  168. listSubd();
  169. msgbox("Proxy Endpoint Added");
  170. //Clear old data
  171. $("#rootname").val("");
  172. $("#proxyDomain").val("");
  173. credentials = [];
  174. updateTable();
  175. }
  176. }
  177. });
  178. }
  179. //Generic functions for delete rp endpoints
  180. function deleteEndpoint(ptype, epoint){
  181. if (confirm("Confirm remove proxy for :" + epoint + " (type: " + ptype + ")?")){
  182. $.ajax({
  183. url: "/api/proxy/del",
  184. data: {ep: epoint, ptype: ptype},
  185. success: function(){
  186. listVdirs();
  187. listSubd();
  188. }
  189. })
  190. }
  191. }
  192. function autoCheckTls(targetDomain){
  193. $.ajax({
  194. url: "/api/proxy/tlscheck",
  195. data: {url: targetDomain},
  196. success: function(data){
  197. if (data.error != undefined){
  198. }else if (data == "https"){
  199. $("#reqTls").parent().checkbox("set checked");
  200. }else if (data == "http"){
  201. $("#reqTls").parent().checkbox("set unchecked");
  202. }
  203. }
  204. })
  205. }
  206. function toggleBasicAuth() {
  207. var basicAuthDiv = document.getElementById('basicAuthOnly');
  208. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  209. $("#basicAuthCredentials").removeClass("disabled");
  210. } else {
  211. $("#basicAuthCredentials").addClass("disabled");
  212. }
  213. }
  214. $("#requireBasicAuth").on('change', toggleBasicAuth);
  215. toggleBasicAuth();
  216. /*
  217. Credential Managements
  218. */
  219. let credentials = []; // Global variable to store credentials
  220. function addCredentials() {
  221. // Retrieve the username and password input values
  222. var username = $('#basicAuthCredUsername').val();
  223. var password = $('#basicAuthCredPassword').val();
  224. if(username == "" || password == ""){
  225. msgbox("Username or password cannot be empty", false, 5000);
  226. return;
  227. }
  228. // Create a new credential object
  229. var credential = {
  230. username: username,
  231. password: password
  232. };
  233. // Add the credential to the global credentials array
  234. credentials.push(credential);
  235. // Clear the input fields
  236. $('#basicAuthCredUsername').val('');
  237. $('#basicAuthCredPassword').val('');
  238. // Update the table body with the credentials
  239. updateTable();
  240. }
  241. function updateTable() {
  242. var tableBody = $('#basicAuthCredentialTable');
  243. tableBody.empty();
  244. if (credentials.length === 0) {
  245. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  246. } else {
  247. for (var i = 0; i < credentials.length; i++) {
  248. var credential = credentials[i];
  249. var username = credential.username;
  250. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  251. var row = '<tr>' +
  252. '<td>' + username + '</td>' +
  253. '<td>' + password + '</td>' +
  254. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  255. '</tr>';
  256. tableBody.append(row);
  257. }
  258. }
  259. }
  260. function removeCredential(index) {
  261. // Remove the credential from the credentials array
  262. credentials.splice(index, 1);
  263. // Update the table body
  264. updateTable();
  265. }
  266. //Check if a string is a valid subdomain
  267. function isSubdomainDomain(str) {
  268. const regex = /^(localhost|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}\.)$/i;
  269. return regex.test(str);
  270. }
  271. /*
  272. Inline editor for subd.html and vdir.html
  273. */
  274. function editEndpoint(endpointType, uuid) {
  275. var row = $('tr[eptuuid="' + uuid + '"]');
  276. var columns = row.find('td[data-label]');
  277. var payload = $(row).attr("payload");
  278. payload = JSON.parse(decodeURIComponent(payload));
  279. //console.log(payload);
  280. columns.each(function(index) {
  281. var column = $(this);
  282. var oldValue = column.text().trim();
  283. if ($(this).attr("editable") == "false"){
  284. //This col do not allow edit. Skip
  285. return;
  286. }
  287. // Create an input element based on the column content
  288. var input;
  289. var datatype = $(this).attr("datatype");
  290. if (datatype == "domain"){
  291. let domain = payload.Domain;
  292. let tls = payload.RequireTLS;
  293. if (tls){
  294. tls = "checked";
  295. }else{
  296. tls = "";
  297. }
  298. input = `
  299. <div class="ui mini fluid input">
  300. <input type="text" class="Domain" value="${domain}">
  301. </div>
  302. <div class="ui checkbox" style="margin-top: 0.4em;">
  303. <input type="checkbox" class="RequireTLS" ${tls}>
  304. <label>Require TLS</label>
  305. </div>
  306. `;
  307. column.empty().append(input);
  308. }else if (datatype == "skipver"){
  309. let skipTLSValidation = payload.SkipCertValidations;
  310. let checkstate = "";
  311. if (skipTLSValidation){
  312. checkstate = "checked";
  313. }
  314. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  315. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  316. <label>Skip Verification</label>
  317. <small>Check this if you are using self signed certificates</small>
  318. </div>`);
  319. }else if (datatype == "basicauth"){
  320. let requireBasicAuth = payload.RequireBasicAuth;
  321. let checkstate = "";
  322. if (requireBasicAuth){
  323. checkstate = "checked";
  324. }
  325. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  326. <input type="checkbox" class="RequireBasicAuth" ${checkstate}>
  327. <label>Require Basic Auth</label>
  328. </div> <button class="ui basic tiny button" style="margin-left: 0.4em;" onclick="editBasicAuthCredentials('${endpointType}','${uuid}');"><i class="ui blue lock icon"></i> Edit Credentials</button>`);
  329. }else if (datatype == 'action'){
  330. column.empty().append(`
  331. <button title="Cancel" onclick="exitProxyInlineEdit('${endpointType}');" class="ui basic small circular icon button"><i class="ui remove icon"></i></button>
  332. <button title="Save" onclick="saveProxyInlineEdit('${uuid}');" class="ui basic small circular icon button"><i class="ui green save icon"></i></button>
  333. `);
  334. }else{
  335. //Unknown field. Leave it untouched
  336. }
  337. });
  338. $("#" + endpointType).find(".editBtn").addClass("disabled");
  339. }
  340. function exitProxyInlineEdit(){
  341. listSubd();
  342. listVdirs();
  343. $("#" + endpointType).find(".editBtn").removeClass("disabled");
  344. }
  345. function saveProxyInlineEdit(uuid){
  346. var row = $('tr[eptuuid="' + uuid + '"]');
  347. if (row.length == 0){
  348. return;
  349. }
  350. var epttype = $(row).attr("class");
  351. if (epttype == "subdEntry"){
  352. epttype = "subd";
  353. }else if (epttype == "vdirEntry"){
  354. epttype = "vdir";
  355. }
  356. let newDomain = $(row).find(".Domain").val();
  357. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  358. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  359. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  360. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  361. $.ajax({
  362. url: "/api/proxy/edit",
  363. method: "POST",
  364. data: {
  365. "type": epttype,
  366. "rootname": uuid,
  367. "ep":newDomain,
  368. "tls" :requireTLS,
  369. "tlsval": skipCertValidations,
  370. "bauth" :requireBasicAuth,
  371. },
  372. success: function(data){
  373. if (data.error !== undefined){
  374. msgbox(data.error, false, 6000);
  375. }else{
  376. msgbox("Proxy endpoint updated");
  377. if (epttype == "subd"){
  378. listSubd();
  379. }else if (epttype == "vdir"){
  380. listVdirs();
  381. }
  382. }
  383. }
  384. })
  385. }
  386. function editBasicAuthCredentials(endpointType, uuid){
  387. let payload = encodeURIComponent(JSON.stringify({
  388. ept: endpointType,
  389. ep: uuid
  390. }));
  391. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  392. }
  393. </script>