typedef.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. Port int //Incoming port
  24. UseTls bool //Use TLS to serve incoming requsts
  25. ForceTLSLatest bool //Force TLS1.2 or above
  26. ListenOnPort80 bool //Enable port 80 http listener
  27. ForceHttpsRedirect bool //Force redirection of http to https endpoint
  28. TlsManager *tlscert.Manager
  29. RedirectRuleTable *redirection.RuleTable
  30. GeodbStore *geodb.Store //GeoIP blacklist and whitelist
  31. StatisticCollector *statistic.Collector
  32. WebDirectory string //The static web server directory containing the templates folder
  33. }
  34. type Router struct {
  35. Option *RouterOption
  36. ProxyEndpoints *sync.Map
  37. Running bool
  38. Root *ProxyEndpoint
  39. mux http.Handler
  40. server *http.Server
  41. tlsListener net.Listener
  42. routingRules []*RoutingRule
  43. tlsRedirectStop chan bool //Stop channel for tls redirection server
  44. tldMap map[string]int //Top level domain map, see tld.json
  45. }
  46. // Auth credential for basic auth on certain endpoints
  47. type BasicAuthCredentials struct {
  48. Username string
  49. PasswordHash string
  50. }
  51. // Auth credential for basic auth on certain endpoints
  52. type BasicAuthUnhashedCredentials struct {
  53. Username string
  54. Password string
  55. }
  56. // Paths to exclude in basic auth enabled proxy handler
  57. type BasicAuthExceptionRule struct {
  58. PathPrefix string
  59. }
  60. // A Virtual Directory endpoint, provide a subset of ProxyEndpoint for better
  61. // program structure than directly using ProxyEndpoint
  62. type VirtualDirectoryEndpoint struct {
  63. MatchingPath string //Matching prefix of the request path, also act as key
  64. Domain string //Domain or IP to proxy to
  65. RequireTLS bool //Target domain require TLS
  66. SkipCertValidations bool //Set to true to accept self signed certs
  67. Disabled bool //If the rule is enabled
  68. proxy *dpcore.ReverseProxy `json:"-"`
  69. }
  70. // A proxy endpoint record, a general interface for handling inbound routing
  71. type ProxyEndpoint struct {
  72. ProxyType int //The type of this proxy, see const def
  73. RootOrMatchingDomain string //Matching domain for host, also act as key
  74. Domain string //Domain or IP to proxy to
  75. //TLS/SSL Related
  76. RequireTLS bool //Target domain require TLS
  77. BypassGlobalTLS bool //Bypass global TLS setting options if TLS Listener enabled (parent.tlsListener != nil)
  78. SkipCertValidations bool //Set to true to accept self signed certs
  79. //Virtual Directories
  80. VirtualDirectories []*VirtualDirectoryEndpoint
  81. //Authentication
  82. RequireBasicAuth bool //Set to true to request basic auth before proxy
  83. BasicAuthCredentials []*BasicAuthCredentials //Basic auth credentials
  84. BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
  85. //Fallback routing logic
  86. DefaultSiteOption int //Fallback routing logic options
  87. DefaultSiteValue string //Fallback routing target, optional
  88. Disabled bool //If the rule is disabled
  89. //Internal Logic Elements
  90. parent *Router
  91. proxy *dpcore.ReverseProxy `json:"-"`
  92. }
  93. /*
  94. Routing type specific interface
  95. These are options that only avaible for a specific interface
  96. when running, these are converted into "ProxyEndpoint" objects
  97. for more generic routing logic
  98. */
  99. // Root options are those that are required for reverse proxy handler to work
  100. const (
  101. DefaultSite_InternalStaticWebServer = 0
  102. DefaultSite_ReverseProxy = 1
  103. DefaultSite_Redirect = 2
  104. DefaultSite_NotFoundPage = 3
  105. )
  106. /*
  107. Web Templates
  108. */
  109. var (
  110. //go:embed templates/forbidden.html
  111. page_forbidden []byte
  112. )