rproot.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
  91. currentDefaultSiteOption = 0;
  92. }else if (selectedDefaultSite == "proxy"){
  93. $("#defaultSiteProxyOptions").show();
  94. $("#rootReqTLS").parent().removeClass("disabled");
  95. $("#proxyRoot").parent().removeClass("disabled");
  96. $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
  97. currentDefaultSiteOption = 1;
  98. }else if (selectedDefaultSite == "redirect"){
  99. $("#defaultSiteRedirectOptions").show();
  100. currentDefaultSiteOption = 2;
  101. }else if (selectedDefaultSite == "notfound"){
  102. currentDefaultSiteOption = 3;
  103. }else{
  104. //Unknown option
  105. return;
  106. }
  107. }
  108. //Bind events to the radio boxes
  109. function bindDefaultSiteRadioCheckboxEvents(){
  110. $('input[type=radio][name=defaultsiteOption]').off("change").on("change", function() {
  111. updateAvaibleDefaultSiteOptions();
  112. });
  113. }
  114. function initRootInfo(callback=undefined){
  115. $.get("/api/proxy/list?type=root", function(data){
  116. if (data == null){
  117. }else{
  118. var $radios = $('input:radio[name=defaultsiteOption]');
  119. let proxyType = data.DefaultSiteOption;
  120. //See typedef.go for enum conversion
  121. if (proxyType == 0){
  122. $radios.filter('[value=webserver]').prop('checked', true);
  123. }else if (proxyType == 1){
  124. $radios.filter('[value=proxy]').prop('checked', true);
  125. $("#proxyRoot").val(data.DefaultSiteValue);
  126. }else if (proxyType == 2){
  127. $radios.filter('[value=redirect]').prop('checked', true);
  128. $("#redirectDomain").val(data.DefaultSiteValue);
  129. }else if (proxyType == 3){
  130. $radios.filter('[value=notfound]').prop('checked', true);
  131. }
  132. updateAvaibleDefaultSiteOptions();
  133. $("#proxyRoot").val(data.Domain);
  134. checkRootRequireTLS(data.Domain);
  135. }
  136. if (callback != undefined){
  137. callback();
  138. }
  139. });
  140. }
  141. initRootInfo(function(){
  142. bindDefaultSiteRadioCheckboxEvents();
  143. });
  144. function isUsingStaticWebServerAsRoot(callback){
  145. let currentProxyRoot = $("#proxyRoot").val().trim();
  146. $.get("/api/webserv/status", function(webservStatus){
  147. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  148. return callback(true);
  149. }
  150. return callback(false);
  151. });
  152. }
  153. //Bind event to tab switch
  154. tabSwitchEventBind["setroot"] = function(){
  155. }
  156. //Check if the given domain will redirect to https
  157. function checkRootRequireTLS(targetDomain){
  158. //Trim off the http or https from the origin
  159. if (targetDomain.startsWith("http://")){
  160. targetDomain = targetDomain.substring(7);
  161. $("#proxyRoot").val(targetDomain);
  162. }else if (targetDomain.startsWith("https://")){
  163. targetDomain = targetDomain.substring(8);
  164. $("#proxyRoot").val(targetDomain);
  165. }
  166. $.ajax({
  167. url: "/api/proxy/tlscheck",
  168. data: {url: targetDomain},
  169. success: function(data){
  170. if (data.error != undefined){
  171. }else if (data == "https"){
  172. $("#rootReqTLS").parent().checkbox("set checked");
  173. }else if (data == "http"){
  174. $("#rootReqTLS").parent().checkbox("set unchecked");
  175. }
  176. }
  177. })
  178. }
  179. //Set the new proxy root option
  180. function setProxyRoot(btn=undefined){
  181. var newpr = $("#proxyRoot").val();
  182. if (newpr.trim() == "" && currentDefaultSiteOption == 0){
  183. //Fill in the web server info
  184. newpr = "127.0.0.1:" + $("#webserv_listenPort").val();
  185. $("#proxyRoot").val(newpr);
  186. }
  187. var rootReqTls = $("#rootReqTLS")[0].checked;
  188. if (btn != undefined){
  189. $(btn).addClass("disabled");
  190. }
  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>