2015-09-26 141 views
3

在CPP中使用TLS的gRPC服务器的任何示例?CPP中的gRPC提供TLS支持

我想构建一个gRPC应用程序。如果客户端想通过TLS而不是TCP连接,服务器应该提供TLS支持。

这是我的服务器

void RunServer() { 
    std::string server_address("0.0.0.0:50051"); 
    GreeterServiceImpl service; 
    ServerBuilder builder; 
    std::shared_ptr<ServerCredentials> creds; 

    if(enable_ssl) 
    { 
      grpc::SslServerCredentialsOptions::PemKeyCertPair pkcp ={"a","b"}; 
      grpc::SslServerCredentialsOptions ssl_opts; 
      ssl_opts.pem_root_certs=""; 
      ssl_opts.pem_key_cert_pairs.push_back(pkcp); 
      creds = grpc::SslServerCredentials(ssl_opts); 
    } 
    else 
      creds=grpc::InsecureServerCredentials(); 
    // Listen on the given address without any authentication mechanism. 
    builder.AddListeningPort(server_address, creds); 
    // Register "service" as the instance through which we'll communicate with 
    // clients. In this case it corresponds to an *synchronous* service. 
    builder.RegisterService(&service); 
    // Finally assemble the server. 
    std::unique_ptr<Server> server(builder.BuildAndStart()); 

错误: 未定义参考GRPC :: SslServerCredetials(GRPC :: ssl_opts) 我已经包括了所有必要的文件..

+2

发现他很有可能意味着加密的东西不是线程本地存储。 –

+0

你!不是关于线程库,而是关于TLS可以使用的方式。 –

回答

1

你的代码看起来正确。如果您要从examples/cpp/helloworld进行适配,则需要在Makefile中将-lgrpc++_unsecure更改为-lgrpc++

对于其他人的利益,使用TLS/SSL代码的例子可以在https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50

+0

lib/libgrpc.so.0.11.0.0:未定义对'EVP_DigestVerifyInit'的引用 lib/libgrpc.so.0.11.0.0:未定义对'SSL_get0_next_proto_negotiated'的引用 /lib/libgrpc.so.0.11.0.0:未定义对' SSL_get_servername' lib/libgrpc.so.0.11.0.0:未定义对'SSL_set_SSL_CTX'的引用 lib/libgrpc.so.0.11.0.0:未定义对'EVP_DigestVerifyFinal'的引用 等等。获取这些未定义的引用,不知道为什么。 –