From 3f9d25c2174d973370af95f700cbba2e2e1f4468 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Mon, 30 May 2022 17:28:32 +0300 Subject: [PATCH] MSP-625 reimplement http proxy --- proxy/http.go | 59 ++++++++++++++++++++++++++++++ proxy/http/client.go | 86 -------------------------------------------- 2 files changed, 59 insertions(+), 86 deletions(-) create mode 100644 proxy/http.go delete mode 100644 proxy/http/client.go diff --git a/proxy/http.go b/proxy/http.go new file mode 100644 index 0000000..39e4d96 --- /dev/null +++ b/proxy/http.go @@ -0,0 +1,59 @@ +package proxy + +import ( + "fmt" + "net/http" + "net/http/httputil" + "net/url" + "strconv" + + "github.com/integration-system/isp-kit/grpc" + "github.com/pkg/errors" + "isp-gate-service/domain" + "isp-gate-service/middleware" +) + +type HttpHostManager interface { + Next() (string, error) +} + +type Http struct { + hostManager HttpHostManager +} + +func NewHttp(hostManager HttpHostManager) Http { + return Http{ + hostManager: hostManager, + } +} + +func (p Http) Handle(ctx *middleware.Context) error { + host, err := p.hostManager.Next() + if err != nil { + return errors.WithMessage(err, "next host") + } + + rawUrl := fmt.Sprintf("http://%s", host) // secure HTTP links are reset connection + target, err := url.Parse(rawUrl) + if err != nil { + return errors.WithMessage(err, "parse url") + } + + ctx.Request.URL.Path = ctx.Path + ctx.Request.Header.Set(grpc.ApplicationIdHeader, strconv.Itoa(ctx.AppId)) + ctx.Request.Header.Set(adminIdHeader, strconv.Itoa(ctx.AdminId)) + reverseProxy := httputil.NewSingleHostReverseProxy(target) + + var resultError error + reverseProxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) { + resultError = errors.WithMessage(err, "serve http") + + ctx.ResponseWriter.WriteHeader(http.StatusServiceUnavailable) + _, err = ctx.ResponseWriter.Write([]byte(domain.ServiceIsNotAvailableErrorMessage)) + if err != nil { + resultError = errors.WithMessagef(resultError, "write error: %v", err) + } + } + reverseProxy.ServeHTTP(ctx.ResponseWriter, ctx.Request) + return resultError +} diff --git a/proxy/http/client.go b/proxy/http/client.go deleted file mode 100644 index adfe965..0000000 --- a/proxy/http/client.go +++ /dev/null @@ -1,86 +0,0 @@ -package http - -import ( - "errors" - "net" - "strings" - - "github.com/integration-system/isp-lib/v2/structure" - log "github.com/integration-system/isp-log" - "github.com/valyala/fasthttp" - "google.golang.org/grpc/codes" - "isp-gate-service/domain" - "isp-gate-service/log_code" - "isp-gate-service/utils" -) - -var ( - errNotInitialized = errors.New("http proxy not initialized") -) - -type httpProxy struct { - client *fasthttp.HostClient - skipAuth bool - skipExistCheck bool -} - -func NewProxy(skipAuth, skipExistCheck bool) *httpProxy { - return &httpProxy{client: nil, skipAuth: skipAuth, skipExistCheck: skipExistCheck} -} - -func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool { - if len(addressList) == 0 { - p.client = nil - return true - } - addresses := make([]string, len(addressList)) - for key, addr := range addressList { - addresses[key] = addr.GetAddress() - } - - p.client = &fasthttp.HostClient{ - Addr: strings.Join(addresses, `,`), - } - return true -} - -func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse { - client := p.client - if client == nil { - log.Error(log_code.ErrorClientHttp, errNotInitialized) - utils.WriteError(ctx, errNotInitialized.Error(), codes.Internal, nil) - return domain.Create(). - SetRequestBody(ctx.Request.Body()). - SetResponseBody(ctx.Response.Body()). - SetError(errNotInitialized) - } - - req := &ctx.Request - res := &ctx.Response - req.URI().SetPath("/" + path) - - if addr, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil { - req.Header.Add("X-Forwarded-For", addr) - } - - err := client.Do(req, res) - if err != nil { - log.Error(log_code.ErrorClientHttp, err) - } - return domain.Create(). - SetRequestBody(ctx.Request.Body()). - SetResponseBody(ctx.Response.Body()). - SetError(err) -} - -func (p *httpProxy) SkipAuth() bool { - return p.skipAuth -} - -func (p *httpProxy) SkipExistCheck() bool { - return p.skipExistCheck -} - -func (p *httpProxy) Close() { - p.client = nil -}