Merge pull request #19 from keeshux/parse-compression-framing-from-push-reply

Parse compression framing from PUSH_REPLY
This commit is contained in:
Davide De Rosa 2018-09-07 15:25:30 +02:00 committed by GitHub
commit aeccabbb6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View File

@ -147,6 +147,9 @@ public protocol SessionReply {
/// The DNS servers set up for this session. /// The DNS servers set up for this session.
var dnsServers: [String] { get } var dnsServers: [String] { get }
/// The optional compression framing.
var compressionFraming: SessionProxy.CompressionFraming? { get }
/// The optional authentication token. /// The optional authentication token.
var authToken: String? { get } var authToken: String? { get }
@ -186,12 +189,14 @@ extension SessionProxy {
private static let dnsRegexp = try! NSRegularExpression(pattern: "dhcp-option DNS6? [\\d\\.a-fA-F:]+", options: []) private static let dnsRegexp = try! NSRegularExpression(pattern: "dhcp-option DNS6? [\\d\\.a-fA-F:]+", options: [])
private static let compRegexp = try! NSRegularExpression(pattern: "comp(ress|-lzo)", options: [])
private static let authTokenRegexp = try! NSRegularExpression(pattern: "auth-token [a-zA-Z0-9/=+]+", options: []) private static let authTokenRegexp = try! NSRegularExpression(pattern: "auth-token [a-zA-Z0-9/=+]+", options: [])
private static let peerIdRegexp = try! NSRegularExpression(pattern: "peer-id [0-9]+", options: []) private static let peerIdRegexp = try! NSRegularExpression(pattern: "peer-id [0-9]+", options: [])
private static let cipherRegexp = try! NSRegularExpression(pattern: "cipher [^,\\s]+", options: []) private static let cipherRegexp = try! NSRegularExpression(pattern: "cipher [^,\\s]+", options: [])
private let original: String private let original: String
let ipv4: IPv4Settings? let ipv4: IPv4Settings?
@ -200,6 +205,8 @@ extension SessionProxy {
let dnsServers: [String] let dnsServers: [String]
let compressionFraming: SessionProxy.CompressionFraming?
let authToken: String? let authToken: String?
let peerId: UInt32? let peerId: UInt32?
@ -224,6 +231,7 @@ extension SessionProxy {
var optIfconfig6Arguments: [String]? var optIfconfig6Arguments: [String]?
var dnsServers: [String] = [] var dnsServers: [String] = []
var compressionFraming: SessionProxy.CompressionFraming?
var authToken: String? var authToken: String?
var peerId: UInt32? var peerId: UInt32?
var cipher: SessionProxy.Cipher? var cipher: SessionProxy.Cipher?
@ -365,6 +373,21 @@ extension SessionProxy {
dnsServers.append($0[1]) dnsServers.append($0[1])
} }
// MARK: Compression
PushReply.compRegexp.enumerateComponents(in: message) {
switch $0[0] {
case "comp-lzo":
compressionFraming = .compLZO
case "compress":
compressionFraming = .compress
default:
break
}
}
// MARK: Authentication // MARK: Authentication
PushReply.authTokenRegexp.enumerateArguments(in: message) { PushReply.authTokenRegexp.enumerateArguments(in: message) {
@ -382,6 +405,7 @@ extension SessionProxy {
} }
self.dnsServers = dnsServers self.dnsServers = dnsServers
self.compressionFraming = compressionFraming
self.authToken = authToken self.authToken = authToken
self.peerId = peerId self.peerId = peerId
self.cipher = cipher self.cipher = cipher
@ -404,13 +428,20 @@ extension SessionProxy {
private extension NSRegularExpression { private extension NSRegularExpression {
func enumerateArguments(in string: String, using block: ([String]) -> Void) { func enumerateArguments(in string: String, using block: ([String]) -> Void) {
enumerateComponents(in: string) { (tokens) in
var args = tokens
args.removeFirst()
block(args)
}
}
func enumerateComponents(in string: String, using block: ([String]) -> Void) {
enumerateMatches(in: string, options: [], range: NSMakeRange(0, string.count)) { (result, flags, stop) in enumerateMatches(in: string, options: [], range: NSMakeRange(0, string.count)) { (result, flags, stop) in
guard let range = result?.range else { guard let range = result?.range else {
return return
} }
let match = (string as NSString).substring(with: range) let match = (string as NSString).substring(with: range)
var tokens = match.components(separatedBy: " ") let tokens = match.components(separatedBy: " ")
tokens.removeFirst()
block(tokens) block(tokens)
} }
} }

View File

@ -1031,6 +1031,10 @@ public class SessionProxy {
log.debug("Set up encryption") log.debug("Set up encryption")
} }
let pushedFraming = pushReply.compressionFraming
if let negFraming = pushedFraming {
log.debug("Negotiated compression framing: \(negFraming.rawValue)")
}
let pushedCipher = pushReply.cipher let pushedCipher = pushReply.cipher
if let negCipher = pushedCipher { if let negCipher = pushedCipher {
log.debug("Negotiated cipher: \(negCipher.rawValue)") log.debug("Negotiated cipher: \(negCipher.rawValue)")
@ -1054,7 +1058,7 @@ public class SessionProxy {
encrypter: bridge.encrypter(), encrypter: bridge.encrypter(),
decrypter: bridge.decrypter(), decrypter: bridge.decrypter(),
peerId: pushReply.peerId ?? PacketPeerIdDisabled, peerId: pushReply.peerId ?? PacketPeerIdDisabled,
compressionFraming: configuration.compressionFraming.native, compressionFraming: (pushedFraming ?? configuration.compressionFraming).native,
maxPackets: link?.packetBufferSize ?? 200, maxPackets: link?.packetBufferSize ?? 200,
usesReplayProtection: CoreConfiguration.usesReplayProtection usesReplayProtection: CoreConfiguration.usesReplayProtection
) )

View File

@ -92,10 +92,18 @@ class PushTests: XCTestCase {
XCTAssertEqual(reply.dnsServers, ["2001:4860:4860::8888", "2001:4860:4860::8844"]) XCTAssertEqual(reply.dnsServers, ["2001:4860:4860::8888", "2001:4860:4860::8844"])
} }
func testNCP() { func testCompressionFraming() {
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-CBC" 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-CBC"
let reply = try! SessionProxy.PushReply(message: msg)! let reply = try! SessionProxy.PushReply(message: msg)!
reply.debug() reply.debug()
XCTAssertEqual(reply.compressionFraming, .compLZO)
}
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)!
reply.debug()
XCTAssertEqual(reply.cipher, .aes256gcm) XCTAssertEqual(reply.cipher, .aes256gcm)
} }