Forward compound SessionReply to delegate

Improves extensibility.
This commit is contained in:
Davide De Rosa 2018-08-24 11:57:09 +02:00
parent 2f7d41a921
commit 5bf7813d56
3 changed files with 37 additions and 24 deletions

View File

@ -451,18 +451,18 @@ extension TunnelKitProvider: SessionProxyDelegate {
// MARK: SessionProxyDelegate (tunnel queue)
/// :nodoc:
public func sessionDidStart(_ proxy: SessionProxy, remoteAddress: String, address: String, gatewayAddress: String, dnsServers: [String]) {
public func sessionDidStart(_ proxy: SessionProxy, remoteAddress: String, reply: SessionReply) {
reasserting = false
log.info("Session did start")
log.info("Returned ifconfig parameters:")
log.info("\tTunnel: \(remoteAddress)")
log.info("\tOwn address: \(address)")
log.info("\tGateway: \(gatewayAddress)")
log.info("\tDNS: \(dnsServers)")
log.info("\tRemote: \(remoteAddress)")
log.info("\tLocal: \(reply.address)/\(reply.addressMask)")
log.info("\tGateway: \(reply.gatewayAddress)")
log.info("\tDNS: \(reply.dnsServers)")
bringNetworkUp(tunnel: remoteAddress, vpn: address, gateway: gatewayAddress, dnsServers: dnsServers) { (error) in
bringNetworkUp(remoteAddress: remoteAddress, reply: reply) { (error) in
if let error = error {
log.error("Failed to configure tunnel: \(error)")
self.pendingStartHandler?(error)
@ -489,19 +489,19 @@ extension TunnelKitProvider: SessionProxyDelegate {
socket?.shutdown()
}
private func bringNetworkUp(tunnel: String, vpn: String, gateway: String, dnsServers: [String], completionHandler: @escaping (Error?) -> Void) {
private func bringNetworkUp(remoteAddress: String, reply: SessionReply, completionHandler: @escaping (Error?) -> Void) {
// route all traffic to VPN
let defaultRoute = NEIPv4Route.default()
defaultRoute.gatewayAddress = gateway
defaultRoute.gatewayAddress = reply.gatewayAddress
let ipv4Settings = NEIPv4Settings(addresses: [vpn], subnetMasks: ["255.255.255.255"])
let ipv4Settings = NEIPv4Settings(addresses: [reply.address], subnetMasks: [reply.addressMask])
ipv4Settings.includedRoutes = [defaultRoute]
ipv4Settings.excludedRoutes = []
let dnsSettings = NEDNSSettings(servers: dnsServers)
let dnsSettings = NEDNSSettings(servers: reply.dnsServers)
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: tunnel)
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: remoteAddress)
newSettings.ipv4Settings = ipv4Settings
newSettings.dnsSettings = dnsSettings

View File

@ -37,8 +37,27 @@
import Foundation
/// Represents the reply of a successful session start.
public protocol SessionReply {
/// The obtained address.
var address: String { get }
/// The obtained address mask.
var addressMask: String { get }
/// The address of the default gateway.
var gatewayAddress: String { get }
/// The DNS servers set up for this session.
var dnsServers: [String] { get }
}
extension SessionProxy {
struct PushReply {
// XXX: parsing is very optimistic
struct PushReply: SessionReply {
private static let ifconfigRegexp = try! NSRegularExpression(pattern: "ifconfig [\\d\\.]+ [\\d\\.]+", options: [])
private static let dnsRegexp = try! NSRegularExpression(pattern: "dhcp-option DNS [\\d\\.]+", options: [])
@ -49,6 +68,8 @@ extension SessionProxy {
let address: String
let addressMask: String
let gatewayAddress: String
let dnsServers: [String]
@ -110,6 +131,7 @@ extension SessionProxy {
}
address = addresses[1]
addressMask = "255.255.255.255"
gatewayAddress = addresses[2]
self.dnsServers = dnsServers
self.authToken = authToken

View File

@ -55,11 +55,9 @@ public protocol SessionProxyDelegate: class {
Called after starting a session.
- Parameter remoteAddress: The address of the VPN server.
- Parameter address: The obtained address.
- Parameter gatewayAddress: The address of the gateway.
- Parameter dnsServers: The DNS servers set up for this session.
- Parameter reply: The compound `SessionReply` containing tunnel settings.
*/
func sessionDidStart(_: SessionProxy, remoteAddress: String, address: String, gatewayAddress: String, dnsServers: [String])
func sessionDidStart(_: SessionProxy, remoteAddress: String, reply: SessionReply)
/**
Called after stopping a session.
@ -905,14 +903,7 @@ public class SessionProxy {
guard let remoteAddress = link?.remoteAddress else {
fatalError("Could not resolve link remote address")
}
delegate?.sessionDidStart(
self,
remoteAddress: remoteAddress,
address: reply.address,
gatewayAddress: reply.gatewayAddress,
dnsServers: reply.dnsServers
)
delegate?.sessionDidStart(self, remoteAddress: remoteAddress, reply: reply)
if let interval = configuration.keepAliveInterval {
queue.asyncAfter(deadline: .now() + interval) { [weak self] in