1 Star 0 Fork 0

Wheat / grpc-proxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
director.go 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
bandl 提交于 2021-10-13 13:56 . fix(server): fix server
package extras
import (
"fmt"
"github.com/mwitkow/grpc-proxy/proxy"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"strings"
)
func GetDirector(config Config) func(context.Context, string) (context.Context, *grpc.ClientConn, error) {
credentialsCache := make(map[string]credentials.TransportCredentials)
return func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
for _, backend := range config.Backends {
if strings.HasPrefix(fullMethodName, backend.Filter) {
if config.Verbose {
fmt.Printf("Found: %s > %s \n", fullMethodName, backend.Backend)
}
if backend.CertFile == "" {
cli, err := grpc.DialContext(ctx, backend.Backend, grpc.WithCodec(proxy.Codec()), grpc.WithInsecure())
return ctx, cli, err
}
creds := GetCredentials(credentialsCache, backend)
if creds != nil {
cli, err := grpc.DialContext(ctx, backend.Backend, grpc.WithCodec(proxy.Codec()),
grpc.WithTransportCredentials(creds))
return ctx, cli, err
}
grpclog.Fatalf("Failed to create TLS credentials")
return ctx, nil, grpc.Errorf(codes.FailedPrecondition, "Backend TLS is not configured properly in grpc-proxy")
}
}
if config.Verbose {
fmt.Println("Not found: ", fullMethodName)
}
return ctx, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
}
}
func GetCredentials(cache map[string]credentials.TransportCredentials, backend Backend) credentials.TransportCredentials {
if cache[backend.Backend] != nil {
return cache[backend.Backend]
}
creds, err := credentials.NewClientTLSFromFile(backend.CertFile, backend.ServerName)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
return nil
}
cache[backend.Backend] = creds
return creds
}
1
https://gitee.com/wheat-os/grpc-proxy.git
git@gitee.com:wheat-os/grpc-proxy.git
wheat-os
grpc-proxy
grpc-proxy
62c37cbdac74

搜索帮助