具有安全连接的 grpc dart 服务器和客户端示例 [英] grpc dart server and client exmple with secure connection

查看:117
本文介绍了具有安全连接的 grpc dart 服务器和客户端示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dart 中搜索了 grpc secure 服务器和客户端示例我找不到任何,可以找到创建 不安全连接 的示例,但这不是我想要的正在寻找.

I have searched for grpc secure server and client example in dart I couldn't find any, example of creating insecure connection can be found but this is not what I am looking for.

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

I have managed to compile it with secure but getting grpc error when connecting between client to server

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

Caught error: gRPC Error (code: 14, codeName: UNAVAILABLE, message: Error connecting: HandshakeException: Connection terminated during handshake, details: 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 服务器和客户端示例吗?.

Can someone answer with working secure grpc server and client example?.

推荐答案

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;
}

这篇关于具有安全连接的 grpc dart 服务器和客户端示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