package loadbalance import ( "net/http" "net/url" "imuslab.com/zoraxy/mod/dynamicproxy/dpcore" ) // StartProxy create and start a HTTP proxy using dpcore // Example of webProxyEndpoint: https://example.com:443 or http://192.168.1.100:8080 func (u *Upstream) StartProxy(webProxyEndpoint string) error { //Create a new proxy agent for this upstream path, err := url.Parse(webProxyEndpoint) if err != nil { return err } proxy := dpcore.NewDynamicProxyCore(path, "", &dpcore.DpcoreOptions{ IgnoreTLSVerification: u.SkipCertValidations, }) u.proxy = proxy return nil } // IsReady return the proxy ready state of the upstream server // Return false if StartProxy() is not called on this upstream before func (u *Upstream) IsReady() bool { return u.proxy != nil } // ServeHTTP uses this upstream proxy router to route the current request func (u *Upstream) ServeHTTP(w http.ResponseWriter, r *http.Request, rrr *dpcore.ResponseRewriteRuleSet) error { //Auto rewrite to upstream origin if not set if rrr.ProxyDomain == "" { rrr.ProxyDomain = u.OriginIpOrDomain } return u.proxy.ServeHTTP(w, r, rrr) } // String return the string representations of endpoints in this upstream func (u *Upstream) String() string { return u.OriginIpOrDomain }