router.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if len(endpoint.ActiveOrigins) == 0 {
  62. //There are no active origins. No need to check for ready
  63. router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
  64. return nil
  65. }
  66. if !router.loadBalancer.UpstreamsReady(endpoint.ActiveOrigins) {
  67. //This endpoint is not prepared
  68. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  69. }
  70. // Push record into running subdomain endpoints
  71. router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
  72. return nil
  73. }
  74. // Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
  75. func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
  76. if !router.loadBalancer.UpstreamsReady(endpoint.ActiveOrigins) {
  77. //This endpoint is not prepared
  78. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  79. }
  80. // Push record into running root endpoints
  81. router.Root = endpoint
  82. return nil
  83. }
  84. // ProxyEndpoint remove provide global access by key
  85. func (router *Router) RemoveProxyEndpointByRootname(rootnameOrMatchingDomain string) error {
  86. targetEpt, err := router.LoadProxy(rootnameOrMatchingDomain)
  87. if err != nil {
  88. return err
  89. }
  90. return targetEpt.Remove()
  91. }