From fa4d69e5efb9ad657814407b9a9f207dea800ca2 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Wed, 23 Oct 2024 11:18:07 +0800 Subject: [PATCH] update peer test --- Networking/Sources/Networking/PKCS12.swift | 47 ++++++------ Networking/Sources/Networking/Peer.swift | 87 +++++++++++++--------- 2 files changed, 77 insertions(+), 57 deletions(-) diff --git a/Networking/Sources/Networking/PKCS12.swift b/Networking/Sources/Networking/PKCS12.swift index b6f6c74c..a352477f 100644 --- a/Networking/Sources/Networking/PKCS12.swift +++ b/Networking/Sources/Networking/PKCS12.swift @@ -21,30 +21,31 @@ public func parseCertificate(data: Data, type: CertificateType) throws -> ( var errorMessage: UnsafeMutablePointer? defer { free(altNamePointer) } - let result: Int32 = switch type { - case .x509: - data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in - parse_certificate( - bytes.baseAddress!.assumingMemoryBound(to: UInt8.self), - data.count, - &publicKeyPointer, - &publicKeyLen, - &altNamePointer, - &errorMessage - ) - } - case .p12: - data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in - parse_pkcs12_certificate( - bytes.baseAddress!.assumingMemoryBound(to: UInt8.self), - data.count, - &publicKeyPointer, - &publicKeyLen, - &altNamePointer, - &errorMessage - ) + let result: Int32 = + switch type { + case .x509: + data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in + parse_certificate( + bytes.baseAddress!.assumingMemoryBound(to: UInt8.self), + data.count, + &publicKeyPointer, + &publicKeyLen, + &altNamePointer, + &errorMessage + ) + } + case .p12: + data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in + parse_pkcs12_certificate( + bytes.baseAddress!.assumingMemoryBound(to: UInt8.self), + data.count, + &publicKeyPointer, + &publicKeyLen, + &altNamePointer, + &errorMessage + ) + } } - } guard result == 0 else { throw CryptoError.parseFailed(String(cString: errorMessage!)) diff --git a/Networking/Sources/Networking/Peer.swift b/Networking/Sources/Networking/Peer.swift index b3f5e072..dc529615 100644 --- a/Networking/Sources/Networking/Peer.swift +++ b/Networking/Sources/Networking/Peer.swift @@ -73,12 +73,14 @@ public final class Peer: Sendable { let registration = try QuicRegistration() let serverConfiguration = try QuicConfiguration( - registration: registration, pkcs12: pkcs12, alpns: allAlpns, client: false, settings: options.serverSettings + registration: registration, pkcs12: pkcs12, alpns: allAlpns, client: false, + settings: options.serverSettings ) let clientAlpn = alpns[options.mode]! let clientConfiguration = try QuicConfiguration( - registration: registration, pkcs12: pkcs12, alpns: [clientAlpn], client: true, settings: options.clientSettings + registration: registration, pkcs12: pkcs12, alpns: [clientAlpn], client: true, + settings: options.clientSettings ) impl = PeerImpl( @@ -104,26 +106,27 @@ public final class Peer: Sendable { let conn = impl.connections.read { connections in connections.byType[mode]?[address] } - return try conn ?? impl.connections.write { connections in - let curr = connections.byType[mode, default: [:]][address] - if let curr { - return curr + return try conn + ?? impl.connections.write { connections in + let curr = connections.byType[mode, default: [:]][address] + if let curr { + return curr + } + let conn = try Connection( + QuicConnection( + handler: PeerEventHandler(self.impl), + registration: self.impl.clientConfiguration.registration, + configuration: self.impl.clientConfiguration + ), + impl: self.impl, + mode: mode, + remoteAddress: address, + initiatedByLocal: true + ) + connections.byType[mode, default: [:]][address] = conn + connections.byId[conn.id] = conn + return conn } - let conn = try Connection( - QuicConnection( - handler: PeerEventHandler(self.impl), - registration: self.impl.clientConfiguration.registration, - configuration: self.impl.clientConfiguration - ), - impl: self.impl, - mode: mode, - remoteAddress: address, - initiatedByLocal: true - ) - connections.byType[mode, default: [:]][address] = conn - connections.byId[conn.id] = conn - return conn - } } public func broadcast(kind: Handler.PresistentHandler.StreamKind, message: any MessageProtocol) { @@ -142,11 +145,14 @@ public final class Peer: Sendable { case .success: break case let .failure(error): - impl.logger.warning("Failed to send message", metadata: [ - "connectionId": "\(connection.id)", - "kind": "\(kind)", - "error": "\(error)", - ]) + impl.logger.warning( + "Failed to send message", + metadata: [ + "connectionId": "\(connection.id)", + "kind": "\(kind)", + "error": "\(error)", + ] + ) } } } @@ -245,11 +251,15 @@ private struct PeerEventHandler: QuicEventHandler { self.impl = impl } - func newConnection(_: QuicListener, connection: QuicConnection, info: ConnectionInfo) -> QuicStatus { + func newConnection(_: QuicListener, connection: QuicConnection, info: ConnectionInfo) + -> QuicStatus + { let addr = info.remoteAddress let mode = impl.alpnLookup[info.negotiatedAlpn] guard let mode else { - logger.warning("unknown alpn: \(String(data: info.negotiatedAlpn, encoding: .utf8) ?? info.negotiatedAlpn.toDebugHexString())") + logger.warning( + "unknown alpn: \(String(data: info.negotiatedAlpn, encoding: .utf8) ?? info.negotiatedAlpn.toDebugHexString())" + ) return .code(.alpnNegFailure) } logger.debug("new connection: \(addr) mode: \(mode)") @@ -266,10 +276,13 @@ private struct PeerEventHandler: QuicEventHandler { return .code(.requiredCert) } do { - let (publicKey, alternativeName) = try parseCertificate(data: certificate,type: .x509) + let (publicKey, alternativeName) = try parseCertificate(data: certificate, type: .x509) logger.debug( "Certificate parsed", - metadata: ["publicKey": "\(publicKey.toHexString())", "alternativeName": "\(alternativeName)"] + metadata: [ + "publicKey": "\(publicKey.toHexString())", + "alternativeName": "\(alternativeName)", + ] ) if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) { return .code(.badCert) @@ -289,7 +302,9 @@ private struct PeerEventHandler: QuicEventHandler { connections.byId[connection.id] } guard let conn else { - logger.warning("Connected but connection is gone?", metadata: ["connectionId": "\(connection.id)"]) + logger.warning( + "Connected but connection is gone?", metadata: ["connectionId": "\(connection.id)"] + ) return } @@ -347,10 +362,14 @@ private struct PeerEventHandler: QuicEventHandler { if let connection { connection.streamClosed(stream: stream, abort: !status.isSucceeded) } else { - logger.warning("Stream closed but connection is gone?", metadata: ["streamId": "\(stream.id)"]) + logger.warning( + "Stream closed but connection is gone?", metadata: ["streamId": "\(stream.id)"] + ) } } else { - logger.warning("Stream closed but stream is gone?", metadata: ["streamId": "\(quicStream.id)"]) + logger.warning( + "Stream closed but stream is gone?", metadata: ["streamId": "\(quicStream.id)"] + ) } } } @@ -385,7 +404,7 @@ public final class MockPeerEventHandler: QuicEventHandler { return .code(.requiredCert) } do { - let (publicKey, alternativeName) = try parseCertificate(data: certificate,type: .x509) + let (publicKey, alternativeName) = try parseCertificate(data: certificate, type: .x509) if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) { return .code(.badCert) }