Просмотр исходного кода

Extract servername from tls connection state.

Mariano Cano 7 лет назад
Родитель
Сommit
64cbac4e81
1 измененных файлов с 12 добавлено и 2 удалено
  1. 12 2
      autocert/examples/hello-mtls/go-grpc/server/server.go

+ 12 - 2
autocert/examples/hello-mtls/go-grpc/server/server.go

@@ -14,6 +14,7 @@ import (
 
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/credentials"
+	"google.golang.org/grpc/peer"
 
 	"github.com/smallstep/certificates/autocert/examples/hello-mtls/go-grpc/hello"
 )
@@ -72,12 +73,21 @@ type Greeter struct{}
 
 // SayHello sends a greeting
 func (g *Greeter) SayHello(ctx context.Context, in *hello.HelloRequest) (*hello.HelloReply, error) {
-	return &hello.HelloReply{Message: "Hello " + in.Name}, nil
+	return &hello.HelloReply{Message: "Hello " + in.Name + " (" + getServerName(ctx) + ")"}, nil
 }
 
 // SayHelloAgain sends another greeting
 func (g *Greeter) SayHelloAgain(ctx context.Context, in *hello.HelloRequest) (*hello.HelloReply, error) {
-	return &hello.HelloReply{Message: "Hello again " + in.Name}, nil
+	return &hello.HelloReply{Message: "Hello again " + in.Name + " (" + getServerName(ctx) + ")"}, nil
+}
+
+func getServerName(ctx context.Context) string {
+	if p, ok := peer.FromContext(ctx); ok {
+		if tlsInfo, ok := p.AuthInfo.(credentials.TLSInfo); ok {
+			return tlsInfo.State.ServerName
+		}
+	}
+	return "unknown"
 }
 
 func main() {