1
0

access.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package dynamicproxy
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "path/filepath"
  7. "imuslab.com/zoraxy/mod/access"
  8. "imuslab.com/zoraxy/mod/netutils"
  9. )
  10. // Handle access check (blacklist / whitelist), return true if request is handled (aka blocked)
  11. // if the return value is false, you can continue process the response writer
  12. func (h *ProxyHandler) handleAccessRouting(ruleID string, w http.ResponseWriter, r *http.Request) bool {
  13. accessRule, err := h.Parent.Option.AccessController.GetAccessRuleByID(ruleID)
  14. if err != nil {
  15. //Unable to load access rule. Target rule not found?
  16. log.Println("[Proxy] Unable to load access rule: " + ruleID)
  17. w.WriteHeader(http.StatusInternalServerError)
  18. w.Write([]byte("500 - Internal Server Error"))
  19. return true
  20. }
  21. isBlocked, blockedReason := accessRequestBlocked(accessRule, h.Parent.Option.WebDirectory, w, r)
  22. if isBlocked {
  23. h.logRequest(r, false, 403, blockedReason, "")
  24. }
  25. return isBlocked
  26. }
  27. // Return boolean, return true if access is blocked
  28. // For string, it will return the blocked reason (if any)
  29. func accessRequestBlocked(accessRule *access.AccessRule, templateDirectory string, w http.ResponseWriter, r *http.Request) (bool, string) {
  30. //Check if this ip is in blacklist
  31. clientIpAddr := netutils.GetRequesterIP(r)
  32. if accessRule.IsBlacklisted(clientIpAddr) {
  33. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  34. w.WriteHeader(http.StatusForbidden)
  35. template, err := os.ReadFile(filepath.Join(templateDirectory, "templates/blacklist.html"))
  36. if err != nil {
  37. w.Write(page_forbidden)
  38. } else {
  39. w.Write(template)
  40. }
  41. return true, "blacklist"
  42. }
  43. //Check if this ip is in whitelist
  44. if !accessRule.IsWhitelisted(clientIpAddr) {
  45. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  46. w.WriteHeader(http.StatusForbidden)
  47. template, err := os.ReadFile(filepath.Join(templateDirectory, "templates/whitelist.html"))
  48. if err != nil {
  49. w.Write(page_forbidden)
  50. } else {
  51. w.Write(template)
  52. }
  53. return true, "whitelist"
  54. }
  55. //Not blocked.
  56. return false, ""
  57. }