router.go 3.7 KB

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