-
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
0631877
commit 9b28634
Showing
9 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
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
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
52 changes: 52 additions & 0 deletions
52
...unityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/DNSRouteCollection.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,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<String, Error>) 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) | ||
} | ||
} | ||
} | ||
|
||
|
24 changes: 24 additions & 0 deletions
24
...CommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSRequest.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,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 | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/DNS/PostDNSResponse.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,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 | ||
} |
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
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
File renamed without changes.
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