1
0

upstream.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package loadbalance
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "time"
  9. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  10. )
  11. // StartProxy create and start a HTTP proxy using dpcore
  12. // Example of webProxyEndpoint: https://example.com:443 or http://192.168.1.100:8080
  13. func (u *Upstream) StartProxy() error {
  14. //Filter the tailing slash if any
  15. domain := u.OriginIpOrDomain
  16. if len(domain) == 0 {
  17. return errors.New("invalid endpoint config")
  18. }
  19. if domain[len(domain)-1:] == "/" {
  20. domain = domain[:len(domain)-1]
  21. }
  22. if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
  23. //TLS is not hardcoded in proxy target domain
  24. if u.RequireTLS {
  25. domain = "https://" + domain
  26. } else {
  27. domain = "http://" + domain
  28. }
  29. }
  30. //Create a new proxy agent for this upstream
  31. path, err := url.Parse(domain)
  32. if err != nil {
  33. return err
  34. }
  35. proxy := dpcore.NewDynamicProxyCore(path, "", &dpcore.DpcoreOptions{
  36. IgnoreTLSVerification: u.SkipCertValidations,
  37. FlushInterval: 100 * time.Millisecond,
  38. })
  39. u.proxy = proxy
  40. return nil
  41. }
  42. // IsReady return the proxy ready state of the upstream server
  43. // Return false if StartProxy() is not called on this upstream before
  44. func (u *Upstream) IsReady() bool {
  45. return u.proxy != nil
  46. }
  47. // Clone return a new deep copy object of the identical upstream
  48. func (u *Upstream) Clone() *Upstream {
  49. newUpstream := Upstream{}
  50. js, _ := json.Marshal(u)
  51. json.Unmarshal(js, &newUpstream)
  52. return &newUpstream
  53. }
  54. // ServeHTTP uses this upstream proxy router to route the current request, return the status code and error if any
  55. func (u *Upstream) ServeHTTP(w http.ResponseWriter, r *http.Request, rrr *dpcore.ResponseRewriteRuleSet) (int, error) {
  56. //Auto rewrite to upstream origin if not set
  57. if rrr.ProxyDomain == "" {
  58. rrr.ProxyDomain = u.OriginIpOrDomain
  59. }
  60. return u.proxy.ServeHTTP(w, r, rrr)
  61. }
  62. // String return the string representations of endpoints in this upstream
  63. func (u *Upstream) String() string {
  64. return u.OriginIpOrDomain
  65. }