Module: Wpxf::Net::UserAgent

Includes:
Versioning::BrowserVersions, Versioning::OSVersions
Included in:
HttpClient
Defined in:
lib/wpxf/net/user_agent.rb

Overview

Provides functionality for generating user agent strings.

Instance Method Summary collapse

Methods included from Versioning::OSVersions

#random_nt_version, #random_osx_version

Methods included from Versioning::BrowserVersions

#random_chrome_build_number, #random_chrome_version, #random_ie_version, #random_opera_version, #random_presto_version, #random_presto_version2, #random_safari_build_number, #random_safari_version, #random_trident_version

Instance Method Details

#clients_by_frequencyHash

Returns a hash of browser and OS combinations grouped by frequencies drawn from real-world statistics.

Returns:

  • (Hash)

    a hash of browser and OS combinations grouped by frequencies drawn from real-world statistics.



47
48
49
50
# File 'lib/wpxf/net/user_agent.rb', line 47

def clients_by_frequency
  json = Wpxf::DataFile.new('json', 'browser_usage_by_frequency.json').content
  JSON.parse(json)
end

#random_browser_and_osHash

A random browser and OS combination.

Returns:

  • (Hash)

    a hash containing a :browser and :os



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/wpxf/net/user_agent.rb', line 12

def random_browser_and_os
  frequencies = clients_by_frequency
  target_frequency = rand(1..100)
  sum = 0

  frequencies.each do |browser_frequency, combinations|
    sum += browser_frequency.to_i
    next if target_frequency > sum

    # This browser encompasses our target frequency, so reset the
    # frequency sum and generate a new target frequency for the
    # operating system selection.
    target_frequency = rand(1..100)
    sum = 0

    # Once we find an OS that covers the target frequency, return
    # the browser/OS combination.
    combinations.each do |os_frequency, combo|
      sum += os_frequency.to_i
      if target_frequency <= sum
        return {
          browser: combo['browser'].to_sym,
          os: combo['os'].to_sym
        }
      end
    end
  end

  # This point should never be reached, as the frequencies should
  # always total 100 and thus we should always retrieve a combo.
  raise 'Browser and OS frequencies exceeded 100'
end

#random_chrome_platform_string(os) ⇒ String

A random Chrome platform string.

Parameters:

  • os

    the operating system that Chrome would be running on.

Returns:

  • (String)

    a random Chrome platform string.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/wpxf/net/user_agent.rb', line 151

def random_chrome_platform_string(os)
  cpu = random_processor_string(os)
  build = random_chrome_build_number
  version = random_chrome_version

  if os == :linux
    return "(X11; Linux #{cpu}) AppleWebKit/#{build} (KHTML, like Gecko)"\
           " Chrome/#{version} Safari/#{build}"
  end

  if os == :osx
    return "(Macintosh; U; #{cpu} Mac OS X #{random_osx_version}) "\
           "AppleWebKit/#{build} (KHTML, like Gecko) Chrome/"\
           "#{version} Safari/#{build}"
  end

  if os == :windows
    return "(Windows NT #{random_nt_version}) AppleWebKit/#{build} "\
           "(KHTML, like Gecko) Chrome/#{version} Safari/#{build}"
  end
end

#random_firefox_platform_string(os) ⇒ String

A random Firefox platform string.

Parameters:

  • os

    the operating system that Firefox would be running on.

Returns:

  • (String)

    a random Firefox platform string.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wpxf/net/user_agent.rb', line 86

def random_firefox_platform_string(os)
  version = random_firefox_version_string
  cpu = random_processor_string(os)

  if os == :linux
    return "(X11; Linux #{cpu}; rv:#{rand(5..7)}.0) #{version}"
  end

  if os == :osx
    return "(Macintosh; #{cpu} Mac OS X #{random_osx_version} "\
           "rv:#{rand(2..6)}.0) #{version}"
  end

  if os == :windows
    return "(Windows NT #{random_nt_version}; en-US; rv:1.9."\
           "#{rand(0..2)}.20) #{version}"
  end
end

#random_firefox_version_stringString

Returns a random Firefox version string.

Returns:

  • (String)

    a random Firefox version string.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wpxf/net/user_agent.rb', line 70

