-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
297 lines (272 loc) · 8.37 KB
/
daemon.go
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"github.com/google/uuid"
_ "modernc.org/sqlite"
)
// The daemon is executed using "eternal daemon".
// It listens to connections from one or more listeners,
// and stores the data into one backend.
// Usage:
//
// eternal daemon [-listen listener]... [-backend scheme]
//
// The default backend is:
// - MaxOS: sqlite://$HOME/Library/Application Support/eternal/history.db
// - other: sqlite://$HOME/.local/share/eternal/history.db
//
const chanSize = 50
type Session struct {
OS string // GOOS/GOARCH (eg, "linux/amd64", "darwin/amd64"...)
Shell string
Parent string
Origin string // remote IP if this is a SSH connection
Hostname string
Username string
TTY string
PID int
}
type Entry struct {
Session
WorkingDir string
Timestamp string
Command string
ExitStatus int
Duration int // microseconds
}
type Command struct {
Input map[string]any
Output chan map[string]any
}
func (c Command) String() string {
return fmt.Sprint(c.Input)
}
func cmdDaemon(args []string) error {
if c, err := connect(); err == nil {
c.Close()
return errors.New("daemon already running")
}
log.Println("Starting daemon")
os.Remove(socketName())
ln, err := net.Listen("unix", socketName())
if err != nil {
return err
}
defer ln.Close()
var dbdir, dbfile string
switch runtime.GOOS {
case "darwin":
dbdir = filepath.Join(os.Getenv("HOME"), "Application Support", "eternal")
default:
dbdir = filepath.Join(os.Getenv("HOME"), ".local", "share", "eternal")
}
err = os.MkdirAll(dbdir, 0700)
if err != nil {
return err
}
dbfile = filepath.Join(dbdir, "history.db")
db, err := sql.Open("sqlite", dbfile)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS eternal_session(id INTEGER primary key, created timestamp not null default (datetime('now','localtime')), session text unique not null, os text not null default '', shell text not null default '', parent text not null default '', origin text not null default '', hostname text not null, username text not null, tty text not null, pid integer not null);
CREATE TABLE IF NOT EXISTS eternal_command (id INTEGER primary key, session_id integer not null references eternal_session(id), working_dir text not null, start timestamp not null default (datetime('now','localtime')), exit integer, duration integer, command text not null);
`)
if err != nil {
return fmt.Errorf("trying to create SQL tables: %w", err)
}
cc := make(chan Command, chanSize)
go daemonBackend(db, cc)
for {
c, err := ln.Accept()
if err != nil {
return err
}
go func(c net.Conn) {
defer c.Close()
dec := json.NewDecoder(c)
enc := json.NewEncoder(c)
var cmd Command
err = dec.Decode(&cmd.Input)
if err != nil {
return
}
cmd.Output = make(chan map[string]any, 1)
cc <- cmd
for response := range cmd.Output {
err = enc.Encode(response)
if err != nil {
return
}
}
}(c)
}
return nil
}
func daemonBackend(db *sql.DB, cc chan Command) {
var err error
for cmd := range cc {
switch cmd.Input["action"] {
case "init":
// Expected: init os hostname username tty pid
m := cmd.Input
m["session"] = uuid.NewString()
err = sqliteInit(db, m)
if err != nil {
log.Printf("Error in \"init\": %v\n", err)
close(cmd.Output)
continue
}
cmd.Output <- map[string]any{"session": m["session"]}
close(cmd.Output)
case "start":
// Expected: start session working_dir command
m := cmd.Input
_, err := sqliteStartCommand(db, m)
if err != nil {
log.Printf("Error in \"start\": %v\n", err)
close(cmd.Output)
continue
}
close(cmd.Output)
case "end":
// Expected: end session exit tstamp_start tstamp_end
m := cmd.Input
err = sqliteEndCommand(db, m)
if err != nil {
log.Printf("Error in \"end\": %v\n", err)
close(cmd.Output)
continue
}
close(cmd.Output)
case "history":
m := cmd.Input
session, _ := m["session"].(string)
history, err := sqliteHistory(db, session)
if err != nil {
log.Printf("Error in \"history\": %v\n", err)
close(cmd.Output)
continue
}
for _, e := range history {
cmd.Output <- map[string]any{
"os": e.OS,
"shell": e.Shell,
"parent": e.Parent,
"origin": e.Origin,
"hostname": e.Hostname,
"username": e.Username,
"tty": e.TTY,
"pid": strconv.Itoa(e.PID),
"working_dir": e.WorkingDir,
"timestamp": e.Timestamp,
"command": e.Command,
"exit_status": e.ExitStatus,
"duration": e.Duration,
}
}
close(cmd.Output)
default:
log.Printf("Error: got %q\n", cmd)
}
}
}
// CREATE TABLE IF NOT EXISTS eternal_session(id INTEGER primary key, created timestamp not null default (datetime('now','localtime')), session text unique not null, os text not null default '', shell text not null default '', parent string not null default '', origin text not null default '', hostname text not null, username text not null, tty text not null, pid int not null);
func sqliteInit(db *sql.DB, m map[string]any) error {
// log.Printf("sqliteInit(%v)", m)
_, err := db.Exec(`
INSERT INTO eternal_session(session, os, shell, parent, origin,
hostname, username, tty, pid)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, m["session"], m["os"], m["shell"], m["parent"], m["origin"],
m["hostname"], m["username"], m["tty"], m["pid"])
if err != nil {
return err
}
return nil
}
func sqliteStartCommand(db *sql.DB, m map[string]any) (int, error) {
// log.Printf("sqliteStartCommand(%v)", m)
var dur int
if start, ok := m["start"].(float64); ok {
dur = int(-start)
}
var id int
row := db.QueryRow(`
INSERT INTO eternal_command(session_id, working_dir, command, duration)
SELECT id, ?, ?, ? FROM eternal_session WHERE session=?
RETURNING id
`, m["working_dir"], m["command"], dur, m["session"])
err := row.Scan(&id)
if err != nil {
return 0, fmt.Errorf("INSERTING command: %w", err)
}
return id, nil
}
func sqliteEndCommand(db *sql.DB, m map[string]any) error {
// log.Printf("sqliteEndCommand(%v)", m)
end, ok := m["end"].(float64)
if !ok {
return fmt.Errorf("sqliteEndCommand: received invalid `end`: %v", m["end"])
}
start, ok := m["start"].(float64)
var duration int
var durLiteral string
if ok {
duration = int(end - start)
durLiteral = "?"
} else {
duration = int(end)
durLiteral = "duration+?"
}
_, err := db.Exec(`
UPDATE eternal_command
SET exit=?, duration=`+durLiteral+`
WHERE exit IS NULL AND id=(SELECT MAX(id) FROM eternal_command WHERE session_id=(SELECT id FROM eternal_session WHERE session=?))
`, m["status"], duration, m["session"])
if err != nil {
return fmt.Errorf("UPDATING command: %w", err)
}
return nil
}
// CREATE TABLE IF NOT EXISTS eternal_session(id INTEGER primary key, created timestamp not null default (datetime('now','localtime')), session text unique not null, os text not null default '', shell text not null default '', parent string not null default '', origin text not null default '', hostname text not null, username text not null, tty text not null, pid int not null);
// CREATE TABLE IF NOT EXISTS eternal_command (id INTEGER primary key, session_id integer not null references eternal_session(id), working_dir text not null, start timestamp not null default (datetime('now','localtime')), exit int, duration int, command text not null);
func sqliteHistory(db *sql.DB, session string) ([]Entry, error) {
var e Entry
rows, err := db.Query(`
SELECT
s.os, s.shell, s.parent, s.origin,
s.hostname, s.username, s.tty, s.pid,
c.working_dir, datetime(c.start) AS start, c.command,
COALESCE(c.exit,'-1') AS status, COALESCE(c.duration,'-1') AS duration
FROM eternal_command c
LEFT JOIN eternal_session s ON c.session_id=s.id
ORDER BY c.id
`, session)
if err != nil {
return nil, fmt.Errorf("SELECT command: %w", err)
}
var h []Entry
for rows.Next() {
if err = rows.Scan(&e.OS, &e.Shell, &e.Parent, &e.Origin,
&e.Hostname, &e.Username, &e.TTY, &e.PID,
&e.WorkingDir, &e.Timestamp, &e.Command,
&e.ExitStatus, &e.Duration); err != nil {
return nil, fmt.Errorf("rows.Scan: %w", err)
}
h = append(h, e)
}
return h, nil
}
// select * FROM eternal_command where session_id=4 and exit is null and id=(select max(id) FROM eternal_command where session_id=4);