rproot.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Set Proxy Root</h2>
  4. <p>The default routing point for all incoming traffics. For all routing not found in the proxy rules, request will be redirected to the proxy root server.</p>
  5. <div class="ui form">
  6. <div class="field">
  7. <label>Proxy Root</label>
  8. <input type="text" id="proxyRoot" onchange="checkRootRequireTLS(this.value);">
  9. <small>E.g. localhost:8080</small>
  10. </div>
  11. <div class="field">
  12. <div class="ui checkbox">
  13. <input type="checkbox" id="rootReqTLS">
  14. <label>Root require TLS connection <br><small>Check this if your proxy root URL starts with https://</small></label>
  15. </div>
  16. </div>
  17. <div class="ui horizontal divider">OR</div>
  18. <div class="field">
  19. <div class="ui checkbox">
  20. <input type="checkbox" id="useStaticWebServer" onchange="handleUseStaticWebServerAsRoot()">
  21. <label>Use Static Web Server as Root <br><small>Check this if you prefer a more Apache Web Server like experience</small></label>
  22. </div>
  23. </div>
  24. <br>
  25. <button class="ui basic button" onclick="setProxyRoot()"><i class="teal home icon" ></i> Update Proxy Root</button>
  26. <div class="ui divider"></div>
  27. <div class="field">
  28. <h4>Root Routing Options</h4>
  29. </div>
  30. <div class="field">
  31. <div class="ui checkbox">
  32. <input type="checkbox" id="unsetRedirect">
  33. <label>Enable redirect for unset subdomains <br><small>Redirect subdomain that is not found to custom domain</small></label>
  34. </div>
  35. </div>
  36. <div class="ui basic segment" id="unsetRedirectDomainWrapper" style="background-color: #f7f7f7; border-radius: 1em; margin-left: 2em; padding-left: 2em; display:none;">
  37. <div style="
  38. position: absolute;
  39. top:0;
  40. left: 1em;
  41. width: 0px;
  42. height: 0px;
  43. margin-top: -10px;
  44. border-left: 10px solid transparent;
  45. border-right: 10px solid transparent;
  46. border-bottom: 10px solid #f7f7f7;">
  47. </div>
  48. <div class="field">
  49. <label>Redirect target domain</label>
  50. <div class="ui input">
  51. <input id="unsetRedirectDomain" type="text" placeholder="http://example.com">
  52. </div>
  53. <small>Unset subdomain will be redirected to the link above. Remember to include the protocol (e.g. http:// or https://)<br>
  54. Leave empty for redirecting to upper level domain (e.g. notfound.example.com <i class="right arrow icon"></i> example.com)</small>
  55. </div>
  56. </div>
  57. <br>
  58. <button class="ui basic button" onclick="updateRootOptions()"><i class="blue save icon" ></i> Save Root Options</button>
  59. </div>
  60. <br>
  61. </div>
  62. </div>
  63. <script>
  64. $("#advanceRootSettings").accordion();
  65. function handleUseStaticWebServerAsRoot(){
  66. let useStaticWebServer = $("#useStaticWebServer")[0].checked;
  67. if (useStaticWebServer){
  68. let staticWebServerURL = "127.0.0.1:" + $("#webserv_listenPort").val();
  69. $("#proxyRoot").val(staticWebServerURL);
  70. $("#proxyRoot").parent().addClass("disabled");
  71. $("#rootReqTLS").parent().checkbox("set unchecked");
  72. $("#rootReqTLS").parent().addClass("disabled");
  73. //Check if web server is enabled. If not, ask if the user want to enable it
  74. /*if (!$("#webserv_enable").parent().checkbox("is checked")){
  75. confirmBox("Enable static web server now?", function(choice){
  76. if (choice == true){
  77. $("#webserv_enable").parent().checkbox("set checked");
  78. }
  79. });
  80. }*/
  81. }else{
  82. $("#rootReqTLS").parent().removeClass("disabled");
  83. $("#proxyRoot").parent().removeClass("disabled");
  84. initRootInfo();
  85. }
  86. }
  87. function initRootInfo(callback=undefined){
  88. $.get("/api/proxy/list?type=root", function(data){
  89. if (data == null){
  90. }else{
  91. $("#proxyRoot").val(data.Domain);
  92. checkRootRequireTLS(data.Domain);
  93. }
  94. if (callback != undefined){
  95. callback();
  96. }
  97. });
  98. }
  99. initRootInfo(function(){
  100. updateWebServerLinkSettings();
  101. });
  102. //Update the current web server port settings
  103. function updateWebServerLinkSettings(){
  104. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  105. if (isUsingWebServ){
  106. $(".webservRootDisabled").addClass("disabled");
  107. $("#useStaticWebServer").parent().checkbox("set checked");
  108. }else{
  109. $(".webservRootDisabled").removeClass("disabled");
  110. $("#useStaticWebServer").parent().checkbox("set unchecked");
  111. }
  112. })
  113. }
  114. function isUsingStaticWebServerAsRoot(callback){
  115. let currentProxyRoot = $("#proxyRoot").val().trim();
  116. $.get("/api/webserv/status", function(webservStatus){
  117. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  118. return callback(true);
  119. }
  120. return callback(false);
  121. });
  122. }
  123. function updateRootSettingStates(){
  124. $.get("/api/cert/tls", function(data){
  125. if (data == true){
  126. $("#disableRootTLS").parent().removeClass('disabled').attr("title", "");
  127. }else{
  128. $("#disableRootTLS").parent().addClass('disabled').attr("title", "TLS listener is not enabled");
  129. }
  130. });
  131. }
  132. //Bind event to tab switch
  133. tabSwitchEventBind["setroot"] = function(){
  134. //On switch over to this page, update root info
  135. updateRootSettingStates();
  136. }
  137. //Toggle the display status of the input box for domain setting
  138. function updateRedirectionDomainSettingInputBox(useRedirect){
  139. if(useRedirect){
  140. $("#unsetRedirectDomainWrapper").stop().finish().slideDown("fast");
  141. }else{
  142. $("#unsetRedirectDomainWrapper").stop().finish().slideUp("fast");
  143. }
  144. }
  145. function checkCustomRedirectForUnsetSubd(){
  146. $.get("/api/proxy/root/listOptions", function(data){
  147. $("#unsetRedirect")[0].checked = data.EnableRedirectForUnsetRules || false;
  148. $("#unsetRedirectDomain").val(data.UnsetRuleRedirectTarget);
  149. updateRedirectionDomainSettingInputBox(data.EnableRedirectForUnsetRules);
  150. //Bind event to the checkbox
  151. $("#unsetRedirect").off("change").on("change", function(){
  152. let useRedirect = $("#unsetRedirect")[0].checked;
  153. updateRedirectionDomainSettingInputBox(useRedirect);
  154. });
  155. });
  156. }
  157. checkCustomRedirectForUnsetSubd();
  158. //Check if the given domain will redirect to https
  159. function checkRootRequireTLS(targetDomain){
  160. //Trim off the http or https from the origin
  161. if (targetDomain.startsWith("http://")){
  162. targetDomain = targetDomain.substring(7);
  163. $("#proxyRoot").val(targetDomain);
  164. }else if (targetDomain.startsWith("https://")){
  165. targetDomain = targetDomain.substring(8);
  166. $("#proxyRoot").val(targetDomain);
  167. }
  168. $.ajax({
  169. url: "/api/proxy/tlscheck",
  170. data: {url: targetDomain},
  171. success: function(data){
  172. if (data.error != undefined){
  173. }else if (data == "https"){
  174. $("#rootReqTLS").parent().checkbox("set checked");
  175. }else if (data == "http"){
  176. $("#rootReqTLS").parent().checkbox("set unchecked");
  177. }
  178. }
  179. })
  180. }
  181. //Set the new proxy root option
  182. function setProxyRoot(){
  183. var newpr = $("#proxyRoot").val();
  184. if (newpr.trim() == ""){
  185. $("#proxyRoot").parent().addClass('error');
  186. return
  187. }else{
  188. $("#proxyRoot").parent().removeClass('error');
  189. }
  190. var rootReqTls = $("#rootReqTLS")[0].checked;
  191. //Create the endpoint by calling add
  192. $.ajax({
  193. url: "/api/proxy/add",
  194. data: {"type": "root", tls: rootReqTls, ep: newpr},
  195. success: function(data){
  196. if (data.error != undefined){
  197. msgbox(data.error, false, 5000);
  198. }else{
  199. //OK
  200. initRootInfo(function(){
  201. //Check if WebServ is enabled
  202. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  203. if (isUsingWebServ){
  204. //Force enable static web server
  205. //See webserv.html for details
  206. setWebServerRunningState(true);
  207. }
  208. setTimeout(function(){
  209. //Update the checkbox
  210. updateWebServerLinkSettings();
  211. msgbox("Proxy Root Updated");
  212. }, 1000);
  213. })
  214. });
  215. }
  216. }
  217. });
  218. }
  219. function updateRootOptions(){
  220. $.ajax({
  221. type: "POST",
  222. url: "/api/proxy/root/updateOptions",
  223. data: {
  224. unsetRedirect: $("#unsetRedirect")[0].checked,
  225. unsetRedirectTarget: $("#unsetRedirectDomain").val().trim(),
  226. },
  227. success: function(data) {
  228. if (data.error != undefined){
  229. msgbox(data.error, false);
  230. }else{
  231. msgbox("Root Routing Options updated");
  232. }
  233. },
  234. error: function(error) {
  235. console.log("Error:", error);
  236. }
  237. });
  238. }
  239. </script>