router.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dynamicproxy
  2. import (
  3. "errors"
  4. "log"
  5. "net/url"
  6. "strings"
  7. "time"
  8. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  9. )
  10. /*
  11. Dynamic Proxy Router Functions
  12. This script handle the proxy rules router spawning
  13. and preparation
  14. */
  15. // Prepare proxy route generate a proxy handler service object for your endpoint
  16. func (router *Router) PrepareProxyRoute(endpoint *ProxyEndpoint) (*ProxyEndpoint, error) {
  17. for _, thisOrigin := range endpoint.ActiveOrigins {
  18. //Create the proxy routing handler
  19. err := thisOrigin.StartProxy()
  20. if err != nil {
  21. log.Println("Unable to setup upstream " + thisOrigin.OriginIpOrDomain + ": " + err.Error())
  22. continue
  23. }
  24. }
  25. endpoint.parent = router
  26. //Prepare proxy routing handler for each of the virtual directories
  27. for _, vdir := range endpoint.VirtualDirectories {
  28. domain := vdir.Domain
  29. if len(domain) == 0 {
  30. //invalid vdir
  31. continue
  32. }
  33. if domain[len(domain)-1:] == "/" {
  34. domain = domain[:len(domain)-1]
  35. }
  36. //Parse the web proxy endpoint
  37. webProxyEndpoint := domain
  38. if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
  39. //TLS is not hardcoded in proxy target domain
  40. if vdir.RequireTLS {
  41. webProxyEndpoint = "https://" + webProxyEndpoint
  42. } else {
  43. webProxyEndpoint = "http://" + webProxyEndpoint
  44. }
  45. }
  46. path, err := url.Parse(webProxyEndpoint)
  47. if err != nil {
  48. return nil, err
  49. }
  50. proxy := dpcore.NewDynamicProxyCore(path, vdir.MatchingPath, &dpcore.DpcoreOptions{
  51. IgnoreTLSVerification: vdir.SkipCertValidations,
  52. FlushInterval: 500 * time.Millisecond,
  53. })
  54. vdir.proxy = proxy
  55. vdir.parent = endpoint
  56. }
  57. return endpoint, nil
  58. }
  59. // Add Proxy Route to current runtime. Call to PrepareProxyRoute before adding to runtime
  60. func (router *Router) AddProxyRouteToRuntime(endpoint *ProxyEndpoint) error {
  61. lookupHostname := strings.ToLower(endpoint.RootOrMatchingDomain)
  62. if len(endpoint.ActiveOrigins) == 0 {
  63. //There are no active origins. No need to check for ready
  64. router.ProxyEndpoints.Store(lookupHostname, endpoint)
  65. return nil
  66. }
  67. if !router.loadBalancer.UpstreamsReady(endpoint.ActiveOrigins) {
  68. //This endpoint is not prepared
  69. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  70. }
  71. // Push record into running subdomain endpoints
  72. router.ProxyEndpoints.Store(lookupHostname, endpoint)
  73. return nil
  74. }
  75. // Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
  76. func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
  77. if !router.loadBalancer.UpstreamsReady(endpoint.ActiveOrigins) {
  78. //This endpoint is not prepared
  79. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  80. }
  81. // Push record into running root endpoints
  82. router.Root = endpoint
  83. return nil
  84. }
  85. // ProxyEndpoint remove provide global access by key
  86. func (router *Router) RemoveProxyEndpointByRootname(rootnameOrMatchingDomain string) error {
  87. targetEpt, err := router.LoadProxy(rootnameOrMatchingDomain)
  88. if err != nil {
  89. return err
  90. }
  91. return targetEpt.Remove()
  92. }