@@ -20,20 +20,65 @@ class URLHandler {
2020 guard deployment. host ( ) == url. host else {
2121 throw . invalidAuthority( url. host ( ) ?? " <none> " )
2222 }
23+ let route : CoderRoute
2324 do {
24- switch try router. match ( url: url) {
25- case let . open( workspace, agent, type) :
26- switch type {
27- case let . rdp( creds) :
28- handleRDP ( workspace: workspace, agent: agent, creds: creds)
29- }
30- }
25+ route = try router. match ( url: url)
3126 } catch {
3227 throw . matchError( url: url)
3328 }
3429
35- func handleRDP( workspace _: String , agent _: String , creds _: RDPCredentials ) {
36- // TODO: Handle RDP
30+ switch route {
31+ case let . open( workspace, agent, type) :
32+ switch type {
33+ case let . rdp( creds) :
34+ try handleRDP ( workspace: workspace, agent: agent, creds: creds)
35+ }
36+ }
37+ }
38+
39+ private func handleRDP( workspace: String , agent: String , creds: RDPCredentials ) throws ( URLError) {
40+ guard vpn. state == . connected else {
41+ throw . openError( . coderConnectOffline)
42+ }
43+
44+ guard let workspace = vpn. menuState. findWorkspace ( name: workspace) else {
45+ throw . openError( . invalidWorkspace( workspace: workspace) )
46+ }
47+
48+ guard let agent = vpn. menuState. findAgent ( workspaceID: workspace. id, name: agent) else {
49+ throw . openError( . invalidAgent( workspace: workspace. name, agent: agent) )
50+ }
51+
52+ var rdpString = " rdp:full address=s: \( agent. primaryHost) :3389 "
53+ if let username = creds. username {
54+ rdpString += " &username=s: \( username) "
55+ }
56+ guard let url = URL ( string: rdpString) else {
57+ throw . openError( . couldNotCreateRDPURL( rdpString) )
58+ }
59+
60+ let alert = NSAlert ( )
61+ alert. messageText = " Opening RDP "
62+ alert. informativeText = " Connecting to \( agent. primaryHost) . "
63+ if let username = creds. username {
64+ alert. informativeText += " \n Username: \( username) "
65+ }
66+ if creds. password != nil {
67+ alert. informativeText += " \n The password will be copied to your clipboard. "
68+ }
69+
70+ alert. alertStyle = . informational
71+ alert. addButton ( withTitle: " Open " )
72+ alert. addButton ( withTitle: " Cancel " )
73+ let response = alert. runModal ( )
74+ if response == . alertFirstButtonReturn {
75+ if let password = creds. password {
76+ NSPasteboard . general. clearContents ( )
77+ NSPasteboard . general. setString ( password, forType: . string)
78+ }
79+ NSWorkspace . shared. open ( url)
80+ } else {
81+ // User cancelled
3782 }
3883 }
3984}
0 commit comments