router.go 3.2 KB

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