Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. fileprivate func updateCurrentVPN(withServer server: Server, completion: ((Bool) -> Void)?) {
  2.  
  3. let passwordRef = KeychainWrapper.standard.dataRef(forKey: self.account.username)
  4. let secretRef = KeychainWrapper.standard.dataRef(forKey: API.ipsecSecretKey())
  5.  
  6. // checking if the strings are correct in a debugger
  7. let password = KeychainWrapper.standard.string(forKey: self.account.username)
  8. let secret = KeychainWrapper.standard.string(forKey: API.ipsecSecretKey())
  9.  
  10. // VPN
  11. let manager = NEVPNManager.shared()
  12. manager.loadFromPreferences { (error) in
  13. if (error != nil)
  14. {
  15. print("Error: ", error.debugDescription)
  16. completion?(false)
  17. }
  18. else
  19. {
  20. let prtcl = NEVPNProtocolIPSec()
  21. prtcl.username = self.account.username
  22. prtcl.passwordReference = passwordRef
  23. prtcl.serverAddress = server.host
  24. prtcl.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
  25. prtcl.sharedSecretReference = secretRef
  26. prtcl.localIdentifier = "Test iOS device"
  27. prtcl.remoteIdentifier = server.name
  28. prtcl.useExtendedAuthentication = true
  29. prtcl.disconnectOnSleep = false
  30.  
  31. manager.isEnabled = true
  32. manager.protocolConfiguration = prtcl
  33. manager.isOnDemandEnabled = false
  34. manager.localizedDescription = "MyVPN Configuration"
  35. manager.saveToPreferences(completionHandler: { (error) in
  36. if (error != nil)
  37. {
  38. print("Error: ", error.debugDescription)
  39. completion?(false)
  40. }
  41. else
  42. {
  43. self.serverButton.setTitle(server.name, for: UIControlState.normal)
  44. print("VPN prefs saved")
  45. completion?(true)
  46. }
  47. })
  48. }
  49. }
  50.  
  51. let manager = NEVPNManager.shared()
  52. do {
  53. try manager.connection.startVPNTunnel()
  54. } catch {
  55. print(error.localizedDescription)
  56. connectButton.setTitle("da failure :(", for: UIControlState.normal)
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement