router.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 len(domain) == 0 {
  18. return nil, errors.New("invalid endpoint config")
  19. }
  20. if domain[len(domain)-1:] == "/" {
  21. domain = domain[:len(domain)-1]
  22. }
  23. endpoint.Domain = domain
  24. //Parse the web proxy endpoint
  25. webProxyEndpoint := domain
  26. if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
  27. //TLS is not hardcoded in proxy target domain
  28. if endpoint.RequireTLS {
  29. webProxyEndpoint = "https://" + webProxyEndpoint
  30. } else {
  31. webProxyEndpoint = "http://" + webProxyEndpoint
  32. }
  33. }
  34. //Create a new proxy agent for this root
  35. path, err := url.Parse(webProxyEndpoint)
  36. if err != nil {
  37. return nil, err
  38. }
  39. //Create the proxy routing handler
  40. proxy := dpcore.NewDynamicProxyCore(path, "", &dpcore.DpcoreOptions{
  41. IgnoreTLSVerification: endpoint.SkipCertValidations,
  42. })
  43. endpoint.proxy = proxy
  44. endpoint.parent = router
  45. //Prepare proxy routing hjandler for each of the virtual directories
  46. for _, vdir := range endpoint.VirtualDirectories {
  47. domain := vdir.Domain
  48. if len(domain) == 0 {
  49. //invalid vdir
  50. continue
  51. }
  52. if domain[len(domain)-1:] == "/" {
  53. domain = domain[:len(domain)-1]
  54. }
  55. //Parse the web proxy endpoint
  56. webProxyEndpoint = domain
  57. if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
  58. //TLS is not hardcoded in proxy target domain
  59. if vdir.RequireTLS {
  60. webProxyEndpoint = "https://" + webProxyEndpoint
  61. } else {
  62. webProxyEndpoint = "http://" + webProxyEndpoint
  63. }
  64. }
  65. path, err := url.Parse(webProxyEndpoint)
  66. if err != nil {
  67. return nil, err
  68. }
  69. proxy := dpcore.NewDynamicProxyCore(path, vdir.MatchingPath, &dpcore.DpcoreOptions{
  70. IgnoreTLSVerification: vdir.SkipCertValidations,
  71. })
  72. vdir.proxy = proxy
  73. vdir.parent = endpoint
  74. }
  75. return endpoint, nil
  76. }
  77. // Add Proxy Route to current runtime. Call to PrepareProxyRoute before adding to runtime
  78. func (router *Router) AddProxyRouteToRuntime(endpoint *ProxyEndpoint) error {
  79. if endpoint.proxy == nil {
  80. //This endpoint is not prepared
  81. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  82. }
  83. // Push record into running subdomain endpoints
  84. router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
  85. return nil
  86. }
  87. // Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
  88. func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
  89. if endpoint.proxy == nil {
  90. //This endpoint is not prepared
  91. return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
  92. }
  93. // Push record into running root endpoints
  94. router.Root = endpoint
  95. return nil
  96. }
  97. // ProxyEndpoint remove provide global access by key
  98. func (router *Router) RemoveProxyEndpointByRootname(rootnameOrMatchingDomain string) error {
  99. targetEpt, err := router.LoadProxy(rootnameOrMatchingDomain)
  100. if err != nil {
  101. return err
  102. }
  103. return targetEpt.Remove()
  104. }