123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package dynamicproxy
- import (
- "errors"
- "log"
- "net/url"
- "strings"
- "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
- )
- /*
- Dynamic Proxy Router Functions
- This script handle the proxy rules router spawning
- and preparation
- */
- // Prepare proxy route generate a proxy handler service object for your endpoint
- func (router *Router) PrepareProxyRoute(endpoint *ProxyEndpoint) (*ProxyEndpoint, error) {
- originDomainFilter := func(domain string) (string, error) {
- //Filter the tailing slash if any
- if len(domain) == 0 {
- return "", errors.New("invalid endpoint config")
- }
- if domain[len(domain)-1:] == "/" {
- domain = domain[:len(domain)-1]
- }
- return domain, nil
- }
- for _, thisOrigin := range endpoint.Origins {
- //Parse the web proxy endpoint
- webProxyEndpoint, err := originDomainFilter(thisOrigin.OriginIpOrDomain)
- if err != nil {
- log.Println("Unable to setup upstream " + thisOrigin.OriginIpOrDomain + ": " + err.Error())
- continue
- }
- if !strings.HasPrefix("http://", webProxyEndpoint) && !strings.HasPrefix("https://", webProxyEndpoint) {
- //TLS is not hardcoded in proxy target domain
- if thisOrigin.RequireTLS {
- webProxyEndpoint = "https://" + webProxyEndpoint
- } else {
- webProxyEndpoint = "http://" + webProxyEndpoint
- }
- }
- //Create the proxy routing handler
- err = thisOrigin.StartProxy(webProxyEndpoint)
- if err != nil {
- log.Println("Unable to setup upstream " + thisOrigin.OriginIpOrDomain + ": " + err.Error())
- continue
- }
- }
- endpoint.parent = router
- //Prepare proxy routing handler for each of the virtual directories
- for _, vdir := range endpoint.VirtualDirectories {
- domain := vdir.Domain
- if len(domain) == 0 {
- //invalid vdir
- continue
- }
- if domain[len(domain)-1:] == "/" {
- domain = domain[:len(domain)-1]
- }
- //Parse the web proxy endpoint
- webProxyEndpoint := domain
- if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
- //TLS is not hardcoded in proxy target domain
- if vdir.RequireTLS {
- webProxyEndpoint = "https://" + webProxyEndpoint
- } else {
- webProxyEndpoint = "http://" + webProxyEndpoint
- }
- }
- path, err := url.Parse(webProxyEndpoint)
- if err != nil {
- return nil, err
- }
- proxy := dpcore.NewDynamicProxyCore(path, vdir.MatchingPath, &dpcore.DpcoreOptions{
- IgnoreTLSVerification: vdir.SkipCertValidations,
- })
- vdir.proxy = proxy
- vdir.parent = endpoint
- }
- return endpoint, nil
- }
- // Add Proxy Route to current runtime. Call to PrepareProxyRoute before adding to runtime
- func (router *Router) AddProxyRouteToRuntime(endpoint *ProxyEndpoint) error {
- if !router.loadBalancer.UpstreamsReady(endpoint.Origins) {
- //This endpoint is not prepared
- return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
- }
- // Push record into running subdomain endpoints
- router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
- return nil
- }
- // Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
- func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
- if !router.loadBalancer.UpstreamsReady(endpoint.Origins) {
- //This endpoint is not prepared
- return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
- }
- // Push record into running root endpoints
- router.Root = endpoint
- return nil
- }
- // ProxyEndpoint remove provide global access by key
- func (router *Router) RemoveProxyEndpointByRootname(rootnameOrMatchingDomain string) error {
- targetEpt, err := router.LoadProxy(rootnameOrMatchingDomain)
- if err != nil {
- return err
- }
- return targetEpt.Remove()
- }
|