def random_firefox_version_string
  [
    "Gecko/#{random_time_string('2011-01-01')} "\
    "Firefox/#{rand(5..7)}.0",
    "Gecko/#{random_time_string('2011-01-01')} "\
    "Firefox/#{rand(5..7)}.0.1",
    "Gecko/#{random_time_string('2010-01-01')} "\
    "Firefox/3.6.#{rand(1..20)}",
    "Gecko/#{random_time_string('2010-01-01')} "\
    'Firefox/3.8'
  ].sample
end

#random_iexplorer_platform_stringString

A random Internet Explorer platform string.

Returns:

  • (String)

    a random Internet Explorer platform string.



126
127
128
129
# File 'lib/wpxf/net/user_agent.rb', line 126

def random_iexplorer_platform_string
  "(compatible; MSIE #{random_ie_version}; Windows NT "\
  "#{random_nt_version}; Trident/#{random_trident_version})"
end

#random_opera_platform_string(os) ⇒ String

A random Opera platform string.

Parameters:

  • os

    the operating system that Opera would be running on.

Returns:

  • (String)

    a random Opera platform string.



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wpxf/net/user_agent.rb', line 134

def random_opera_platform_string(os)
  cpu = random_processor_string(os)
  presto = random_presto_version
  version = random_presto_version2

  if os == :linux
    return "(X11; Linux #{cpu}; U; en-US) Presto/#{presto} "\
           "Version/#{version}"
  else
    return "(Windows NT #{random_nt_version}; U; en-US) "\
           "Presto/#{presto} Version/#{version}"
  end
end

#random_processor_string(os) ⇒ String

A random CPU type.

Parameters:

  • os

    the operating system that the CPU would be used with.

Returns:

  • (String)

    a random CPU type.



64
65
66
67
# File 'lib/wpxf/net/user_agent.rb', line 64

def random_processor_string(os)
  return ['i686', 'x86_64'].sample if os == :linux
  return ['Intel', 'PPC', 'U; Intel', 'U; PPC'].sample if os == :osx
end

#random_safari_platform_string(os) ⇒ String

A random Safari platform string.

Parameters:

  • os

    the operating system that Safari would be running on.

Returns:

  • (String)

    a random Safari platform string.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wpxf/net/user_agent.rb', line 108

def random_safari_platform_string(os)
  build = random_safari_build_number
  cpu = random_processor_string(os)
  version = random_safari_version

  if os == :osx
    return "(Macintosh; U; #{cpu} Mac OS X #{random_osx_version} rv:"\
           "#{rand(2..6)}.0; en-US) AppleWebKit/#{build} "\
           "(KHTML, like Gecko) Version/#{version} Safari/#{build}"
  else
    return "(Windows; U; Windows NT #{random_nt_version}) AppleWebKit/"\
           "#{build} (KHTML, like Gecko) Version/#{version} "\
           "Safari/#{build}"
  end
end

#random_time_string(min, max = Time.now.to_s, format = '%Y%m%d') ⇒ String

A random date between two dates in a specific format.

Parameters:

  • min

    the minimum date to return.

  • max (defaults to: Time.now.to_s)

    the maximum date to return.

  • format (defaults to: '%Y%m%d')

    the format string to use when formatting the date.

Returns:

  • (String)

    a formatted random date.



57
58
59
# File 'lib/wpxf/net/user_agent.rb', line 57

def random_time_string(min, max = Time.now.to_s, format = '%Y%m%d')
  rand(Time.parse(min)..Time.parse(max)).strftime(format)
end

#random_user_agentString

Returns a random browser user agent.

Returns:

  • (String)

    a random browser user agent.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/wpxf/net/user_agent.rb', line 174

def random_user_agent
  platform = random_browser_and_os
  os = platform[:os]

  if platform[:browser] == :firefox
    return "Mozilla/5.0 #{random_firefox_platform_string(os)}"
  elsif platform[:browser] == :safari
    return "Mozilla/5.0 #{random_safari_platform_string(os)}"
  elsif platform[:browser] == :iexplorer
    return "Mozilla/5.0 #{random_iexplorer_platform_string}"
  elsif platform[:browser] == :opera
    return "Opera/#{random_opera_version} "\
           "#{random_opera_platform_string(os)}"
  else
    return "Mozilla/5.0 #{random_chrome_platform_string(os)}"
  end
end