upstream.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package loadbalance
  2. import (
  3. "net/http"
  4. "net/url"
  5. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  6. )
  7. // StartProxy create and start a HTTP proxy using dpcore
  8. // Example of webProxyEndpoint: https://example.com:443 or http://192.168.1.100:8080
  9. func (u *Upstream) StartProxy(webProxyEndpoint string) error {
  10. //Create a new proxy agent for this upstream
  11. path, err := url.Parse(webProxyEndpoint)
  12. if err != nil {
  13. return err
  14. }
  15. proxy := dpcore.NewDynamicProxyCore(path, "", &dpcore.DpcoreOptions{
  16. IgnoreTLSVerification: u.SkipCertValidations,
  17. })
  18. u.proxy = proxy
  19. return nil
  20. }
  21. // IsReady return the proxy ready state of the upstream server
  22. // Return false if StartProxy() is not called on this upstream before
  23. func (u *Upstream) IsReady() bool {
  24. return u.proxy != nil
  25. }
  26. // ServeHTTP uses this upstream proxy router to route the current request
  27. func (u *Upstream) ServeHTTP(w http.ResponseWriter, r *http.Request, rrr *dpcore.ResponseRewriteRuleSet) error {
  28. //Auto rewrite to upstream origin if not set
  29. if rrr.ProxyDomain == "" {
  30. rrr.ProxyDomain = u.OriginIpOrDomain
  31. }
  32. return u.proxy.ServeHTTP(w, r, rrr)
  33. }
  34. // String return the string representations of endpoints in this upstream
  35. func (u *Upstream) String() string {
  36. return u.OriginIpOrDomain
  37. }