typedef.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dynamicproxy
  2. import (
  3. _ "embed"
  4. "net"
  5. "net/http"
  6. "sync"
  7. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  8. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  9. "imuslab.com/zoraxy/mod/geodb"
  10. "imuslab.com/zoraxy/mod/statistic"
  11. "imuslab.com/zoraxy/mod/tlscert"
  12. )
  13. const (
  14. ProxyType_Root = 0
  15. ProxyType_Host = 1
  16. ProxyType_Vdir = 2
  17. )
  18. type ProxyHandler struct {
  19. Parent *Router
  20. }
  21. type RouterOption struct {
  22. HostUUID string //The UUID of Zoraxy, use for heading mod
  23. HostVersion string //The version of Zoraxy, use for heading mod
  24. Port int //Incoming port
  25. UseTls bool //Use TLS to serve incoming requsts
  26. ForceTLSLatest bool //Force TLS1.2 or above
  27. NoCache bool //Force set Cache-Control: no-store
  28. ListenOnPort80 bool //Enable port 80 http listener
  29. ForceHttpsRedirect bool //Force redirection of http to https endpoint
  30. TlsManager *tlscert.Manager
  31. RedirectRuleTable *redirection.RuleTable
  32. GeodbStore *geodb.Store //GeoIP blacklist and whitelist
  33. StatisticCollector *statistic.Collector
  34. WebDirectory string //The static web server directory containing the templates folder
  35. }
  36. type Router struct {
  37. Option *RouterOption
  38. ProxyEndpoints *sync.Map
  39. Running bool
  40. Root *ProxyEndpoint
  41. mux http.Handler
  42. server *http.Server
  43. tlsListener net.Listener
  44. routingRules []*RoutingRule
  45. tlsRedirectStop chan bool //Stop channel for tls redirection server
  46. tldMap map[string]int //Top level domain map, see tld.json
  47. }
  48. // Auth credential for basic auth on certain endpoints
  49. type BasicAuthCredentials struct {
  50. Username string
  51. PasswordHash string
  52. }
  53. // Auth credential for basic auth on certain endpoints
  54. type BasicAuthUnhashedCredentials struct {
  55. Username string
  56. Password string
  57. }
  58. // Paths to exclude in basic auth enabled proxy handler
  59. type BasicAuthExceptionRule struct {
  60. PathPrefix string
  61. }
  62. // User defined headers to add into a proxy endpoint
  63. type UserDefinedHeader struct {
  64. Key string
  65. Value string
  66. }
  67. // A Virtual Directory endpoint, provide a subset of ProxyEndpoint for better
  68. // program structure than directly using ProxyEndpoint
  69. type VirtualDirectoryEndpoint struct {
  70. MatchingPath string //Matching prefix of the request path, also act as key
  71. Domain string //Domain or IP to proxy to
  72. RequireTLS bool //Target domain require TLS
  73. SkipCertValidations bool //Set to true to accept self signed certs
  74. Disabled bool //If the rule is enabled
  75. proxy *dpcore.ReverseProxy `json:"-"`
  76. parent *ProxyEndpoint `json:"-"`
  77. }
  78. // A proxy endpoint record, a general interface for handling inbound routing
  79. type ProxyEndpoint struct {
  80. ProxyType int //The type of this proxy, see const def
  81. RootOrMatchingDomain string //Matching domain for host, also act as key
  82. Domain string //Domain or IP to proxy to
  83. //TLS/SSL Related
  84. RequireTLS bool //Target domain require TLS
  85. BypassGlobalTLS bool //Bypass global TLS setting options if TLS Listener enabled (parent.tlsListener != nil)
  86. SkipCertValidations bool //Set to true to accept self signed certs
  87. SkipWebSocketOriginCheck bool //Skip origin check on websocket upgrade connections
  88. //Virtual Directories
  89. VirtualDirectories []*VirtualDirectoryEndpoint
  90. //Custom Headers
  91. UserDefinedHeaders []*UserDefinedHeader //Custom headers to append when proxying requests from this endpoint
  92. //Authentication
  93. RequireBasicAuth bool //Set to true to request basic auth before proxy
  94. BasicAuthCredentials []*BasicAuthCredentials //Basic auth credentials
  95. BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
  96. //Fallback routing logic
  97. DefaultSiteOption int //Fallback routing logic options
  98. DefaultSiteValue string //Fallback routing target, optional
  99. Disabled bool //If the rule is disabled
  100. //Internal Logic Elements
  101. parent *Router
  102. proxy *dpcore.ReverseProxy `json:"-"`
  103. }
  104. /*
  105. Routing type specific interface
  106. These are options that only avaible for a specific interface
  107. when running, these are converted into "ProxyEndpoint" objects
  108. for more generic routing logic
  109. */
  110. // Root options are those that are required for reverse proxy handler to work
  111. const (
  112. DefaultSite_InternalStaticWebServer = 0
  113. DefaultSite_ReverseProxy = 1
  114. DefaultSite_Redirect = 2
  115. DefaultSite_NotFoundPage = 3
  116. )
  117. /*
  118. Web Templates
  119. */
  120. var (
  121. //go:embed templates/forbidden.html
  122. page_forbidden []byte
  123. )