From b690de0d14c08091a86a931fde1761b7c0bff92c Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Mon, 15 Oct 2018 13:35:24 +0530 Subject: [PATCH] VPN: Add TunnelsManager to manage the tunnels in the app --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 12 +++ WireGuard/WireGuard/VPN/TunnelsManager.swift | 82 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 WireGuard/WireGuard/VPN/TunnelsManager.swift diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index 5611dc4..ca3ca1c 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -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 = ""; }; 6F7774E72172020C006A79B3 /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = ""; }; 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPAddressRange.swift; sourceTree = ""; }; + 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsManager.swift; sourceTree = ""; }; 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 = ""; }; 6FF4AC21211EC472002C96EB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -71,6 +73,14 @@ path = Model; sourceTree = ""; }; + 6F7774ED21722D0C006A79B3 /* VPN */ = { + isa = PBXGroup; + children = ( + 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */, + ); + path = VPN; + sourceTree = ""; + }; 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 */, diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift new file mode 100644 index 0000000..ff1cf78 --- /dev/null +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -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] + } +}