rproot.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. if (selectedDefaultSite == "webserver"){
  83. //Use build in web server as target
  84. let staticWebServerURL = "127.0.0.1:" + $("#webserv_listenPort").val();
  85. $("#proxyRoot").val(staticWebServerURL);
  86. $("#proxyRoot").parent().addClass("disabled");
  87. $("#rootReqTLS").parent().checkbox("set unchecked");
  88. $("#rootReqTLS").parent().addClass("disabled");
  89. currentDefaultSiteOption = 0;
  90. }else if (selectedDefaultSite == "proxy"){
  91. $("#defaultSiteProxyOptions").show();
  92. $("#rootReqTLS").parent().removeClass("disabled");
  93. $("#proxyRoot").parent().removeClass("disabled");
  94. currentDefaultSiteOption = 1;
  95. }else if (selectedDefaultSite == "redirect"){
  96. $("#defaultSiteRedirectOptions").show();
  97. currentDefaultSiteOption = 2;
  98. }else if (selectedDefaultSite == "notfound"){
  99. currentDefaultSiteOption = 3;
  100. }else{
  101. //Unknown option
  102. return;
  103. }
  104. }
  105. //Bind events to the radio boxes
  106. function bindDefaultSiteRadioCheckboxEvents(){
  107. $('input[type=radio][name=defaultsiteOption]').off("change").on("change", function() {
  108. updateAvaibleDefaultSiteOptions();
  109. });
  110. }
  111. function initRootInfo(callback=undefined){
  112. $.get("/api/proxy/list?type=root", function(data){
  113. if (data == null){
  114. }else{
  115. var $radios = $('input:radio[name=defaultsiteOption]');
  116. let proxyType = data.DefaultSiteOption;
  117. //See typedef.go for enum conversion
  118. if (proxyType == 0){
  119. $radios.filter('[value=webserver]').prop('checked', true);
  120. }else if (proxyType == 1){
  121. $radios.filter('[value=proxy]').prop('checked', true);
  122. $("#proxyRoot").val(data.DefaultSiteValue);
  123. }else if (proxyType == 2){
  124. $radios.filter('[value=redirect]').prop('checked', true);
  125. $("#redirectDomain").val(data.DefaultSiteValue);
  126. }else if (proxyType == 3){
  127. $radios.filter('[value=notfound]').prop('checked', true);
  128. }
  129. updateAvaibleDefaultSiteOptions();
  130. $("#proxyRoot").val(data.Domain);
  131. checkRootRequireTLS(data.Domain);
  132. }
  133. if (callback != undefined){
  134. callback();
  135. }
  136. });
  137. }
  138. initRootInfo(function(){
  139. bindDefaultSiteRadioCheckboxEvents();
  140. });
  141. function isUsingStaticWebServerAsRoot(callback){
  142. let currentProxyRoot = $("#proxyRoot").val().trim();
  143. $.get("/api/webserv/status", function(webservStatus){
  144. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  145. return callback(true);
  146. }
  147. return callback(false);
  148. });
  149. }
  150. //Bind event to tab switch
  151. tabSwitchEventBind["setroot"] = function(){
  152. }
  153. //Check if the given domain will redirect to https
  154. function checkRootRequireTLS(targetDomain){
  155. //Trim off the http or https from the origin
  156. if (targetDomain.startsWith("http://")){
  157. targetDomain = targetDomain.substring(7);
  158. $("#proxyRoot").val(targetDomain);
  159. }else if (targetDomain.startsWith("https://")){
  160. targetDomain = targetDomain.substring(8);
  161. $("#proxyRoot").val(targetDomain);
  162. }
  163. $.ajax({
  164. url: "/api/proxy/tlscheck",
  165. data: {url: targetDomain},
  166. success: function(data){
  167. if (data.error != undefined){
  168. }else if (data == "https"){
  169. $("#rootReqTLS").parent().checkbox("set checked");
  170. }else if (data == "http"){
  171. $("#rootReqTLS").parent().checkbox("set unchecked");
  172. }
  173. }
  174. })
  175. }
  176. //Set the new proxy root option
  177. function setProxyRoot(btn=undefined){
  178. if (btn != undefined){
  179. $(btn).addClass("disabled");
  180. }
  181. var newpr = $("#proxyRoot").val();
  182. if (newpr.trim() == ""){
  183. $("#proxyRoot").parent().addClass('error');
  184. return
  185. }else{
  186. $("#proxyRoot").parent().removeClass('error');
  187. }
  188. var rootReqTls = $("#rootReqTLS")[0].checked;
  189. //proxy mode or redirect mode, check for input values
  190. var defaultSiteValue = "";
  191. if (currentDefaultSiteOption == 1){
  192. if ($("#proxyRoot").val().trim() == ""){
  193. $("#proxyRoot").parent().addClass("error");
  194. return;
  195. }
  196. defaultSiteValue = $("#proxyRoot").val().trim();
  197. $("#proxyRoot").parent().removeClass("error");
  198. }else if (currentDefaultSiteOption == 2){
  199. if ($("#redirectDomain").val().trim() == ""){
  200. $("#redirectDomain").parent().addClass("error");
  201. return;
  202. }
  203. defaultSiteValue = $("#redirectDomain").val().trim();
  204. $("#redirectDomain").parent().removeClass("error");
  205. }
  206. //Create the endpoint by calling add
  207. $.ajax({
  208. url: "/api/proxy/add",
  209. data: {
  210. "type": "root",
  211. "tls": rootReqTls,
  212. "ep": newpr,
  213. "defaultSiteOpt": currentDefaultSiteOption,
  214. "defaultSiteVal":defaultSiteValue,
  215. },
  216. method: "POST",
  217. success: function(data){
  218. if (data.error != undefined){
  219. msgbox(data.error, false, 5000);
  220. }else{
  221. //OK
  222. initRootInfo(function(){
  223. //Check if WebServ is enabled
  224. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  225. if (isUsingWebServ){
  226. //Force enable static web server
  227. //See webserv.html for details
  228. setWebServerRunningState(true);
  229. }
  230. setTimeout(function(){
  231. //Update the checkbox
  232. msgbox("Proxy Root Updated");
  233. }, 100);
  234. })
  235. });
  236. }
  237. if (btn != undefined){
  238. $(btn).removeClass("disabled");
  239. }
  240. }
  241. });
  242. }
  243. </script>