1
0

redirect.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "imuslab.com/zoraxy/mod/utils"
  8. )
  9. /*
  10. Redirect.go
  11. This script handle all the http handlers
  12. related to redirection function in the reverse proxy
  13. */
  14. // Handle request for listing all stored redirection rules
  15. func handleListRedirectionRules(w http.ResponseWriter, r *http.Request) {
  16. rules := redirectTable.GetAllRedirectRules()
  17. js, _ := json.Marshal(rules)
  18. utils.SendJSONResponse(w, string(js))
  19. }
  20. // Handle request for adding new redirection rule
  21. func handleAddRedirectionRule(w http.ResponseWriter, r *http.Request) {
  22. redirectUrl, err := utils.PostPara(r, "redirectUrl")
  23. if err != nil {
  24. utils.SendErrorResponse(w, "redirect url cannot be empty")
  25. return
  26. }
  27. destUrl, err := utils.PostPara(r, "destUrl")
  28. if err != nil {
  29. utils.SendErrorResponse(w, "destination url cannot be empty")
  30. }
  31. forwardChildpath, err := utils.PostPara(r, "forwardChildpath")
  32. if err != nil {
  33. //Assume true
  34. forwardChildpath = "true"
  35. }
  36. redirectTypeString, err := utils.PostPara(r, "redirectType")
  37. if err != nil {
  38. redirectTypeString = "307"
  39. }
  40. redirectionStatusCode, err := strconv.Atoi(redirectTypeString)
  41. if err != nil {
  42. utils.SendErrorResponse(w, "invalid status code number")
  43. return
  44. }
  45. err = redirectTable.AddRedirectRule(redirectUrl, destUrl, forwardChildpath == "true", redirectionStatusCode)
  46. if err != nil {
  47. utils.SendErrorResponse(w, err.Error())
  48. return
  49. }
  50. utils.SendOK(w)
  51. }
  52. // Handle remove of a given redirection rule
  53. func handleDeleteRedirectionRule(w http.ResponseWriter, r *http.Request) {
  54. redirectUrl, err := utils.PostPara(r, "redirectUrl")
  55. if err != nil {
  56. utils.SendErrorResponse(w, "redirect url cannot be empty")
  57. return
  58. }
  59. err = redirectTable.DeleteRedirectRule(redirectUrl)
  60. if err != nil {
  61. utils.SendErrorResponse(w, err.Error())
  62. return
  63. }
  64. utils.SendOK(w)
  65. }
  66. // Toggle redirection regex support. Note that this cost another O(n) time complexity to each page load
  67. func handleToggleRedirectRegexpSupport(w http.ResponseWriter, r *http.Request) {
  68. enabled, err := utils.PostPara(r, "enable")
  69. if err != nil {
  70. //Return the current state of the regex support
  71. js, _ := json.Marshal(redirectTable.AllowRegex)
  72. utils.SendJSONResponse(w, string(js))
  73. return
  74. }
  75. //Update the current regex support rule enable state
  76. enableRegexSupport := strings.EqualFold(strings.TrimSpace(enabled), "true")
  77. redirectTable.AllowRegex = enableRegexSupport
  78. err = sysdb.Write("Redirect", "regex", enableRegexSupport)
  79. if enableRegexSupport {
  80. SystemWideLogger.PrintAndLog("redirect", "Regex redirect rule enabled", nil)
  81. } else {
  82. SystemWideLogger.PrintAndLog("redirect", "Regex redirect rule disabled", nil)
  83. }
  84. if err != nil {
  85. utils.SendErrorResponse(w, "unable to save settings")
  86. return
  87. }
  88. utils.SendOK(w)
  89. }