diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift index 4af33da..5777022 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift @@ -12,13 +12,19 @@ protocol NoContext {} final class CommonContext { let nodesService: NodesServiceType + let tunnelManager: TunnelManagerType init( - nodesService: NodesServiceType + nodesService: NodesServiceType, + tunnelManager: TunnelManagerType ) { self.nodesService = nodesService + self.tunnelManager = tunnelManager } } protocol HasNodesService { var nodesService: NodesServiceType { get } } extension CommonContext: HasNodesService {} + +protocol HasTunnelManager { var tunnelManager: TunnelManagerType { get } } +extension CommonContext: HasTunnelManager {} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift index f8b101a..3799775 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift @@ -11,12 +11,17 @@ import SOLARAPI /// This class should configure all required services and inject them into a Context final class ContextBuilder { func buildContext() -> CommonContext { + let generalSettingsStorage = GeneralSettingsStorage() + let nodesProvider = NodesProvider(configuration: .init(baseURL: ClientConstants.backendURL)) let nodesService = NodesService(nodesProvider: nodesProvider) + let tunnelManager = TunnelManager(storage: generalSettingsStorage) + return CommonContext( - nodesService: nodesService + nodesService: nodesService, + tunnelManager: tunnelManager ) } } diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift index 5627dc7..4fd586d 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift @@ -25,6 +25,7 @@ extension DVPNServer { do { let api = app.grouped(.init(stringLiteral: ClientConstants.apiPath)) try api.register(collection: NodesRouteCollection(context: context)) + try api.register(collection: TunnelRouteCollection(context: context)) try app.start() } catch { fatalError(error.localizedDescription) diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/PostConnectionRequest.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/PostConnectionRequest.swift new file mode 100644 index 0000000..01bbe0d --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/PostConnectionRequest.swift @@ -0,0 +1,24 @@ +// +// PostConnectionRequest.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Lika Vorobeva on 30.09.2022. +// + +import Foundation + +struct PostConnectionRequest: Codable { + let nodeAddress: String + + init(nodeAddress: String) { + self.nodeAddress = nodeAddress + } +} + +// MARK: - Codable implementation + +extension PostConnectionRequest { + enum CodingKeys: String, CodingKey { + case nodeAddress + } +} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/TunnelRouteCollection.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/TunnelRouteCollection.swift new file mode 100644 index 0000000..8380300 --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Routes/TunnelRouteCollection.swift @@ -0,0 +1,29 @@ +// +// TunnelRouteCollection.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Lika Vorobeva on 30.09.2022. +// + +import Vapor + +struct TunnelRouteCollection: RouteCollection { + let context: HasTunnelManager + + func boot(routes: RoutesBuilder) throws { + routes.post("connection", use: createNewSession) + routes.delete("connection", use: startDeactivationOfActiveTunnel) + } +} + +extension TunnelRouteCollection { + func startDeactivationOfActiveTunnel(_ req: Request) async throws -> Bool { + return !context.tunnelManager.startDeactivationOfActiveTunnel() + } + + func createNewSession(_ req: Request) async throws -> Bool { + let continentCode = req.query[String.self, at: PostConnectionRequest.CodingKeys.nodeAddress.rawValue] + + return true + } +}