Skip to content

Commit 90cce22

Browse files
committed
Adds URL scheme
Adds the ability to start and stop the VPN connection via a custom URL scheme.
1 parent 56101e5 commit 90cce22

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

LocalDevVPN/Info.plist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>CFBundleURLTypes</key>
6+
<array>
7+
<dict>
8+
<key>CFBundleURLName</key>
9+
<string>com.localdevvpn</string>
10+
<key>CFBundleURLSchemes</key>
11+
<array>
12+
<string>localdevvpn</string>
13+
</array>
14+
</dict>
15+
</array>
516
<key>NSBonjourServices</key>
617
<array>
718
<string>_apple-mobdev2._tcp</string>

LocalDevVPN/StosVPNApp.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ struct LocalDevVPNApp: App {
1212
var body: some Scene {
1313
WindowGroup {
1414
ContentView()
15+
.onOpenURL { url in
16+
handleURL(url)
17+
}
18+
}
19+
}
20+
21+
private func handleURL(_ url: URL) {
22+
guard url.scheme == "localdevvpn" else { return }
23+
24+
let tunnelManager = TunnelManager.shared
25+
26+
switch url.host {
27+
case "enable":
28+
tunnelManager.startVPN()
29+
if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
30+
let schemeParam = components.queryItems?.first(where: { $0.name == "scheme" })?.value {
31+
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
32+
let callbackURL = URL(string: "\(schemeParam)://")!
33+
UIApplication.shared.open(callbackURL)
34+
}
35+
}
36+
case "disable":
37+
tunnelManager.stopVPN()
38+
if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
39+
let schemeParam = components.queryItems?.first(where: { $0.name == "scheme" })?.value {
40+
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
41+
let callbackURL = URL(string: "\(schemeParam)://")!
42+
UIApplication.shared.open(callbackURL)
43+
}
44+
}
45+
default:
46+
break
1547
}
1648
}
1749
}

0 commit comments

Comments
 (0)