Class: CookieExtractor::BrowserDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/cookie_extractor/browser_detector.rb

Constant Summary collapse

{
  "chrome" => "~/.config/google-chrome/Default/Cookies",
  "chromium" => "~/.config/chromium/Default/Cookies",
  "firefox" => "~/.mozilla/firefox/*.default/cookies.sqlite"
}

Class Method Summary collapse

Class Method Details

.browser_extractor(browser) ⇒ Object

Open a browser’s cookie file using intelligent guesswork



30
31
32
33
34
35
36
37
# File 'lib/cookie_extractor/browser_detector.rb', line 30

def self.browser_extractor(browser)
  raise InvalidBrowserNameException, "Browser must be one of: #{self.supported_browsers.join(', ')}" unless self.supported_browsers.include?(browser)
  paths = Dir.glob(File.expand_path(COOKIE_LOCATIONS[browser]))
  if paths.length < 1 or not File.exists?(paths.first)
    raise NoCookieFileFoundException, "File #{paths.first} does not exist!"
  end
  self.new_extractor(paths.first)
end

.detect_browser(db_filename) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cookie_extractor/browser_detector.rb', line 52

def self.detect_browser(db_filename)
  db = SQLite3::Database.new(db_filename)
  browser = 
    if has_table?(db, 'moz_cookies')
      'Firefox'
    elsif has_table?(db, 'cookies')
      'Chrome'
    end
  db.close
  browser
end

.guessObject

Returns the extractor of the most recently used browser’s cookies

or raise NoCookieFileFoundException if there are no cookies


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cookie_extractor/browser_detector.rb', line 15

def self.guess
  most_recently_used_detected_browsers.each { |browser, path|
    begin
      extractor = self.browser_extractor(browser)
    rescue BrowserNotDetectedException, NoCookieFileFoundException
      # better try the next one...
    else
      return extractor
    end
  }
  # If we make it here, we've failed...
  raise NoCookieFileFoundException, "Couldn't find any browser's cookies"
end

.has_table?(db, table_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/cookie_extractor/browser_detector.rb', line 64

def self.has_table?(db, table_name)
  db.table_info(table_name).size > 0
end

.new_extractor(db_filename) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/cookie_extractor/browser_detector.rb', line 39

def self.new_extractor(db_filename)
  browser = detect_browser(db_filename)
  if browser
    CookieExtractor.const_get("#{browser}CookieExtractor").new(db_filename)
  else
    raise BrowserNotDetectedException, "Could not detect browser type."
  end
end

.supported_browsersObject



48
49
50
# File 'lib/cookie_extractor/browser_detector.rb', line 48

def self.supported_browsers
  COOKIE_LOCATIONS.keys
end