rproot.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Default Site</h2>
  4. <p>Default routing options for inbound traffic (previously called Proxy Root)</p>
  5. <div class="ui form">
  6. <div class="grouped fields">
  7. <label>What to show when Zoraxy is hit with an unknown Host?</label>
  8. <div class="field">
  9. <div class="ui radio defaultsite checkbox">
  10. <input type="radio" name="defaultsiteOption" checked="checked" value="webserver">
  11. <label>Internal Static Web Server<br>
  12. <small>Check this if you prefer a more Apache / Nginx like experience</small>
  13. </label>
  14. </div>
  15. </div>
  16. <div class="field">
  17. <div class="ui radio defaultsite checkbox">
  18. <input type="radio" name="defaultsiteOption" value="proxy">
  19. <label>Reverse Proxy Target<br>
  20. <small>Proxy the request to a target IP / domain</small>
  21. </label>
  22. </div>
  23. </div>
  24. <div class="field">
  25. <div class="ui radio defaultsite checkbox">
  26. <input type="radio" name="defaultsiteOption" value="redirect">
  27. <label>Redirect<br>
  28. <small>Redirect the user to a new location</small>
  29. </label>
  30. </div>
  31. </div>
  32. <div class="field">
  33. <div class="ui radio defaultsite checkbox">
  34. <input type="radio" name="defaultsiteOption" value="notfound">
  35. <label>Show 404 NOT FOUND<br>
  36. <small>Respond to request with a 404 page</small>
  37. </label>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <!-- Reverse Proxy as Default Site Options -->
  43. <div id="defaultSiteProxyOptions" class="ui basic segment advanceoptions defaultSiteOptionDetails" style="display:none; ">
  44. <div class="ui form">
  45. <div class="field">
  46. <label>Reverse Proxy Target</label>
  47. <input type="text" id="proxyRoot" onchange="checkRootRequireTLS(this.value);">
  48. <small>e.g. localhost:8080 / 192.168.0.100:80 / example.com</small>
  49. </div>
  50. <div class="field">
  51. <div class="ui checkbox">
  52. <input type="checkbox" id="rootReqTLS">
  53. <label>Reverse proxy target require TLS connection <br><small>Check this if your proxy target URL require connection with https://</small></label>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. <!-- Redirect as default site Options-->
  59. <div id="defaultSiteRedirectOptions" class="ui basic segment advanceoptions defaultSiteOptionDetails" style="display:none;"">
  60. <div class="ui form">
  61. <div class="field">
  62. <label>Redirect target domain</label>
  63. <div class="ui input">
  64. <input id="redirectDomain" type="text" placeholder="http://example.com">
  65. </div>
  66. <small>Unset subdomain will be redirected to the link above. Remember to include the protocol (e.g. http:// or https://)</small>
  67. </div>
  68. </div>
  69. </div>
  70. <button class="ui basic button" onclick="setProxyRoot(this)"><i class="green checkmark icon" ></i> Apply Changes</button>
  71. <button class="ui basic button" onclick="initRootInfo()"><i class="refresh icon" ></i> Reset</button>
  72. <br>
  73. </div>
  74. </div>
  75. <script>
  76. var currentDefaultSiteOption = 0; //For enum see typedef.go
  77. $("#advanceRootSettings").accordion();
  78. //Handle toggle events of option radio boxes
  79. function updateAvaibleDefaultSiteOptions(){
  80. let selectedDefaultSite = $('input[name="defaultsiteOption"]:checked').val();
  81. $(".defaultSiteOptionDetails").hide();
  82. $("#useRootProxyRouterForVdir").parent().addClass("disabled");
  83. if (selectedDefaultSite == "webserver"){
  84. //Use build in web server as target
  85. let staticWebServerURL = "127.0.0.1:" + $("#webserv_listenPort").val();
  86. $("#proxyRoot").val(staticWebServerURL);
  87. $("#proxyRoot").parent().addClass("disabled");
  88. $("#rootReqTLS").parent().checkbox("set unchecked");
  89. $("#rootReqTLS").parent().addClass("disabled");
  90. currentDefaultSiteOption = 0;
  91. }else if (selectedDefaultSite == "proxy"){
  92. $("#defaultSiteProxyOptions").show();
  93. $("#rootReqTLS").parent().removeClass("disabled");
  94. $("#proxyRoot").parent().removeClass("disabled");
  95. $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
  96. currentDefaultSiteOption = 1;
  97. }else if (selectedDefaultSite == "redirect"){
  98. $("#defaultSiteRedirectOptions").show();
  99. currentDefaultSiteOption = 2;
  100. }else if (selectedDefaultSite == "notfound"){
  101. currentDefaultSiteOption = 3;
  102. }else{
  103. //Unknown option
  104. return;
  105. }
  106. }
  107. //Bind events to the radio boxes
  108. function bindDefaultSiteRadioCheckboxEvents(){
  109. $('input[type=radio][name=defaultsiteOption]').off("change").on("change", function() {
  110. updateAvaibleDefaultSiteOptions();
  111. });
  112. }
  113. function initRootInfo(callback=undefined){
  114. $.get("/api/proxy/list?type=root", function(data){
  115. if (data == null){
  116. }else{
  117. var $radios = $('input:radio[name=defaultsiteOption]');
  118. let proxyType = data.DefaultSiteOption;
  119. //See typedef.go for enum conversion
  120. if (proxyType == 0){
  121. $radios.filter('[value=webserver]').prop('checked', true);
  122. }else if (proxyType == 1){
  123. $radios.filter('[value=proxy]').prop('checked', true);
  124. $("#proxyRoot").val(data.DefaultSiteValue);
  125. }else if (proxyType == 2){
  126. $radios.filter('[value=redirect]').prop('checked', true);
  127. $("#redirectDomain").val(data.DefaultSiteValue);
  128. }else if (proxyType == 3){
  129. $radios.filter('[value=notfound]').prop('checked', true);
  130. }
  131. updateAvaibleDefaultSiteOptions();
  132. $("#proxyRoot").val(data.Domain);
  133. checkRootRequireTLS(data.Domain);
  134. }
  135. if (callback != undefined){
  136. callback();
  137. }
  138. });
  139. }
  140. initRootInfo(function(){
  141. bindDefaultSiteRadioCheckboxEvents();
  142. });
  143. function isUsingStaticWebServerAsRoot(callback){
  144. let currentProxyRoot = $("#proxyRoot").val().trim();
  145. $.get("/api/webserv/status", function(webservStatus){
  146. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  147. return callback(true);
  148. }
  149. return callback(false);
  150. });
  151. }
  152. //Bind event to tab switch
  153. tabSwitchEventBind["setroot"] = function(){
  154. }
  155. //Check if the given domain will redirect to https
  156. function checkRootRequireTLS(targetDomain){
  157. //Trim off the http or https from the origin
  158. if (targetDomain.startsWith("http://")){
  159. targetDomain = targetDomain.substring(7);
  160. $("#proxyRoot").val(targetDomain);
  161. }else if (targetDomain.startsWith("https://")){
  162. targetDomain = targetDomain.substring(8);
  163. $("#proxyRoot").val(targetDomain);
  164. }
  165. $.ajax({
  166. url: "/api/proxy/tlscheck",
  167. data: {url: targetDomain},
  168. success: function(data){
  169. if (data.error != undefined){
  170. }else if (data == "https"){
  171. $("#rootReqTLS").parent().checkbox("set checked");
  172. }else if (data == "http"){
  173. $("#rootReqTLS").parent().checkbox("set unchecked");
  174. }
  175. }
  176. })
  177. }
  178. //Set the new proxy root option
  179. function setProxyRoot(btn=undefined){
  180. if (btn != undefined){
  181. $(btn).addClass("disabled");
  182. }
  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. //proxy mode or redirect mode, check for input values
  192. var defaultSiteValue = "";
  193. if (currentDefaultSiteOption == 1){
  194. if ($("#proxyRoot").val().trim() == ""){
  195. $("#proxyRoot").parent().addClass("error");
  196. return;
  197. }
  198. defaultSiteValue = $("#proxyRoot").val().trim();
  199. $("#proxyRoot").parent().removeClass("error");
  200. }else if (currentDefaultSiteOption == 2){
  201. if ($("#redirectDomain").val().trim() == ""){
  202. $("#redirectDomain").parent().addClass("error");
  203. return;
  204. }
  205. defaultSiteValue = $("#redirectDomain").val().trim();
  206. $("#redirectDomain").parent().removeClass("error");
  207. }
  208. //Create the endpoint by calling add
  209. $.ajax({
  210. url: "/api/proxy/add",
  211. data: {
  212. "type": "root",
  213. "tls": rootReqTls,
  214. "ep": newpr,
  215. "defaultSiteOpt": currentDefaultSiteOption,
  216. "defaultSiteVal":defaultSiteValue,
  217. },
  218. method: "POST",
  219. success: function(data){
  220. if (data.error != undefined){
  221. msgbox(data.error, false, 5000);
  222. }else{
  223. //OK
  224. initRootInfo(function(){
  225. //Check if WebServ is enabled
  226. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  227. if (isUsingWebServ){
  228. //Force enable static web server
  229. //See webserv.html for details
  230. setWebServerRunningState(true);
  231. }
  232. setTimeout(function(){
  233. //Update the checkbox
  234. msgbox("Proxy Root Updated");
  235. }, 100);
  236. })
  237. });
  238. }
  239. if (btn != undefined){
  240. $(btn).removeClass("disabled");
  241. }
  242. }
  243. });
  244. }
  245. </script>