router.go 3.3 KB

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