Parse pushed cipher if any

This commit is contained in:
Davide De Rosa 2018-09-02 01:25:27 +02:00
parent e900454504
commit cff359fceb
2 changed files with 26 additions and 0 deletions

View File

@ -146,6 +146,12 @@ public protocol SessionReply {
/// The DNS servers set up for this session.
var dnsServers: [String] { get }
/// The optional 24-bit peer-id.
var peerId: UInt32? { get }
/// The negotiated cipher if any (NCP).
var cipher: SessionProxy.Cipher? { get }
}
extension SessionProxy {
@ -179,6 +185,8 @@ extension SessionProxy {
private static let peerIdRegexp = try! NSRegularExpression(pattern: "peer-id [0-9]+", options: [])
private static let cipherRegexp = try! NSRegularExpression(pattern: "cipher [^\\s]+", options: [])
let ipv4: IPv4Settings?
let ipv6: IPv6Settings?
@ -189,6 +197,8 @@ extension SessionProxy {
let peerId: UInt32?
let cipher: SessionProxy.Cipher?
init?(message: String) throws {
guard message.hasPrefix("PUSH_REPLY") else {
return nil
@ -207,6 +217,7 @@ extension SessionProxy {
var dnsServers: [String] = []
var authToken: String?
var peerId: UInt32?
var cipher: SessionProxy.Cipher?
// MARK: Routing (IPv4)
@ -354,10 +365,17 @@ extension SessionProxy {
PushReply.peerIdRegexp.enumerateArguments(in: message) {
peerId = UInt32($0[0])
}
// MARK: NCP
PushReply.cipherRegexp.enumerateArguments(in: message) {
cipher = SessionProxy.Cipher(rawValue: $0[0].uppercased())
}
self.dnsServers = dnsServers
self.authToken = authToken
self.peerId = peerId
self.cipher = cipher
}
}
}

View File

@ -91,4 +91,12 @@ class PushTests: XCTestCase {
XCTAssertEqual(reply.ipv6?.defaultGateway, "fe80::601:30ff:feb7:dc02")
XCTAssertEqual(reply.dnsServers, ["2001:4860:4860::8888", "2001:4860:4860::8844"])
}
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-CBC"
let reply = try! SessionProxy.PushReply(message: msg)!
reply.debug()
XCTAssertEqual(reply.cipher, .aes256cbc)
}
}