-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
85 lines (75 loc) · 2.26 KB
/
Copy pathsession.go
File metadata and controls
85 lines (75 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
func generateRandomToken() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
// Critical: cannot generate secure random token
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
return hex.EncodeToString(b)
}
// listSessions lists all sessions from the relay, grouped by working directory
func listSessions(relayClient *RelayClient) {
sessions, err := relayClient.ListAllSessions()
if err != nil {
fmt.Printf("%sError: could not list sessions: %v%s\n", red, err, reset)
return
}
if len(sessions) == 0 {
fmt.Printf("%sNo sessions found.%s\n", dim, reset)
return
}
// Group by working directory
groups := make(map[string][]SessionInfo)
var order []string
for _, s := range sessions {
if _, exists := groups[s.WorkingDir]; !exists {
order = append(order, s.WorkingDir)
}
groups[s.WorkingDir] = append(groups[s.WorkingDir], s)
}
fmt.Printf("%sSessions:%s\n\n", bold, reset)
for _, wd := range order {
fmt.Printf(" %s%s%s\n", cyan, wd, reset)
for _, s := range groups[wd] {
fmt.Printf(" %s %s %s\n", s.ID, s.AgentType, s.CreatedAt)
}
}
fmt.Println()
}
// killAllSessions purges all sessions for this PC from the relay
func killAllSessions(relayClient *RelayClient) {
count, err := relayClient.PurgeAllSessions()
if err != nil {
fmt.Printf("%sError: could not purge sessions: %v%s\n", red, err, reset)
return
}
if count > 0 {
fmt.Printf("%s✓ Killed %d session(s).%s\n", green, count, reset)
} else {
fmt.Printf("%sNo sessions to kill.%s\n", dim, reset)
}
}
// killSessionByID deletes a specific session by ID
func killSessionByID(id string, relayClient *RelayClient) {
sessions, err := relayClient.ListAllSessions()
if err != nil {
fmt.Printf("%sError: could not query sessions: %v%s\n", red, err, reset)
return
}
for _, s := range sessions {
if s.ID == id || (len(id) >= 8 && len(s.ID) >= 8 && s.ID[:8] == id[:8]) {
if err := relayClient.DeleteSession(s.ID); err != nil {
fmt.Printf("%sError: could not delete session: %v%s\n", red, err, reset)
return
}
fmt.Printf("%s✓ Killed session %s (%s)%s\n", green, s.ID[:8]+"...", s.WorkingDir, reset)
return
}
}
fmt.Printf("%sError: session not found: %s%s\n", red, id, reset)
}