-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchrome-session-dump.go
578 lines (463 loc) · 13.1 KB
/
chrome-session-dump.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package main
//Written by Aetnaeus
//Original Source: https://github.com/lemnos/chrome-session-dump
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"unicode/utf16"
)
//Rather than storing session state directly chrome appends a pickled command to a
//session file as tabs are manipulated. These commands are subsequently used to
//reconstruct the session when the browser is restarted. Thus obtaining the
//working tab set involves attemping to simulate the reconstruction process
//performed by chrome (which is an implementation detail liable to change).
//A file has the following format:
//"SNSS"
//int32 version number (should be 1)
//<command>...
//Where each command has the following format
//<int16(size)><int8(type id)><payload (size-1 bytes)>...
//Where payload is a pickled struct containing data of the
//given type.
//See https://source.chromium.org/chromium/chromium/src/+/master:components/sessions/core/session_service_commands.cc;bpv=1;bpt=1?q=kCommandUpdateTabNavigation&ss=chromium%2Fchromium%2Fsrc
//Source:
//https://source.chromium.org/chromium/chromium/src/+/master:components/sessions/core/session_service_commands.cc;drc=948de71be4a38bc27197146904266867c509f4c0;bpv=1;bpt=1;l=25
const (
kCommandUpdateTabNavigation = 6
kCommandSetSelectedTabInIndex = 8
kCommandSetTabWindow = 0
kCommandSetTabGroup = 25
kCommandSetTabGroupMetadata2 = 27
kCommandSetSelectedNavigationIndex = 7
kCommandTabClosed = 16
kCommandWindowClosed = 17
kCommandSetTabIndexInWindow = 2
kCommandSetActiveWindow = 20
kCommandLastActiveTime = 21
)
type group struct {
high uint64
low uint64
name string
}
type window struct {
activeTabIdx uint32
id uint32
deleted bool
tabs []*tab
}
type histItem struct {
idx uint32
url string
title string
}
type tab struct {
id uint32
history []*histItem
idx uint32 //The tab position in the window (a relative value)
win uint32 //the id of the window to which the tab belongs
deleted bool
currentHistoryIdx uint32
group *group //May be null
}
//indexed by id
var tabs = map[uint32]*tab{}
var windows = map[uint32]*window{}
var groups = map[string]*group{}
func getWindow(id uint32) *window {
if _, ok := windows[id]; !ok {
windows[id] = &window{id: id}
}
return windows[id]
}
func getGroup(high uint64, low uint64) *group {
key := fmt.Sprintf("%x%x", high, low)
if _, ok := groups[key]; !ok {
groups[key] = &group{high, low, ""}
}
return groups[key]
}
func getTab(id uint32) *tab {
if _, ok := tabs[id]; !ok {
tabs[id] = &tab{id: id}
}
return tabs[id]
}
func readUint8(r io.Reader) uint8 {
var b [1]byte
if n, err := r.Read(b[:]); err != nil || n != 1 {
if err != nil {
panic(err)
}
panic(fmt.Errorf("Failed to read int8."))
}
return uint8(b[0])
}
func readUint16(r io.Reader) uint16 {
var b [2]byte
if n, err := r.Read(b[:]); err != nil || n != 2 {
if err != nil {
panic(err)
}
panic(fmt.Errorf("Failed to read int16."))
}
return uint16(b[0]) | uint16(b[1])<<8
}
func readUint32(r io.Reader) uint32 {
var b [4]byte
if n, err := r.Read(b[:]); err != nil || n != 4 {
if err != nil {
panic(err)
}
panic(fmt.Errorf("Failed to read uint32."))
}
return uint32(b[3])<<24 | uint32(b[2])<<16 | uint32(b[1])<<8 | uint32(b[0])
}
func readUint64(r io.Reader) uint64 {
var b [8]byte
if n, err := r.Read(b[:]); err != nil || n != 8 {
if err != nil {
panic(err)
}
panic(fmt.Errorf("Failed to read uint64."))
}
return uint64(b[7])<<56 |
uint64(b[6])<<48 |
uint64(b[5])<<40 |
uint64(b[4])<<32 |
uint64(b[3])<<24 |
uint64(b[2])<<16 |
uint64(b[1])<<8 |
uint64(b[0])
}
func readString(r io.Reader) string {
sz := readUint32(r)
rsz := sz
if rsz%4 != 0 { //Chrome 32bit aligns pickled data
rsz += 4 - (rsz % 4)
}
b := make([]byte, rsz)
if n, err := io.ReadFull(r, b); err != nil {
panic(err)
} else if n != len(b) {
panic(fmt.Errorf("Failed to read string"))
}
return string(b[:sz]) //don't return padding
}
//Reads a pickled 16 bit string: NOTE: chrome appears to store the internal c++ std representation
//which is not guaranteed to be UTF16 and may vary by platform and locale (aka this works on
//MY machine :P).
func readString16(r io.Reader) string {
sz := readUint32(r)
rsz := sz * 2
if rsz%4 != 0 { //Chrome 32bit aligns pickled data
rsz += 4 - (rsz % 4)
}
b := make([]byte, rsz)
if n, err := io.ReadFull(r, b); err != nil {
panic(err)
} else if n != len(b) {
panic(fmt.Errorf("Failed to read string"))
}
var s []uint16
for i := 0; i < int(sz*2); i += 2 {
s = append(s, uint16(b[i+1])<<8|uint16(b[i]))
}
return string(utf16.Decode(s))
}
//Normalized output structures (as distinct from the lower case internal ones which correspond to SNSS structures)
type Result struct {
Windows []*Window `json:"windows"`
}
type Tab struct {
Active bool `json:"active"`
History []*HistoryItem `json:"history"`
Url string `json:"url"`
Title string `json:"title"`
Deleted bool `json:"deleted"`
Group string `json:"group"`
}
type Window struct {
Tabs []*Tab `json:"tabs"`
Active bool `json:"active"`
Deleted bool `json:"deleted"`
}
type HistoryItem struct {
Url string `json:"url"`
Title string `json:"title"`
}
func parse(path string) Result {
fh, err := os.Open(path)
if err != nil {
panic(err)
}
var magic [4]byte
if n, err := fh.Read(magic[:4]); err != nil || n != 4 {
panic(err)
}
ver := readUint32(fh)
if magic != [4]byte{0x53, 0x4E, 0x53, 0x53} || //0x534E5353 == "SNSS"
(ver != 1 && ver != 3) { //TODO (hotfix): Review https://source.chromium.org/chromium/chromium/src/+/807acce36a4baa1004d23ae896b07e2148ea1533 and implement neccesary changes.
panic(fmt.Errorf("Invalid SNSS file: (version %d)", ver))
}
var activeWindow *window
readCommand := func() (typ uint8, data io.Reader, eof bool) {
defer func() {
if e := recover(); e == io.EOF {
eof = true
return
} else if e != nil {
panic(err)
}
}()
sz := int(readUint16(fh)) - 1
typ = readUint8(fh)
buf := make([]byte, sz)
if n, err := fh.Read(buf); err != nil {
panic(err)
} else if n != sz {
panic(fmt.Errorf("Failed to read %d bytes", n))
}
return typ, bytes.NewBuffer(buf), false
}
for {
typ, data, eof := readCommand()
if eof {
break
}
//Note: Some commands are pickled whilst others are raw struct
//dumps from memory, the former have a 32 bit size header whilst the
//latter may include padding between members.
switch typ {
case kCommandUpdateTabNavigation:
readUint32(data) //size of the data (again)
id := readUint32(data)
histIdx := readUint32(data)
url := readString(data)
title := readString16(data)
t := getTab(id)
var item *histItem
for _, h := range t.history {
if h.idx == histIdx {
item = h
break
}
}
if item == nil {
item = &histItem{idx: histIdx}
t.history = append(t.history, item)
}
item.url = url
item.title = title
case kCommandSetSelectedTabInIndex: //Sets the active tab index in window, note that 'tab index' is a derived value and not present in any data.
id := readUint32(data)
idx := readUint32(data)
getWindow(id).activeTabIdx = idx
case kCommandSetTabGroupMetadata2:
readUint32(data) //Size
high := readUint64(data)
low := readUint64(data)
name := readString16(data)
getGroup(high, low).name = name
case kCommandSetTabGroup:
id := readUint32(data)
readUint32(data) //Struct padding
high := readUint64(data)
low := readUint64(data)
getTab(id).group = getGroup(high, low)
case kCommandSetTabWindow:
win := readUint32(data)
id := readUint32(data)
getTab(id).win = win
case kCommandWindowClosed:
id := readUint32(data)
getWindow(id).deleted = true
case kCommandTabClosed:
id := readUint32(data)
getTab(id).deleted = true
case kCommandSetTabIndexInWindow:
id := readUint32(data)
index := readUint32(data)
getTab(id).idx = index
case kCommandSetActiveWindow:
id := readUint32(data)
activeWindow = getWindow(id)
case kCommandLastActiveTime: //TODO implement properly
//id := readUint32(data)
//time := readUint64(data)
//getTab(id)._lastActiveTime = time //figure out how to interpret this.
case kCommandSetSelectedNavigationIndex:
id := readUint32(data)
idx := readUint32(data) //The current position within history
getTab(id).currentHistoryIdx = idx
}
}
for _, t := range tabs {
sort.Slice(t.history, func(i, j int) bool {
return t.history[i].idx < t.history[j].idx
})
w := getWindow(t.win)
w.tabs = append(w.tabs, t)
}
for _, w := range windows {
sort.Slice(w.tabs, func(i, j int) bool {
return w.tabs[i].idx < w.tabs[j].idx
})
}
var Windows []*Window
for _, w := range windows {
W := &Window{Active: w == activeWindow, Deleted: w.deleted}
idx := 0
for _, t := range w.tabs {
groupName := ""
if t.group != nil {
groupName = t.group.name
}
T := &Tab{Active: idx == int(w.activeTabIdx), Deleted: t.deleted, Group: groupName}
for _, h := range t.history {
T.History = append(T.History, &HistoryItem{h.url, h.title})
if h.idx == t.currentHistoryIdx { //Truncate history to avoid having to deal with trees TODO: find a better way to export this.
T.Url = h.url
T.Title = h.title
break
}
}
W.Tabs = append(W.Tabs, T)
if !t.deleted {
idx++
}
}
Windows = append(Windows, W)
}
return Result{Windows}
}
func findSession(_path string) string {
var cfile = ""
ents, err := ioutil.ReadDir(_path)
if err != nil {
panic(err)
}
cmp := func(candidate string) {
if cfile != "" {
info1, err := os.Stat(candidate)
if err != nil {
panic(err)
}
info2, err := os.Stat(cfile)
if err != nil {
panic(err)
}
if info1.ModTime().Sub(info2.ModTime()) > 0 {
cfile = candidate
}
} else {
cfile = candidate
}
}
for _, ent := range ents {
if ent.IsDir() {
if cand := findSession(path.Join(_path, ent.Name())); cand != "" {
cmp(cand)
}
} else if strings.Index(ent.Name(), "Session_") == 0 {
cmp(path.Join(_path, ent.Name()))
}
}
return cfile
}
func tabPrintf(format string, tab *Tab, includeHistory bool) {
if includeHistory {
for _, item := range tab.History {
s := strings.Replace(format, "%u", item.Url, -1)
s = strings.Replace(s, "%g", tab.Group, -1)
s = strings.Replace(s, "%t", item.Title, -1)
s = strings.Replace(s, "\\n", "\n", -1)
s = strings.Replace(s, "\\t", "\t", -1)
s = strings.Replace(s, "\\0", "\x00", -1)
os.Stdout.Write([]byte(s))
}
} else {
s := strings.Replace(format, "%u", tab.Url, -1)
s = strings.Replace(s, "%g", tab.Group, -1)
s = strings.Replace(s, "%t", tab.Title, -1)
s = strings.Replace(s, "\\n", "\n", -1)
s = strings.Replace(s, "\\t", "\t", -1)
s = strings.Replace(s, "\\0", "\x00", -1)
os.Stdout.Write([]byte(s))
}
}
func main() {
var jsonFlag bool
var activeFlag bool
var deletedFlag bool
var historyFlag bool
var outputFmt string
flag.BoolVar(&jsonFlag, "json", false, "Produce json formatted output. Note that this includes all tabs along with their history and any corresponding metadata. Useful for other scripts.")
flag.BoolVar(&activeFlag, "active", false, "Print the currently active tab.")
flag.StringVar(&outputFmt, "printf", "%u\n", "The output format for tabs if -json is not specified (%u = url, %t = title, %g = group).")
flag.BoolVar(&deletedFlag, "deleted", false, "Include tabs which have been deleted.")
flag.BoolVar(&historyFlag, "history", false, "Include the history of each tab in the output.")
flag.Usage = func() {
fmt.Printf("Usage:
-session-dump [options] ([session file] | [chrome dir])\n\n")
fmt.Printf(`If a chrome directory is supplied the most recent session file
contained within it is used. If neither a directory or file
is supplied then the program will use ~/.config/chrome by
default
`)
flag.PrintDefaults()
}
flag.Parse()
target := os.ExpandEnv("$HOME/.config/chromium")
if _, err := os.Stat(target); os.IsNotExist(err) {
target = os.ExpandEnv("$HOME/.config/google-chrome")
}
if _, err := os.Stat(target); os.IsNotExist(err) {
target = os.ExpandEnv("$HOME/.config/chrome")
}
if len(flag.Args()) >= 1 {
target = flag.Args()[0]
}
if info, err := os.Stat(target); err == nil && info.IsDir() {
target = findSession(target)
}
if target == "" {
panic(fmt.Errorf("Unable to find session file."))
}
data := parse(target)
if jsonFlag {
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(b))
} else if activeFlag {
for _, win := range data.Windows {
if win.Active {
for _, tab := range win.Tabs {
if tab.Active {
tabPrintf(outputFmt, tab, historyFlag)
}
}
}
}
} else {
for _, win := range data.Windows {
if deletedFlag || !win.Deleted {
for _, tab := range win.Tabs {
if deletedFlag || !tab.Deleted {
tabPrintf(outputFmt, tab, historyFlag)
}
}
}
}
}
}