Class: CMSScanner::Browser
- Inherits:
-
Object
- Object
- CMSScanner::Browser
show all
- Extended by:
- Actions
- Defined in:
- lib/cms_scanner/browser.rb,
lib/cms_scanner/browser/actions.rb,
lib/cms_scanner/browser/options.rb
Overview
Options available in the Browser
Defined Under Namespace
Modules: Actions
Constant Summary
collapse
- OPTIONS =
[
:cache_ttl,
:cookie_jar,
:cookie_string,
:connect_timeout,
:http_auth,
:max_threads,
:user_agent,
:user_agents_list,
:proxy,
:proxy_auth,
:random_user_agent,
:request_timeout
]
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Actions
get, get_and_follow_location, head, post
Constructor Details
#initialize(parsed_options = {}) ⇒ Void
12
13
14
|
# File 'lib/cms_scanner/browser.rb', line 12
def initialize(parsed_options = {})
load_options(parsed_options)
end
|
Class Method Details
.instance(parsed_options = {}) ⇒ Browser
21
22
23
|
# File 'lib/cms_scanner/browser.rb', line 21
def self.instance(parsed_options = {})
@@instance ||= new(parsed_options)
end
|
.reset ⇒ Object
25
26
27
|
# File 'lib/cms_scanner/browser.rb', line 25
def self.reset
@@instance = nil
end
|
Instance Method Details
#default_request_params ⇒ Hash
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/cms_scanner/browser.rb', line 38
def default_request_params
params = {
ssl_verifypeer: false, ssl_verifyhost: 2, headers: { 'User-Agent' => user_agent },
method: :get
}
{ connecttimeout: :connect_timeout, cache_ttl: :cache_ttl,
proxy: :proxy, timeout: :request_timeout, cookiejar: :cookie_jar,
cookiefile: :cookie_jar, cookie: :cookie_string
}.each do |typhoeus_opt, browser_opt|
attr_value = public_send(browser_opt)
params[typhoeus_opt] = attr_value unless attr_value.nil?
end
params[:proxyauth] = "#{proxy_auth[:username]}:#{proxy_auth[:password]}" if proxy_auth
params[:userpwd] = "#{http_auth[:username]}:#{http_auth[:password]}" if http_auth
params
end
|
#default_user_agent ⇒ String
61
62
63
|
# File 'lib/cms_scanner/browser/options.rb', line 61
def default_user_agent
"CMSScanner v#{VERSION}"
end
|
#forge_request(url, params = {}) ⇒ Typhoeus::Request
33
34
35
|
# File 'lib/cms_scanner/browser.rb', line 33
def forge_request(url, params = {})
Typhoeus::Request.new(url, request_params(params))
end
|
#hydra ⇒ Object
28
29
30
|
# File 'lib/cms_scanner/browser/options.rb', line 28
def hydra
@hydra ||= Typhoeus::Hydra.new(max_concurrency: max_threads || 1)
end
|
#load_options(options = {}) ⇒ Object
22
23
24
25
26
|
# File 'lib/cms_scanner/browser/options.rb', line 22
def load_options(options = {})
OPTIONS.each do |sym|
send("#{sym}=", options[sym]) if options.key?(sym)
end
end
|
#max_threads=(number) ⇒ Object
Set the threads attribute and update the max_concurrency of Typhoeus::Hydra
36
37
38
39
|
# File 'lib/cms_scanner/browser/options.rb', line 36
def max_threads=(number)
@max_threads = number.to_i > 0 ? number.to_i : 1
hydra.max_concurrency = @max_threads
end
|
#request_params(params = {}) ⇒ Hash
62
63
64
65
66
|
# File 'lib/cms_scanner/browser.rb', line 62
def request_params(params = {})
default_request_params.merge(params) do |key, oldval, newval|
key == :headers ? oldval.merge(newval) : newval
end
end
|
#user_agent ⇒ String
66
67
68
|
# File 'lib/cms_scanner/browser/options.rb', line 66
def user_agent
@user_agent ||= random_user_agent ? user_agents.sample : default_user_agent
end
|
#user_agents ⇒ Array<String>
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/cms_scanner/browser/options.rb', line 47
def user_agents
return @user_agents if @user_agents
@user_agents = []
File.open(user_agents_list).each do |line|
next if line == "\n" || line[0, 1] == '#'
@user_agents << line.chomp
end
@user_agents
end
|
#user_agents_list ⇒ String
Returns The path to the user agents list.
42
43
44
|
# File 'lib/cms_scanner/browser/options.rb', line 42
def user_agents_list
@user_agents_list ||= File.join(APP_DIR, 'user_agents.txt')
end
|