- Notifications
You must be signed in to change notification settings - Fork3
chore: support operating the VPN without the app#36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
cf9cfd0
951fc1a
f617f6f
b7c9f58
27ab4a7
c0a46aa
95bf6b5
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -47,9 +47,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { | ||
} | ||
} | ||
// This function MUST eventually call `NSApp.reply(toApplicationShouldTerminate: true)` | ||
// or return `.terminateNow` | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func applicationShouldTerminate(_: NSApplication) -> NSApplication.TerminateReply { | ||
if !settings.stopVPNOnQuit { return .terminateNow } | ||
Task { | ||
await vpn.stop() | ||
NSApp.reply(toApplicationShouldTerminate: true) | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
return .terminateLater | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -24,13 +24,12 @@ enum NetworkExtensionState: Equatable { | ||
/// An actor that handles configuring, enabling, and disabling the VPN tunnel via the | ||
/// NetworkExtension APIs. | ||
extension CoderVPNService { | ||
func hasNetworkExtensionConfig() async -> Bool { | ||
do { | ||
_ =try await getTunnelManager() | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
@@ -71,37 +70,29 @@ extension CoderVPNService { | ||
} | ||
} | ||
funcstartTunnel() async { | ||
do { | ||
let tm = try await getTunnelManager() | ||
try tm.connection.startVPNTunnel() | ||
} catch { | ||
logger.error("start tunnel: \(error)") | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
neState = .failed(error.localizedDescription) | ||
return | ||
} | ||
logger.debug("started tunnel") | ||
neState = .enabled | ||
} | ||
funcstopTunnel() async { | ||
do { | ||
let tm = try await getTunnelManager() | ||
tm.connection.stopVPNTunnel() | ||
} catch { | ||
logger.error("stop tunnel: \(error)") | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
neState = .failed(error.localizedDescription) | ||
return | ||
} | ||
logger.debug("stopped tunnel") | ||
neState = .disabled | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -41,7 +41,6 @@ enum VPNServiceError: Error, Equatable { | ||
final class CoderVPNService: NSObject, VPNService { | ||
var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "vpn") | ||
lazy var xpc: VPNXPCInterface = .init(vpn: self) | ||
var workspaces: [UUID: String] = [:] | ||
@Published var tunnelState: VPNServiceState = .disabled | ||
@@ -68,8 +67,14 @@ final class CoderVPNService: NSObject, VPNService { | ||
super.init() | ||
installSystemExtension() | ||
Task { | ||
neState = if await hasNetworkExtensionConfig() { | ||
.disabled | ||
} else { | ||
.unconfigured | ||
} | ||
} | ||
xpc.connect() | ||
xpc.getPeerState() | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(vpnDidUpdate(_:)), | ||
@@ -82,6 +87,11 @@ final class CoderVPNService: NSObject, VPNService { | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
func clearPeers() { | ||
agents = [:] | ||
workspaces = [:] | ||
} | ||
func start() async { | ||
switch tunnelState { | ||
case .disabled, .failed: | ||
@@ -90,31 +100,18 @@ final class CoderVPNService: NSObject, VPNService { | ||
return | ||
} | ||
awaitstartTunnel() | ||
xpc.connect() | ||
xpc.ping() | ||
logger.debug("network extension enabled") | ||
} | ||
func stop() async { | ||
guard tunnelState == .connected else { return } | ||
awaitstopTunnel() | ||
logger.info("network extension stopped") | ||
} | ||
func configureTunnelProviderProtocol(proto: NETunnelProviderProtocol?) { | ||
Task { | ||
if let proto { | ||
@@ -145,6 +142,22 @@ final class CoderVPNService: NSObject, VPNService { | ||
} | ||
} | ||
func onExtensionPeerState(_ data: Data?) { | ||
guard let data else { | ||
logger.error("could not retrieve peer state from network extension, it may not be running") | ||
return | ||
} | ||
logger.info("received network extension peer state") | ||
do { | ||
let msg = try Vpn_PeerUpdate(serializedBytes: data) | ||
debugPrint(msg) | ||
clearPeers() | ||
applyPeerUpdate(with: msg) | ||
Comment on lines +154 to +155 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Doesn't this clear call make handling the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||
} catch { | ||
logger.error("failed to decode peer update \(error)") | ||
} | ||
} | ||
func applyPeerUpdate(with update: Vpn_PeerUpdate) { | ||
// Delete agents | ||
update.deletedAgents | ||
@@ -204,9 +217,6 @@ extension CoderVPNService { | ||
} | ||
switch connection.status { | ||
case .disconnected: | ||
connection.fetchLastDisconnectError { err in | ||
self.tunnelState = if let err { | ||
.failed(.internalError(err.localizedDescription)) | ||