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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package config
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"time"
)
// SessionState is the on-disk record for one active Colab runtime.
type SessionState struct {
Endpoint string `json:"endpoint"`
URL string `json:"url"`
Token string `json:"token"`
TokenExpiresAt time.Time `json:"tokenExpiresAt"`
KernelID string `json:"kernelId,omitempty"`
}
// Valid returns true if the session's proxy token has not yet expired.
func (s *SessionState) Valid() bool {
return s.Token != "" && time.Now().Before(s.TokenExpiresAt)
}
type sessionFile struct {
Sessions map[string]*SessionState `json:"sessions"`
}
func sessionsPath() (string, error) {
dir, err := Dir()
if err != nil {
return "", err
}
return filepath.Join(dir, "sessions.json"), nil
}
func loadSessionFile() (*sessionFile, error) {
path, err := sessionsPath()
if err != nil {
return nil, err
}
f, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
return &sessionFile{Sessions: map[string]*SessionState{}}, nil
}
if err != nil {
return nil, err
}
defer f.Close()
var sf sessionFile
if err := json.NewDecoder(f).Decode(&sf); err != nil {
return &sessionFile{Sessions: map[string]*SessionState{}}, nil
}
if sf.Sessions == nil {
sf.Sessions = map[string]*SessionState{}
}
return &sf, nil
}
func saveSessionFile(sf *sessionFile) error {
dir, err := Dir()
if err != nil {
return err
}
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
path, err := sessionsPath()
if err != nil {
return err
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(sf)
}
// SaveSession persists a session under the given endpoint key.
func SaveSession(s *SessionState) error {
sf, err := loadSessionFile()
if err != nil {
return err
}
sf.Sessions[s.Endpoint] = s
return saveSessionFile(sf)
}
// LoadSession returns the saved session for the given endpoint, or nil if not
// found or the token has expired.
func LoadSession(endpoint string) (*SessionState, error) {
sf, err := loadSessionFile()
if err != nil {
return nil, err
}
s, ok := sf.Sessions[endpoint]
if !ok {
return nil, nil
}
return s, nil
}
// ListSessions returns all saved sessions regardless of token validity.
func ListSessions() ([]*SessionState, error) {
sf, err := loadSessionFile()
if err != nil {
return nil, err
}
out := make([]*SessionState, 0, len(sf.Sessions))
for _, s := range sf.Sessions {
out = append(out, s)
}
return out, nil
}
// DeleteSession removes a saved session by endpoint.
func DeleteSession(endpoint string) error {
sf, err := loadSessionFile()
if err != nil {
return err
}
delete(sf.Sessions, endpoint)
return saveSessionFile(sf)
}
|