-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0a896d
commit c08f8ab
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...unityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/SessionsService/SessionServiceError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// SessionsServiceError.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Lika Vorobeva on 29.11.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
enum SessionsServiceError: LocalizedError { | ||
case invalidURL | ||
|
||
case connectionParsingFailed | ||
case nodeMisconfigured | ||
case noQuota | ||
|
||
case serverLocalized(String) | ||
case other(Error) | ||
} | ||
|
||
extension SessionsServiceError { | ||
static var allCases: [SessionsServiceError] { | ||
[.connectionParsingFailed, .nodeMisconfigured, .noQuota] | ||
} | ||
|
||
var innerCodes: [Int] { | ||
switch self { | ||
case .nodeMisconfigured: | ||
return [3, 4, 5] | ||
case .noQuota: | ||
return [9, 10] | ||
case .connectionParsingFailed: | ||
return [6, 7, 8] | ||
default: | ||
return [] | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...CommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/SessionsService/SessionsService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// SessionsService.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Lika Vorobeva on 20.07.2022. | ||
// | ||
|
||
import Foundation | ||
import SentinelWallet | ||
import SOLARAPI | ||
import WireGuardKit | ||
import Vapor | ||
|
||
final class SessionsService { | ||
private let walletService: WalletService | ||
private let sessionProvider: NodeSessionProviderType | ||
private let subscriptionsProvider: SubscriptionsProviderType | ||
|
||
init( | ||
sessionProvider: NodeSessionProviderType, | ||
subscriptionsProvider: SubscriptionsProviderType, | ||
walletService: WalletService | ||
) { | ||
self.sessionProvider = sessionProvider | ||
self.subscriptionsProvider = subscriptionsProvider | ||
self.walletService = walletService | ||
} | ||
} | ||
|
||
// MARK: - SubscriptionsServiceType | ||
|
||
extension SessionsService: SessionsServiceType { | ||
func loadActiveSessions(completion: @escaping (Result<[SentinelWallet.Session], Error>) -> Void) { | ||
subscriptionsProvider.queryActiveSessions(for: walletService.currentWalletAddress, completion: completion) | ||
} | ||
|
||
func stopActiveSessions(completion: @escaping (Result<Void, Error>) -> Void) { | ||
guard let sender = walletService.createTransactionSender() else { | ||
completion(.failure(SubscriptionsServiceError.missingMnemonic)) | ||
return | ||
} | ||
|
||
subscriptionsProvider.stopActiveSessions(sender: sender, completion: completion) | ||
} | ||
|
||
func startSession(on subscriptionID: UInt64, node: String, completion: @escaping (Result<UInt64, Error>) -> Void) { | ||
guard let sender = walletService.createTransactionSender() else { | ||
completion(.failure(SubscriptionsServiceError.missingMnemonic)) | ||
return | ||
} | ||
|
||
subscriptionsProvider.startNewSession(on: subscriptionID, sender: sender, node: node, completion: completion) | ||
} | ||
|
||
func fetchConnectionData( | ||
remoteURLString: String, | ||
id: UInt64, | ||
accountAddress: String, | ||
signature: String, | ||
completion: @escaping (Result<(Data, PrivateKey), SessionsServiceError>) -> Void | ||
) { | ||
guard var components = URLComponents(string: remoteURLString) else { | ||
completion(.failure(SessionsServiceError.invalidURL)) | ||
return | ||
} | ||
components.scheme = "http" | ||
|
||
guard let urlString = components.string, let remoteURL = URL(string: urlString) else { | ||
completion(.failure(SessionsServiceError.invalidURL)) | ||
return | ||
} | ||
let wgKey = PrivateKey() | ||
|
||
sessionProvider.createClient( | ||
remoteURL: remoteURL, | ||
address: accountAddress, | ||
id: "\(id)", | ||
request: .init(key: wgKey.publicKey.base64Key, signature: signature)) { result in | ||
switch result { | ||
case .success(let infoResult): | ||
guard infoResult.success, let stringData = infoResult.result else { | ||
completion(.failure(.connectionParsingFailed)) | ||
return | ||
} | ||
guard let data = Data(base64Encoded: stringData), data.bytes.count == 58 else { | ||
completion(.failure(.connectionParsingFailed)) | ||
return | ||
} | ||
|
||
completion(.success((data, wgKey))) | ||
case .failure(let error): | ||
#warning("TODO: map error") | ||
// let mapper = Self.mapNetworkError(handleSpecificGenericErrors: { error -> SessionsServiceError? in | ||
// return SessionsServiceError.allCases.first(where: { $0.innerCodes.contains(error.code) }) | ||
// }) | ||
// completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...unityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/SessionsService/SessionsServiceType.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// SessionsServiceType.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Lika Vorobeva on 20.07.2022. | ||
// | ||
|
||
import Foundation | ||
import SentinelWallet | ||
import WireGuardKit | ||
|
||
protocol SessionsServiceType { | ||
func loadActiveSessions(completion: @escaping (Result<[Session], Error>) -> Void) | ||
func stopActiveSessions(completion: @escaping (Result<Void, Error>) -> Void) | ||
func startSession(on subscriptionID: UInt64, node: String, completion: @escaping (Result<UInt64, Error>) -> Void) | ||
func fetchConnectionData( | ||
remoteURLString: String, | ||
id: UInt64, | ||
accountAddress: String, | ||
signature: String, | ||
completion: @escaping (Result<(Data, PrivateKey), SessionsServiceError>) -> Void | ||
) | ||
} |