【问题标题】:grpc dart server and client exmple with secure connection具有安全连接的 grpc dart 服务器和客户端示例
【发布时间】:2021-07-10 19:26:37
【问题描述】:

我在 dart 中搜索了 grpc secure 服务器和客户端示例,但找不到任何创建 insecure connection 的示例,但这不是我的正在寻找。

我已经设法安全地编译它,但在客户端与服务器之间连接时出现 grpc 错误

捕获错误:gRPC 错误(代码:14,代号:不可用,消息: 连接错误:HandshakeException:连接期间终止 握手,详细信息:null,rawResponse:null)

我的服务器凭据实现:

final String myPath = 'password_file.pem';
final File f = File(myPath);
final Uint8List bytes = f.readAsBytesSync();

final server = Server(
  [myPbServer()],
  const <Interceptor>[],
  CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
);
await server.serve(
  port: 6053,
  security: ServerTlsCredentials(certificate: bytes),
);

我的客户端凭据实现:

final String myPath = 'password_file.pem';
final File f = File(myPath);
final Uint8List bytes = f.readAsBytesSync();

return channel = ClientChannel(
  deviceIp,
  port: 6053,
  options: ChannelOptions(
    credentials: ChannelCredentials.secure(
      certificates: myPath,
    ),
  ),
);

有人可以用安全的 grpc 服务器和客户端示例来回答吗?

【问题讨论】:

    标签: dart security protocol-buffers grpc credentials


    【解决方案1】:

    test cases of grpc-dart中所写

    服务器:

    final server = Server([YourServerClass()]);
    await server.serve(
      port: portNumber,
      security: ServerTlsCredentials(
        certificate: File('crtFilePath/file.crt').readAsBytesSync(),
        privateKey: File('keycrtFilePath/file.key').readAsBytesSync(),
      ),
    );
    

    客户:

    import 'package:grpc/src/client/http2_connection.dart';
    
    FixedConnectionClientChannel(
      Http2ClientConnection(
        serverAddress,
        portNumber,
        ChannelOptions(
          credentials: ChannelCredentials.secure(
            certificates: File('crtFilePath/file.crt').readAsBytesSync(),
            authority: 'localhost',
          ),
        ),
      ),
    );
    
    
    class FixedConnectionClientChannel extends ClientChannelBase {
      final Http2ClientConnection clientConnection;
      List<ConnectionState> states = <ConnectionState>[];
      FixedConnectionClientChannel(this.clientConnection) {
        clientConnection.onStateChanged = (c) => states.add(c.state);
      }
      @override
      ClientConnection createConnection() => clientConnection;
    }
    

    【讨论】:

      【解决方案2】:

      不确定这是否会对您有很大帮助,但我的问题上发布了相同的答案:

      class MyChannelCredentials extends ChannelCredentials {
        final Uint8List? certificateChain;
        final Uint8List? privateKey;
      
        MyChannelCredentials({
          Uint8List? trustedRoots,
          this.certificateChain,
          this.privateKey,
          String? authority,
          BadCertificateHandler? onBadCertificate,
        }) : super.secure(
                  certificates: trustedRoots,
                  authority: authority,
                  onBadCertificate: onBadCertificate);
      
        @override
        SecurityContext get securityContext {
          final ctx = super.securityContext;
          if (certificateChain != null) {
            ctx.useCertificateChainBytes(certificateChain);
          }
          if (privateKey != null) {
            ctx.usePrivateKeyBytes(privateKey);
          }
          return ctx;
        }
      }
      
      final cred = MyChannelCredentials(
        trustedRoots: File('pems/ca-cert.pem').readAsBytesSync(),
        certificateChain: File('pems/client-cert.pem').readAsBytesSync(),
        privateKey: File('pems/client-key.pem').readAsBytesSync(),
        authority: 'localhost',
      );
      

      【讨论】:

        猜你喜欢
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 1970-01-01
        • 2017-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多