diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj index b6f2e05..f77abb3 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj @@ -38,6 +38,9 @@ 225A83A328EB14B200F66619 /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22C3EF0028E6FD95007DB01B /* PacketTunnelProvider.swift */; }; 225A83A628EC81E500F66619 /* WebSocketDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 225A83A528EC81E500F66619 /* WebSocketDelegate.swift */; }; 225A83AC28EDB3C100F66619 /* InfoEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 225A83AB28EDB3C100F66619 /* InfoEvent.swift */; }; + 225A83B028EED6F800F66619 /* DNSRouteCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 225A83AF28EED6F800F66619 /* DNSRouteCollection.swift */; }; + 225A83B228EED93C00F66619 /* PostDNSRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 225A83B128EED93C00F66619 /* PostDNSRequest.swift */; }; + 225A83B428EED95500F66619 /* PostDNSResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 225A83B328EED95500F66619 /* PostDNSResponse.swift */; }; 22C3EE9028E4638E007DB01B /* WireGuardNetworkExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 22C3EE8728E4638E007DB01B /* WireGuardNetworkExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 22C3EE9728E463D6007DB01B /* WireGuardKit in Frameworks */ = {isa = PBXBuildFile; productRef = 22C3EE9628E463D6007DB01B /* WireGuardKit */; }; 22C3EE9A28E464A1007DB01B /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 22C3EE9928E464A1007DB01B /* Alamofire */; }; @@ -206,6 +209,9 @@ 225A83A128EB06BF00F66619 /* NodesProviderType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NodesProviderType.swift; sourceTree = ""; }; 225A83A528EC81E500F66619 /* WebSocketDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketDelegate.swift; sourceTree = ""; }; 225A83AB28EDB3C100F66619 /* InfoEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoEvent.swift; sourceTree = ""; }; + 225A83AF28EED6F800F66619 /* DNSRouteCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSRouteCollection.swift; sourceTree = ""; }; + 225A83B128EED93C00F66619 /* PostDNSRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostDNSRequest.swift; sourceTree = ""; }; + 225A83B328EED95500F66619 /* PostDNSResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostDNSResponse.swift; sourceTree = ""; }; 22C3EE8728E4638E007DB01B /* WireGuardNetworkExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WireGuardNetworkExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 22C3EE9B28E467E4007DB01B /* SOLARdVPNCommunityCoreiOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SOLARdVPNCommunityCoreiOS.entitlements; sourceTree = ""; }; 22C3EE9D28E4733C007DB01B /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = ""; }; @@ -440,6 +446,25 @@ path = Models; sourceTree = ""; }; + 225A83AD28EED6AE00F66619 /* DNS */ = { + isa = PBXGroup; + children = ( + 225A83AF28EED6F800F66619 /* DNSRouteCollection.swift */, + 225A83B128EED93C00F66619 /* PostDNSRequest.swift */, + 225A83B328EED95500F66619 /* PostDNSResponse.swift */, + ); + path = DNS; + sourceTree = ""; + }; + 225A83AE28EED6C600F66619 /* Server */ = { + isa = PBXGroup; + children = ( + 225A83A528EC81E500F66619 /* WebSocketDelegate.swift */, + 92D6B41328E1E133004CF9DF /* DVPNServer.swift */, + ); + path = Server; + sourceTree = ""; + }; 22C3EE9C28E4733C007DB01B /* Shared */ = { isa = PBXGroup; children = ( @@ -562,11 +587,10 @@ isa = PBXGroup; children = ( 923C372328E72FDA003CFC03 /* RouteCollections */, + 225A83AE28EED6C600F66619 /* Server */, 92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */, - 92D6B41328E1E133004CF9DF /* DVPNServer.swift */, 92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */, 92D6B40028E19E20004CF9DF /* ViewController.swift */, - 225A83A528EC81E500F66619 /* WebSocketDelegate.swift */, ); path = Root; sourceTree = ""; @@ -605,9 +629,10 @@ 923C372328E72FDA003CFC03 /* RouteCollections */ = { isa = PBXGroup; children = ( - 923C373528EC7904003CFC03 /* Wallet */, + 225A83AD28EED6AE00F66619 /* DNS */, 923C372428E72FF0003CFC03 /* Nodes */, 225A83A428EC297500F66619 /* Tunnel */, + 923C373528EC7904003CFC03 /* Wallet */, ); path = RouteCollections; sourceTree = ""; @@ -1040,6 +1065,7 @@ 923C373128EB0CB2003CFC03 /* CountryExtra.swift in Sources */, 92D6B3FD28E19E20004CF9DF /* AppDelegate.swift in Sources */, 923C371C28E5DA05003CFC03 /* OrderType.swift in Sources */, + 225A83B228EED93C00F66619 /* PostDNSRequest.swift in Sources */, 225A838C28EAE01400F66619 /* SubscriptionsService.swift in Sources */, 92D6B46F28E47E2E004CF9DF /* NodesService.swift in Sources */, 22C3EEA728E4733C007DB01B /* Config.swift in Sources */, @@ -1064,6 +1090,7 @@ 225A838B28EAE01400F66619 /* SubscriptionsServiceType.swift in Sources */, 22C3EEAD28E4733C007DB01B /* ConfigurationParseError.swift in Sources */, 225A83A628EC81E500F66619 /* WebSocketDelegate.swift in Sources */, + 225A83B428EED95500F66619 /* PostDNSResponse.swift in Sources */, 22C3EED228E48B52007DB01B /* TunnelContainer.swift in Sources */, 22488A9A28E729E600FE29C3 /* GeneralSettingsStorage.swift in Sources */, 22C3EEFB28E48F95007DB01B /* NotificationToken.swift in Sources */, @@ -1098,6 +1125,7 @@ 923C373E28EDB109003CFC03 /* PostMnemonicResponse.swift in Sources */, 923C373328EB150A003CFC03 /* GetContinentResponse.swift in Sources */, 923C372A28EAFAA5003CFC03 /* NodesServiceError.swift in Sources */, + 225A83B028EED6F800F66619 /* DNSRouteCollection.swift in Sources */, 225A836C28EACE6C00F66619 /* ConnectionNodeModel.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift index c14d3ad..a8ea35e 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift @@ -116,3 +116,10 @@ extension CommonContext: HasWalletStorage { storage as StoresWallet } } + +protocol HasDNSServersStorage { var dnsServersStorage: StoresDNSServers { get } } +extension CommonContext: HasDNSServersStorage { + var dnsServersStorage: StoresDNSServers { + storage as StoresDNSServers + } +} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/DNSRouteCollection.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/DNSRouteCollection.swift new file mode 100644 index 0000000..e66e4de --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/DNSRouteCollection.swift @@ -0,0 +1,52 @@ +// +// DNSRouteCollection.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Lika Vorobeva on 06.10.2022. +// + +import Foundation +import Vapor + +private struct Constants { + let path: PathComponent = "dns" +} +private let constants = Constants() + +struct DNSRouteCollection: RouteCollection { + let context: HasDNSServersStorage & HasTunnelManager + + func boot(routes: RoutesBuilder) throws { + routes.get(constants.path, use: getAvailableDNS) + routes.put(constants.path, use: putDNS) + } +} + +extension DNSRouteCollection { + func getAvailableDNS(_ req: Request) async throws -> String { + try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation) in + let servers = DNSServerType.allCases.map { AvailableDNSServer(name: $0.rawValue, addresses: $0.address) } + let body = PostDNSResponse(servers: servers) + + Encoder.encode(model: body, continuation: continuation) + }) + } + + func putDNS(_ req: Request) throws -> Response { + do { + let body = try req.content.decode(PostDNSRequest.self) + let server = body.server + + guard let type = DNSServerType(rawValue: server) else { + return Response(status: .badRequest) + } + context.dnsServersStorage.set(dns: type) + context.tunnelManager.update(with: type.address) + return Response(status: .ok) + } catch { + return Response(status: .badRequest) + } + } +} + + diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSRequest.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSRequest.swift new file mode 100644 index 0000000..16dd2f2 --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSRequest.swift @@ -0,0 +1,24 @@ +// +// PostDNSRequest.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Lika Vorobeva on 06.10.2022. +// + +import Foundation + +struct PostDNSRequest: Codable { + let server: String + + init(server: String) { + self.server = server + } +} + +// MARK: - Codable implementation + +extension PostDNSRequest { + enum CodingKeys: String, CodingKey { + case server + } +} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSResponse.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSResponse.swift new file mode 100644 index 0000000..d061e03 --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSResponse.swift @@ -0,0 +1,17 @@ +// +// PostDNSResponse.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Lika Vorobeva on 06.10.2022. +// + +import Foundation + +struct PostDNSResponse: Codable { + let servers: [AvailableDNSServer] +} + +struct AvailableDNSServer: Codable { + let name: String + let addresses: String +} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Tunnel/TunnelRouteCollection.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Tunnel/TunnelRouteCollection.swift index dcaba4f..d888ce8 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Tunnel/TunnelRouteCollection.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Tunnel/TunnelRouteCollection.swift @@ -106,7 +106,6 @@ extension TunnelRouteCollection { } private func updateConnection(isConnected: Bool) { - log.debug(isConnected) let event = InfoEvent( type: constants.connectionType, value: isConnected ? "connected" : "disconnected" diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift similarity index 96% rename from SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift rename to SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift index b5ec78d..82730dc 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/DVPNServer.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift @@ -28,6 +28,7 @@ extension DVPNServer { let api = app.grouped(.init(stringLiteral: ClientConstants.apiPath)) try api.register(collection: NodesRouteCollection(context: context)) try api.register(collection: WalletRouteCollection(context: context)) + try api.register(collection: DNSRouteCollection(context: context)) try api.register( collection: TunnelRouteCollection( diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/WebSocketDelegate.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/WebSocketDelegate.swift similarity index 100% rename from SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/WebSocketDelegate.swift rename to SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/WebSocketDelegate.swift diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Manager/TunnelManager.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Manager/TunnelManager.swift index 07486dc..c34607c 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Manager/TunnelManager.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/Tunnel/Manager/TunnelManager.swift @@ -114,11 +114,11 @@ extension TunnelManager: TunnelManagerType { } public func update(with server: String) { - tunnelModel.interfaceModel[.dns] = server - - guard tunnelsService?.tunnels.last != nil else { + guard isTunnelActive else { return } + + tunnelModel.interfaceModel[.dns] = server upsertTunnel(startActivation: false) }