typedef.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_Subdomain = 0
  15. ProxyType_Vdir = 1
  16. )
  17. type ProxyHandler struct {
  18. Parent *Router
  19. }
  20. type RouterOption struct {
  21. HostUUID string //The UUID of Zoraxy, use for heading mod
  22. Port int //Incoming port
  23. UseTls bool //Use TLS to serve incoming requsts
  24. ForceTLSLatest bool //Force TLS1.2 or above
  25. ListenOnPort80 bool //Enable port 80 http listener
  26. ForceHttpsRedirect bool //Force redirection of http to https endpoint
  27. TlsManager *tlscert.Manager
  28. RedirectRuleTable *redirection.RuleTable
  29. GeodbStore *geodb.Store //GeoIP blacklist and whitelist
  30. StatisticCollector *statistic.Collector
  31. WebDirectory string //The static web server directory containing the templates folder
  32. }
  33. type Router struct {
  34. Option *RouterOption
  35. ProxyEndpoints *sync.Map
  36. SubdomainEndpoint *sync.Map
  37. Running bool
  38. Root *ProxyEndpoint
  39. RootRoutingOptions *RootRoutingOptions
  40. mux http.Handler
  41. server *http.Server
  42. tlsListener net.Listener
  43. routingRules []*RoutingRule
  44. tlsRedirectStop chan bool //Stop channel for tls redirection server
  45. tldMap map[string]int //Top level domain map, see tld.json
  46. }
  47. // Auth credential for basic auth on certain endpoints
  48. type BasicAuthCredentials struct {
  49. Username string
  50. PasswordHash string
  51. }
  52. // Auth credential for basic auth on certain endpoints
  53. type BasicAuthUnhashedCredentials struct {
  54. Username string
  55. Password string
  56. }
  57. // Paths to exclude in basic auth enabled proxy handler
  58. type BasicAuthExceptionRule struct {
  59. PathPrefix string
  60. }
  61. // A proxy endpoint record
  62. type ProxyEndpoint struct {
  63. ProxyType int //The type of this proxy, see const def
  64. RootOrMatchingDomain string //Root for vdir or Matching domain for subd, also act as key
  65. Domain string //Domain or IP to proxy to
  66. RequireTLS bool //Target domain require TLS
  67. BypassGlobalTLS bool //Bypass global TLS setting options if TLS Listener enabled (parent.tlsListener != nil)
  68. SkipCertValidations bool //Set to true to accept self signed certs
  69. RequireBasicAuth bool //Set to true to request basic auth before proxy
  70. BasicAuthCredentials []*BasicAuthCredentials `json:"-"` //Basic auth credentials
  71. BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
  72. Proxy *dpcore.ReverseProxy `json:"-"`
  73. parent *Router
  74. }
  75. // Root options are those that are required for reverse proxy handler to work
  76. type RootOptions struct {
  77. ProxyLocation string //Proxy Root target, all unset traffic will be forward to here
  78. RequireTLS bool //Proxy root target require TLS connection (not recommended)
  79. BypassGlobalTLS bool //Bypass global TLS setting and make root http only (not recommended)
  80. SkipCertValidations bool //Skip cert validation, suitable for self-signed certs, CURRENTLY NOT USED
  81. //Basic Auth Related
  82. RequireBasicAuth bool //Require basic auth, CURRENTLY NOT USED
  83. BasicAuthCredentials []*BasicAuthCredentials
  84. BasicAuthExceptionRules []*BasicAuthExceptionRule
  85. }
  86. // Additional options are here for letting router knows how to route exception cases for root
  87. type RootRoutingOptions struct {
  88. //Root only configs
  89. EnableRedirectForUnsetRules bool //Force unset rules to redirect to custom domain
  90. UnsetRuleRedirectTarget string //Custom domain to redirect to for unset rules
  91. }
  92. type VdirOptions struct {
  93. RootName string
  94. Domain string
  95. RequireTLS bool
  96. BypassGlobalTLS bool
  97. SkipCertValidations bool
  98. RequireBasicAuth bool
  99. BasicAuthCredentials []*BasicAuthCredentials
  100. BasicAuthExceptionRules []*BasicAuthExceptionRule
  101. }
  102. type SubdOptions struct {
  103. MatchingDomain string
  104. Domain string
  105. RequireTLS bool
  106. BypassGlobalTLS bool
  107. SkipCertValidations bool
  108. RequireBasicAuth bool
  109. BasicAuthCredentials []*BasicAuthCredentials
  110. BasicAuthExceptionRules []*BasicAuthExceptionRule
  111. }
  112. /*
  113. Web Templates
  114. */
  115. var (
  116. //go:embed templates/forbidden.html
  117. page_forbidden []byte
  118. )