Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ShadowVPN for iOS
=================

[ShadowVPN](https://github.com/clowwindy/ShadowVPN) for iOS 9, using the new Network Extension API.
[ShadowVPN](https://github.com/clowwindy/ShadowVPN) for iOS 10, using the new Network Extension API.

Features
- Stateless VPN
Expand Down
6 changes: 6 additions & 0 deletions ShadowVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
62A4274C1B5A3C9700BB4AD9 = {
CreatedOnToolsVersion = 7.0;
DevelopmentTeam = LND7PS23X9;
LastSwiftMigration = 0810;
SystemCapabilities = {
com.apple.ApplicationGroups.iOS = {
enabled = 1;
Expand All @@ -339,6 +340,7 @@
62A4276C1B5A3ED800BB4AD9 = {
CreatedOnToolsVersion = 7.0;
DevelopmentTeam = LND7PS23X9;
LastSwiftMigration = 0810;
};
};
};
Expand Down Expand Up @@ -569,6 +571,7 @@
SKIP_INSTALL = NO;
SWIFT_OBJC_BRIDGING_HEADER = "ShadowVPN/ShadowVPN-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -588,6 +591,7 @@
PROVISIONING_PROFILE = "";
SKIP_INSTALL = NO;
SWIFT_OBJC_BRIDGING_HEADER = "ShadowVPN/ShadowVPN-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -605,6 +609,7 @@
PROVISIONING_PROFILE = "";
SKIP_INSTALL = YES;
SWIFT_OBJC_BRIDGING_HEADER = "$(SRCROOT)/tunnel/tunnel-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -622,6 +627,7 @@
PROVISIONING_PROFILE = "";
SKIP_INSTALL = YES;
SWIFT_OBJC_BRIDGING_HEADER = "$(SRCROOT)/tunnel/tunnel-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
16 changes: 8 additions & 8 deletions ShadowVPN/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window = UIWindow(frame: UIScreen.main.bounds)
if let window = self.window {
let viewController = UINavigationController(rootViewController: MainViewController(style: .Grouped))
let viewController = UINavigationController(rootViewController: MainViewController(style: .grouped))
window.rootViewController = viewController
window.makeKeyAndVisible()
}
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
2 changes: 1 addition & 1 deletion ShadowVPN/ConfigurationTextCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConfigurationTextCell: UITableViewCell {
var textField: UITextField = UITextField()

init() {
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
super.init(style: UITableViewCellStyle.default, reuseIdentifier: nil)
self.contentView.addSubview(textField)
}

Expand Down
34 changes: 29 additions & 5 deletions ShadowVPN/ConfigurationValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,36 @@
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}


class ConfigurationValidator: NSObject {

// return nil if there's no error
class func validateIP(ip: String) -> String? {
let parts = ip.componentsSeparatedByString(".")
class func validateIP(_ ip: String) -> String? {
let parts = ip.components(separatedBy: ".")
if parts.count != 4 {
return "Invalid IP: " + ip
}
Expand All @@ -26,7 +50,7 @@ class ConfigurationValidator: NSObject {
}

// return nil if there's no error
class func validate(configuration: [String: AnyObject]) -> String? {
class func validate(_ configuration: [String: AnyObject]) -> String? {
// 1. server must be not empty
if configuration["server"] == nil || configuration["server"]?.length == 0 {
return "Server must not be empty"
Expand All @@ -46,7 +70,7 @@ class ConfigurationValidator: NSObject {
// 4. usertoken must be empty or hex of 8 bytes
if configuration["usertoken"] != nil {
if let usertoken = configuration["usertoken"] as? String {
if NSData.fromHexString(usertoken).length != 8 && NSData.fromHexString(usertoken).length != 0 {
if Data.fromHexString(usertoken).count != 8 && Data.fromHexString(usertoken).count != 0 {
return "Usertoken must be HEX of 8 bytes (example: 7e335d67f1dc2c01)"
}
}
Expand Down Expand Up @@ -76,7 +100,7 @@ class ConfigurationValidator: NSObject {
return "DNS must not be empty"
}
if let dns = configuration["dns"] as? String {
let ips = dns.componentsSeparatedByString(",")
let ips = dns.components(separatedBy: ",")
if ips.count == 0 {
return "DNS must not be empty"
}
Expand Down
98 changes: 49 additions & 49 deletions ShadowVPN/ConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,52 @@ class ConfigurationViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Save, target: self, action: "save")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(ConfigurationViewController.save))
self.title = providerManager?.protocolConfiguration?.serverAddress
let conf:NETunnelProviderProtocol = self.providerManager?.protocolConfiguration as! NETunnelProviderProtocol
// Dictionary in Swift is a struct. This is a copy
self.configuration = conf.providerConfiguration!
self.configuration = conf.providerConfiguration! as [String : AnyObject]
}

func updateConfiguration() {
for (k, v) in self.bindMap {
self.configuration[k] = v.text
self.configuration[k] = v.text as AnyObject?
}
// self.configuration["route"] = "chnroutes"
}

func save() {
updateConfiguration()
if let result = ConfigurationValidator.validate(self.configuration) {
let alertController = UIAlertController(title: "Error", message: result, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { (action) -> Void in
let alertController = UIAlertController(title: "Error", message: result, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (action) -> Void in
}))
self.presentViewController(alertController, animated: true, completion: { () -> Void in
self.present(alertController, animated: true, completion: { () -> Void in
})
return
}
(self.providerManager?.protocolConfiguration as! NETunnelProviderProtocol).providerConfiguration = self.configuration
self.providerManager?.protocolConfiguration?.serverAddress = self.configuration["server"] as? String
self.providerManager?.localizedDescription = self.configuration["server"] as? String

self.providerManager?.saveToPreferencesWithCompletionHandler { (error) -> Void in
self.navigationController?.popViewControllerAnimated(true)
self.providerManager?.saveToPreferences { (error) -> Void in
self.navigationController?.popViewController(animated: true)
}
}

func bindData(textField: UITextField, property: String) {
func bindData(_ textField: UITextField, property: String) {
let val: AnyObject? = configuration[property]
if let val = val {
textField.text = String(val)
textField.text = String(describing: val)
}
bindMap[property] = textField
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 10
Expand All @@ -73,11 +73,11 @@ class ConfigurationViewController: UITableViewController {
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = ConfigurationTextCell()
cell.selectionStyle = .None
cell.selectionStyle = .none
switch indexPath.row {
case 0:
cell.textLabel?.text = "Description"
Expand All @@ -86,69 +86,69 @@ class ConfigurationViewController: UITableViewController {
case 1:
cell.textLabel?.text = "Server"
cell.textField.placeholder = "Server IP"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
bindData(cell.textField, property: "server")
case 2:
cell.textLabel?.text = "Port"
cell.textField.placeholder = "Server Port"
cell.textField.text = "1123"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.keyboardType = .NumberPad
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.textField.keyboardType = .numberPad
bindData(cell.textField, property: "port")
case 3:
cell.textLabel?.text = "Password"
cell.textField.placeholder = "Required"
cell.textField.text = ""
cell.textField.secureTextEntry = true
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.isSecureTextEntry = true
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
bindData(cell.textField, property: "password")
case 4:
cell.textLabel?.text = "User Token"
cell.textField.placeholder = "Optional"
cell.textField.text = ""
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
bindData(cell.textField, property: "usertoken")
case 5:
cell.textLabel?.text = "IP"
cell.textField.placeholder = "Required"
cell.textField.text = "10.7.0.2"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.keyboardType = .DecimalPad
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.textField.keyboardType = .decimalPad
bindData(cell.textField, property: "ip")
case 6:
cell.textLabel?.text = "Subnet"
cell.textField.placeholder = "Required"
cell.textField.text = "255.255.255.0"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.keyboardType = .DecimalPad
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.textField.keyboardType = .decimalPad
bindData(cell.textField, property: "subnet")
case 7:
cell.textLabel?.text = "DNS"
cell.textField.placeholder = "DNS Server Address"
cell.textField.text = "114.114.114.114,223.5.5.5,8.8.8.8,8.8.4.4,208.67.222.222"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
bindData(cell.textField, property: "dns")
case 8:
cell.textLabel?.text = "MTU"
cell.textField.placeholder = "MTU"
cell.textField.text = "1350"
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
cell.textField.keyboardType = .NumberPad
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.textField.keyboardType = .numberPad
bindData(cell.textField, property: "mtu")
case 9:
cell.textLabel?.text = "Route"
cell.textField.text = "chnroutes"
cell.textField.enabled = false
cell.accessoryType = .DisclosureIndicator
cell.selectionStyle = .Default
cell.textField.isEnabled = false
cell.accessoryType = .disclosureIndicator
cell.selectionStyle = .default
bindData(cell.textField, property: "route")
return cell
default:
Expand All @@ -158,35 +158,35 @@ class ConfigurationViewController: UITableViewController {
case 1:
let cell = UITableViewCell()
cell.textLabel?.text = "Delete This Configuration"
cell.textLabel?.textColor = UIColor.redColor()
cell.textLabel?.textColor = UIColor.red
return cell
default:
return UITableViewCell()
}
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if (indexPath.section == 0) {
if (indexPath.row == 9) {
let controller = SimpleTableViewController(labels: ["Default", "CHNRoutes"], values: ["default", "chnroutes"], initialValue: self.configuration["route"] as? String, selectionBlock: { (result) -> Void in
let controller = SimpleTableViewController(labels: ["Default", "CHNRoutes"], values: ["default", "chnroutes"], initialValue: self.configuration["route"] as? String as NSObject!, selectionBlock: { (result) -> Void in
// else we'll lost unsaved modifications
self.updateConfiguration()
self.configuration["route"] = result
self.tableView.reloadData()
})
self.navigationController?.pushViewController(controller, animated: true)
self.navigationController?.pushViewController(controller!, animated: true)
}
} else if (indexPath.section == 1) {
let alertController = UIAlertController(title: nil, message: "Delete this configuration?", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Delete", style: .Destructive, handler: { (action) -> Void in
self.providerManager?.removeFromPreferencesWithCompletionHandler({ (error) -> Void in
self.navigationController?.popViewControllerAnimated(true)
let alertController = UIAlertController(title: nil, message: "Delete this configuration?", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (action) -> Void in
self.providerManager?.removeFromPreferences(completionHandler: { (error) -> Void in
self.navigationController?.popViewController(animated: true)
})
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
}))
self.presentViewController(alertController, animated: true, completion: { () -> Void in
self.present(alertController, animated: true, completion: { () -> Void in
})
}
}
Expand Down
Loading