Newer
Older
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"database/sql"
"fmt"
"strings"
"time"
"git.autistici.org/ai3/go-common/sqlutil"
)
type location struct {
IP string `json:"ip"`
City string `json:"city,omitempty"`
Country string `json:"country,omitempty"`
}
type session struct {
ID string
BootTime int64
User string
LocalUser string
SSHKeyFingerprint string
StartedAt time.Time
EndedAt time.Time
EndedReboot bool
Location location
}
// A session is active if it lacks an EndedAt timestamp.
func (s *session) isActive(bTime int64) bool {
return s.EndedAt.IsZero() && s.BootTime == bTime
}
const (
entryTypeOpen = iota
entryTypeClose
)
func (s *session) logEntry(entryType int) *logEntry {
l := &logEntry{
ID: s.ID,
BootTime: s.BootTime,
User: s.User,
LocalUser: s.LocalUser,
SSHKeyFingerprint: s.SSHKeyFingerprint,
Location: s.Location,
}
switch entryType {
case entryTypeOpen:
l.Type = "ssh_open_session"
case entryTypeClose:
l.Type = "ssh_close_session"
l.Duration = int(s.EndedAt.Sub(s.StartedAt).Seconds())
}
return l
}
const timeFmt = "Mon Jan _2 15:04"
func (s *session) formatString(bTime int64) string {
locStr := s.Location.IP
var locAdd []string
if s.Location.City != "" {
locAdd = append(locAdd, s.Location.City)
}
if s.Location.Country != "" {
locAdd = append(locAdd, s.Location.Country)
}
if len(locAdd) > 0 {
locStr += fmt.Sprintf(" (%s)", strings.Join(locAdd, ", "))
}
if s.isActive(bTime) {
return fmt.Sprintf(
"%-15s %-15s %-30s %-30s %-16s still logged in",
s.User,
s.LocalUser,
s.SSHKeyFingerprint,
locStr,
s.StartedAt.Format(timeFmt),
)
}
var durationStr string
if s.BootTime != bTime {
durationStr = "reboot"
} else {
// Round duration to nearest second.
duration := s.EndedAt.Sub(s.StartedAt)
duration = time.Duration(duration.Seconds()) * time.Second
durationStr = fmt.Sprintf("%-16s (%s)", s.EndedAt.Format(timeFmt), duration)
}
return fmt.Sprintf(
"%-15s %-15s %-30s %-30s %-16s - %s",
s.User,
s.LocalUser,
s.SSHKeyFingerprint,
locStr,
s.StartedAt.Format(timeFmt),
durationStr,
)
}
type logEntry struct {
Type string `json:"log_type"`
ID string `json:"session_id"`
BootTime int64 `json:"btime"`
User string `json:"user"`
LocalUser string `json:"local_user"`
SSHKeyFingerprint string `json:"ssh_key_fp"`
Location location `json:"location"`
Duration int `json:"duration,omitempty"`
}
// The table does not have a primary key (except for the implicit
// rowid), because we can't guarantee the uniqueness of session_id
// except for knowing that there can't be two "open" sessions with the
// same ID at the same time.
var migrations = []func(*sql.Tx) error{
sqlutil.Statement(`
CREATE TABLE sessions (
session_id SMALLTEXT NOT NULL,
btime INTEGER NOT NULL,
user SMALLTEXT NOT NULL,
local_user SMALLTEXT NOT NULL,
ssh_key_fp TEXT NOT NULL,
started_at DATETIME NOT NULL,
ended_at DATETIME,
ended_reboot BOOLEAN,
location_ip SMALLTEXT,
location_country SMALLTEXT,
location_city SMALLTEXT
);
`, `
CREATE INDEX idx_sessions_id_btime ON sessions(session_id, btime)
`, `
CREATE INDEX idx_sessions_user ON sessions(user)
`),
}
func openDB(path string) (*sql.DB, error) {
return sqlutil.OpenDB(
path, sqlutil.WithMigrations(migrations))
}
func createSession(tx *sql.Tx, s *session) error {
_, err := tx.Exec(`
INSERT INTO sessions
(session_id, btime, user, local_user, ssh_key_fp, started_at, location_ip, location_country, location_city)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
s.ID, s.BootTime, s.User, s.LocalUser, s.SSHKeyFingerprint, s.StartedAt, s.Location.IP, s.Location.Country, s.Location.City,
)
return err
}
func closeSession(tx *sql.Tx, s *session, isReboot bool) error {
s.EndedAt = time.Now()
s.EndedReboot = isReboot
_, err := tx.Exec(`
UPDATE sessions SET
ended_at=?, ended_reboot=?
WHERE
session_id = ? AND btime = ? AND ended_at IS NULL
`,
s.EndedAt, s.EndedReboot, s.ID, s.BootTime,
)
return err
}
// Matches both sql.Row and sql.Rows.
type scanSource interface {
Scan(...any) error
}
// Field list must match the src.Scan() call in scanSessionRow.
var sessionFields = []string{
"session_id", "btime", "user", "local_user", "ssh_key_fp", "started_at", "ended_at", "ended_reboot", "location_ip", "location_country", "location_city",
}
func scanSessionQuery(where string) string {
return fmt.Sprintf("SELECT %s FROM sessions %s", strings.Join(sessionFields, ", "), where)
}
func scanSessionRow(src scanSource) (*session, error) {
var (
s session
endedAt sql.NullTime
endedReboot sql.NullBool
locCity, locCountry, locIP sql.NullString
)
if err := src.Scan(
&s.ID, &s.BootTime, &s.User, &s.LocalUser, &s.SSHKeyFingerprint, &s.StartedAt,
&endedAt, &endedReboot, &locIP, &locCountry, &locCity); err != nil {
return nil, err
}
if endedAt.Valid {
s.EndedAt = endedAt.Time
}
if endedReboot.Valid {
s.EndedReboot = endedReboot.Bool
}
if locIP.Valid {
s.Location.IP = locIP.String
}
if locCity.Valid {
s.Location.City = locCity.String
}
if locCountry.Valid {
s.Location.Country = locCountry.String
}
return &s, nil
}
func scanSessionsRows(rows *sql.Rows) ([]*session, error) {
var out []*session
for rows.Next() {
s, err := scanSessionRow(rows)
if err != nil {
return nil, err
}
out = append(out, s)
}
return out, rows.Err()
}
func getOpenSession(tx *sql.Tx, sessionID string, bTime int64) (*session, error) {
return scanSessionRow(tx.QueryRow(scanSessionQuery(`
WHERE session_id = ? AND btime = ? AND ended_at IS NULL
`),
sessionID, bTime))
}
func getLatestSessions(tx *sql.Tx, n int) ([]*session, error) {
rows, err := tx.Query(scanSessionQuery(`
ORDER BY started_at DESC LIMIT ?
`),
n,
)
if err != nil {
return nil, err
}
defer rows.Close()
return scanSessionsRows(rows)
}
func getUserSessions(tx *sql.Tx, user string) ([]*session, error) {
rows, err := tx.Query(scanSessionQuery(`
WHERE user = ?
ORDER BY started_at DESC
`),
user,
)
if err != nil {
return nil, err
}
defer rows.Close()
return scanSessionsRows(rows)
}
func getPendingSessions(tx *sql.Tx, bTime int64) ([]*session, error) {
rows, err := tx.Query(scanSessionQuery(`
WHERE ended_at IS NULL AND btime != ?
`),
bTime,
)
if err != nil {
return nil, err
}
defer rows.Close()
return scanSessionsRows(rows)
}