typedef.go 5.7 KB

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