Class: Yawast::Scanner::Plugins::Spider::Spider

Inherits:
Object
  • Object
show all
Defined in:
lib/scanner/plugins/spider/spider.rb

Class Method Summary collapse

Class Method Details



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scanner/plugins/spider/spider.rb', line 45

def self.get_links(uri)
  # get the page, and work out from there
  res = Yawast::Shared::Http.get_with_code uri
  doc = Nokogiri::HTML res[:body]

  results = doc.css('a').map { |link| link['href'] }

  results.each do |link|
    # check to see if this link is in scope
    if link.to_s.include?(@uri.to_s) && res[:code] == '200'
      # check to see if we've already seen this one
      unless @links.include? link.to_s
        @links.push link.to_s
        @results.push link.to_s

        @workers.push(Thread.new {get_links URI.parse(link)})
      end
    end
  end
end

.spider(uri, silent = false) ⇒ Object



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
# File 'lib/scanner/plugins/spider/spider.rb', line 10

def self.spider(uri, silent = false)
  @uri = uri.copy

  @workers = []
  @results = Queue.new

  @links = []
  @links.push @uri.to_s
  puts 'Spidering site...' unless silent
  get_links @uri

  results = Thread.new do
    begin
      while true
        if @results.length.positive?
          out = @results.pop(true)

          Yawast::Utilities.puts_info out unless silent

          Yawast::Shared::Output.log_append_value 'spider', 'get', out
        end
      end
    rescue ThreadError # rubocop:disable Lint/HandleExceptions
      # do nothing
    end
  end

  @workers.map(&:join)
  results.terminate

  puts

  @links
end