MSP-625 reimplement http proxy

This commit is contained in:
DeniskaRediska authored and d1slike committed 2022-07-27 10:44:07 +03:00
1 parent 828ac1cd4f
commit 3f9d25c217
2 files changed
+59 -86

No files matched your search

+59
View File
@@ -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
}
-86
View File
@@ -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
}