189 lines
5.2 KiB
Go
189 lines
5.2 KiB
Go
// nolint:gochecknoinits
|
|
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"io"
|
|
"maps"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"isp-gate-service/httperrors"
|
|
"isp-gate-service/request"
|
|
|
|
"github.com/txix-open/isp-kit/errors"
|
|
"github.com/txix-open/isp-kit/grpc"
|
|
"github.com/txix-open/isp-kit/grpc/client"
|
|
"github.com/txix-open/isp-kit/grpc/isp"
|
|
"github.com/txix-open/isp-kit/json"
|
|
"github.com/txix-open/isp-kit/requestid"
|
|
_ "google.golang.org/genproto/googleapis/rpc/errdetails"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func init() {
|
|
for httpCode, grpcCode := range codeMap {
|
|
inverseCodeMap[grpcCode] = httpCode
|
|
}
|
|
}
|
|
|
|
var (
|
|
codeMap = map[int]codes.Code{
|
|
http.StatusOK: codes.OK,
|
|
http.StatusRequestTimeout: codes.Canceled,
|
|
http.StatusBadRequest: codes.InvalidArgument,
|
|
http.StatusGatewayTimeout: codes.DeadlineExceeded,
|
|
http.StatusNotFound: codes.NotFound,
|
|
http.StatusConflict: codes.AlreadyExists,
|
|
http.StatusForbidden: codes.PermissionDenied,
|
|
http.StatusUnauthorized: codes.Unauthenticated,
|
|
http.StatusTooManyRequests: codes.ResourceExhausted,
|
|
http.StatusPreconditionFailed: codes.FailedPrecondition,
|
|
http.StatusNotImplemented: codes.Unimplemented,
|
|
http.StatusInternalServerError: codes.Internal,
|
|
http.StatusServiceUnavailable: codes.Unavailable,
|
|
}
|
|
inverseCodeMap = map[codes.Code]int{}
|
|
)
|
|
|
|
type Grpc struct {
|
|
cli *client.Client
|
|
skipAuth bool
|
|
timeout time.Duration
|
|
}
|
|
|
|
func NewGrpc(cli *client.Client, skipAuth bool, timeout time.Duration) Grpc {
|
|
return Grpc{
|
|
cli: cli,
|
|
skipAuth: skipAuth,
|
|
timeout: timeout,
|
|
}
|
|
}
|
|
|
|
func (p Grpc) Handle(ctx *request.Context) error {
|
|
body, err := io.ReadAll(ctx.Request().Body)
|
|
if err != nil {
|
|
return errors.WithMessage(err, "grpc: read body")
|
|
}
|
|
|
|
requestId := requestid.FromContext(ctx.Context())
|
|
md := p.writeMetadata(ctx)
|
|
requestContext := metadata.NewOutgoingContext(ctx.Context(), md)
|
|
|
|
requestContext, cancel := context.WithTimeout(requestContext, p.timeout)
|
|
defer cancel()
|
|
result, err := p.cli.BackendClient().Request(requestContext, &isp.Message{
|
|
Body: &isp.Message_BytesBody{BytesBody: body},
|
|
})
|
|
if err != nil {
|
|
return p.handleError(err, ctx.ResponseWriter(), ctx.EndpointMeta().Endpoint)
|
|
}
|
|
|
|
return p.writeResponse(http.StatusOK, result.GetBytesBody(), requestId, ctx.ResponseWriter())
|
|
}
|
|
|
|
func (p Grpc) handleError(err error, w http.ResponseWriter, endpoint string) error {
|
|
status, ok := status.FromError(err)
|
|
if !ok {
|
|
return httperrors.New(
|
|
http.StatusServiceUnavailable,
|
|
"upstream is not available",
|
|
errors.WithMessage(err, "grpc proxy"),
|
|
)
|
|
}
|
|
|
|
statusCode := p.codeToHttpStatus(status.Code())
|
|
for _, detail := range status.Details() {
|
|
switch typeOfDetail := detail.(type) {
|
|
case *isp.Message:
|
|
switch {
|
|
case typeOfDetail.GetBytesBody() != nil:
|
|
return p.writeResponse(statusCode, typeOfDetail.GetBytesBody(), "", w)
|
|
case typeOfDetail.GetListBody() != nil:
|
|
return p.writeProto(statusCode, typeOfDetail.GetListBody(), w)
|
|
case typeOfDetail.GetStructBody() != nil:
|
|
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), w)
|
|
}
|
|
default:
|
|
return p.writeProto(statusCode, typeOfDetail, w)
|
|
}
|
|
}
|
|
|
|
return httperrors.New(
|
|
statusCode,
|
|
status.Message(),
|
|
errors.WithMessagef(err, "isp-routing-service: proxy '%s'", endpoint),
|
|
)
|
|
}
|
|
|
|
func (p Grpc) writeProto(statusCode int, proto any, w http.ResponseWriter) error {
|
|
data, err := json.Marshal(proto)
|
|
if err != nil {
|
|
return errors.WithMessage(err, "marshal grpc details to json")
|
|
}
|
|
return p.writeResponse(statusCode, data, "", w)
|
|
}
|
|
|
|
func (p Grpc) writeResponse(statusCode int, data []byte, requestId string, w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
if requestId != "" {
|
|
w.Header().Set(requestid.Header, requestId)
|
|
}
|
|
w.WriteHeader(statusCode)
|
|
_, err := w.Write(data)
|
|
if err != nil {
|
|
return errors.WithMessage(err, "response write")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p Grpc) codeToHttpStatus(code codes.Code) int {
|
|
s, ok := inverseCodeMap[code]
|
|
if !ok {
|
|
return http.StatusInternalServerError
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func (p Grpc) writeMetadata(ctx *request.Context) metadata.MD {
|
|
requestId := requestid.FromContext(ctx.Context())
|
|
md := metadata.MD{
|
|
grpc.ProxyMethodNameHeader: {ctx.EndpointMeta().Endpoint},
|
|
requestid.Header: {requestId},
|
|
}
|
|
|
|
if p.skipAuth {
|
|
return md
|
|
}
|
|
|
|
appAuthData, err := ctx.GetAuthData()
|
|
if err == nil {
|
|
md[grpc.ApplicationIdHeader] = []string{strconv.Itoa(appAuthData.ApplicationId)}
|
|
encodedAppName := base64.StdEncoding.EncodeToString([]byte(appAuthData.AppName))
|
|
md[grpc.ApplicationNameHeader] = []string{encodedAppName}
|
|
}
|
|
|
|
userAuthData, err := ctx.GetUserAuthData()
|
|
if err == nil {
|
|
proxyHeaders := maps.Clone(userAuthData.ExtraHeaders)
|
|
proxyHeaders[userAuthData.IdentityHeader] = []string{userAuthData.Identity}
|
|
for header, values := range proxyHeaders {
|
|
header = strings.ToLower(header)
|
|
if strings.HasPrefix(header, "x-") {
|
|
md.Set(header, values...)
|
|
}
|
|
}
|
|
}
|
|
|
|
if ctx.IsAdminAuthenticated() {
|
|
md[xAdminIdHeader] = []string{strconv.Itoa(ctx.AdminId())}
|
|
}
|
|
return md
|
|
}
|