Module: Browserctl::Commands::Session

Extended by:
CliOutput
Defined in:
lib/browserctl/commands/session.rb

Constant Summary collapse

USAGE =
"Usage: browserctl session <save|load|list|delete|export|import> [args]"
PBKDF2_ITERATIONS =
100_000
PBKDF2_KEY_LEN =
32
SALT_LEN =
16
NONCE_LEN =
12
SENSITIVE_BASENAMES =
%w[cookies.json local_storage.json session_storage.json].freeze

Class Method Summary collapse

Methods included from CliOutput

print_result

Class Method Details

.run(client, args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/browserctl/commands/session.rb', line 26

def self.run(client, args)
  sub = args.shift or abort USAGE
  case sub
  when "save"   then run_save(client, args)
  when "load"   then run_load(client, args)
  when "list"   then run_list(client)
  when "delete" then run_delete(client, args)
  when "export" then run_export(args)
  when "import" then run_import(args)
  else abort "unknown session subcommand '#{sub}'\n#{USAGE}"
  end
end

.run_delete(client, args) ⇒ Object



54
55
56
57
# File 'lib/browserctl/commands/session.rb', line 54

def self.run_delete(client, args)
  name = args.shift or abort "usage: browserctl session delete <name>"
  print_result(client.session_delete(name))
end

.run_export(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/browserctl/commands/session.rb', line 59

def self.run_export(args)
  encrypt = args.delete("--encrypt")
  name    = args.shift or abort "usage: browserctl session export <name> <path> [--encrypt]"
  dest    = args.shift or abort "usage: browserctl session export <name> <path> [--encrypt]"

  session_dir = File.join(Browserctl::BROWSERCTL_DIR, "sessions", name)
  abort "session '#{name}' not found" unless Dir.exist?(session_dir)

  dest = File.expand_path(dest)

  if encrypt
    passphrase = prompt_passphrase(confirm: true)
    encrypt_export(session_dir, name, dest, passphrase)
  else
    pid = Process.spawn("zip", "-r", dest, name, chdir: File.join(Browserctl::BROWSERCTL_DIR, "sessions"))
    Process.wait(pid)
  end

  puts({ ok: true, path: dest }.to_json)
end

.run_import(args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/browserctl/commands/session.rb', line 80

def self.run_import(args)
  zip_path = args.shift or abort "usage: browserctl session import <path>"
  zip_path = File.expand_path(zip_path)
  abort "zip file not found: #{zip_path}" unless File.exist?(zip_path)

  sessions_dir = File.join(Browserctl::BROWSERCTL_DIR, "sessions")
  FileUtils.mkdir_p(sessions_dir)

  if encrypted_zip?(zip_path)
    passphrase = prompt_passphrase
    decrypt_import(zip_path, sessions_dir, passphrase)
  else
    pid = Process.spawn("unzip", "-o", zip_path, "-d", sessions_dir)
    Process.wait(pid)
  end

  puts({ ok: true }.to_json)
end

.run_list(client) ⇒ Object



50
51
52
# File 'lib/browserctl/commands/session.rb', line 50

def self.run_list(client)
  print_result(client.session_list)
end

.run_load(client, args) ⇒ Object



45
46
47
48
# File 'lib/browserctl/commands/session.rb', line 45

def self.run_load(client, args)
  name = args.shift or abort "usage: browserctl session load <name>"
  print_result(client.session_load(name))
end

.run_save(client, args) ⇒ Object



39
40
41
42
43
# File 'lib/browserctl/commands/session.rb', line 39

def self.run_save(client, args)
  encrypt = args.delete("--encrypt")
  name    = args.shift or abort "usage: browserctl session save <name> [--encrypt]"
  print_result(client.session_save(name, encrypt: !!encrypt))
end