rproot.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 class="field">
  41. <div class="ui radio defaultsite checkbox">
  42. <input type="radio" name="defaultsiteOption" value="closeresp">
  43. <label>Close Connection<br>
  44. <small>Close the connection without any response</small>
  45. </label>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <!-- Reverse Proxy as Default Site Options -->
  51. <div id="defaultSiteProxyOptions" class="ui basic segment advanceoptions defaultSiteOptionDetails" style="display:none; ">
  52. <div class="ui form">
  53. <div class="field">
  54. <label>Reverse Proxy Target</label>
  55. <input type="text" id="proxyRoot" onchange="checkRootRequireTLS(this.value);">
  56. <small>e.g. localhost:8080 / 192.168.0.100:80 / example.com</small>
  57. </div>
  58. <div class="field">
  59. <div class="ui checkbox">
  60. <input type="checkbox" id="rootReqTLS">
  61. <label>Reverse proxy target require TLS connection <br><small>Check this if your proxy target URL require connection with https://</small></label>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <!-- Redirect as default site Options-->
  67. <div id="defaultSiteRedirectOptions" class="ui basic segment advanceoptions defaultSiteOptionDetails" style="display:none;"">
  68. <div class="ui form">
  69. <div class="field">
  70. <label>Redirect target domain</label>
  71. <div class="ui input">
  72. <input id="redirectDomain" type="text" placeholder="http://example.com">
  73. </div>
  74. <small>Unset subdomain will be redirected to the link above. Remember to include the protocol (e.g. http:// or https://)</small>
  75. </div>
  76. </div>
  77. </div>
  78. <button class="ui basic button" onclick="setProxyRoot(this)"><i class="green checkmark icon" ></i> Apply Changes</button>
  79. <button class="ui basic button" onclick="initRootInfo()"><i class="refresh icon" ></i> Reset</button>
  80. <br>
  81. </div>
  82. </div>
  83. <script>
  84. var currentDefaultSiteOption = 0; //For enum see typedef.go
  85. $("#advanceRootSettings").accordion();
  86. //Handle toggle events of option radio boxes
  87. function updateAvaibleDefaultSiteOptions(){
  88. let selectedDefaultSite = $('input[name="defaultsiteOption"]:checked').val();
  89. $(".defaultSiteOptionDetails").hide();
  90. $("#useRootProxyRouterForVdir").parent().addClass("disabled");
  91. if (selectedDefaultSite == "webserver"){
  92. //Use build in web server as target
  93. let staticWebServerURL = "127.0.0.1:" + $("#webserv_listenPort").val();
  94. $("#proxyRoot").val(staticWebServerURL);
  95. $("#proxyRoot").parent().addClass("disabled");
  96. $("#rootReqTLS").parent().checkbox("set unchecked");
  97. $("#rootReqTLS").parent().addClass("disabled");
  98. $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
  99. currentDefaultSiteOption = 0;
  100. }else if (selectedDefaultSite == "proxy"){
  101. $("#defaultSiteProxyOptions").show();
  102. $("#rootReqTLS").parent().removeClass("disabled");
  103. $("#proxyRoot").parent().removeClass("disabled");
  104. $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
  105. currentDefaultSiteOption = 1;
  106. }else if (selectedDefaultSite == "redirect"){
  107. $("#defaultSiteRedirectOptions").show();
  108. currentDefaultSiteOption = 2;
  109. }else if (selectedDefaultSite == "notfound"){
  110. currentDefaultSiteOption = 3;
  111. }else if (selectedDefaultSite == "closeresp"){
  112. currentDefaultSiteOption = 4;
  113. }else{
  114. //Unknown option
  115. return;
  116. }
  117. }
  118. //Bind events to the radio boxes
  119. function bindDefaultSiteRadioCheckboxEvents(){
  120. $('input[type=radio][name=defaultsiteOption]').off("change").on("change", function() {
  121. updateAvaibleDefaultSiteOptions();
  122. });
  123. }
  124. function initRootInfo(callback=undefined){
  125. $.get("/api/proxy/list?type=root", function(data){
  126. if (data == null){
  127. msgbox("Default site load failed", false);
  128. }else{
  129. var $radios = $('input:radio[name=defaultsiteOption]');
  130. let proxyType = data.DefaultSiteOption;
  131. //See typedef.go for enum conversion
  132. if (proxyType == 0){
  133. $radios.filter('[value=webserver]').prop('checked', true);
  134. }else if (proxyType == 1){
  135. $radios.filter('[value=proxy]').prop('checked', true);
  136. $("#proxyRoot").val(data.DefaultSiteValue);
  137. }else if (proxyType == 2){
  138. $radios.filter('[value=redirect]').prop('checked', true);
  139. $("#redirectDomain").val(data.DefaultSiteValue);
  140. }else if (proxyType == 3){
  141. $radios.filter('[value=notfound]').prop('checked', true);
  142. }else if (proxyType == 4){
  143. $radios.filter('[value=closeresp]').prop('checked', true);
  144. }
  145. updateAvaibleDefaultSiteOptions();
  146. $("#proxyRoot").val(data.ActiveOrigins[0].OriginIpOrDomain);
  147. checkRootRequireTLS(data.ActiveOrigins[0].OriginIpOrDomain);
  148. }
  149. if (callback != undefined){
  150. callback();
  151. }
  152. });
  153. }
  154. initRootInfo(function(){
  155. bindDefaultSiteRadioCheckboxEvents();
  156. });
  157. function isUsingStaticWebServerAsRoot(callback){
  158. let currentProxyRoot = $("#proxyRoot").val().trim();
  159. $.get("/api/webserv/status", function(webservStatus){
  160. if (currentProxyRoot == "127.0.0.1:" + webservStatus.ListeningPort || currentProxyRoot == "localhost:" + webservStatus.ListeningPort){
  161. return callback(true);
  162. }
  163. return callback(false);
  164. });
  165. }
  166. //Bind event to tab switch
  167. tabSwitchEventBind["setroot"] = function(){
  168. }
  169. //Check if the given domain will redirect to https
  170. function checkRootRequireTLS(targetDomain){
  171. //Trim off the http or https from the origin
  172. if (targetDomain.startsWith("http://")){
  173. targetDomain = targetDomain.substring(7);
  174. $("#proxyRoot").val(targetDomain);
  175. }else if (targetDomain.startsWith("https://")){
  176. targetDomain = targetDomain.substring(8);
  177. $("#proxyRoot").val(targetDomain);
  178. }
  179. $.cjax({
  180. url: "/api/proxy/tlscheck",
  181. method: "POST",
  182. data: {url: targetDomain},
  183. success: function(data){
  184. if (data.error != undefined){
  185. }else if (data == "https"){
  186. $("#rootReqTLS").parent().checkbox("set checked");
  187. }else if (data == "http"){
  188. $("#rootReqTLS").parent().checkbox("set unchecked");
  189. }
  190. }
  191. })
  192. }
  193. //Set the new proxy root option
  194. function setProxyRoot(btn=undefined){
  195. var newpr = $("#proxyRoot").val();
  196. if (newpr.trim() == "" && currentDefaultSiteOption == 0){
  197. //Fill in the web server info
  198. newpr = "127.0.0.1:" + $("#webserv_listenPort").val();
  199. $("#proxyRoot").val(newpr);
  200. }
  201. var rootReqTls = $("#rootReqTLS")[0].checked;
  202. if (btn != undefined){
  203. $(btn).addClass("disabled");
  204. }
  205. //proxy mode or redirect mode, check for input values
  206. var defaultSiteValue = "";
  207. if (currentDefaultSiteOption == 1){
  208. if ($("#proxyRoot").val().trim() == ""){
  209. $("#proxyRoot").parent().addClass("error");
  210. return;
  211. }
  212. defaultSiteValue = $("#proxyRoot").val().trim();
  213. $("#proxyRoot").parent().removeClass("error");
  214. }else if (currentDefaultSiteOption == 2){
  215. if ($("#redirectDomain").val().trim() == ""){
  216. $("#redirectDomain").parent().addClass("error");
  217. return;
  218. }
  219. defaultSiteValue = $("#redirectDomain").val().trim();
  220. $("#redirectDomain").parent().removeClass("error");
  221. }
  222. //Create the endpoint by calling add
  223. $.cjax({
  224. url: "/api/proxy/add",
  225. data: {
  226. "type": "root",
  227. "tls": rootReqTls,
  228. "ep": newpr,
  229. "defaultSiteOpt": currentDefaultSiteOption,
  230. "defaultSiteVal":defaultSiteValue,
  231. },
  232. method: "POST",
  233. success: function(data){
  234. if (data.error != undefined){
  235. msgbox(data.error, false, 5000);
  236. }else{
  237. //OK
  238. initRootInfo(function(){
  239. //Check if WebServ is enabled
  240. isUsingStaticWebServerAsRoot(function(isUsingWebServ){
  241. if (isUsingWebServ){
  242. //Force enable static web server
  243. //See webserv.html for details
  244. setWebServerRunningState(true);
  245. }
  246. msgbox("Default Site Updated");
  247. })
  248. });
  249. }
  250. if (btn != undefined){
  251. $(btn).removeClass("disabled");
  252. }
  253. },
  254. error: function(){
  255. msgbox("Unknown error occured", false);
  256. }
  257. });
  258. }
  259. </script>