domains_scanner 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea761f85e758619f9d262dc93c6d28ed3b528788
4
+ data.tar.gz: 9cf162281540446d281a98ec9f7b596215fdba47
5
+ SHA512:
6
+ metadata.gz: 9a39d231cd4afaf155659605ee0f04faa920f52d2b1b4c0af5fb545b0e5fcb634b58911a0a01986d5ebbf6430a261c350b5921c798fe4789566de7c07ebe9729
7
+ data.tar.gz: aa736332989a71b16ed09465375d29906eee1ddd1f1219b1582e3cd3a1e2e26e39c2ab44de2b3d58cecee064e1447e10791cb121b02a83cc3f933d95c9ebbc13
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.byebug_history
11
+ /domains_scanner-*.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in domains_scanner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Martin Hong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # DomainsScanner
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/domains_scanner`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'domains_scanner'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install domains_scanner
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/HackerPie/domains_scanner.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'domains_scanner'
5
+ require 'ansi_colors'
6
+
7
+ PRINT_LINE_LENGTH=80
8
+ def print_list(list)
9
+ list.each do |group_name, group|
10
+ puts "Found domains for .#{group_name}\n#{"-" * 50}".green
11
+ group.each do |domain, engines|
12
+ tips_para = "Search Engine: #{engines.join(", ")}"
13
+ points_count = [PRINT_LINE_LENGTH - domain.length - tips_para.length - 2, 0].max
14
+ puts "#{domain.dup.red} #{'·' * points_count} #{tips_para.red}"
15
+ end
16
+ puts ""
17
+ end;nil
18
+ end
19
+
20
+ top_level_domains = nil
21
+ OptionParser.new do |opts|
22
+ opts.banner = "Usage: domains_scanner [options] domain_name"
23
+ opts.separator ""
24
+ opts.separator "Specific options:"
25
+
26
+ opts.on("-v", "--[no-]verbose", "Run verbosely, default: false") do |v|
27
+ DomainsScanner.verbose = true
28
+ end
29
+
30
+ opts.on("--top-domains=", "search top level domains, split by comma, default: #{DomainsScanner::TOP_LEVEL_DOMAINS}") do |t|
31
+ puts "Manually configured top_level_domains=#{t.split(",")}"
32
+ top_level_domains = t.split(",")
33
+ end
34
+
35
+ opts.on("-e", "--engines=", "search engines, split by comma, default: [google, baidu]") do |engines|
36
+ puts "Manually configured engines=#{engines.split(",")}"
37
+ DomainsScanner.engines = engines.split(",")
38
+ end
39
+
40
+ opts.on("--max-page=", "Maximum number of pages to scan, default: 20") do |p|
41
+ puts "Manually configured max_page=#{p}"
42
+ DomainsScanner.max_page = p.to_i
43
+ end
44
+
45
+ opts.separator ""
46
+ opts.separator "Common options:"
47
+
48
+ opts.on_tail("-h", "--help", "Show this message") do
49
+ puts opts
50
+ exit
51
+ end
52
+
53
+ # Another typical switch to print the version.
54
+ opts.on_tail("--version", "Show version") do
55
+ puts DomainsScanner::VERSION
56
+ exit
57
+ end
58
+ end.parse!
59
+ domain_word = ARGV.pop
60
+
61
+ list = DomainsScanner.scan(domain_word: domain_word, top_level_domains: top_level_domains)
62
+ print_list(list)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'domains_scanner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "domains_scanner"
8
+ spec.version = DomainsScanner::VERSION
9
+ spec.authors = ["Martin Hong"]
10
+ spec.email = ["hongzeqin@gmail.com"]
11
+
12
+ spec.summary = %q{search possible domains for specified keyword}
13
+ spec.description = %q{search possible domains by search engines and other tools}
14
+ spec.homepage = "https://rubygems.org/gems/domains_scanner"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables << 'domains_scanner'
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'byebug', '~> 9.1'
25
+
26
+ spec.add_dependency 'mechanize', '~> 2.7', '>= 2.7.5'
27
+ end
@@ -0,0 +1,59 @@
1
+ # https://gist.githubusercontent.com/Martin91/3e21412209f8ee5e6112/raw/deb59949a92c400b04c61f53913970f7e91e614a/ansi_colors.rb
2
+ # ansi_colors.rb
3
+ # Implement add ansi color escape code to normal string
4
+ #
5
+ # Created by Martin Hong on 1/17/2015
6
+ # Copyright (c) 2015 Martin Hong. All rights reserved.
7
+ #
8
+
9
+ String.class_eval do
10
+ # ==== Start colors with ANSI escape code ====
11
+ #
12
+ # ANSI colors excape code format:
13
+ # => Color=\033[code;前景;背景m
14
+ #
15
+ # More details can be retrieved from: http://en.wikipedia.org/wiki/ANSI_escape_code
16
+ #
17
+ # ANSI
18
+ # Foreground Background Color
19
+ # ---------------------------------------
20
+ # 30 40 Black
21
+ # 31 41 Red
22
+ # 32 42 Green
23
+ # 33 43 Yellow
24
+ # 34 44 Blue
25
+ # 35 45 Purple
26
+ # 36 46 Blue-Green
27
+ # 37 47 White
28
+ # 1 1 Transparent
29
+ #
30
+ # Code Function
31
+ # -------------------------
32
+ # 0 OFF
33
+ # 1 Highline
34
+ # 4 Underline
35
+ # 5 Flash
36
+ # 7 Reverse Display
37
+ # 8 Invisiable
38
+ #
39
+ RED = "\033[0;31;1m" # Dangerous Color
40
+ YELLOW = "\033[0;33;1m" # Warning Color
41
+ GREEN = "\033[0;32;1m" # Infos Color
42
+ DEFAULT = "\033[0;37;1m" # RESET COLOR
43
+ # ==== End colors with ANSI escape code ====
44
+
45
+ # Usages:
46
+ #
47
+ # "Hello world".red
48
+ # "Hello world".green
49
+ # "Hello world".yellow
50
+ #
51
+ %w(red yellow green).each do |method|
52
+ unless method_defined?(method)
53
+ define_method method do
54
+ prepend self.class.const_get(method.upcase)
55
+ concat DEFAULT
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,41 @@
1
+ module DomainsScanner
2
+ module Crawlers
3
+ class Baidu < Base
4
+ def search(domain_name, top_level_domain, page = 1)
5
+ set_user_agent
6
+ query = search_keyword(domain_name, top_level_domain)
7
+ start = (page - 1) * 10
8
+ doc = agent.get("https://www.baidu.com/s?wd=#{query}&pn=#{start}")
9
+
10
+ results = parse_results(doc)
11
+ have_next_page = have_next_page?(doc)
12
+
13
+ DomainsScanner::Results.new(results, have_next_page)
14
+ end
15
+
16
+ # [{title: "xxx", url: "xxx"}, ...]
17
+ def parse_results(doc)
18
+ items = doc.search(".result")
19
+ items.map do |i|
20
+ title = i.search("h3.t > a").text
21
+ # Baidu encrypted the target url, so we can use show url only, but it is enough!
22
+ # bbs.abc.net/for...php?...
23
+ show_url = i.search("div:last-child > a.c-showurl")
24
+ url = if show_url
25
+ if show_url.text.start_with?("http")
26
+ show_url.text
27
+ else
28
+ "http://#{show_url.text}"
29
+ end
30
+ end
31
+
32
+ { title: i.text, url: URI.encode(url) }
33
+ end
34
+ end
35
+
36
+ def have_next_page?(doc)
37
+ doc.search("#page strong+a").any?
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ require 'domains_scanner/results'
2
+
3
+ module DomainsScanner
4
+ module Crawlers
5
+ class Base
6
+ def initialize
7
+ end
8
+
9
+ def agent
10
+ @agent ||= Mechanize.new
11
+ end
12
+
13
+ def set_user_agent
14
+ agent.user_agent_alias = available_agent_alias.sample
15
+ end
16
+
17
+ def available_agent_alias
18
+ @available_agent_alias ||= Mechanize::AGENT_ALIASES.keys - ['Mechanize']
19
+ end
20
+
21
+ def search_keyword(domain_name, top_level_domain)
22
+ "site:*.#{domain_name}.#{top_level_domain}"
23
+ end
24
+
25
+ def search(domain_name, top_level_domain, page = 1)
26
+ raise NotImplementedError, "#{self.class.name}#search need to be implmented in sub class"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ module DomainsScanner
2
+ module Crawlers
3
+ class Google < Base
4
+ def search(domain_name, top_level_domain, page = 1)
5
+ set_user_agent
6
+ query = search_keyword(domain_name, top_level_domain)
7
+ start = (page - 1) * 10
8
+ doc = agent.get("https://google.com/search?q=#{query}&start=#{start}")
9
+
10
+ results = parse_results(doc)
11
+ have_next_page = have_next_page?(doc)
12
+
13
+ DomainsScanner::Results.new(results, have_next_page)
14
+ end
15
+
16
+ # [{title: "xxx", url: "xxx"}, ...]
17
+ def parse_results(doc)
18
+ items = doc.search(".g h3.r a")
19
+ items.map do |i|
20
+ title = i.text
21
+ href = i.attributes["href"] && i.attributes["href"].value
22
+ # https://bbs.abc.net/thread-144889-1-1.html&sa=U&ved=0ahUKEwjpmNT0ltnXAhXMxLwKHQJIAmE4ChAWCBQwAA&usg=AOvVaw31kkGPP7ZVlFGlAby9OkzE
23
+ url = if href
24
+ href.sub("/url?q=", "")
25
+ end
26
+
27
+ { title: i.text, url: url }
28
+ end
29
+ end
30
+
31
+ def have_next_page?(doc)
32
+ doc.search("div#foot .cur+td").any?
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ require 'mechanize'
2
+ require 'domains_scanner/crawlers/base'
3
+ require 'domains_scanner/crawlers/baidu'
4
+ require 'domains_scanner/crawlers/google'
5
+
6
+ module DomainsScanner
7
+ module Crawlers
8
+ def self.build(engine)
9
+ case engine.to_s
10
+ when "google"
11
+ Google.new
12
+ when "baidu"
13
+ Baidu.new
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module DomainsScanner
2
+ class ResultItem
3
+ attr_reader :title, :url, :uri
4
+
5
+ def initialize(title:, url:)
6
+ @title = title
7
+ @url = url
8
+
9
+ if url
10
+ @uri = URI(@url)
11
+ end
12
+ end
13
+
14
+ def host
15
+ @uri && @uri.host
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ require 'domains_scanner/result_item'
2
+
3
+ module DomainsScanner
4
+ class Results
5
+ attr_reader :items, :have_next_page
6
+
7
+ def initialize(results, have_next_page)
8
+ @items = results.map do |result|
9
+ ResultItem.new(title: result[:title], url: result[:url])
10
+ end
11
+ @have_next_page = have_next_page
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module DomainsScanner
2
+ class Runner
3
+ attr_reader :workers
4
+
5
+ def initialize(domain_word:, top_level_domain:)
6
+ @domain_word = domain_word
7
+ @top_level_domain = top_level_domain
8
+ end
9
+
10
+ def run
11
+ domain = "#{@domain_word}.#{@top_level_domain}"
12
+ puts "Start scan #{domain}" if DomainsScanner.verbose
13
+ @workers = DomainsScanner.engines.map do |engine|
14
+ crawler = DomainsScanner::Crawlers.build(engine)
15
+ page = 1
16
+
17
+ Thread.new do
18
+ loop do
19
+ puts "Scanning #{domain} with #{engine} on page: #{page}" if DomainsScanner.verbose
20
+
21
+ begin
22
+ results = crawler.search(@domain_word, @top_level_domain, page)
23
+ results.items.each do |item|
24
+ DomainsScanner.output_queue.push({
25
+ domain: item.host, top_level_domain: @top_level_domain, engine: engine
26
+ })
27
+ end
28
+ break unless results.have_next_page
29
+ rescue Mechanize::ResponseCodeError => e
30
+ puts "search in #{engine} error, skip now" if DomainsScanner.verbose
31
+ end
32
+
33
+ break unless page < DomainsScanner.max_page
34
+ page += 1
35
+ end
36
+ end
37
+ end
38
+
39
+ @workers.each(&:join)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module DomainsScanner
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'uri'
2
+ require 'thread'
3
+ require "domains_scanner/version"
4
+ require "domains_scanner/crawlers"
5
+ require "domains_scanner/runner"
6
+
7
+ module DomainsScanner
8
+ TOP_LEVEL_DOMAINS = %w(com cn com.cn net org ltd cc mobi live io co me hk).freeze
9
+
10
+ class << self
11
+ attr_accessor :verbose, :max_page, :engines
12
+ DomainsScanner.verbose = false
13
+ DomainsScanner.max_page = 20
14
+ DomainsScanner.engines = %w(baidu google)
15
+
16
+ def output_queue
17
+ @output_queue ||= Queue.new
18
+ end
19
+
20
+ def scan(domain_word:, top_level_domains: TOP_LEVEL_DOMAINS)
21
+ top_level_domains ||= TOP_LEVEL_DOMAINS
22
+
23
+ runners = top_level_domains.map do |top_level_domain|
24
+ Runner.new(domain_word: domain_word, top_level_domain: top_level_domain)
25
+ end
26
+ runners.each(&:run)
27
+
28
+ puts "Start analysis crawl results..." if DomainsScanner.verbose
29
+ analysis_output_queue
30
+ end
31
+
32
+ def analysis_output_queue
33
+ list = {}
34
+ begin
35
+ while(result = output_queue.pop(non_block = true)) do
36
+ domain, top_level_domain, engine = result.values_at(:domain, :top_level_domain, :engine)
37
+ next unless domain
38
+
39
+ group = list[top_level_domain] ||= {}
40
+ group[domain] ||= []
41
+
42
+ group[domain] << engine unless group[domain].include?(engine)
43
+ end
44
+ rescue ThreadError # queue is empty
45
+ end
46
+
47
+ list
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domains_scanner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Hong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '9.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '9.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mechanize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.7.5
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '2.7'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.7.5
75
+ description: search possible domains by search engines and other tools
76
+ email:
77
+ - hongzeqin@gmail.com
78
+ executables:
79
+ - domains_scanner
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - Gemfile
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - bin/domains_scanner
89
+ - domains_scanner.gemspec
90
+ - lib/ansi_colors.rb
91
+ - lib/domains_scanner.rb
92
+ - lib/domains_scanner/crawlers.rb
93
+ - lib/domains_scanner/crawlers/baidu.rb
94
+ - lib/domains_scanner/crawlers/base.rb
95
+ - lib/domains_scanner/crawlers/google.rb
96
+ - lib/domains_scanner/result_item.rb
97
+ - lib/domains_scanner/results.rb
98
+ - lib/domains_scanner/runner.rb
99
+ - lib/domains_scanner/version.rb
100
+ homepage: https://rubygems.org/gems/domains_scanner
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.6.6
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: search possible domains for specified keyword
124
+ test_files: []