简体   繁体   中英

gRPC in CPP providing TLS support

Any examples of gRPC server using TLS in CPP??

I am trying to build a gRPC application. The server should provide TLS support if client wants to connect over TLS instead of TCP.

This is my server

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());

Error: undefined reference to grpc::SslServerCredetials(grpc::ssl_opts) I have included all the necessary files..

You code looks right. If you are adapting from examples/cpp/helloworld , you need to change -lgrpc++_unsecure to -lgrpc++ in the Makefile.

For the benefits of others, an example of using the tls/ssl code can be found at https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM