tunnelkit/Demo/BasicTunnel-macOS/ViewController.swift

147 lines
4.5 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 10/15/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 Cocoa
import NetworkExtension
2018-08-23 09:46:02 +00:00
import TunnelKit
2018-08-23 08:19:25 +00:00
2020-06-12 22:54:44 +00:00
private let appGroup = "DTDYD63ZX9.group.com.algoritmico.macos.demo.BasicTunnel"
2018-10-06 13:39:06 +00:00
2020-06-12 22:54:44 +00:00
private let tunnelIdentifier = "com.algoritmico.macos.demo.BasicTunnel.Extension"
2018-08-23 09:46:02 +00:00
class ViewController: NSViewController {
2018-08-23 08:19:25 +00:00
@IBOutlet var textUsername: NSTextField!
@IBOutlet var textPassword: NSTextField!
@IBOutlet var textServer: NSTextField!
@IBOutlet var textDomain: NSTextField!
@IBOutlet var textPort: NSTextField!
@IBOutlet var buttonConnection: NSButton!
2020-06-12 23:00:47 +00:00
private let vpn = StandardVPNProvider(bundleIdentifier: tunnelIdentifier)
2020-06-12 22:54:44 +00:00
2018-08-23 08:19:25 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2020-06-12 22:54:44 +00:00
textServer.stringValue = "es"
textDomain.stringValue = "lazerpenguin.com"
textPort.stringValue = "443"
textUsername.stringValue = ""
textPassword.stringValue = ""
2018-08-23 08:19:25 +00:00
2018-08-23 09:46:02 +00:00
NotificationCenter.default.addObserver(
self,
selector: #selector(VPNStatusDidChange(notification:)),
name: VPN.didChangeStatus,
2018-08-23 09:46:02 +00:00
object: nil
)
2018-08-23 08:19:25 +00:00
2020-06-12 22:54:44 +00:00
vpn.prepare(completionHandler: nil)
2018-08-23 08:19:25 +00:00
testFetchRef()
}
@IBAction func connectionClicked(_ sender: Any) {
2020-06-12 22:54:44 +00:00
switch vpn.status {
case .disconnected:
connect()
case .connected, .connecting, .disconnecting:
disconnect()
2018-08-23 08:19:25 +00:00
}
}
func connect() {
2020-06-12 22:54:44 +00:00
let server = textServer.stringValue
let domain = textDomain.stringValue
let hostname = ((domain == "") ? server : [server, domain].joined(separator: "."))
let port = UInt16(textPort.stringValue)!
let credentials = OpenVPN.Credentials(textUsername.stringValue, textPassword.stringValue)
let cfg = Configuration.make(hostname: hostname, port: port, socketType: .udp)
let proto = try! cfg.generatedTunnelProtocol(
withBundleIdentifier: tunnelIdentifier,
appGroup: appGroup,
credentials: credentials
)
let neCfg = NetworkExtensionVPNConfiguration(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:54:44 +00:00
func disconnect() {
vpn.disconnect(completionHandler: nil)
2018-08-23 08:19:25 +00:00
}
2020-06-12 22:54:44 +00:00
2018-08-23 08:19:25 +00:00
func updateButton() {
2020-06-12 22:54:44 +00:00
switch vpn.status {
2018-08-23 08:19:25 +00:00
case .connected, .connecting:
buttonConnection.title = "Disconnect"
case .disconnected:
buttonConnection.title = "Connect"
case .disconnecting:
buttonConnection.title = "Disconnecting"
}
}
@objc private func VPNStatusDidChange(notification: NSNotification) {
2020-06-12 22:54:44 +00:00
print("VPNStatusDidChange: \(vpn.status)")
2018-08-23 08:19:25 +00:00
updateButton()
}
2020-06-12 22:54:44 +00:00
2018-08-23 08:19:25 +00:00
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)")
}
}