rproot.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. msgbox("Default site load failed", false);
  118. }else{
  119. var $radios = $('input:radio[name=defaultsiteOption]');
  120. let proxyType = data.DefaultSiteOption;
  121. //See typedef.go for enum conversion
  122. if (proxyType == 0){
  123. $radios.filter('[value=webserver]').prop('checked', true);
  124. }else if (proxyType == 1){
  125. $radios.filter('[value=proxy]').prop('checked', true);
  126. $("#proxyRoot").val(data.DefaultSiteValue);
  127. }else if (proxyType == 2){
  128. $radios.filter('[value=redirect]').prop('checked', true);
  129. $("#redirectDomain").val(data.DefaultSiteValue);
  130. }else if (proxyType == 3){
  131. $radios.filter('[value=notfound]').prop('checked', true);
  132. }
  133. updateAvaibleDefaultSiteOptions();
  134. $("#proxyRoot").val(data.ActiveOrigins[0].OriginIpOrDomain);
  135. checkRootRequireTLS(data.ActiveOrigins[0].OriginIpOrDomain);
  136. }
  137. if (callback != undefined){
  138. callback();
  139. }
  140. });
  141. }
  142. initRootInfo(function(){
  143. bindDefaultSiteRadioCheckboxEvents();
  144. });
  145. function isUsingStaticWebServerAsRoot(callback){
  146. let currentProxyRoot = $("#proxyRoot").val().trim();
  147. $.get("/api/webserv/status", function(webservStatus){
  148. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  149. return callback(true);
  150. }
  151. return callback(false);
  152. });
  153. }
  154. //Bind event to tab switch
  155. tabSwitchEventBind["setroot"] = function(){
  156. }
  157. //Check if the given domain will redirect to https
  158. function checkRootRequireTLS(targetDomain){
  159. //Trim off the http or https from the origin
  160. if (targetDomain.startsWith("http://")){
  161. targetDomain = targetDomain.substring(7);
  162. $("#proxyRoot").val(targetDomain);
  163. }else if (targetDomain.startsWith("https://")){
  164. targetDomain = targetDomain.substring(8);
  165. $("#proxyRoot").val(targetDomain);
  166. }
  167. $.cjax({
  168. url: "/api/proxy/tlscheck",
  169. method: "POST",
  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(btn=undefined){
  183. var newpr = $("#proxyRoot").val();
  184. if (newpr.trim() == "" && currentDefaultSiteOption == 0){
  185. //Fill in the web server info
  186. newpr = "127.0.0.1:" + $("#webserv_listenPort").val();
  187. $("#proxyRoot").val(newpr);
  188. }
  189. var rootReqTls = $("#rootReqTLS")[0].checked;
  190. if (btn != undefined){
  191. $(btn).addClass("disabled");
  192. }
  193. //proxy mode or redirect mode, check for input values
  194. var defaultSiteValue = "";
  195. if (currentDefaultSiteOption == 1){
  196. if ($("#proxyRoot").val().trim() == ""){
  197. $("#proxyRoot").parent().addClass("error");
  198. return;
  199. }
  200. defaultSiteValue = $("#proxyRoot").val().trim();
  201. $("#proxyRoot").parent().removeClass("error");
  202. }else if (currentDefaultSiteOption == 2){
  203. if ($("#redirectDomain").val().trim() == ""){
  204. $("#redirectDomain").parent().addClass("error");
  205. return;
  206. }
  207. defaultSiteValue = $("#redirectDomain").val().trim();
  208. $("#redirectDomain").parent().removeClass("error");
  209. }
  210. //Create the endpoint by calling add
  211. $.cjax({
  212. url: "/api/proxy/add",
  213. data: {
  214. "type": "root",
  215. "tls": rootReqTls,
  216. "ep": newpr,
  217. "defaultSiteOpt": currentDefaultSiteOption,
  218. "defaultSiteVal":defaultSiteValue,
  219. },
  220. method: "POST",
  221. success: function(data){
  222. if (data.error != undefined){
  223. msgbox(data.error, false, 5000);
  224. }else{
  225. //OK
  226. initRootInfo(function(){
  227. //Check if WebServ is enabled
  228. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  229. if (isUsingWebServ){
  230. //Force enable static web server
  231. //See webserv.html for details
  232. setWebServerRunningState(true);
  233. }
  234. msgbox("Default Site Updated");
  235. })
  236. });
  237. }
  238. if (btn != undefined){
  239. $(btn).removeClass("disabled");
  240. }
  241. },
  242. error: function(){
  243. msgbox("Unknown error occured", false);
  244. }
  245. });
  246. }
  247. </script>