1
0

redirect.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "imuslab.com/zoraxy/mod/utils"
  7. )
  8. /*
  9. Redirect.go
  10. This script handle all the http handlers
  11. related to redirection function in the reverse proxy
  12. */
  13. func handleListRedirectionRules(w http.ResponseWriter, r *http.Request) {
  14. rules := redirectTable.GetAllRedirectRules()
  15. js, _ := json.Marshal(rules)
  16. utils.SendJSONResponse(w, string(js))
  17. }
  18. func handleAddRedirectionRule(w http.ResponseWriter, r *http.Request) {
  19. redirectUrl, err := utils.PostPara(r, "redirectUrl")
  20. if err != nil {
  21. utils.SendErrorResponse(w, "redirect url cannot be empty")
  22. return
  23. }
  24. destUrl, err := utils.PostPara(r, "destUrl")
  25. if err != nil {
  26. utils.SendErrorResponse(w, "destination url cannot be empty")
  27. }
  28. forwardChildpath, err := utils.PostPara(r, "forwardChildpath")
  29. if err != nil {
  30. //Assume true
  31. forwardChildpath = "true"
  32. }
  33. redirectTypeString, err := utils.PostPara(r, "redirectType")
  34. if err != nil {
  35. redirectTypeString = "307"
  36. }
  37. redirectionStatusCode, err := strconv.Atoi(redirectTypeString)
  38. if err != nil {
  39. utils.SendErrorResponse(w, "invalid status code number")
  40. return
  41. }
  42. err = redirectTable.AddRedirectRule(redirectUrl, destUrl, forwardChildpath == "true", redirectionStatusCode)
  43. if err != nil {
  44. utils.SendErrorResponse(w, err.Error())
  45. return
  46. }
  47. utils.SendOK(w)
  48. }
  49. func handleDeleteRedirectionRule(w http.ResponseWriter, r *http.Request) {
  50. redirectUrl, err := utils.PostPara(r, "redirectUrl")
  51. if err != nil {
  52. utils.SendErrorResponse(w, "redirect url cannot be empty")
  53. return
  54. }
  55. err = redirectTable.DeleteRedirectRule(redirectUrl)
  56. if err != nil {
  57. utils.SendErrorResponse(w, err.Error())
  58. return
  59. }
  60. utils.SendOK(w)
  61. }