Parse compression framing from PUSH_REPLY

This commit is contained in:
Davide De Rosa 2018-09-07 15:07:47 +02:00
parent 7898b940a1
commit 0304c4a5eb
1 changed files with 34 additions and 3 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,6 +189,8 @@ 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: [])
@ -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:
compressionFraming = .disabled
}
}
// 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)
} }
} }