tunnelkit/Demo/BasicTunnel-iOS/ViewController.swift

338 lines
15 KiB
Swift
Raw Normal View History

2018-08-23 08:19:25 +00:00
//
// ViewController.swift
// BasicTunnel-iOS
//
// Created by Davide De Rosa on 2/11/17.
// Copyright (c) 2018 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/>.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright (c) 2018-Present Private Internet Access
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
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
2020-06-12 22:33:45 +00:00
private let appGroup = "group.com.algoritmico.ios.demo.BasicTunnel"
private let tunnelIdentifier = "com.algoritmico.ios.demo.BasicTunnel.Extension"
2019-05-23 21:25:35 +00:00
private let ca = OpenVPN.CryptoContainer(pem: """
2018-10-06 13:39:06 +00:00
-----BEGIN CERTIFICATE-----
2020-06-12 22:33:45 +00:00
MIIG6zCCBNOgAwIBAgIJAJhm2PWFkE8NMA0GCSqGSIb3DQEBCwUAMIGpMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxEzAR
BgNVBAoTCkdvb2dsZSBJbmMxEzARBgNVBAsTCkRldmVsb3BlcnMxFjAUBgNVBAMT
DUdvb2dsZSBJbmMgQ0ExEDAOBgNVBCkTB0Vhc3lSU0ExITAfBgkqhkiG9w0BCQEW
EnN1cHBvcnRAZ29vZ2xlLmNvbTAeFw0xNTAyMDIwNTMwMDlaFw0yNTAxMzAwNTMw
MDlaMIGpMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50
YWluIFZpZXcxEzARBgNVBAoTCkdvb2dsZSBJbmMxEzARBgNVBAsTCkRldmVsb3Bl
cnMxFjAUBgNVBAMTDUdvb2dsZSBJbmMgQ0ExEDAOBgNVBCkTB0Vhc3lSU0ExITAf
BgkqhkiG9w0BCQEWEnN1cHBvcnRAZ29vZ2xlLmNvbTCCAiIwDQYJKoZIhvcNAQEB
BQADggIPADCCAgoCggIBAN8T5cgRQ8+zsE2FWRpArqTlBh7MvoQU9Z4659eJ3Mhq
+pvR960HG9Bg6MkH0gwdcU65l0TLTwweOLBIZoxhLB+OVvl/x0FD4EnK9Pmp5SIU
P7cEqcqqRfRAI+9k0jwiGcPOl7KKqfz70c6QsQYn2VvrTMqgDt4IS/zpaToZsftq
ibCtKh0bPv4UMLg6Y31cItYlVIrrbGrM4Kvdb8yN8ho3ms5KV421G9s9w/6KYBZt
zr3mHoI9o+njE0ScT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75lZp
tjF1aYGTAhC/8yoAYmBKzbKJXyNW2Vq93/9y+43OUJridoijB7cqbUpZFOVdtnZ5
LHb3h7hLV/3C2WgehM73f/UMc65fIk+9CpwD7Cgpu9duBknf0c0s0Sw3HA/s6SL6
V4FhARi7flTF9TGR6+e0i2oreXEwJXP3GoXpazOqzrGekSXRMqwLY83fJ/RzP0Ap
PMc5TfiQVcL/h92CUAwwH1vRJkAhrTvNXh1Ynd7zdFT/wYWrK0twm4qlTjKYpbVL
RIoeppgOUG+1t82/HW2geWLYSNRfZiTbpAvm00HJavD12qOD0NUIErlQnOZvW2UC
/RzA/yu9ZguEIlV+8qmkiUCKyajyLFydWqqScMYAeJMh6aJzfQ4UHu2bzr9Qo2MV
HiT8esMeX+/orMetzuTPgZInMhznvVdNdfwAfibwlXOKvm154UgDVgnKV405oNM=
2018-10-06 13:39:06 +00:00
-----END CERTIFICATE-----
""")
2020-06-12 22:33:45 +00:00
private let clientCertificate = OpenVPN.CryptoContainer(pem: """
-----BEGIN CERTIFICATE-----
MIIHPTCCBSWgAwIBAgIBAjANBgkqhkiG9w0BAQsFADCBqTELMAkGA1UEBhMCVVMx
CzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH
b29nbGUgSW5jMRMwEQYDVQQLEwpEZXZlbG9wZXJzMRYwFAYDVQQDEw1Hb29nbGUg
SW5jIENBMRAwDgYDVQQpEwdFYXN5UlNBMSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0
QGdvb2dsZS5jb20wHhcNMTUwMjAyMDUzODUxWhcNMjUwMTMwMDUzODUxWjCBojEL
MAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3
MRMwEQYDVQQKEwpHb29nbGUgSW5jMRMwEQYDVQQLEwpEZXZlbG9wZXJzMQ8wDQYD
VQQDEwZjbGllbnQxEDAOBgNVBCkTB0Vhc3lSU0ExITAfBgkq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SwQ+IR8Vyo+eAJ
MfDx1gFh+AvLNPUrZRHcmVevf+meL3mBW1LKRZffIbDhFT5mn+1qkA+MkTHVXOP1
/554vWAixW49zFG9PjL4o065zsqoZ/iA1tvpH2HSHtjU6G3RiDQqINN1OZMLP1zV
4VtZHweoni/TnjlukONXKq2uhhtgPnCSh5KEa30zX57H+PPQNlPptPCLtzVkn6rf
CUOWrYYCDP4JI9fQafmzOq0tgooGhGaB9ctRRCC9zl5bPO9iLxF8VdznXPj2xPyW
D/WZ8tL/36S08qTHa/YCro+qfBDFZlUG7tIZeaECggEAeTrERzoR2se73InIetV3
g+UcAT/gVR+VNOZcSjjfa2xFqkwtNjDfknHyERM/gajT9OHvOtge0Ln2yUKmTbUr
Fwq5BgSECbhC4SQ1EFMUndG0V4myvKhjST1Y5JewNAWyG5o5h9SKGxn2+iVpdYqy
QTcq75c1681CiJORUB3hH9LTToi50M7YvqTt7jxuCaWwsMd1k4SQda8o5a92Sa4s
MqzyQ318zt8tL+KZNWyw03s64flIDbJJVUImD+smnlSQ9HXFBbGd6q1K3K/D+xSS
zcJZoqJ9H3F+MjSK284FlMDMc3dHX7dTZmHI6jIG6Q+ZI/ec/0uaLsN+kpDR5ZFm
OwKCAQEA11nK0Orlb85QRRNWIp2TiclXPBK/x6fhtDDEtyIfNtw0cVLr8EjABepP
12H57Hs1f9qLeWFa20dbTh2OeEvOnqzdXR1/27sjSc8UqreEwzrv/bkmBEF92xxy
66LIr0o2S72ZT6E6IImJ2N563GrOWla7LpQN5V64RAc3C2vb5DiL70oCE0Qpb9Vn
M69t86apMrAxkUxVJAWLRBd9fbYyzJgTW61tFqXWTZpiz6bhuWApSEzaHcL3/f5l
3qibvYTFj6CIqcdHA6Sy+UTEyb7zWnFwWVNEwAadsMmq45mhdoFjlm/5onPrpj+l
1LXZrtjAB4U+/F7um6YyAavpHYq9hg==
-----END PRIVATE KEY-----
""")
2018-08-23 09:46:02 +00:00
extension ViewController {
2020-06-12 22:33:45 +00:00
private func makeConfiguration() -> OpenVPNTunnelProvider.Configuration {
2018-08-23 09:46:02 +00:00
let server = textServer.text!
let domain = textDomain.text!
let hostname = ((domain == "") ? server : [server, domain].joined(separator: "."))
let port = UInt16(textPort.text!)!
2020-06-12 22:33:45 +00:00
2019-05-23 21:25:35 +00:00
var sessionBuilder = OpenVPN.ConfigurationBuilder()
2019-04-04 17:31:05 +00:00
sessionBuilder.ca = ca
sessionBuilder.cipher = .aes128cbc
2018-10-25 18:40:22 +00:00
sessionBuilder.digest = .sha1
sessionBuilder.compressionFraming = .compLZO
sessionBuilder.renegotiatesAfter = nil
2019-04-09 19:02:39 +00:00
sessionBuilder.hostname = hostname
2019-04-04 17:31:05 +00:00
let socketType: SocketType = switchTCP.isOn ? .tcp : .udp
sessionBuilder.endpointProtocols = [EndpointProtocol(socketType, port)]
2020-06-12 22:33:45 +00:00
sessionBuilder.clientCertificate = clientCertificate
sessionBuilder.clientKey = clientKey
2019-05-23 21:25:35 +00:00
var builder = OpenVPNTunnelProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
2018-08-23 09:46:02 +00:00
builder.mtu = 1350
builder.shouldDebug = true
builder.masksPrivateData = false
2018-08-23 09:46:02 +00:00
2020-06-12 22:33:45 +00:00
return builder.build()
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-06-12 22:33:45 +00:00
let vpn = StandardVPNProvider(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: .VPNDidChangeStatus,
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() {
2020-06-12 22:33:45 +00:00
let credentials = OpenVPN.Credentials(textUsername.text!, textPassword.text!)
let proto = try! makeConfiguration().generatedTunnelProtocol(
withBundleIdentifier: tunnelIdentifier,
appGroup: appGroup,
credentials: credentials
)
let cfg = NetworkExtensionVPNConfiguration(protocolConfiguration: proto, onDemandRules: [])
vpn.reconnect(configuration: cfg) { (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)")
}
}