typedef.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package dynamicproxy
  2. import (
  3. _ "embed"
  4. "net"
  5. "net/http"
  6. "sync"
  7. "imuslab.com/zoraxy/mod/access"
  8. "imuslab.com/zoraxy/mod/auth/sso"
  9. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  10. "imuslab.com/zoraxy/mod/dynamicproxy/loadbalance"
  11. "imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
  12. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  13. "imuslab.com/zoraxy/mod/dynamicproxy/rewrite"
  14. "imuslab.com/zoraxy/mod/geodb"
  15. "imuslab.com/zoraxy/mod/info/logger"
  16. "imuslab.com/zoraxy/mod/statistic"
  17. "imuslab.com/zoraxy/mod/tlscert"
  18. )
  19. const (
  20. ProxyType_Root = 0
  21. ProxyType_Host = 1
  22. ProxyType_Vdir = 2
  23. )
  24. type ProxyHandler struct {
  25. Parent *Router
  26. }
  27. /* Router Object Options */
  28. type RouterOption struct {
  29. HostUUID string //The UUID of Zoraxy, use for heading mod
  30. HostVersion string //The version of Zoraxy, use for heading mod
  31. Port int //Incoming port
  32. UseTls bool //Use TLS to serve incoming requsts
  33. ForceTLSLatest bool //Force TLS1.2 or above
  34. NoCache bool //Force set Cache-Control: no-store
  35. ListenOnPort80 bool //Enable port 80 http listener
  36. ForceHttpsRedirect bool //Force redirection of http to https endpoint
  37. TlsManager *tlscert.Manager //TLS manager for serving SAN certificates
  38. RedirectRuleTable *redirection.RuleTable //Redirection rules handler and table
  39. GeodbStore *geodb.Store //GeoIP resolver
  40. AccessController *access.Controller //Blacklist / whitelist controller
  41. StatisticCollector *statistic.Collector //Statistic collector for storing stats on incoming visitors
  42. WebDirectory string //The static web server directory containing the templates folder
  43. LoadBalancer *loadbalance.RouteManager //Load balancer that handle load balancing of proxy target
  44. SSOHandler *sso.SSOHandler //SSO handler for handling SSO requests, interception mode only
  45. Logger *logger.Logger //Logger for reverse proxy requets
  46. }
  47. /* Router Object */
  48. type Router struct {
  49. Option *RouterOption
  50. ProxyEndpoints *sync.Map
  51. Running bool
  52. Root *ProxyEndpoint
  53. mux http.Handler
  54. server *http.Server
  55. tlsListener net.Listener
  56. loadBalancer *loadbalance.RouteManager //Load balancer routing manager
  57. routingRules []*RoutingRule
  58. tlsRedirectStop chan bool //Stop channel for tls redirection server
  59. rateLimterStop chan bool //Stop channel for rate limiter
  60. rateLimitCounter RequestCountPerIpTable //Request counter for rate limter
  61. }
  62. /* Basic Auth Related Data structure*/
  63. // Auth credential for basic auth on certain endpoints
  64. type BasicAuthCredentials struct {
  65. Username string
  66. PasswordHash string
  67. }
  68. // Auth credential for basic auth on certain endpoints
  69. type BasicAuthUnhashedCredentials struct {
  70. Username string
  71. Password string
  72. }
  73. // Paths to exclude in basic auth enabled proxy handler
  74. type BasicAuthExceptionRule struct {
  75. PathPrefix string
  76. }
  77. /* Routing Rule Data Structures */
  78. // A Virtual Directory endpoint, provide a subset of ProxyEndpoint for better
  79. // program structure than directly using ProxyEndpoint
  80. type VirtualDirectoryEndpoint struct {
  81. MatchingPath string //Matching prefix of the request path, also act as key
  82. Domain string //Domain or IP to proxy to
  83. RequireTLS bool //Target domain require TLS
  84. SkipCertValidations bool //Set to true to accept self signed certs
  85. Disabled bool //If the rule is enabled
  86. proxy *dpcore.ReverseProxy `json:"-"`
  87. parent *ProxyEndpoint `json:"-"`
  88. }
  89. // A proxy endpoint record, a general interface for handling inbound routing
  90. type ProxyEndpoint struct {
  91. ProxyType int //The type of this proxy, see const def
  92. RootOrMatchingDomain string //Matching domain for host, also act as key
  93. MatchingDomainAlias []string //A list of domains that alias to this rule
  94. ActiveOrigins []*loadbalance.Upstream //Activated Upstream or origin servers IP or domain to proxy to
  95. InactiveOrigins []*loadbalance.Upstream //Disabled Upstream or origin servers IP or domain to proxy to
  96. UseStickySession bool //Use stick session for load balancing
  97. UseActiveLoadBalance bool //Use active loadbalancing, default passive
  98. Disabled bool //If the rule is disabled
  99. //Inbound TLS/SSL Related
  100. BypassGlobalTLS bool //Bypass global TLS setting options if TLS Listener enabled (parent.tlsListener != nil)
  101. //Virtual Directories
  102. VirtualDirectories []*VirtualDirectoryEndpoint
  103. //Custom Headers
  104. UserDefinedHeaders []*rewrite.UserDefinedHeader //Custom headers to append when proxying requests from this endpoint
  105. RequestHostOverwrite string //If not empty, this domain will be used to overwrite the Host field in request header
  106. HSTSMaxAge int64 //HSTS max age, set to 0 for disable HSTS headers
  107. EnablePermissionPolicyHeader bool //Enable injection of permission policy header
  108. PermissionPolicy *permissionpolicy.PermissionsPolicy //Permission policy header
  109. DisableHopByHopHeaderRemoval bool //Do not remove hop-by-hop headers
  110. //Authentication
  111. RequireBasicAuth bool //Set to true to request basic auth before proxy
  112. BasicAuthCredentials []*BasicAuthCredentials //Basic auth credentials
  113. BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
  114. UseSSOIntercept bool //Allow SSO to intercept this endpoint and provide authentication via Oauth2 credentials
  115. // Rate Limiting
  116. RequireRateLimit bool
  117. RateLimit int64 // Rate limit in requests per second
  118. //Access Control
  119. AccessFilterUUID string //Access filter ID
  120. //Fallback routing logic (Special Rule Sets Only)
  121. DefaultSiteOption int //Fallback routing logic options
  122. DefaultSiteValue string //Fallback routing target, optional
  123. //Internal Logic Elements
  124. parent *Router `json:"-"`
  125. }
  126. /*
  127. Routing type specific interface
  128. These are options that only avaible for a specific interface
  129. when running, these are converted into "ProxyEndpoint" objects
  130. for more generic routing logic
  131. */
  132. // Root options are those that are required for reverse proxy handler to work
  133. const (
  134. DefaultSite_InternalStaticWebServer = 0
  135. DefaultSite_ReverseProxy = 1
  136. DefaultSite_Redirect = 2
  137. DefaultSite_NotFoundPage = 3
  138. )
  139. /*
  140. Web Templates
  141. */
  142. var (
  143. //go:embed templates/forbidden.html
  144. page_forbidden []byte
  145. //go:embed templates/hosterror.html
  146. page_hosterror []byte
  147. )