agentry 3.1.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -1
- data/README.md +97 -75
- data/lib/{browser → agentry}/accept_language.rb +2 -2
- data/lib/agentry/action_controller.rb +27 -0
- data/lib/agentry/agentry.rb +213 -0
- data/lib/agentry/aliases.rb +33 -0
- data/lib/{browser → agentry}/alipay.rb +1 -1
- data/lib/{browser → agentry}/base.rb +3 -3
- data/lib/{browser → agentry}/blackberry.rb +1 -1
- data/lib/{browser → agentry}/bot/empty_user_agent_matcher.rb +1 -1
- data/lib/{browser → agentry}/bot/ip_ranges.rb +3 -3
- data/lib/{browser → agentry}/bot/keyword_matcher.rb +1 -1
- data/lib/{browser → agentry}/bot/known_bots_matcher.rb +2 -2
- data/lib/{browser → agentry}/bot.rb +9 -9
- data/lib/agentry/browser_constant.rb +15 -0
- data/lib/{browser → agentry}/cache.rb +6 -6
- data/lib/{browser → agentry}/chrome.rb +1 -1
- data/lib/{browser → agentry}/client_hints.rb +1 -1
- data/lib/{browser → agentry}/cloudflare.rb +2 -2
- data/lib/{browser → agentry}/detect_version.rb +1 -1
- data/lib/{browser → agentry}/detection.rb +1 -1
- data/lib/{browser → agentry}/device/android.rb +1 -1
- data/lib/{browser → agentry}/device/base.rb +1 -1
- data/lib/{browser → agentry}/device/blackberry_playbook.rb +1 -1
- data/lib/{browser → agentry}/device/ipad.rb +1 -1
- data/lib/{browser → agentry}/device/iphone.rb +1 -1
- data/lib/{browser → agentry}/device/ipod_touch.rb +1 -1
- data/lib/{browser → agentry}/device/kindle.rb +1 -1
- data/lib/{browser → agentry}/device/kindle_fire.rb +1 -1
- data/lib/{browser → agentry}/device/playstation3.rb +1 -1
- data/lib/{browser → agentry}/device/playstation4.rb +1 -1
- data/lib/{browser → agentry}/device/psp.rb +1 -1
- data/lib/{browser → agentry}/device/psvita.rb +1 -1
- data/lib/{browser → agentry}/device/samsung.rb +2 -2
- data/lib/{browser → agentry}/device/surface.rb +1 -1
- data/lib/{browser → agentry}/device/switch.rb +1 -1
- data/lib/{browser → agentry}/device/tv.rb +1 -1
- data/lib/{browser → agentry}/device/unknown.rb +1 -1
- data/lib/{browser → agentry}/device/wii.rb +1 -1
- data/lib/{browser → agentry}/device/wiiu.rb +1 -1
- data/lib/{browser → agentry}/device/xbox_360.rb +1 -1
- data/lib/{browser → agentry}/device/xbox_one.rb +1 -1
- data/lib/{browser → agentry}/device.rb +1 -1
- data/lib/{browser → agentry}/duck_duck_go.rb +1 -1
- data/lib/{browser → agentry}/edge.rb +1 -1
- data/lib/{browser → agentry}/electron.rb +1 -1
- data/lib/{browser → agentry}/epiphany.rb +1 -1
- data/lib/{browser → agentry}/facebook.rb +1 -1
- data/lib/{browser → agentry}/firefox.rb +1 -1
- data/lib/{browser → agentry}/google_search_app.rb +1 -1
- data/lib/{browser → agentry}/huawei_browser.rb +1 -1
- data/lib/{browser → agentry}/instagram.rb +1 -1
- data/lib/{browser → agentry}/internet_explorer.rb +1 -1
- data/lib/{browser → agentry}/maxthon.rb +1 -1
- data/lib/{browser → agentry}/meta/base.rb +1 -1
- data/lib/{browser → agentry}/meta/device.rb +1 -1
- data/lib/{browser → agentry}/meta/generic_browser.rb +1 -1
- data/lib/{browser → agentry}/meta/id.rb +1 -1
- data/lib/{browser → agentry}/meta/ie.rb +1 -1
- data/lib/{browser → agentry}/meta/ios.rb +1 -1
- data/lib/{browser → agentry}/meta/mobile.rb +1 -1
- data/lib/{browser → agentry}/meta/platform.rb +1 -1
- data/lib/{browser → agentry}/meta/proxy.rb +1 -1
- data/lib/{browser → agentry}/meta/safari.rb +1 -1
- data/lib/{browser → agentry}/meta/tablet.rb +1 -1
- data/lib/{browser → agentry}/meta/webkit.rb +1 -1
- data/lib/{browser → agentry}/micro_messenger.rb +1 -1
- data/lib/{browser → agentry}/middleware/context/additions.rb +4 -2
- data/lib/agentry/middleware/context/url_methods.rb +17 -0
- data/lib/{browser → agentry}/middleware/context.rb +9 -3
- data/lib/{browser → agentry}/middleware.rb +2 -2
- data/lib/{browser → agentry}/miui_browser.rb +1 -1
- data/lib/{browser → agentry}/nokia.rb +1 -1
- data/lib/{browser → agentry}/opera.rb +1 -1
- data/lib/{browser → agentry}/otter.rb +1 -1
- data/lib/{browser → agentry}/phantom_js.rb +1 -1
- data/lib/{browser → agentry}/platform/adobe_air.rb +1 -1
- data/lib/{browser → agentry}/platform/android.rb +1 -1
- data/lib/{browser → agentry}/platform/base.rb +1 -1
- data/lib/{browser → agentry}/platform/blackberry.rb +1 -1
- data/lib/{browser → agentry}/platform/chrome_os.rb +1 -1
- data/lib/{browser → agentry}/platform/firefox_os.rb +1 -1
- data/lib/{browser → agentry}/platform/ios.rb +1 -1
- data/lib/{browser → agentry}/platform/kai_os.rb +1 -1
- data/lib/{browser → agentry}/platform/linux.rb +1 -1
- data/lib/{browser → agentry}/platform/mac.rb +1 -1
- data/lib/{browser → agentry}/platform/unknown.rb +1 -1
- data/lib/{browser → agentry}/platform/windows.rb +1 -1
- data/lib/{browser → agentry}/platform/windows_mobile.rb +1 -1
- data/lib/{browser → agentry}/platform/windows_phone.rb +1 -1
- data/lib/{browser → agentry}/platform.rb +1 -1
- data/lib/{browser → agentry}/qq.rb +1 -1
- data/lib/agentry/rails.rb +44 -0
- data/lib/{browser → agentry}/safari.rb +1 -1
- data/lib/{browser → agentry}/samsung_browser.rb +1 -1
- data/lib/{browser → agentry}/snapchat.rb +1 -1
- data/lib/{browser → agentry}/sougou_browser.rb +1 -1
- data/lib/{browser → agentry}/sputnik.rb +1 -1
- data/lib/agentry/testing.rb +30 -0
- data/lib/{browser → agentry}/uc_browser.rb +1 -1
- data/lib/{browser → agentry}/unknown.rb +1 -1
- data/lib/{browser → agentry}/version.rb +2 -2
- data/lib/{browser → agentry}/weibo.rb +1 -1
- data/lib/{browser → agentry}/yandex.rb +1 -1
- data/lib/agentry.rb +2 -1
- data/lib/browser/aliases.rb +2 -31
- data/lib/browser/browser.rb +2 -202
- data/lib/browser/rails.rb +3 -22
- data/lib/browser/testing.rb +3 -27
- data/lib/browser.rb +2 -2
- metadata +272 -252
- data/lib/browser/action_controller.rb +0 -21
- data/lib/browser/middleware/context/url_methods.rb +0 -15
- /data/lib/{browser → agentry}/meta.rb +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../agentry"
|
|
4
|
+
require "forwardable"
|
|
5
|
+
|
|
6
|
+
module Agentry
|
|
7
|
+
module Aliases
|
|
8
|
+
PLATFORM_ALIASES = %w[
|
|
9
|
+
adobe_air? android? blackberry? chrome_os? firefox_os? ios? ios_app?
|
|
10
|
+
ios_webview? linux? mac? windows10? windows7? windows8? windows8_1?
|
|
11
|
+
windows? windows_mobile? windows_phone? windows_rt?
|
|
12
|
+
windows_touchscreen_desktop? windows_vista? windows_wow64? windows_x64?
|
|
13
|
+
windows_x64_inclusive? windows_xp?
|
|
14
|
+
].freeze
|
|
15
|
+
|
|
16
|
+
DEVICE_ALIASES = %w[
|
|
17
|
+
blackberry_playbook? console? ipad? iphone? ipod_touch? kindle?
|
|
18
|
+
kindle_fire? mobile? nintendo? nintendo_switch? nintendo_wii?
|
|
19
|
+
nintendo_wiiu? playbook? playstation3? playstation4? playstation?
|
|
20
|
+
playstation_vita? ps3? ps4? psp? psp_vita? silk? surface? tablet? tv?
|
|
21
|
+
vita? wii? wiiu? xbox? xbox_360? xbox_one?
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
def self.included(target)
|
|
25
|
+
target.class_eval do
|
|
26
|
+
extend Forwardable
|
|
27
|
+
|
|
28
|
+
def_delegators :platform, *PLATFORM_ALIASES
|
|
29
|
+
def_delegators :device, *DEVICE_ALIASES
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
class Base
|
|
5
5
|
include DetectVersion
|
|
6
6
|
|
|
@@ -292,9 +292,9 @@ module Browser
|
|
|
292
292
|
actual_bytesize = input.bytesize
|
|
293
293
|
size_limit =
|
|
294
294
|
if subject == :user_agent
|
|
295
|
-
|
|
295
|
+
Agentry.user_agent_size_limit
|
|
296
296
|
else
|
|
297
|
-
|
|
297
|
+
Agentry.accept_language_size_limit
|
|
298
298
|
end
|
|
299
299
|
|
|
300
300
|
return if actual_bytesize < size_limit
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "ipaddr"
|
|
4
4
|
|
|
5
|
-
module
|
|
5
|
+
module Agentry
|
|
6
6
|
class Bot
|
|
7
7
|
# The IP ranges crawler owners publish, from bot_ip_ranges.yml (rewritten
|
|
8
8
|
# weekly by script/sync_ip_ranges.rb) or fetched again by `refresh`.
|
|
@@ -162,7 +162,7 @@ module Browser
|
|
|
162
162
|
|
|
163
163
|
# Prefixes ("66.249.64.0/27"), keyed by owner.
|
|
164
164
|
def self.prefixes_by_owner
|
|
165
|
-
@prefixes_by_owner ||=
|
|
165
|
+
@prefixes_by_owner ||= Agentry.load_data("bot_ip_ranges").transform_keys(&:to_sym)
|
|
166
166
|
end
|
|
167
167
|
|
|
168
168
|
def self.ranges_by_owner
|
|
@@ -170,7 +170,7 @@ module Browser
|
|
|
170
170
|
end
|
|
171
171
|
|
|
172
172
|
# For long-lived processes: fetches every owner's ranges again and swaps
|
|
173
|
-
# them in at once. Raises
|
|
173
|
+
# them in at once. Raises Agentry::Error and keeps the current ranges when
|
|
174
174
|
# a source fails.
|
|
175
175
|
def self.refresh
|
|
176
176
|
load(fetch)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
class Bot
|
|
5
5
|
class KnownBotsMatcher
|
|
6
6
|
def self.call(ua, _browser)
|
|
7
|
-
!
|
|
7
|
+
!Agentry::Bot.known_bot_rank(ua, lowest: false).nil?
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
class Bot
|
|
5
5
|
GENERIC_NAME = "Generic Bot"
|
|
6
6
|
AI_CATEGORIES = %i[ai_agent ai_assistant ai_crawler ai_search].freeze
|
|
@@ -17,28 +17,28 @@ module Browser
|
|
|
17
17
|
# browser's API. The gem ships its lists as JSON only: load_yaml("bots.yml")
|
|
18
18
|
# reads data/bots.json.
|
|
19
19
|
def self.load_yaml(path)
|
|
20
|
-
json_path =
|
|
20
|
+
json_path = Agentry.root.join("data", path.to_s.sub(/\.yml\z/, ".json"))
|
|
21
21
|
return JSON.load_file(json_path) if json_path.file?
|
|
22
22
|
|
|
23
23
|
require "yaml"
|
|
24
|
-
YAML.load_file(
|
|
24
|
+
YAML.load_file(Agentry.root.join(path))
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def self.bots
|
|
28
|
-
@bots ||=
|
|
28
|
+
@bots ||= Agentry.load_data("bots")
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def self.bot_exceptions
|
|
32
|
-
@bot_exceptions ||=
|
|
32
|
+
@bot_exceptions ||= Agentry.load_data("bot_exceptions")
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def self.search_engines
|
|
36
|
-
@search_engines ||=
|
|
36
|
+
@search_engines ||= Agentry.load_data("search_engines")
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def self.category_by_keyword
|
|
40
40
|
@category_by_keyword ||=
|
|
41
|
-
|
|
41
|
+
Agentry.load_data("bot_categories").each_with_object({}) do |(category, keywords), category_by_keyword|
|
|
42
42
|
keywords.each { |keyword| category_by_keyword[keyword] = category.to_sym }
|
|
43
43
|
end
|
|
44
44
|
end
|
|
@@ -119,7 +119,7 @@ module Browser
|
|
|
119
119
|
def self.why?(ua)
|
|
120
120
|
ua = ua.scrub unless ua.valid_encoding?
|
|
121
121
|
ua = ua.downcase.strip
|
|
122
|
-
browser =
|
|
122
|
+
browser = Agentry.new(ua)
|
|
123
123
|
matchers.find { |matcher| matcher.call(ua, browser) }
|
|
124
124
|
end
|
|
125
125
|
|
|
@@ -214,7 +214,7 @@ module Browser
|
|
|
214
214
|
end
|
|
215
215
|
|
|
216
216
|
private def browser
|
|
217
|
-
@browser ||=
|
|
217
|
+
@browser ||= Agentry.new(ua)
|
|
218
218
|
end
|
|
219
219
|
|
|
220
220
|
private :ua
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Loaded only by the browser/* require paths kept for 4.x: `require "agentry"` alone defines no Browser. Fires at
|
|
4
|
+
# boot, so only RUBYOPT=-W:deprecated shows it.
|
|
5
|
+
require_relative "agentry"
|
|
6
|
+
|
|
7
|
+
if Gem.loaded_specs.key?("browser")
|
|
8
|
+
raise LoadError, "agentry's browser/* require paths define Browser, which the browser gem defines too: " \
|
|
9
|
+
"Agentry 4 cannot share a bundle with the browser gem, remove it (or the gem that needs it) from the Gemfile"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Browser = Agentry
|
|
13
|
+
|
|
14
|
+
Agentry.deprecate("require \"browser\" (or browser/*) and the Browser constant are deprecated and go away in 5.0: " \
|
|
15
|
+
"require \"agentry\" and call Agentry")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
# The detections of the last user agents and the parsed Accept-Language
|
|
5
5
|
# headers, shared by the threads of a process. Reads take no lock: under the
|
|
6
6
|
# GVL a Hash read is atomic, and a thread that misses detects again. Writes
|
|
@@ -20,7 +20,7 @@ module Browser
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def self.detection(ua)
|
|
23
|
-
return Detection.new(ua) if
|
|
23
|
+
return Detection.new(ua) if Agentry.cache_size.zero?
|
|
24
24
|
|
|
25
25
|
cached_detection(ua) || @mutex.synchronize do
|
|
26
26
|
ua = -ua
|
|
@@ -30,7 +30,7 @@ module Browser
|
|
|
30
30
|
|
|
31
31
|
# A copy each time: callers may change the array they get.
|
|
32
32
|
def self.accept_language(header)
|
|
33
|
-
return AcceptLanguage.parse(header) if
|
|
33
|
+
return AcceptLanguage.parse(header) if Agentry.cache_size.zero?
|
|
34
34
|
|
|
35
35
|
languages = @languages_by_header[header]
|
|
36
36
|
return languages.dup if languages
|
|
@@ -64,7 +64,7 @@ module Browser
|
|
|
64
64
|
# The bot list holds hundreds of entries: compared by identity and size,
|
|
65
65
|
# like Bot.keywords_pattern, not entry by entry on every request.
|
|
66
66
|
def self.lists_unchanged?
|
|
67
|
-
@browser_matchers ==
|
|
67
|
+
@browser_matchers == Agentry.matchers &&
|
|
68
68
|
@platform_matchers == Platform.matchers &&
|
|
69
69
|
@device_matchers == Device.matchers &&
|
|
70
70
|
@bot_matchers == Bot.matchers &&
|
|
@@ -73,7 +73,7 @@ module Browser
|
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
def self.remember_lists
|
|
76
|
-
@browser_matchers =
|
|
76
|
+
@browser_matchers = Agentry.matchers.dup
|
|
77
77
|
@platform_matchers = Platform.matchers.dup
|
|
78
78
|
@device_matchers = Device.matchers.dup
|
|
79
79
|
@bot_matchers = Bot.matchers.dup
|
|
@@ -88,7 +88,7 @@ module Browser
|
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
def self.store(value_by_key, key, value)
|
|
91
|
-
value_by_key.clear if value_by_key.size >=
|
|
91
|
+
value_by_key.clear if value_by_key.size >= Agentry.cache_size
|
|
92
92
|
value_by_key[key] = value
|
|
93
93
|
end
|
|
94
94
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
# What Cloudflare knows of the request, from the headers a Transform Rule
|
|
5
5
|
# sets (docs/guide.html#cloudflare) and its own CF-IPCountry. Each site names
|
|
6
|
-
# its headers in its rule:
|
|
6
|
+
# its headers in its rule: Agentry::Cloudflare.header_names= changes them.
|
|
7
7
|
class Cloudflare
|
|
8
8
|
# Cloudflare's values are a few bytes: a longer one, or invalid UTF-8, is forged, and ignored.
|
|
9
9
|
HEADER_SIZE_LIMIT_BYTES = 128
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
module DetectVersion
|
|
5
5
|
# Gem::Version.new and Gem::Version#release keep every version they build in class variables, for the life of
|
|
6
6
|
# the process: a version read from a header must not reach them, or each new one an attacker sends stays.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module Agentry
|
|
4
4
|
class Device
|
|
5
5
|
class Samsung < Base
|
|
6
6
|
REGEX = /\(Linux.*?; Android.*?; (SAMSUNG )?(SM-[A-Z0-9]+).*?\)/i
|
|
7
7
|
|
|
8
8
|
def self.names
|
|
9
|
-
@names ||=
|
|
9
|
+
@names ||= Agentry.load_data("samsung")
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def id
|