VPN: Add TunnelsManager to manage the tunnels in the app

This commit is contained in:
Roopesh Chander 2018-10-15 13:35:24 +05:30
parent 66307fd3c5
commit b690de0d14
2 changed files with 94 additions and 0 deletions

View File

@ -12,6 +12,7 @@
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */; };
6F7774E82172020C006A79B3 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E72172020C006A79B3 /* Configuration.swift */; };
6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */; };
6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */; };
6FF4AC1F211EC472002C96EB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6FF4AC1E211EC472002C96EB /* Assets.xcassets */; };
6FF4AC22211EC472002C96EB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */; };
6FF4AC472120B9E0002C96EB /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FF4AC462120B9E0002C96EB /* NetworkExtension.framework */; };
@ -23,6 +24,7 @@
6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = "<group>"; };
6F7774E72172020C006A79B3 /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
6F7774E9217229DB006A79B3 /* IPAddressRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPAddressRange.swift; sourceTree = "<group>"; };
6F7774EE21722D97006A79B3 /* TunnelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsManager.swift; sourceTree = "<group>"; };
6FF4AC14211EC46F002C96EB /* WireGuard.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WireGuard.app; sourceTree = BUILT_PRODUCTS_DIR; };
6FF4AC1E211EC472002C96EB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6FF4AC21211EC472002C96EB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@ -71,6 +73,14 @@
path = Model;
sourceTree = "<group>";
};
6F7774ED21722D0C006A79B3 /* VPN */ = {
isa = PBXGroup;
children = (
6F7774EE21722D97006A79B3 /* TunnelsManager.swift */,
);
path = VPN;
sourceTree = "<group>";
};
6FF4AC0B211EC46F002C96EB = {
isa = PBXGroup;
children = (
@ -93,6 +103,7 @@
children = (
6F7774E6217201E0006A79B3 /* Model */,
6F7774DD217181B1006A79B3 /* UI */,
6F7774ED21722D0C006A79B3 /* VPN */,
6FF4AC482120B9E0002C96EB /* WireGuard.entitlements */,
6FF4AC1E211EC472002C96EB /* Assets.xcassets */,
6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */,
@ -186,6 +197,7 @@
buildActionMask = 2147483647;
files = (
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */,
6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */,
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */,
6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */,
6F7774E82172020C006A79B3 /* Configuration.swift in Sources */,

View File

@ -0,0 +1,82 @@
//
// TunnelsManager.swift
// WireGuard
//
// Created by Roopesh Chander on 13/10/18.
// Copyright © 2018 WireGuard LLC. All rights reserved.
//
import Foundation
class TunnelProviderManager {
// Mock of NETunnelProviderManager
var name: String
var tunnelConfiguration: TunnelConfiguration
init(tunnelConfiguration: TunnelConfiguration) {
self.name = tunnelConfiguration.name
self.tunnelConfiguration = tunnelConfiguration
}
}
class TunnelContainer {
var name: String { return tunnelProvider.name }
let tunnelProvider: TunnelProviderManager
var index: Int
init(tunnel: TunnelProviderManager, index: Int) {
self.tunnelProvider = tunnel
self.index = index
}
}
class TunnelsManager {
var tunnels: [TunnelContainer]
enum TunnelsManagerError: Error {
case tunnelsUninitialized
}
init(tunnelProviders: [TunnelProviderManager]) {
var tunnels: [TunnelContainer] = []
for (i, tunnelProvider) in tunnelProviders.enumerated() {
let tunnel = TunnelContainer(tunnel: tunnelProvider, index: i)
tunnels.append(tunnel)
}
self.tunnels = tunnels
}
static func create(completionHandler: (TunnelsManager?) -> Void) {
completionHandler(TunnelsManager(tunnelProviders: []))
}
func add(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) {
let tunnelProvider = TunnelProviderManager(tunnelConfiguration: tunnelConfiguration)
for tunnel in tunnels {
tunnel.index = tunnel.index + 1
}
let tunnel = TunnelContainer(tunnel: tunnelProvider, index: 0)
tunnels.insert(tunnel, at: 0)
completionHandler(nil)
}
func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) {
tunnel.tunnelProvider.tunnelConfiguration = tunnelConfiguration
completionHandler(nil)
}
func remove(tunnel: TunnelContainer, completionHandler: @escaping (Error?) -> Void) {
for i in ((tunnel.index + 1) ..< tunnels.count) {
tunnels[i].index = tunnels[i].index + 1
}
tunnels.remove(at: tunnel.index)
completionHandler(nil)
}
func numberOfTunnels() -> Int {
return tunnels.count
}
func tunnel(at index: Int) -> TunnelContainer {
return tunnels[index]
}
}