NEVPNManager with L2TP Protocol - swift

I'm working with VPN and I ask this question but now I would like to create a VPN profile using L2TP Protocol, not the IPSec protocol.
I have all the information I need (user, server, password, pre sharedKey) and the service is correctly ON. I'm trying so to create an App that simply connect to the VPN by creating the right Setting Profile like the app '1.1.1.1' in the App Store.
I'm using the NEVPNProtocolIPSec Class but I think is wrong. The is no class for L2PT protocol?
In the device setting I'm able to manually configure the VPN for L2PT but how ca I do it using NEVPNManager??
Here my code:
class VPN {
let vpnManager = NEVPNManager.shared();
private var vpnLoadHandler: (Error?) -> Void { return
{ (error:Error?) in
if ((error) != nil) {
print("Could not load VPN Configurations")
return;
}
let p = NEVPNProtocolIPSec()
p.username = "myUsername"
p.serverAddress = "myAddressServer"
p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
let kcs = KeychainService();
kcs.save(key: "SHARED", value: "sharedPsw")
kcs.save(key: "VPN_PASSWORD", value: "password")
p.sharedSecretReference = kcs.load(key: "SHARED")
p.passwordReference = kcs.load(key: "VPN_PASSWORD")
p.useExtendedAuthentication = true
p.disconnectOnSleep = false
self.vpnManager.protocolConfiguration = p
self.vpnManager.localizedDescription = "myDescription"
self.vpnManager.isEnabled = true
self.vpnManager.isOnDemandEnabled = true
self.vpnManager.saveToPreferences(completionHandler: self.vpnSaveHandler)
}
}
private var vpnSaveHandler: (Error?) -> Void { return
{ (error:Error?) in
if (error != nil) {
print("Could not save VPN Configurations")
return
} else {
do {
try self.vpnManager.connection.startVPNTunnel()
} catch let error {
print("Error starting VPN Connection \(error.localizedDescription)");
}
}
}
}
public func connectVPN() {
//For no known reason the process of saving/loading the VPN configurations fails.On the 2nd time it works
do {
try self.vpnManager.loadFromPreferences(completionHandler: self.vpnLoadHandler)
} catch let error {
print("Could not start VPN Connection: \(error.localizedDescription)" )
}
}
public func disconnectVPN() ->Void {
vpnManager.connection.stopVPNTunnel()
}
I't working but it create a VPN Configuration for IPSec but I want the L2PT. Can somebody else help me please?
Maybe somebody face the same problem.

Related

How to configure AWS S3Client to use Cognito credentials provider? - AWS Swift SDK

I am trying to give an unathenticated application access to certain AWS services and understand I can do so by setting up a Federated Identity Pool and then letting the application request an ID using the identityPoolID.
func authenticate() async {
print("authenticating...")
if let id = UserDefaults.standard.string(forKey: cognitoIDKey) {
print("Current ID: \(id)")
return
}
let input = GetIdInput(identityPoolId: identityPoolID)
do {
if let id = try await client?.getId(input: input), let identityId = id.identityId {
print("Success getting id: \(identityId)")
UserDefaults.standard.set(identityId, forKey: cognitoIDKey)
} else {
UserDefaults.standard.set(nil, forKey: cognitoIDKey)
}
} catch {
print("Error getting id: \(error)")
}
}
How can I now use this to configure the S3Client such that the permissions granted to this identity pool are applied ?
The only thing I can see that might apply is the credentialsProvider in the S3ClientConfiguration.
public init() async {
do {
let defaultConfig = try DefaultSDKRuntimeConfiguration("S3Client",
clientLogMode: .requestAndResponse)
let s3Config = try await S3Client.S3ClientConfiguration(runtimeConfig: defaultConfig
credentialsProvider: ???)
let s3 = S3Client(config: s3Config)
client = try await S3Client()
} catch {
print("ERROR: ", dump(error, name: "Initializing s3 client"))
exit(1)
}
}

singleton property set with async\await func is nil after called later

a property from a singleton is nil when checkin is value.
calling this function from viewDidLoad like this
Task {
do {
try await CXOneChat.shared.connect(environment: .NA1, brandId: 1111, channelId: "keyID")
self.checkForConfig()
} catch {
print(error.localizedDescription)
}
}
this connect function checks for several issues and load from network the config
public func connect(environment: Environment, brandId: Int, channelId: String) async throws {
self.environment = environment
self.brandId = brandId
self.channelId = channelId
try connectToSocket()
channelConfig = try await loadChannelConfiguration()
generateDestinationId()
generateVisitor()
if customer == nil {
customer = Customer(senderId: UUID().uuidString, displayName: "")
try await authorizeCustomer()
} else if authorizationCode.isEmpty{
try await reconnectCustomer()
} else {
try await authorizeCustomer()
}
}
this work ok the self.checkForConfig does some stuff with config in singleton object.
after tapping a button and go for another ViewController. call this func
func loadThread() {
do {
if CXOneChat.shared.getChannelConfiguration()?.settings.hasMultipleThreadsPerEndUser ?? false {
try CXOneChat.shared.loadThreads()
} else {
try CXOneChat.shared.loadThread()
}
} catch {
print(error)
}
}
depending on the config call one or another config value load one or the function. in this case the getChannelConfiguration() is nil and call to loadThread().
func loadThread(threadId: UUID? = nil) throws {
guard let config = channelConfig else {
throw CXOneChatError.missingChannelConfig
}
let eventType = config.isLiveChat ? EventType.recoverLivechat : EventType.recoverThread
guard let brandId = brandId else { throw CXOneChatError.invalidBrandId }
guard let channelId = channelId else { throw CXOneChatError.invalidChannelId }
guard let id = getIdentity(with: false) else { throw CXOneChatError.invalidCustomerId }
let retrieveThread = EventFactory.shared.recoverLivechatThreadEvent(brandId: brandId, channelId: channelId, customer: id, eventType: eventType, threadId: threadId)
guard let data = getDataFrom(retrieveThread) else { throw CXOneChatError.invalidData }
let string = getStringFromData(data)
socketService.send(message: string)
}
here checks for he config in singleton object but throws error because config is nil.
my question is why is nil config in this check. the config is downloaded and store. but when get the values got a null value. Im missing some code here or what I is wrong with this.

Vapor 3 - How to check for similar email before saving object

I would like to create a route to let users update their data (e.g. changing their email or their username). To make sure a user cannot use the same username as another user, I would like to check if a user with the same username already exists in the database.
I have already made the username unique in the migrations.
I have a user model that looks like this:
struct User: Content, SQLiteModel, Migration {
var id: Int?
var username: String
var name: String
var email: String
var password: String
var creationDate: Date?
// Permissions
var staff: Bool = false
var superuser: Bool = false
init(username: String, name: String, email: String, password: String) {
self.username = username
self.name = name
self.email = email
self.password = password
self.creationDate = Date()
}
}
This is the piece of code where I want to use it:
func create(_ req: Request) throws -> EventLoopFuture<User> {
return try req.content.decode(UserCreationRequest.self).flatMap { userRequest in
// Check if `userRequest.email` already exists
// If if does -> throw Abort(.badRequest, reason: "Email already in use")
// Else -> Go on with creation
let digest = try req.make(BCryptDigest.self)
let hashedPassword = try digest.hash(userRequest.password)
let persistedUser = User(name: userRequest.name, email: userRequest.email, password: hashedPassword)
return persistedUser.save(on: req)
}
}
I could do it like this (see next snippet) but it seems a strange option as it requires a lot of nesting when more checks for e.g. uniqueness would have to be performed (for instance in the case of updating a user).
func create(_ req: Request) throws -> EventLoopFuture<User> {
return try req.content.decode(UserCreationRequest.self).flatMap { userRequest in
let userID = userRequest.email
return User.query(on: req).filter(\.userID == userID).first().flatMap { existingUser in
guard existingUser == nil else {
throw Abort(.badRequest, reason: "A user with this email already exists")
}
let digest = try req.make(BCryptDigest.self)
let hashedPassword = try digest.hash(userRequest.password)
let persistedUser = User(name: userRequest.name, email: userRequest.email, password: hashedPassword)
return persistedUser.save(on: req)
}
}
}
As one of the answers suggested I've tried to add Error middleware (see next snippet) but this does not correctly catch the error (maybe I am doing something wrong in the code - just started with Vapor).
import Vapor
import FluentSQLite
enum InternalError: Error {
case emailDuplicate
}
struct EmailDuplicateErrorMiddleware: Middleware {
func respond(to request: Request, chainingTo next: Responder) throws -> EventLoopFuture<Response> {
let response: Future<Response>
do {
response = try next.respond(to: request)
} catch is SQLiteError {
response = request.eventLoop.newFailedFuture(error: InternalError.emailDuplicate)
}
return response.catchFlatMap { error in
if let response = error as? ResponseEncodable {
do {
return try response.encode(for: request)
} catch {
return request.eventLoop.newFailedFuture(error: InternalError.emailDuplicate)
}
} else {
return request.eventLoop.newFailedFuture(error: error)
}
}
}
}
The quick way of doing it is to do something like User.query(on: req).filter(\.email == email).count() and check that equals 0 before attempting the save.
However, whilst this will work fine for almost everyone, you still risk edge cases where two users try to register with the same username at the exact same time - the only way to handle this is to catch the save failure, check if it was because the unique constraint on the email and return the error to the user. However the chances of you actually hitting that are pretty rare, even for big apps.
I would make the field unique in the model using a Migration such as:
extension User: Migration {
static func prepare(on connection: SQLiteConnection) -> Future<Void> {
return Database.create(self, on: connection) { builder in
try addProperties(to: builder)
builder.unique(on: \.email)
}
}
}
If you use a default String as the field type for email, then you will need to reduce it as this creates a field VARCHAR(255) which is too big for a UNIQUE key. I would then use a bit of custom Middleware to trap the error that arises when a second attempt to save a record is made using the same email.
struct DupEmailErrorMiddleware: Middleware
{
func respond(to request: Request, chainingTo next: Responder) throws -> EventLoopFuture<Response>
{
let response: Future<Response>
do {
response = try next.respond(to: request)
} catch is MySQLError {
// needs a bit more sophistication to check the specific error
response = request.eventLoop.newFailedFuture(error: InternalError.dupEmail)
}
return response.catchFlatMap
{
error in
if let response = error as? ResponseEncodable
{
do
{
return try response.encode(for: request)
}
catch
{
return request.eventLoop.newFailedFuture(error: InternalError.dupEmail)
}
} else
{
return request.eventLoop.newFailedFuture(error: error )
}
}
}
}
EDIT:
Your custom error needs to be something like:
enum InternalError: Debuggable, ResponseEncodable
{
func encode(for request: Request) throws -> EventLoopFuture<Response>
{
let response = request.response()
let eventController = EventController()
//TODO make this return to correct view
eventController.message = reason
return try eventController.index(request).map
{
html in
try response.content.encode(html)
return response
}
}
case dupEmail
var identifier:String
{
switch self
{
case .dupEmail: return "dupEmail"
}
}
var reason:String
{
switch self
{
case .dupEmail: return "Email address already used"
}
}
}
In the code above, the actual error is displayed to the user by setting a value in the controller, which is then picked up in the view and an alert displayed. This method allows a general-purpose error handler to take care of displaying the error messages. However, in your case, it might be that you could just create the response in the catchFlatMap.

Display all available WIFI connections with Swift in OS X

I'm trying to display all available WIFI connections. It doesn't work. Here is my code:
import Foundation
import CoreWLAN
var cwInterface = CWInterface()
do {
let routers = try cwInterface.scanForNetworksWithSSID(nil)
print(routers)
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
I don't get any result. What I'm doing wrong?
It works if you initialize CWInterface with an interface name, like "en1".
But it's better to not use harcoded names, so we'll also use CWWiFiClient.sharedWiFiClient().interface() which returns the default WIFI interface.
Example of a class to manage all this:
class Discovery {
var currentInterface: CWInterface
var interfacesNames: [String] = []
var networks: Set<CWNetwork> = []
// Failable init using default interface
init?() {
if let defaultInterface = CWWiFiClient.sharedWiFiClient().interface(),
name = defaultInterface.interfaceName {
self.currentInterface = defaultInterface
self.interfacesNames.append(name)
self.findNetworks()
} else {
return nil
}
}
// Init with the literal interface name, like "en1"
init(interfaceWithName name: String) {
self.currentInterface = CWInterface(interfaceName: name)
self.interfacesNames.append(name)
self.findNetworks()
}
// Fetch detectable WIFI networks
func findNetworks() {
do {
self.networks = try currentInterface.scanForNetworksWithSSID(nil)
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
}
Call it with the default interface:
if let discovery = Discovery() {
print(discovery.networks)
for network in discovery.networks {
print(network.ssid!)
}
}
Or with an interface name:
let discovery = Discovery(interfaceWithName: "en1")
let results = discovery.networks
Results contains all the scanned networks:
[<CWNetwork: 0x608000001bd0> [ssid=SomeNetworkName, bssid=xxxx, security=WPA Enterprise, rssi=xx, channel=<CWChannel: 0x600000004fb0> [channelNumber=11(2GHz), channelWidth={20MHz}], ibss=0], etc]

Google's nearby messages: not receiving any messages

I am trying out Google's nearby messages API, which seems to be easy to use, but it's for some reason not working as expected. I suspect that the problem is something trivial, but I have not been able to solve this.
I double-checked that the API-key is correct and I have also added permissions for NSMicrophoneUsageDescription and NSBluetoothPeripheralUsageDescription in the Info.plist.
The Nearby Messages API is enabled in Google's developer console and the API keys has been set to be restricted to the app's bundle identifier. It won't work either if this restrictions is removed.
class ViewController: UIViewController {
private var messageManager: GNSMessageManager?
override func viewDidLoad() {
super.viewDidLoad()
GNSMessageManager.setDebugLoggingEnabled(true)
messageManager = GNSMessageManager(apiKey: "<my-api-key>", paramsBlock: { (params: GNSMessageManagerParams?) -> Void in
guard let params = params else { return }
params.microphonePermissionErrorHandler = { hasError in
if hasError {
print("Nearby works better if microphone use is allowed")
}
}
params.bluetoothPermissionErrorHandler = { hasError in
if hasError {
print("Nearby works better if Bluetooth use is allowed")
}
}
params.bluetoothPowerErrorHandler = { hasError in
if hasError {
print("Nearby works better if Bluetooth is turned on")
}
}
})
// publish
messageManager?.publication(with: GNSMessage(content: "Hello".data(using: .utf8)))
// subscribe
messageManager?.subscription(messageFoundHandler: { message in
print("message received: \(String(describing: message))")
}, messageLostHandler: { message in
print("message lost: \(String(describing: message))")
})
}
}
Did anybody else have issues setting this up?
Ok, for whoever has the same problem, the solution was quite simple and almost embarrassing. It is necessary to hold the publication and the subscription result in a class variable:
private var publication: GNSPublication?
private var subscription: GNSSubscription?
override func viewDidLoad() {
super.viewDidLoad()
messageManager = GNSMessageManager(apiKey: "<my-api-key>")
// publish
publication = messageManager?.publication(with: GNSMessage(content: "Hello".data(using: .utf8)))
// subscribe
subscription = messageManager?.subscription(messageFoundHandler: { message in
print("message received: \(String(describing: message))")
}, messageLostHandler: { message in
print("message lost: \(String(describing: message))")
})
}