Class: UserAgentGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/spoof.rb

Instance Method Summary collapse

Constructor Details

#initializeUserAgentGenerator

Returns a new instance of UserAgentGenerator.



4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
# File 'lib/spoof.rb', line 4

def initialize
  @user_agents = {
    'chrome' => [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
      'Mozilla/5.0 (Linux; Android %{android_version}; Nexus 5X Build/N2G48H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Mobile Safari/537.36',
    ],
    'firefox' => [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:%{version}.0) Gecko/20100101 Firefox/%{version}.0',
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7; rv:%{version}.0) Gecko/20100101 Firefox/%{version}.0',
      'Mozilla/5.0 (Linux; Android %{android_version}; Nexus 5 Build/MRA58N) Gecko/20100101 Firefox/%{version}.0',
    ],
    'safari' => [
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%{version}.0.0.0 Safari/605.1.15',
      'Mozilla/5.0 (iPhone; CPU iPhone OS %{ios_version} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%{version}.0 Mobile/15E148 Safari/604.1',
    ],
    'edge' => [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36 Edg/%{version}.0.%{build}',
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; AS; rv:%{version}.0) like Gecko',
    ],
    'ie' => [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; AS; rv:%{version}.0) like Gecko',
      'Mozilla/5.0 (compatible; MSIE %{version}.0; Windows NT 10.0; Trident/7.0)',
    ],
    'android' => [
      'Mozilla/5.0 (Linux; Android %{android_version}; Nexus 5X Build/N2G48H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Mobile Safari/537.36',
      'Mozilla/5.0 (Linux; Android %{android_version}; SAMSUNG SM-G920F Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Mobile Safari/537.36',
      'Mozilla/5.0 (Linux; Android %{android_version}; Redmi 6A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Mobile Safari/537.36',
      'Mozilla/5.0 (Linux; Android %{android_version}; Infinix X6837 Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/%{version}.0.%{build}.0 Mobile Safari/',
    ],
    'ios' => [
      'Mozilla/5.0 (iPhone; CPU iPhone OS %{ios_version} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%{version}.0 Mobile/15E148 Safari/604.1',
      'Mozilla/5.0 (iPad; CPU OS %{ios_version} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%{version}.0 Mobile/15E148 Safari/604.1',
    ],
    'linux' => [
      'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
      'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
    ],
    'mac' => [
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7; rv:%{version}.0) Gecko/20100101 Firefox/%{version}.0',
    ],
    'windows' => [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%{version}.0.%{build}.0 Safari/537.36',
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; AS; rv:%{version}.0) like Gecko',
    ]
  }
end

Instance Method Details

#generate_user_agent(platform, version = nil, build = nil, android_version = nil, ios_version = nil) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spoof.rb', line 53

def generate_user_agent(platform, version = nil, build = nil, android_version = nil, ios_version = nil)
  raise ArgumentError, "Platform not supported. Choose from: #{@user_agents.keys.join(', ')}" unless @user_agents.key?(platform)

  user_agent_template = @user_agents[platform].sample

  version ||= rand(80..100).to_s
  build ||= rand(1000..9999).to_s
  android_version ||= rand(4..14).to_s
  ios_version ||= rand(10..15).to_s

  user_agent = user_agent_template % {
    version: version,
    build: build,
    android_version: android_version,
    ios_version: ios_version
  }

  user_agent
end