具有安全连接的 grpc dart 服务器和客户端示例
grpc dart server and client exmple with secure connection
我在 dart 中搜索了 grpc secure server and client example 我找不到,创建 insecure connection 的例子可以找到了,但这不是我要找的。
我已经设法安全地编译它,但在客户端与服务器之间连接时出现 grpc 错误
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 服务器和客户端示例来回答吗?
中所写
服务器:
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;
}
不确定这是否对您有很大帮助,但我的问题有相同的答案:
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',
);
我在 dart 中搜索了 grpc secure server and client example 我找不到,创建 insecure connection 的例子可以找到了,但这不是我要找的。
我已经设法安全地编译它,但在客户端与服务器之间连接时出现 grpc 错误
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 服务器和客户端示例来回答吗?
服务器:
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;
}
不确定这是否对您有很大帮助,但我的问题有相同的答案:
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',
);