tunnelkit/TunnelKit/Demo/iOS/ViewController.swift

160 lines
4.9 KiB
Swift
Raw Normal View History

2018-08-23 08:19:25 +00:00
//
// ViewController.swift
2020-06-12 23:00:47 +00:00
// Demo
2018-08-23 08:19:25 +00:00
//
// Created by Davide De Rosa on 2/11/17.
2020-06-12 23:00:47 +00:00
// Copyright (c) 2020 Davide De Rosa. All rights reserved.
//
// https://github.com/keeshux
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
2018-08-23 08:19:25 +00:00
import UIKit
2018-08-23 09:46:02 +00:00
import TunnelKit
2018-08-23 08:19:25 +00:00
2021-01-03 17:23:56 +00:00
private let appGroup = "group.com.algoritmico.TunnelKit.Demo"
2020-06-12 22:33:45 +00:00
2021-01-03 17:23:56 +00:00
private let tunnelIdentifier = "com.algoritmico.ios.TunnelKit.Demo.Tunnel"
2020-06-12 22:33:45 +00:00
2018-08-23 09:46:02 +00:00
class ViewController: UIViewController, URLSessionDataDelegate {
2018-08-23 08:19:25 +00:00
@IBOutlet var textUsername: UITextField!
@IBOutlet var textPassword: UITextField!
@IBOutlet var textServer: UITextField!
@IBOutlet var textDomain: UITextField!
@IBOutlet var textPort: UITextField!
@IBOutlet var switchTCP: UISwitch!
@IBOutlet var buttonConnection: UIButton!
@IBOutlet var textLog: UITextView!
2020-11-22 21:02:25 +00:00
private let vpn = OpenVPNProvider(bundleIdentifier: tunnelIdentifier)
2018-08-23 08:19:25 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2020-06-12 22:33:45 +00:00
textServer.text = "es"
textDomain.text = "lazerpenguin.com"
textPort.text = "443"
2018-08-23 08:19:25 +00:00
switchTCP.isOn = false
2020-06-12 22:33:45 +00:00
textUsername.text = ""
textPassword.text = ""
2018-08-23 08:19:25 +00:00
2020-06-12 22:33:45 +00:00
NotificationCenter.default.addObserver(
self,
selector: #selector(VPNStatusDidChange(notification:)),
name: VPN.didChangeStatus,
2020-06-12 22:33:45 +00:00
object: nil
)
2018-08-23 08:19:25 +00:00
2020-06-12 22:33:45 +00:00
vpn.prepare(completionHandler: nil)
2018-08-23 08:19:25 +00:00
testFetchRef()
}
@IBAction func connectionClicked(_ sender: Any) {
2020-06-12 22:33:45 +00:00
switch vpn.status {
case .disconnected:
connect()
case .connected, .connecting, .disconnecting:
disconnect()
2018-08-23 08:19:25 +00:00
}
}
@IBAction func tcpClicked(_ sender: Any) {
}
func connect() {
let server = textServer.text!
let domain = textDomain.text!
let hostname = ((domain == "") ? server : [server, domain].joined(separator: "."))
let port = UInt16(textPort.text!)!
let socketType: SocketType = switchTCP.isOn ? .tcp : .udp
2020-06-12 22:33:45 +00:00
let credentials = OpenVPN.Credentials(textUsername.text!, textPassword.text!)
let cfg = Configuration.make(hostname: hostname, port: port, socketType: socketType)
let proto = try! cfg.generatedTunnelProtocol(
2020-06-12 22:33:45 +00:00
withBundleIdentifier: tunnelIdentifier,
appGroup: appGroup,
credentials: credentials
)
let neCfg = NetworkExtensionVPNConfiguration(title: "BasicTunnel", protocolConfiguration: proto, onDemandRules: [])
vpn.reconnect(configuration: neCfg) { (error) in
2018-08-23 08:19:25 +00:00
if let error = error {
print("configure error: \(error)")
return
}
2020-06-12 22:33:45 +00:00
}
2018-08-23 08:19:25 +00:00
}
func disconnect() {
2020-06-12 22:33:45 +00:00
vpn.disconnect(completionHandler: nil)
2018-08-23 08:19:25 +00:00
}
@IBAction func displayLog() {
2020-06-12 22:33:45 +00:00
vpn.requestDebugLog(fallback: { "" }) { (log) in
2018-08-23 08:19:25 +00:00
self.textLog.text = log
}
}
func updateButton() {
2020-06-12 22:33:45 +00:00
switch vpn.status {
2018-08-23 08:19:25 +00:00
case .connected, .connecting:
buttonConnection.setTitle("Disconnect", for: .normal)
case .disconnected:
buttonConnection.setTitle("Connect", for: .normal)
case .disconnecting:
buttonConnection.setTitle("Disconnecting", for: .normal)
}
}
@objc private func VPNStatusDidChange(notification: NSNotification) {
2020-06-12 22:33:45 +00:00
print("VPNStatusDidChange: \(vpn.status)")
2018-08-23 08:19:25 +00:00
updateButton()
}
private func testFetchRef() {
// let keychain = Keychain(group: ViewController.APP_GROUP)
// let username = "foo"
// let password = "bar"
//
// guard let _ = try? keychain.set(password: password, for: username) else {
// print("Couldn't set password")
// return
// }
// guard let passwordReference = try? keychain.passwordReference(for: username) else {
// print("Couldn't get password reference")
// return
// }
// guard let fetchedPassword = try? Keychain.password(for: username, reference: passwordReference) else {
// print("Couldn't fetch password")
// return
// }
//
// print("\(username) -> \(password)")
// print("\(username) -> \(fetchedPassword)")
}
}