fix proxy path & add skip auth

This commit is contained in:
DeniskaRediska committed 2019-11-25 15:44:03 +03:00
1 parent 38806190e7
commit 244d8fbdc2
13 files changed
+137 -95

No files matched your search

+22 -7
View File
@@ -22,8 +22,9 @@ const (
type (
Proxy interface {
ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse
Consumer([]structure.AddressConfiguration) bool
SkipAuth() bool
Close()
}
)
@@ -34,15 +35,15 @@ func Init(location conf.Location) (Proxy, error) {
}
switch location.Protocol {
case httpProtocol:
proxy := http.NewProxy()
proxy := http.NewProxy(location.SkipAuth)
store[location.PathPrefix] = proxy
return proxy, nil
case grpcProtocol:
proxy := grpc.NewProxy()
proxy := grpc.NewProxy(location.SkipAuth)
store[location.PathPrefix] = proxy
return proxy, nil
case healthCheckProtocol:
proxy := health_check.NewProxy()
proxy := health_check.NewProxy(location.SkipAuth)
store[location.PathPrefix] = proxy
return proxy, nil
default:
@@ -50,13 +51,13 @@ func Init(location conf.Location) (Proxy, error) {
}
}
func Find(path string) Proxy {
func Find(path string) (Proxy, string) {
for pathPrefix, proxy := range store {
if strings.HasPrefix(path, pathPrefix) {
return proxy
return proxy, getPathWithoutPrefix(path)
}
}
return nil
return nil, ""
}
func Close() {
@@ -64,3 +65,17 @@ func Close() {
p.Close()
}
}
func getPathWithoutPrefix(path string) string {
firstFound := false
for i, value := range path {
if value == '/' {
if firstFound {
return path[i+1:]
} else {
firstFound = true
}
}
}
return ""
}
+21 -15
View File
@@ -16,23 +16,26 @@ import (
)
type grpcProxy struct {
client *backend.RxGrpcClient
client *backend.RxGrpcClient
skipAuth bool
}
func NewProxy() *grpcProxy {
return &grpcProxy{client: backend.NewRxGrpcClient(
backend.WithDialOptions(
grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
),
backend.WithDialingErrorHandler(func(err error) {
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
}),
)}
func NewProxy(skiAuth bool) *grpcProxy {
return &grpcProxy{
client: backend.NewRxGrpcClient(
backend.WithDialOptions(
grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
),
backend.WithDialingErrorHandler(func(err error) {
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
})),
skipAuth: skiAuth,
}
}
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
if p.client.InternalGrpcClient == nil {
msg := "client undefined"
log.Error(log_code.ErrorClientGrpc, msg)
@@ -43,14 +46,17 @@ func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
SetError(errors.New(msg))
}
uri := string(ctx.RequestURI())
return handlers.Handler.Get(ctx).Complete(ctx, uri, p.client)
return handlers.Handler.Get(ctx).Complete(ctx, path, p.client)
}
func (p *grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
return p.client.ReceiveAddressList(addr)
}
func (p *grpcProxy) SkipAuth() bool {
return p.skipAuth
}
func (p *grpcProxy) Close() {
p.client.Close()
}
+1 -1
View File
@@ -128,7 +128,7 @@ func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Dur
}
func makeMetadata(r *fasthttp.RequestHeader, method string) (metadata.MD, string) {
method = strings.TrimPrefix(method, "/api/")
//method = strings.TrimPrefix(method, "/api/")
md := metadata.Pairs(utils.ProxyMethodNameHeader, method)
r.VisitAll(func(key, v []byte) {
lowerHeader := strings.ToLower(string(key))
-7
View File
@@ -19,13 +19,7 @@ var handleJson handleJsonDesc
type handleJsonDesc struct{}
func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) domain.ProxyResponse {
//body, err := utils.ReadJsonBody(c)
body := c.Request.Body()
/*if err != nil {
streaming.LogError(log_code.TypeData.JsonContent, method, err)
streaming.SendError(err.Error(), codes.InvalidArgument, []interface{}{err.Error()}, c)
return
}*/
md, methodName := makeMetadata(&c.Request.Header, method)
ctx := metadata.NewOutgoingContext(context.Background(), md)
@@ -40,7 +34,6 @@ func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *
return domain.Create().SetError(err)
}
//structBody := u.ConvertInterfaceToGrpcStruct(body)
resp, invokerErr := cli.Request(
ctx,
&isp.Message{
+10 -3
View File
@@ -7,20 +7,27 @@ import (
)
type healthCheckProxy struct {
skipAuth bool
}
func NewProxy() *healthCheckProxy {
return &healthCheckProxy{}
func NewProxy(skipAuth bool) *healthCheckProxy {
return &healthCheckProxy{skipAuth: skipAuth}
}
func (p *healthCheckProxy) Consumer(addressList []structure.AddressConfiguration) bool {
return true
}
func (p *healthCheckProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
func (p *healthCheckProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
ctx.Response.SetBody(ctx.Request.Body())
ctx.Request.SetRequestURI(path)
return domain.Create()
}
func (p *healthCheckProxy) SkipAuth() bool {
return p.skipAuth
}
func (p *healthCheckProxy) Close() {
}
+10 -4
View File
@@ -14,11 +14,12 @@ import (
)
type httpProxy struct {
client *fasthttp.HostClient
client *fasthttp.HostClient
skipAuth bool
}
func NewProxy() *httpProxy {
return &httpProxy{client: nil}
func NewProxy(skipAuth bool) *httpProxy {
return &httpProxy{client: nil, skipAuth: skipAuth}
}
func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool {
@@ -33,7 +34,7 @@ func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool
return true
}
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
if p.client == nil {
msg := "client undefined"
log.Error(log_code.ErrorClientHttp, msg)
@@ -46,6 +47,7 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
req := &ctx.Request
res := &ctx.Response
req.SetRequestURI(path)
if addr, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil {
req.Header.Add("X-Forwarded-For", addr)
@@ -61,6 +63,10 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
SetError(err)
}
func (p *httpProxy) SkipAuth() bool {
return p.skipAuth
}
func (p *httpProxy) Close() {
p.client = nil
}