Class: Probe::CrawlingPolicy

Inherits:
Policy
  • Object
show all
Defined in:
lib/domain-probe/crawling_policy.rb

Instance Method Summary collapse

Instance Method Details

#possible_host_under_domain(domain) ⇒ Object

return a set of possilbe host under specific domain by crawling the URL within HTML tags



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/domain-probe/crawling_policy.rb', line 25

def possible_host_under_domain(domain)
  #sanity check
  return [].to_set if !domain
  
  set = Set.new()
  begin
    
    #choose the default website to try
    page = Nokogiri::HTML(open(Util.polish_url(domain)))
  
    #a lambda to check if the attribute is of an absolute url or not
    absolute_url_checker = lambda { |element,attribute| 
      attrib = element[attribute] 
      attrib && (attrib.downcase =~ /^http[s]?:\/\// || attrib.downcase.start_with?("//") )
    }
  
    #try to find some subdomains by a tags
    links = page.css(TAG_NAME_A).select{ |element| absolute_url_checker.call(element,ATTR_NAME_HREF) }
    set |= iterate_elements_with_attr(links,ATTR_NAME_HREF,domain)
  
    #try to find some subdomains by script tags
    scripts = page.css(TAG_NAME_SCRIPT).select{ |element| absolute_url_checker.call(element,ATTR_NAME_SRC) }
    set |= iterate_elements_with_attr(scripts,ATTR_NAME_SRC,domain)
  
    #try to find some subdomains by img tags
    images = page.css(TAG_NAME_IMG).select{ |element| absolute_url_checker.call(element,ATTR_NAME_SRC) }
    set |= iterate_elements_with_attr(images,ATTR_NAME_SRC,domain)
    
    #try to find some subdomains by img tags
    forms = page.css(TAG_NAME_FORM).select{ |element| absolute_url_checker.call(element,ATTR_NAME_ACTION) }
    set |= iterate_elements_with_attr(forms,ATTR_NAME_ACTION,domain)
    
  rescue => ex
    puts "#{ex.class}: #{ex.message}"
  end
  set
end