From 367e8b7e087e636bd3e21538bfa1b977ce7a7876 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 23 Feb 2019 19:54:38 +0100 Subject: [PATCH] Track whether server pushed a compression option --- .../Sources/Core/SessionProxy+PushReply.swift | 13 ++++++-- TunnelKitTests/PushTests.swift | 32 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/TunnelKit/Sources/Core/SessionProxy+PushReply.swift b/TunnelKit/Sources/Core/SessionProxy+PushReply.swift index ca6d224..63929f2 100644 --- a/TunnelKit/Sources/Core/SessionProxy+PushReply.swift +++ b/TunnelKit/Sources/Core/SessionProxy+PushReply.swift @@ -150,6 +150,9 @@ public protocol SessionReply { /// The optional compression framing. var compressionFraming: SessionProxy.CompressionFraming? { get } + /// True if uses compression. + var usesCompression: Bool { get } + /// The optional keep-alive interval. var ping: Int? { get } @@ -193,7 +196,7 @@ extension SessionProxy { static let dns = NSRegularExpression("dhcp-option DNS6? [\\d\\.a-fA-F:]+") - static let comp = NSRegularExpression("comp(ress|-lzo)") + static let comp = NSRegularExpression("comp(ress|-lzo)[ \\w]*") static let ping = NSRegularExpression("ping \\d+") @@ -214,6 +217,8 @@ extension SessionProxy { let compressionFraming: SessionProxy.CompressionFraming? + let usesCompression: Bool + let ping: Int? let authToken: String? @@ -241,6 +246,7 @@ extension SessionProxy { var dnsServers: [String] = [] var compressionFraming: SessionProxy.CompressionFraming? + var usesCompression = false var ping: Int? var authToken: String? var peerId: UInt32? @@ -389,10 +395,12 @@ extension SessionProxy { switch $0[0] { case "comp-lzo": compressionFraming = .compLZO + usesCompression = !(($0.count == 2) && ($0[1] == "no")) case "compress": compressionFraming = .compress - + usesCompression = ($0.count > 1) + default: break } @@ -422,6 +430,7 @@ extension SessionProxy { self.dnsServers = dnsServers self.compressionFraming = compressionFraming + self.usesCompression = usesCompression self.ping = ping self.authToken = authToken self.peerId = peerId diff --git a/TunnelKitTests/PushTests.swift b/TunnelKitTests/PushTests.swift index e60fab2..20d31fa 100644 --- a/TunnelKitTests/PushTests.swift +++ b/TunnelKitTests/PushTests.swift @@ -28,6 +28,8 @@ import XCTest private extension SessionReply { func debug() { + print("Compression framing: \(dnsServers)") + print("Compression: \(usesCompression)") print("IPv4: \(ipv4?.description ?? "none")") print("IPv6: \(ipv6?.description ?? "none")") print("DNS: \(dnsServers)") @@ -100,6 +102,36 @@ class PushTests: XCTestCase { XCTAssertEqual(reply.compressionFraming, .compLZO) } + func testCompression() { + let msg = "PUSH_REPLY,dhcp-option DNS 8.8.8.8,dhcp-option DNS 4.4.4.4,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-CBC" + var reply: SessionReply + + reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo no"))! + reply.debug() + XCTAssertEqual(reply.compressionFraming, .compLZO) + XCTAssertFalse(reply.usesCompression) + + reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo"))! + reply.debug() + XCTAssertEqual(reply.compressionFraming, .compLZO) + XCTAssertTrue(reply.usesCompression) + + reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo yes"))! + reply.debug() + XCTAssertEqual(reply.compressionFraming, .compLZO) + XCTAssertTrue(reply.usesCompression) + + reply = try! SessionProxy.PushReply(message: msg.appending(",compress"))! + reply.debug() + XCTAssertEqual(reply.compressionFraming, .compress) + XCTAssertFalse(reply.usesCompression) + + reply = try! SessionProxy.PushReply(message: msg.appending(",compress lz4"))! + reply.debug() + XCTAssertEqual(reply.compressionFraming, .compress) + XCTAssertTrue(reply.usesCompression) + } + func testNCP() { let msg = "PUSH_REPLY,dhcp-option DNS 8.8.8.8,dhcp-option DNS 4.4.4.4,comp-lzo no,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM" let reply = try! SessionProxy.PushReply(message: msg)!