redirect.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. func handleEditRedirectionRule(w http.ResponseWriter, r *http.Request) {
  67. originalRedirectUrl, err := utils.PostPara(r, "originalRedirectUrl")
  68. if err != nil {
  69. utils.SendErrorResponse(w, "original redirect url cannot be empty")
  70. return
  71. }
  72. newRedirectUrl, err := utils.PostPara(r, "newRedirectUrl")
  73. if err != nil {
  74. utils.SendErrorResponse(w, "redirect url cannot be empty")
  75. return
  76. }
  77. destUrl, err := utils.PostPara(r, "destUrl")
  78. if err != nil {
  79. utils.SendErrorResponse(w, "destination url cannot be empty")
  80. }
  81. forwardChildpath, err := utils.PostPara(r, "forwardChildpath")
  82. if err != nil {
  83. //Assume true
  84. forwardChildpath = "true"
  85. }
  86. redirectTypeString, err := utils.PostPara(r, "redirectType")
  87. if err != nil {
  88. redirectTypeString = "307"
  89. }
  90. redirectionStatusCode, err := strconv.Atoi(redirectTypeString)
  91. if err != nil {
  92. utils.SendErrorResponse(w, "invalid status code number")
  93. return
  94. }
  95. err = redirectTable.EditRedirectRule(originalRedirectUrl, newRedirectUrl, destUrl, forwardChildpath == "true", redirectionStatusCode)
  96. if err != nil {
  97. utils.SendErrorResponse(w, err.Error())
  98. return
  99. }
  100. utils.SendOK(w)
  101. }
  102. // Toggle redirection regex support. Note that this cost another O(n) time complexity to each page load
  103. func handleToggleRedirectRegexpSupport(w http.ResponseWriter, r *http.Request) {
  104. enabled, err := utils.PostPara(r, "enable")
  105. if err != nil {
  106. //Return the current state of the regex support
  107. js, _ := json.Marshal(redirectTable.AllowRegex)
  108. utils.SendJSONResponse(w, string(js))
  109. return
  110. }
  111. //Update the current regex support rule enable state
  112. enableRegexSupport := strings.EqualFold(strings.TrimSpace(enabled), "true")
  113. redirectTable.AllowRegex = enableRegexSupport
  114. err = sysdb.Write("redirect", "regex", enableRegexSupport)
  115. if enableRegexSupport {
  116. SystemWideLogger.PrintAndLog("redirect", "Regex redirect rule enabled", nil)
  117. } else {
  118. SystemWideLogger.PrintAndLog("redirect", "Regex redirect rule disabled", nil)
  119. }
  120. if err != nil {
  121. utils.SendErrorResponse(w, "unable to save settings")
  122. return
  123. }
  124. utils.SendOK(w)
  125. }