typedef.go 6.1 KB

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