Skip to content

Instantly share code, notes, and snippets.

@kamaljoshi
Last active May 27, 2017 21:57
Show Gist options
  • Save kamaljoshi/2cce5f6d35cd28de8f6dbb27d586f064 to your computer and use it in GitHub Desktop.
Save kamaljoshi/2cce5f6d35cd28de8f6dbb27d586f064 to your computer and use it in GitHub Desktop.

Revisions

  1. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions check_cloudflare.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # Except sqlite3 all the libraries are standard that should be present with a Ruby installation.
    # If you don't have sqlite3 installed. Use `gem install sqlite3`

    require 'fileutils'
    require 'sqlite3'
    require 'uri'
    @@ -11,6 +14,9 @@

    sqlite_db = SQLite3::Database.new temp_location
    chrome_history = sqlite_db.execute('SELECT DISTINCT(url) FROM urls;').flatten; nil

    FileUtils.rm([temp_location])

    domain_set = Set.new
    cloudflare_set = Set.new
    query_uris = Array.new
    @@ -32,6 +38,4 @@
    end
    end.each(&:join); nil

    FileUtils.rm([temp_location])

    p cloudflare_set.to_a.sort
    p cloudflare_set.to_a.sort
  2. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_cloudflare.rb
    Original file line number Diff line number Diff line change
    @@ -34,4 +34,4 @@

    FileUtils.rm([temp_location])

    puts cloudflare_set
    p cloudflare_set.to_a.sort
  3. kamaljoshi renamed this gist Feb 24, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ss.rb → check_cloudflare.rb
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,6 @@
    end
    end.each(&:join); nil

    FileUtils.rm([temp_location])
    FileUtils.rm([temp_location])

    puts cloudflare_set
  4. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions ss.rb
    Original file line number Diff line number Diff line change
    @@ -21,16 +21,15 @@
    domain_set.add(host)
    end; nil

    uri_mutex, set_mutex = Mutex.new, Mutex.new
    threads = 16.times.map do
    uri_mutex, set_mutex, read_mutex = Mutex.new, Mutex.new, Mutex.new
    (1..16).map do
    Thread.new(query_uris, cloudflare_set) do |query_uris, cloudflare_set|
    while !(uri = uri_mutex.synchronize { query_uris.pop }).nil?
    cf_header_present = !Net::HTTP.get_response(uri)['cf-ray'].nil? rescue nil
    print("#{query_uris.length} remaining\r")
    read_mutex.synchronize{ print("#{query_uris.length} remaining\r") }
    set_mutex.synchronize { cloudflare_set.add(uri.host) } if cf_header_present
    end
    end
    end; nil
    threads.each(&:join); nil
    end.each(&:join); nil

    FileUtils.rm([temp_location])
  5. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ss.rb
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,6 @@
    end
    end
    end; nil
    threads.map(&:join); nil
    threads.each(&:join); nil

    FileUtils.rm([temp_location])
  6. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ss.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    FileUtils.cp(chrome_history_location, temp_location)

    sqlite_db = SQLite3::Database.new temp_location
    chrome_history = db.execute('SELECT DISTINCT(url) FROM urls;').flatten; nil
    chrome_history = sqlite_db.execute('SELECT DISTINCT(url) FROM urls;').flatten; nil
    domain_set = Set.new
    cloudflare_set = Set.new
    query_uris = Array.new
  7. kamaljoshi revised this gist Feb 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ss.rb
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@
    Thread.new(query_uris, cloudflare_set) do |query_uris, cloudflare_set|
    while !(uri = uri_mutex.synchronize { query_uris.pop }).nil?
    cf_header_present = !Net::HTTP.get_response(uri)['cf-ray'].nil? rescue nil
    puts uri.host
    print("#{query_uris.length} remaining\r")
    set_mutex.synchronize { cloudflare_set.add(uri.host) } if cf_header_present
    end
    end
  8. kamaljoshi created this gist Feb 24, 2017.
    36 changes: 36 additions & 0 deletions ss.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    require 'fileutils'
    require 'sqlite3'
    require 'uri'
    require 'net/http'
    require 'set'
    require 'thread'

    chrome_history_location = "#{ENV['HOME']}/Library/Application\ Support/Google/Chrome/Default/History"
    temp_location = "/tmp/Chrome_history"
    FileUtils.cp(chrome_history_location, temp_location)

    sqlite_db = SQLite3::Database.new temp_location
    chrome_history = db.execute('SELECT DISTINCT(url) FROM urls;').flatten; nil
    domain_set = Set.new
    cloudflare_set = Set.new
    query_uris = Array.new

    chrome_history.each do |url|
    host = URI.parse(url).host rescue nil
    query_uris += [URI::HTTPS.build({host: host}), URI::HTTP.build({host: host})] if !domain_set.include?(host) && !host.nil?
    domain_set.add(host)
    end; nil

    uri_mutex, set_mutex = Mutex.new, Mutex.new
    threads = 16.times.map do
    Thread.new(query_uris, cloudflare_set) do |query_uris, cloudflare_set|
    while !(uri = uri_mutex.synchronize { query_uris.pop }).nil?
    cf_header_present = !Net::HTTP.get_response(uri)['cf-ray'].nil? rescue nil
    puts uri.host
    set_mutex.synchronize { cloudflare_set.add(uri.host) } if cf_header_present
    end
    end
    end; nil
    threads.map(&:join); nil

    FileUtils.rm([temp_location])