-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the encrypted DNS FFI as a transport provider
- Loading branch information
1 parent
c61a054
commit f08f1e5
Showing
6 changed files
with
142 additions
and
14 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
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
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,61 @@ | ||
// | ||
// EncryptedDNSProxy.swift | ||
// MullvadRustRuntime | ||
// | ||
// Created by Emils on 24/09/2024. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadRustRuntimeProxy | ||
|
||
enum EncryptedDnsProxyError: Error { | ||
case start(err: Int32) | ||
} | ||
|
||
public class EncryptedDNSProxy { | ||
private var proxyConfig: ProxyHandle | ||
private var stateLock = NSLock() | ||
private var didStart = false | ||
private let state: OpaquePointer | ||
|
||
public init() { | ||
state = encrypted_dns_proxy_init() | ||
proxyConfig = ProxyHandle(context: nil, port: 0) | ||
} | ||
|
||
public func localPort() -> UInt16 { | ||
stateLock.lock() | ||
defer { stateLock.unlock() } | ||
return proxyConfig.port | ||
} | ||
|
||
public func start() throws { | ||
stateLock.lock() | ||
defer { stateLock.unlock() } | ||
guard didStart == false else { return } | ||
|
||
let err = encrypted_dns_proxy_start(state, &proxyConfig) | ||
if err != 0 { | ||
throw EncryptedDnsProxyError.start(err: err) | ||
} | ||
didStart = true | ||
} | ||
|
||
public func stop() { | ||
stateLock.lock() | ||
defer { stateLock.unlock() } | ||
guard didStart == true else { return } | ||
didStart = false | ||
|
||
encrypted_dns_proxy_stop(&proxyConfig) | ||
} | ||
|
||
deinit { | ||
if didStart { | ||
encrypted_dns_proxy_stop(&proxyConfig) | ||
} | ||
|
||
encrypted_dns_proxy_free(state) | ||
} | ||
} |
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