Parse arguments from regexp extension

Further code simplification.
This commit is contained in:
Davide De Rosa 2018-09-02 01:07:45 +02:00
parent 208fc48dd7
commit 474e633e48
1 changed files with 47 additions and 55 deletions

View File

@ -194,15 +194,15 @@ extension SessionProxy {
return nil return nil
} }
var optTopologyComponents: [String]? var optTopologyArguments: [String]?
var optIfconfig4Components: [String]? var optIfconfig4Arguments: [String]?
var optGateway4Components: [String]? var optGateway4Arguments: [String]?
let address4: String let address4: String
let addressMask4: String let addressMask4: String
let defaultGateway4: String let defaultGateway4: String
var routes4: [IPv4Settings.Route] = [] var routes4: [IPv4Settings.Route] = []
var optIfconfig6Components: [String]? var optIfconfig6Arguments: [String]?
var dnsServers: [String] = [] var dnsServers: [String] = []
var authToken: String? var authToken: String?
@ -210,27 +210,27 @@ extension SessionProxy {
// MARK: Routing (IPv4) // MARK: Routing (IPv4)
PushReply.topologyRegexp.enumerateMatches(in: message) { PushReply.topologyRegexp.enumerateArguments(in: message) {
optTopologyComponents = $0.components(separatedBy: " ") optTopologyArguments = $0
} }
guard let topologyComponents = optTopologyComponents, topologyComponents.count == 2 else { guard let topologyArguments = optTopologyArguments, topologyArguments.count == 1 else {
throw SessionError.malformedPushReply throw SessionError.malformedPushReply
} }
// assumes "topology" to be always pushed to clients, even when not explicitly set (defaults to net30) // assumes "topology" to be always pushed to clients, even when not explicitly set (defaults to net30)
guard let topology = Topology(rawValue: topologyComponents[1]) else { guard let topology = Topology(rawValue: topologyArguments[0]) else {
fatalError("Bad topology regexp, accepted unrecognized value: \(topologyComponents[1])") fatalError("Bad topology regexp, accepted unrecognized value: \(topologyArguments[0])")
} }
PushReply.ifconfigRegexp.enumerateMatches(in: message) { PushReply.ifconfigRegexp.enumerateArguments(in: message) {
optIfconfig4Components = $0.components(separatedBy: " ") optIfconfig4Arguments = $0
} }
guard let ifconfig4Components = optIfconfig4Components, ifconfig4Components.count == 3 else { guard let ifconfig4Arguments = optIfconfig4Arguments, ifconfig4Arguments.count == 2 else {
throw SessionError.malformedPushReply throw SessionError.malformedPushReply
} }
PushReply.gatewayRegexp.enumerateMatches(in: message) { PushReply.gatewayRegexp.enumerateArguments(in: message) {
optGateway4Components = $0.components(separatedBy: " ") optGateway4Arguments = $0
} }
// //
@ -250,32 +250,32 @@ extension SessionProxy {
case .subnet: case .subnet:
// default gateway required when topology is subnet // default gateway required when topology is subnet
guard let gateway4Components = optGateway4Components, gateway4Components.count == 2 else { guard let gateway4Arguments = optGateway4Arguments, gateway4Arguments.count == 1 else {
throw SessionError.malformedPushReply throw SessionError.malformedPushReply
} }
address4 = ifconfig4Components[1] address4 = ifconfig4Arguments[0]
addressMask4 = ifconfig4Components[2] addressMask4 = ifconfig4Arguments[1]
defaultGateway4 = gateway4Components[1] defaultGateway4 = gateway4Arguments[0]
default: default:
address4 = ifconfig4Components[1] address4 = ifconfig4Arguments[0]
addressMask4 = "255.255.255.255" addressMask4 = "255.255.255.255"
defaultGateway4 = ifconfig4Components[2] defaultGateway4 = ifconfig4Arguments[1]
} }
PushReply.routeRegexp.enumerateMatches(in: message) { PushReply.routeRegexp.enumerateArguments(in: message) {
let routeEntryComponents = $0.components(separatedBy: " ") let routeEntryArguments = $0
let address = routeEntryComponents[1] let address = routeEntryArguments[0]
let mask: String? let mask: String?
let gateway: String? let gateway: String?
if routeEntryComponents.count > 2 { if routeEntryArguments.count > 1 {
mask = routeEntryComponents[2] mask = routeEntryArguments[1]
} else { } else {
mask = nil mask = nil
} }
if routeEntryComponents.count > 3 { if routeEntryArguments.count > 2 {
gateway = routeEntryComponents[3] gateway = routeEntryArguments[2]
} else { } else {
gateway = defaultGateway4 gateway = defaultGateway4
} }
@ -291,11 +291,11 @@ extension SessionProxy {
// MARK: Routing (IPv6) // MARK: Routing (IPv6)
PushReply.ifconfig6Regexp.enumerateMatches(in: message) { PushReply.ifconfig6Regexp.enumerateArguments(in: message) {
optIfconfig6Components = $0.components(separatedBy: " ") optIfconfig6Arguments = $0
} }
if let ifconfig6Components = optIfconfig6Components, ifconfig6Components.count == 3 { if let ifconfig6Arguments = optIfconfig6Arguments, ifconfig6Arguments.count == 2 {
let address6Components = ifconfig6Components[1].components(separatedBy: "/") let address6Components = ifconfig6Arguments[0].components(separatedBy: "/")
guard address6Components.count == 2 else { guard address6Components.count == 2 else {
throw SessionError.malformedPushReply throw SessionError.malformedPushReply
} }
@ -303,13 +303,13 @@ extension SessionProxy {
throw SessionError.malformedPushReply throw SessionError.malformedPushReply
} }
let address6 = address6Components[0] let address6 = address6Components[0]
let defaultGateway6 = ifconfig6Components[2] let defaultGateway6 = ifconfig6Arguments[1]
var routes6: [IPv6Settings.Route] = [] var routes6: [IPv6Settings.Route] = []
PushReply.route6Regexp.enumerateMatches(in: message) { PushReply.route6Regexp.enumerateArguments(in: message) {
let routeEntryComponents = $0.components(separatedBy: " ") let routeEntryArguments = $0
let destinationComponents = routeEntryComponents[1].components(separatedBy: "/") let destinationComponents = routeEntryArguments[0].components(separatedBy: "/")
guard destinationComponents.count == 2 else { guard destinationComponents.count == 2 else {
// throw SessionError.malformedPushReply // throw SessionError.malformedPushReply
return return
@ -321,8 +321,8 @@ extension SessionProxy {
let destination = destinationComponents[0] let destination = destinationComponents[0]
let gateway: String? let gateway: String?
if routeEntryComponents.count > 2 { if routeEntryArguments.count > 1 {
gateway = routeEntryComponents[2] gateway = routeEntryArguments[1]
} else { } else {
gateway = defaultGateway6 gateway = defaultGateway6
} }
@ -341,28 +341,18 @@ extension SessionProxy {
// MARK: DNS // MARK: DNS
PushReply.dnsRegexp.enumerateMatches(in: message) { PushReply.dnsRegexp.enumerateArguments(in: message) {
let dnsEntryComponents = $0.components(separatedBy: " ") dnsServers.append($0[1])
dnsServers.append(dnsEntryComponents[2])
} }
// MARK: Authentication // MARK: Authentication
PushReply.authTokenRegexp.enumerateMatches(in: message) { PushReply.authTokenRegexp.enumerateArguments(in: message) {
let tokenComponents = $0.components(separatedBy: " ") authToken = $0[0]
if tokenComponents.count > 1 {
authToken = tokenComponents[1]
}
} }
PushReply.peerIdRegexp.enumerateMatches(in: message) { PushReply.peerIdRegexp.enumerateArguments(in: message) {
let tokenComponents = $0.components(separatedBy: " ") peerId = UInt32($0[0])
if tokenComponents.count > 1 {
peerId = UInt32(tokenComponents[1])
}
} }
self.dnsServers = dnsServers self.dnsServers = dnsServers
@ -373,13 +363,15 @@ extension SessionProxy {
} }
private extension NSRegularExpression { private extension NSRegularExpression {
func enumerateMatches(in string: String, using block: (String) -> Void) { func enumerateArguments(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)
block(match) var tokens = match.components(separatedBy: " ")
tokens.removeFirst()
block(tokens)
} }
} }
} }