Server.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package dynamicproxy
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "imuslab.com/arozos/ReverseProxy/mod/geodb"
  7. )
  8. /*
  9. Server.go
  10. Main server for dynamic proxy core
  11. */
  12. func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  13. //Check if this ip is in blacklist
  14. clientIpAddr := geodb.GetRequesterIP(r)
  15. if h.Parent.Option.GeodbStore.IsBlacklisted(clientIpAddr) {
  16. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  17. w.WriteHeader(http.StatusForbidden)
  18. template, err := os.ReadFile("./web/forbidden.html")
  19. if err != nil {
  20. w.Write([]byte("403 - Forbidden"))
  21. } else {
  22. w.Write(template)
  23. }
  24. return
  25. }
  26. //Check if this is a redirection url
  27. if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) {
  28. h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r)
  29. return
  30. }
  31. //Extract request host to see if it is virtual directory or subdomain
  32. domainOnly := r.Host
  33. if strings.Contains(r.Host, ":") {
  34. hostPath := strings.Split(r.Host, ":")
  35. domainOnly = hostPath[0]
  36. }
  37. if strings.Contains(r.Host, ".") {
  38. //This might be a subdomain. See if there are any subdomain proxy router for this
  39. //Remove the port if any
  40. sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly)
  41. if sep != nil {
  42. h.subdomainRequest(w, r, sep)
  43. return
  44. }
  45. }
  46. //Clean up the request URI
  47. proxyingPath := strings.TrimSpace(r.RequestURI)
  48. targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath)
  49. if targetProxyEndpoint != nil {
  50. h.proxyRequest(w, r, targetProxyEndpoint)
  51. } else {
  52. h.proxyRequest(w, r, h.Parent.Root)
  53. }
  54. }