diff --git a/Demo/.swiftlint.yml b/Demo/.swiftlint.yml new file mode 100644 index 0000000..c27605e --- /dev/null +++ b/Demo/.swiftlint.yml @@ -0,0 +1,12 @@ +analyzer_rules: + - unused_declaration + - unused_import +disabled_rules: + - cyclomatic_complexity + - file_length + - force_cast + - function_body_length + - identifier_name + - line_length + - nesting + - todo diff --git a/Demo/Demo/Configuration.swift b/Demo/Demo/Configuration.swift index c05e447..1e73782 100644 --- a/Demo/Demo/Configuration.swift +++ b/Demo/Demo/Configuration.swift @@ -88,20 +88,32 @@ aeb893d9a96d1f15519bb3c4dcb40ee3 -----END OpenVPN Static key V1----- """, direction: .client)! - static func make(_ title: String, appGroup: String, hostname: String, port: UInt16, socketType: SocketType) -> OpenVPN.ProviderConfiguration { + struct Parameters { + let title: String + + let appGroup: String + + let hostname: String + + let port: UInt16 + + let socketType: SocketType + } + + static func make(params: Parameters) -> OpenVPN.ProviderConfiguration { var builder = OpenVPN.ConfigurationBuilder() builder.ca = ca builder.cipher = .aes256cbc builder.digest = .sha512 builder.compressionFraming = .compLZO builder.renegotiatesAfter = nil - builder.remotes = [Endpoint(hostname, EndpointProtocol(socketType, port))] + builder.remotes = [Endpoint(params.hostname, EndpointProtocol(params.socketType, params.port))] builder.tlsWrap = TLSWrap(strategy: .auth, key: tlsKey) builder.mtu = 1350 builder.routingPolicies = [.IPv4, .IPv6] let cfg = builder.build() - var providerConfiguration = OpenVPN.ProviderConfiguration(title, appGroup: appGroup, configuration: cfg) + var providerConfiguration = OpenVPN.ProviderConfiguration(params.title, appGroup: params.appGroup, configuration: cfg) providerConfiguration.shouldDebug = true providerConfiguration.masksPrivateData = false return providerConfiguration @@ -110,24 +122,43 @@ aeb893d9a96d1f15519bb3c4dcb40ee3 } extension WireGuard { + struct Parameters { + let title: String + + let appGroup: String + + let clientPrivateKey: String + + let clientAddress: String + + let serverPublicKey: String + + let serverAddress: String + + let serverPort: String + } + struct DemoConfiguration { - static func make( - _ title: String, - appGroup: String, - clientPrivateKey: String, - clientAddress: String, - serverPublicKey: String, - serverAddress: String, - serverPort: String - ) -> WireGuard.ProviderConfiguration? { - var builder = try! WireGuard.ConfigurationBuilder(clientPrivateKey) - builder.addresses = [clientAddress] + static func make(params: Parameters) -> WireGuard.ProviderConfiguration? { + var builder: WireGuard.ConfigurationBuilder + do { + builder = try WireGuard.ConfigurationBuilder(params.clientPrivateKey) + } catch { + print(">>> \(error)") + return nil + } + builder.addresses = [params.clientAddress] builder.dnsServers = ["1.1.1.1", "1.0.0.1"] - try! builder.addPeer(serverPublicKey, endpoint: "\(serverAddress):\(serverPort)") + do { + try builder.addPeer(params.serverPublicKey, endpoint: "\(params.serverAddress):\(params.serverPort)") + } catch { + print(">>> \(error)") + return nil + } builder.addDefaultGatewayIPv4(toPeer: 0) let cfg = builder.build() - return WireGuard.ProviderConfiguration(title, appGroup: appGroup, configuration: cfg) + return WireGuard.ProviderConfiguration(params.title, appGroup: params.appGroup, configuration: cfg) } } } diff --git a/Demo/Demo/iOS/OpenVPNViewController.swift b/Demo/Demo/iOS/OpenVPNViewController.swift index b736a17..dd6ef35 100644 --- a/Demo/Demo/iOS/OpenVPNViewController.swift +++ b/Demo/Demo/iOS/OpenVPNViewController.swift @@ -108,13 +108,13 @@ class OpenVPNViewController: UIViewController { let socketType: SocketType = switchTCP.isOn ? .tcp : .udp let credentials = OpenVPN.Credentials(textUsername.text!, textPassword.text!) - cfg = OpenVPN.DemoConfiguration.make( - "TunnelKit.OpenVPN", + cfg = OpenVPN.DemoConfiguration.make(params: .init( + title: "TunnelKit.OpenVPN", appGroup: appGroup, hostname: hostname, port: port, socketType: socketType - ) + )) cfg?.username = credentials.username let passwordReference: Data diff --git a/Demo/Demo/iOS/WireGuardViewController.swift b/Demo/Demo/iOS/WireGuardViewController.swift index c67cb51..ff8c5f9 100644 --- a/Demo/Demo/iOS/WireGuardViewController.swift +++ b/Demo/Demo/iOS/WireGuardViewController.swift @@ -94,15 +94,15 @@ class WireGuardViewController: UIViewController { let serverAddress = textServerAddress.text! let serverPort = textServerPort.text! - guard let cfg = WireGuard.DemoConfiguration.make( - "TunnelKit.WireGuard", + guard let cfg = WireGuard.DemoConfiguration.make(params: .init( + title: "TunnelKit.WireGuard", appGroup: appGroup, clientPrivateKey: clientPrivateKey, clientAddress: clientAddress, serverPublicKey: serverPublicKey, serverAddress: serverAddress, serverPort: serverPort - ) else { + )) else { print("Configuration incomplete") return } diff --git a/Demo/Demo/macOS/OpenVPNViewController.swift b/Demo/Demo/macOS/OpenVPNViewController.swift index 6a405c8..d504338 100644 --- a/Demo/Demo/macOS/OpenVPNViewController.swift +++ b/Demo/Demo/macOS/OpenVPNViewController.swift @@ -99,13 +99,13 @@ class OpenVPNViewController: NSViewController { let port = UInt16(textPort.stringValue)! let credentials = OpenVPN.Credentials(textUsername.stringValue, textPassword.stringValue) - cfg = OpenVPN.DemoConfiguration.make( - "TunnelKit.OpenVPN", + cfg = OpenVPN.DemoConfiguration.make(params: .init( + title: "TunnelKit.OpenVPN", appGroup: appGroup, hostname: hostname, port: port, socketType: .udp - ) + )) cfg?.username = credentials.username let passwordReference: Data diff --git a/Demo/Demo/macOS/WireGuardViewController.swift b/Demo/Demo/macOS/WireGuardViewController.swift index f23f207..2b4120b 100644 --- a/Demo/Demo/macOS/WireGuardViewController.swift +++ b/Demo/Demo/macOS/WireGuardViewController.swift @@ -94,15 +94,15 @@ class WireGuardViewController: NSViewController { let serverAddress = textServerAddress.stringValue let serverPort = textServerPort.stringValue - guard let cfg = WireGuard.DemoConfiguration.make( - "TunnelKit.WireGuard", + guard let cfg = WireGuard.DemoConfiguration.make(params: .init( + title: "TunnelKit.WireGuard", appGroup: appGroup, clientPrivateKey: clientPrivateKey, clientAddress: clientAddress, serverPublicKey: serverPublicKey, serverAddress: serverAddress, serverPort: serverPort - ) else { + )) else { print("Configuration incomplete") return }