- Notifications
You must be signed in to change notification settings - Fork5
chore: add handler and router forcoder
scheme URIs#145
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
19 changes: 16 additions & 3 deletionsCoder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
This file contains hidden or 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
15 changes: 15 additions & 0 deletionsCoder-Desktop/Coder-Desktop/Info.plist
This file contains hidden or 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
39 changes: 39 additions & 0 deletionsCoder-Desktop/Coder-Desktop/URLHandler.swift
This file contains hidden or 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,39 @@ | ||
import Foundation | ||
import VPNLib | ||
@MainActor | ||
class URLHandler { | ||
let state: AppState | ||
let vpn: any VPNService | ||
let router: CoderRouter | ||
init(state: AppState, vpn: any VPNService) { | ||
self.state = state | ||
self.vpn = vpn | ||
router = CoderRouter() | ||
} | ||
func handle(_ url: URL) throws(RouterError) { | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
guard state.hasSession, let deployment = state.baseAccessURL else { | ||
throw .noSession | ||
} | ||
guard deployment.host() == url.host else { | ||
throw .invalidAuthority(url.host() ?? "<none>") | ||
} | ||
do { | ||
switch try router.match(url: url) { | ||
case let .open(workspace, agent, type): | ||
switch type { | ||
case let .rdp(creds): | ||
handleRDP(workspace: workspace, agent: agent, creds: creds) | ||
} | ||
} | ||
} catch { | ||
throw .matchError(url: url) | ||
} | ||
func handleRDP(workspace _: String, agent _: String, creds _: RDPCredentials) { | ||
// TODO: Handle RDP | ||
} | ||
} | ||
} |
Binary file addedCoder-Desktop/Resources/1024Icon.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletionsCoder-Desktop/VPNLib/CoderRouter.swift
This file contains hidden or 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,64 @@ | ||
import Foundation | ||
import URLRouting | ||
// This is in VPNLib to avoid depending on `swift-collections` in both the app & extension. | ||
public struct CoderRouter: ParserPrinter { | ||
public init() {} | ||
public var body: some ParserPrinter<URLRequestData, CoderRoute> { | ||
Route(.case(CoderRoute.open(workspace:agent:route:))) { | ||
Scheme("coder") | ||
// v0/open/ws/<workspace>/agent/<agent>/<openType> | ||
Path { "v0"; "open"; "ws"; Parse(.string); "agent"; Parse(.string) } | ||
openRouter | ||
} | ||
} | ||
var openRouter: some ParserPrinter<URLRequestData, OpenRoute> { | ||
OneOf { | ||
Route(.memberwise(OpenRoute.rdp)) { | ||
Path { "rdp" } | ||
Query { | ||
Parse(.memberwise(RDPCredentials.init)) { | ||
Optionally { Field("username") } | ||
Optionally { Field("password") } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
public enum RouterError: Error { | ||
case invalidAuthority(String) | ||
case matchError(url: URL) | ||
case noSession | ||
public var description: String { | ||
switch self { | ||
case let .invalidAuthority(authority): | ||
"Authority '\(authority)' does not match the host of the current Coder deployment." | ||
case let .matchError(url): | ||
"Failed to handle \(url.absoluteString) because the format is unsupported." | ||
case .noSession: | ||
"Not logged in." | ||
} | ||
} | ||
public var localizedDescription: String { description } | ||
} | ||
public enum CoderRoute: Equatable, Sendable { | ||
case open(workspace: String, agent: String, route: OpenRoute) | ||
} | ||
public enum OpenRoute: Equatable, Sendable { | ||
case rdp(RDPCredentials) | ||
} | ||
// Due to a Swift Result builder limitation, we can't flatten this out to `case rdp(String?, String?)` | ||
// https://github.com/pointfreeco/swift-url-routing/issues/50 | ||
public struct RDPCredentials: Equatable, Sendable { | ||
public let username: String? | ||
public let password: String? | ||
} |
108 changes: 108 additions & 0 deletionsCoder-Desktop/VPNLibTests/CoderRouterTests.swift
This file contains hidden or 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,108 @@ | ||
import Foundation | ||
import Testing | ||
import URLRouting | ||
@testable import VPNLib | ||
@MainActor | ||
@Suite(.timeLimit(.minutes(1))) | ||
struct CoderRouterTests { | ||
let router: CoderRouter | ||
init() { | ||
router = CoderRouter() | ||
} | ||
struct RouteTestCase: CustomStringConvertible, Sendable { | ||
let urlString: String | ||
let expectedRoute: CoderRoute? | ||
let description: String | ||
} | ||
@Test("RDP routes", arguments: [ | ||
// Valid routes | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/myworkspace/agent/dev/rdp?username=user&password=pass", | ||
expectedRoute: .open( | ||
workspace: "myworkspace", | ||
agent: "dev", | ||
route: .rdp(RDPCredentials(username: "user", password: "pass")) | ||
), | ||
description: "RDP with username and password" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp", | ||
expectedRoute: .open( | ||
workspace: "workspace-123", | ||
agent: "agent-456", | ||
route: .rdp(RDPCredentials(username: nil, password: nil)) | ||
), | ||
description: "RDP without credentials" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp?username=user", | ||
expectedRoute: .open( | ||
workspace: "workspace-123", | ||
agent: "agent-456", | ||
route: .rdp(RDPCredentials(username: "user", password: nil)) | ||
), | ||
description: "RDP with username only" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp?password=pass", | ||
expectedRoute: .open( | ||
workspace: "workspace-123", | ||
agent: "agent-456", | ||
route: .rdp(RDPCredentials(username: nil, password: "pass")) | ||
), | ||
description: "RDP with password only" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/ws-special-chars/agent/agent-with-dashes/rdp", | ||
expectedRoute: .open( | ||
workspace: "ws-special-chars", | ||
agent: "agent-with-dashes", | ||
route: .rdp(RDPCredentials(username: nil, password: nil)) | ||
), | ||
description: "RDP with special characters in workspace and agent IDs" | ||
), | ||
// Invalid routes | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/invalid/path", | ||
expectedRoute: nil, | ||
description: "Completely invalid path" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v1/open/ws/workspace-123/agent/agent-456/rdp", | ||
expectedRoute: nil, | ||
description: "Invalid version prefix (v1 instead of v0)" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/workspace-123/agent/agent-456/rdp", | ||
expectedRoute: nil, | ||
description: "Missing 'ws' segment" | ||
), | ||
RouteTestCase( | ||
urlString: "coder://coder.example.com/v0/open/ws/workspace-123/rdp", | ||
expectedRoute: nil, | ||
description: "Missing agent segment" | ||
), | ||
RouteTestCase( | ||
urlString: "http://coder.example.com/v0/open/ws/workspace-123/agent/agent-456", | ||
expectedRoute: nil, | ||
description: "Wrong scheme" | ||
), | ||
]) | ||
func testRdpRoutes(testCase: RouteTestCase) throws { | ||
let url = URL(string: testCase.urlString)! | ||
if let expectedRoute = testCase.expectedRoute { | ||
let route = try router.match(url: url) | ||
#expect(route == expectedRoute) | ||
} else { | ||
#expect(throws: (any Error).self) { | ||
_ = try router.match(url: url) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletionsCoder-Desktop/project.yml
This file contains hidden or 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
1 change: 1 addition & 0 deletionsMakefile
This file contains hidden or 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
1 change: 1 addition & 0 deletionsscripts/build.sh
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.