Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: display offline workspaces#41

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
ethanndickson merged 7 commits intomainfromethan/offline-workspaces
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletionCoder Desktop/Coder Desktop/About.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
import SwiftUI

enumAbout{
publicstaticletrepo:String="https://github.com/coder/coder-desktop-macos"
privatestaticvarcredits:NSAttributedString{
letcoder=NSMutableAttributedString(
string:"Coder.com",
Expand All@@ -21,7 +22,7 @@ enum About {
string:"GitHub",
attributes:[
.foregroundColor:NSColor.labelColor,
.link:NSURL(string:"https://github.com/coder/coder-desktop-macos")!,
.link:NSURL(string:About.repo)!,
.font:NSFont.systemFont(ofSize:NSFont.systemFontSize),
]
)
Expand Down
26 changes: 13 additions & 13 deletionsCoder Desktop/Coder Desktop/Preview Content/PreviewVPN.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,29 +3,29 @@ import SwiftUI

@MainActor
finalclass PreviewVPN:Coder_Desktop.VPNService{
@Publishedvarstate:Coder_Desktop.VPNServiceState=.disabled
@Publishedvaragents:[UUID:Coder_Desktop.Agent]=[
UUID():Agent(id:UUID(), name:"dev", status:.error,copyableDNS:"asdf.coder", wsName:"dogfood2",
@Publishedvarstate:Coder_Desktop.VPNServiceState=.connected
@PublishedvarmenuState:VPNMenuState=.init(agents:[
UUID():Agent(id:UUID(), name:"dev", status:.error,hosts:["asdf.coder"], wsName:"dogfood2",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.okay,copyableDNS:"asdf.coder",
UUID():Agent(id:UUID(), name:"dev", status:.okay,hosts:["asdf.coder"],
wsName:"testing-a-very-long-name", wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.warn,copyableDNS:"asdf.coder", wsName:"opensrc",
UUID():Agent(id:UUID(), name:"dev", status:.warn,hosts:["asdf.coder"], wsName:"opensrc",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.off,copyableDNS:"asdf.coder", wsName:"gvisor",
UUID():Agent(id:UUID(), name:"dev", status:.off,hosts:["asdf.coder"], wsName:"gvisor",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.off,copyableDNS:"asdf.coder", wsName:"example",
UUID():Agent(id:UUID(), name:"dev", status:.off,hosts:["asdf.coder"], wsName:"example",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.error,copyableDNS:"asdf.coder", wsName:"dogfood2",
UUID():Agent(id:UUID(), name:"dev", status:.error,hosts:["asdf.coder"], wsName:"dogfood2",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.okay,copyableDNS:"asdf.coder",
UUID():Agent(id:UUID(), name:"dev", status:.okay,hosts:["asdf.coder"],
wsName:"testing-a-very-long-name", wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.warn,copyableDNS:"asdf.coder", wsName:"opensrc",
UUID():Agent(id:UUID(), name:"dev", status:.warn,hosts:["asdf.coder"], wsName:"opensrc",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.off,copyableDNS:"asdf.coder", wsName:"gvisor",
UUID():Agent(id:UUID(), name:"dev", status:.off,hosts:["asdf.coder"], wsName:"gvisor",
wsID:UUID()),
UUID():Agent(id:UUID(), name:"dev", status:.off,copyableDNS:"asdf.coder", wsName:"example",
UUID():Agent(id:UUID(), name:"dev", status:.off,hosts:["asdf.coder"], wsName:"example",
wsID:UUID()),
]
], workspaces:[:])
letshouldFail:Bool
letlongError="This is a long error to test the UI with long error messages"

Expand Down
140 changes: 140 additions & 0 deletionsCoder Desktop/Coder Desktop/VPNMenuState.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
import Foundation
import SwiftUI
import VPNLib

structAgent:Identifiable,Equatable,Comparable{
letid:UUID
letname:String
letstatus:AgentStatus
lethosts:[String]
letwsName:String
letwsID:UUID

// Agents are sorted by status, and then by name
staticfunc<(lhs:Agent, rhs:Agent)->Bool{
if lhs.status!= rhs.status{
return lhs.status< rhs.status
}
return lhs.wsName.localizedCompare(rhs.wsName)==.orderedAscending
}

// Hosts arrive sorted by length, the shortest looks best in the UI.
varprimaryHost:String?{ hosts.first}
}

enumAgentStatus:Int,Equatable,Comparable{
case okay=0
case warn=1
case error=2
case off=3

publicvarcolor:Color{
switchself{
case.okay:.green
case.warn:.yellow
case.error:.red
case.off:.gray
}
}

staticfunc<(lhs:AgentStatus, rhs:AgentStatus)->Bool{
lhs.rawValue< rhs.rawValue
}
}

structWorkspace:Identifiable,Equatable,Comparable{
letid:UUID
letname:String
varagents:Set<UUID>

staticfunc<(lhs:Workspace, rhs:Workspace)->Bool{
lhs.name.localizedCompare(rhs.name)==.orderedAscending
}
}

structVPNMenuState{
varagents:[UUID:Agent]=[:]
varworkspaces:[UUID:Workspace]=[:]
// Upserted agents that don't belong to any known workspace, have no FQDNs,
// or have any invalid UUIDs.
varinvalidAgents:[Vpn_Agent]=[]

mutatingfunc upsertAgent(_ agent:Vpn_Agent){
guard
let id=UUID(uuidData: agent.id),
let wsID=UUID(uuidData: agent.workspaceID),
var workspace=workspaces[wsID],
!agent.fqdn.isEmpty
else{
invalidAgents.append(agent)
return
}
// An existing agent with the same name, belonging to the same workspace
// is from a previous workspace build, and should be removed.
agents.filter{ $0.value.name== agent.name && $0.value.wsID== wsID}
.forEach{agents[$0.key]=nil}
workspace.agents.insert(id)
workspaces[wsID]= workspace

agents[id]=Agent(
id: id,
name: agent.name,
// If last handshake was not within last five minutes, the agent is unhealthy
status: agent.lastHandshake.date>Date.now.addingTimeInterval(-300)?.okay:.warn,
// Remove trailing dot if present
hosts: agent.fqdn.map{ $0.hasSuffix(".")?String($0.dropLast()): $0},
wsName: workspace.name,
wsID: wsID
)
}

mutatingfunc deleteAgent(withId id:Data){
guardlet agentUUID=UUID(uuidData: id)else{return}
// Update Workspaces
iflet agent=agents[agentUUID],var ws=workspaces[agent.wsID]{
ws.agents.remove(agentUUID)
workspaces[agent.wsID]= ws
}
agents[agentUUID]=nil
// Remove from invalid agents if present
invalidAgents.removeAll{ invalidAgentin
invalidAgent.id== id
}
}

mutatingfunc upsertWorkspace(_ workspace:Vpn_Workspace){
guardlet wsID=UUID(uuidData: workspace.id)else{return}
workspaces[wsID]=Workspace(id: wsID, name: workspace.name, agents:[])
// Check if we can associate any invalid agents with this workspace
invalidAgents.filter{ agentin
agent.workspaceID== workspace.id
}.forEach{ agentin
invalidAgents.removeAll{ $0== agent}
upsertAgent(agent)
}
}

mutatingfunc deleteWorkspace(withId id:Data){
guardlet wsID=UUID(uuidData: id)else{return}
agents.filter{ _, valuein
value.wsID== wsID
}.forEach{ key, _in
agents[key]=nil
}
workspaces[wsID]=nil
}

varsorted:[VPNMenuItem]{
varitems= agents.values.map{VPNMenuItem.agent($0)}
// Workspaces with no agents are shown as offline
items+= workspaces.filter{ _, valuein
value.agents.isEmpty
}.map{VPNMenuItem.offlineWorkspace(Workspace(id: $0.key, name: $0.value.name, agents: $0.value.agents))}
return items.sorted()
}

mutatingfunc clear(){
agents.removeAll()
workspaces.removeAll()
}
}
64 changes: 8 additions & 56 deletionsCoder Desktop/Coder Desktop/VPNService.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,7 @@ import VPNLib
@MainActor
protocolVPNService:ObservableObject{
varstate:VPNServiceState{get}
varagents:[UUID:Agent]{get}
varmenuState:VPNMenuState{get}
func start()async
func stop()async
func configureTunnelProviderProtocol(proto:NETunnelProviderProtocol?)
Expand DownExpand Up@@ -41,7 +41,6 @@ enum VPNServiceError: Error, Equatable {
finalclassCoderVPNService:NSObject,VPNService{
varlogger=Logger(subsystem:Bundle.main.bundleIdentifier!, category:"vpn")
lazyvarxpc:VPNXPCInterface=.init(vpn:self)
varworkspaces:[UUID:String]=[:]

@PublishedvartunnelState:VPNServiceState=.disabled
@PublishedvarsysExtnState:SystemExtensionState=.uninstalled
Expand All@@ -56,7 +55,7 @@ final class CoderVPNService: NSObject, VPNService {
return tunnelState
}

@Publishedvaragents:[UUID:Agent]=[:]
@PublishedvarmenuState:VPNMenuState=.init()

// systemExtnDelegate holds a reference to the SystemExtensionDelegate so that it doesn't get
// garbage collected while the OSSystemExtensionRequest is in flight, since the OS framework
Expand DownExpand Up@@ -85,11 +84,6 @@ final class CoderVPNService: NSObject, VPNService {
NotificationCenter.default.removeObserver(self)
}

func clearPeers(){
agents=[:]
workspaces=[:]
}

func start()async{
switch tunnelState{
case.disabled,.failed:
Expand DownExpand Up@@ -150,7 +144,7 @@ final class CoderVPNService: NSObject, VPNService {
do{
letmsg=tryVpn_PeerUpdate(serializedBytes: data)
debugPrint(msg)
clearPeers()
menuState.clear()
applyPeerUpdate(with: msg)
}catch{
logger.error("failed to decode peer update\(error)")
Expand All@@ -159,53 +153,11 @@ final class CoderVPNService: NSObject, VPNService {

func applyPeerUpdate(with update:Vpn_PeerUpdate){
// Delete agents
update.deletedAgents
.compactMap{UUID(uuidData: $0.id)}
.forEach{ agentIDin
agents[agentID]=nil
}
update.deletedWorkspaces
.compactMap{UUID(uuidData: $0.id)}
.forEach{ workspaceIDin
workspaces[workspaceID]=nil
for(id, agent)in agentswhere agent.wsID== workspaceID{
agents[id]=nil
}
}

// Update workspaces
forworkspaceProtoin update.upsertedWorkspaces{
iflet workspaceID=UUID(uuidData: workspaceProto.id){
workspaces[workspaceID]= workspaceProto.name
}
}

foragentProtoin update.upsertedAgents{
guardlet agentID=UUID(uuidData: agentProto.id)else{
continue
}
guardlet workspaceID=UUID(uuidData: agentProto.workspaceID)else{
continue
}
letworkspaceName=workspaces[workspaceID]??"Unknown Workspace"
letnewAgent=Agent(
id: agentID,
name: agentProto.name,
// If last handshake was not within last five minutes, the agent is unhealthy
status: agentProto.lastHandshake.date>Date.now.addingTimeInterval(-300)?.okay:.off,
copyableDNS: agentProto.fqdn.first??"UNKNOWN",
wsName: workspaceName,
wsID: workspaceID
)

// An existing agent with the same name, belonging to the same workspace
// is from a previous workspace build, and should be removed.
agents
.filter{ $0.value.name== agentProto.name && $0.value.wsID== workspaceID}
.forEach{agents[$0.key]=nil}

agents[agentID]= newAgent
}
update.deletedAgents.forEach{ menuState.deleteAgent(withId: $0.id)}
update.deletedWorkspaces.forEach{ menuState.deleteWorkspace(withId: $0.id)}
// Upsert workspaces before agents to populate agent workspace names
update.upsertedWorkspaces.forEach{ menuState.upsertWorkspace($0)}
update.upsertedAgents.forEach{ menuState.upsertAgent($0)}
}
}

Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp