Class: HttpMimic::Downloader

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

Defined Under Namespace

Classes: DownloadError, UnsupportedPlatformError

Constant Summary collapse

DEFAULT_GITHUB_REPO =
'lexiforest/curl-impersonate'
DEFAULT_VERSION =
'v2.1.1'
DEFAULT_QJS_REPO =
'quickjs-ng/quickjs'
DEFAULT_QJS_VERSION =
'v0.16.2'
DEFAULT_OBSCURA_REPO =
'h4ckf0r0day/obscura'
DEFAULT_OBSCURA_VERSION =
'v0.2.1'

Class Method Summary collapse

Class Method Details

.available_binaries(install_dir: nil) ⇒ Object



273
274
275
276
277
278
279
280
# File 'lib/http_mimic/downloader.rb', line 273

def available_binaries(install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  return [] unless Dir.exist?(target_dir)

  Dir.glob(File.join(target_dir, '*')).select do |file|
    File.file?(file) && File.executable?(file)
  end.map { |f| File.basename(f) }
end

.binary_path(name, install_dir: nil) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/http_mimic/downloader.rb', line 265

def binary_path(name, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  candidate = File.join(target_dir, binary_name_for_platform(name))
  return candidate if File.file?(candidate) && File.executable?(candidate)

  nil
end

.download!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object



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
# File 'lib/http_mimic/downloader.rb', line 26

def download!(version: nil, install_dir: nil, repo: nil, force: false)
  target_version = normalize_version(version || HttpMimic.configuration.driver_version || DEFAULT_VERSION)
  target_dir     = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  target_repo    = repo || HttpMimic.configuration.github_repo || DEFAULT_GITHUB_REPO

  FileUtils.mkdir_p(target_dir)

  # Check if the target version is already installed and intact
  if !force && installed?(version: target_version, install_dir: target_dir)
    log_info("curl-impersonate #{target_version} is already installed in #{target_dir}")
    return target_dir
  end

  slug = platform_slug
  asset_name = "curl-impersonate-#{target_version}.#{slug}.tar.gz"
  download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"

  log_info("Downloading curl-impersonate (#{target_version}) [#{asset_name}]...")

  archive_data = fetch_binary(download_url)

  log_info("Extracting archive to #{target_dir}...")
  extract_tar_gz(archive_data, target_dir)

  # Write version marker file
  File.write(version_file_path(target_dir), target_version)

  log_info("curl-impersonate #{target_version} installation complete!")
  target_dir
end

.download_obscura!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/http_mimic/downloader.rb', line 171

def download_obscura!(version: nil, install_dir: nil, repo: nil, force: false)
  target_version = normalize_version(version || HttpMimic.configuration.obscura_version || DEFAULT_OBSCURA_VERSION)
  target_dir     = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  target_repo    = repo || HttpMimic.configuration.obscura_github_repo || DEFAULT_OBSCURA_REPO

  FileUtils.mkdir_p(target_dir)

  dest_binary = File.join(target_dir, binary_name_for_platform('obscura'))

  if !force && obscura_installed?(version: target_version, install_dir: target_dir)
    log_info("obscura #{target_version} is already installed in #{target_dir}")
    return dest_binary
  end

  asset_name = obscura_platform_asset
  download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"

  log_info("Downloading Obscura (#{target_version}) [#{asset_name}]...")
  archive_data = fetch_binary(download_url)

  log_info("Extracting Obscura archive to #{target_dir}...")
  extract_tar_gz(archive_data, target_dir)

  # Ensure executable permissions on extracted binaries
  %w[obscura obscura-worker].each do |name|
    p = File.join(target_dir, binary_name_for_platform(name))
    File.chmod(0755, p) if File.exist?(p)
  end

  File.write(obscura_version_file_path(target_dir), target_version)

  log_info("Obscura #{target_version} installation complete! (#{dest_binary})")
  dest_binary
end

.download_qjs!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/http_mimic/downloader.rb', line 72

def download_qjs!(version: nil, install_dir: nil, repo: nil, force: false)
  target_version = normalize_version(version || HttpMimic.configuration.qjs_version || DEFAULT_QJS_VERSION)
  target_dir     = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  target_repo    = repo || HttpMimic.configuration.qjs_github_repo || DEFAULT_QJS_REPO

  FileUtils.mkdir_p(target_dir)

  dest_binary = File.join(target_dir, binary_name_for_platform('qjs'))

  if !force && qjs_installed?(version: target_version, install_dir: target_dir)
    log_info("qjs #{target_version} is already installed in #{target_dir}")
    return dest_binary
  end

  asset_name = qjs_platform_asset
  download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"

  log_info("Downloading QuickJS (#{target_version}) [#{asset_name}]...")
  binary_data = fetch_binary(download_url)

  File.binwrite(dest_binary, binary_data)
  File.chmod(0755, dest_binary)

  File.write(qjs_version_file_path(target_dir), target_version)

  log_info("QuickJS #{target_version} installation complete! (#{dest_binary})")
  dest_binary
end

.installed?(version: nil, install_dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/http_mimic/downloader.rb', line 57

def installed?(version: nil, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  core_binary = File.join(target_dir, binary_name_for_platform('curl-impersonate'))
  return false unless File.file?(core_binary) && File.executable?(core_binary)

  if version
    target_version = normalize_version(version)
    v_file = version_file_path(target_dir)
    return false unless File.file?(v_file)
    return File.read(v_file).strip == target_version
  end

  true
end

.obscura_installed?(version: nil, install_dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/http_mimic/downloader.rb', line 206

def obscura_installed?(version: nil, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  bin = File.join(target_dir, binary_name_for_platform('obscura'))
  return false unless File.file?(bin) && File.executable?(bin)

  if version
    target_version = normalize_version(version)
    v_file = obscura_version_file_path(target_dir)
    return false unless File.file?(v_file)
    return File.read(v_file).strip == target_version
  end

  true
end

.obscura_path(install_dir: nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/http_mimic/downloader.rb', line 221

def obscura_path(install_dir: nil)
  return HttpMimic.configuration.obscura_path if HttpMimic.configuration.obscura_path && File.executable?(HttpMimic.configuration.obscura_path)

  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  candidate = File.join(target_dir, binary_name_for_platform('obscura'))
  return candidate if File.file?(candidate) && File.executable?(candidate)

  # Fallback to system PATH
  sys_bin = `which obscura 2>/dev/null`.strip
  return sys_bin if !sys_bin.empty? && File.executable?(sys_bin)

  nil
end

.obscura_platform_assetObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/http_mimic/downloader.rb', line 235

def obscura_platform_asset
  os  = host_os
  cpu = host_cpu

  case os
  when :macos
    case cpu
    when :arm64 then 'obscura-aarch64-macos-stealth.tar.gz'
    when :x86_64 then 'obscura-x86_64-macos-stealth.tar.gz'
    else
      raise UnsupportedPlatformError, "Unsupported macOS CPU architecture for Obscura: #{cpu}"
    end
  when :linux
    case cpu
    when :x86_64 then 'obscura-x86_64-linux-stealth.tar.gz'
    when :aarch64, :arm64 then 'obscura-aarch64-linux-stealth.tar.gz'
    else
      raise UnsupportedPlatformError, "Unsupported Linux CPU architecture for Obscura: #{cpu}"
    end
  when :windows
    'obscura-x86_64-windows-stealth.zip'
  else
    raise UnsupportedPlatformError, "Unsupported operating system for Obscura: #{RbConfig::CONFIG['host_os']}"
  end
end

.obscura_version_file_path(dir) ⇒ Object



261
262
263
# File 'lib/http_mimic/downloader.rb', line 261

def obscura_version_file_path(dir)
  File.join(dir, '.obscura_version')
end

.platform_slugObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/http_mimic/downloader.rb', line 282

def platform_slug
  os  = host_os
  cpu = host_cpu

  case os
  when :macos
    case cpu
    when :arm64 then 'arm64-macos'
    when :x86_64 then 'x86_64-macos'
    else
      raise UnsupportedPlatformError, "Unsupported macOS CPU architecture: #{cpu}"
    end
  when :linux
    libc = linux_libc
    case cpu
    when :x86_64
      libc == :musl ? 'x86_64-linux-musl' : 'x86_64-linux-gnu'
    when :aarch64, :arm64
      libc == :musl ? 'aarch64-linux-musl' : 'aarch64-linux-gnu'
    when :i386, :i686
      'i386-linux-gnu'
    when :arm
      'arm-linux-gnueabihf'
    when :riscv64
      'riscv64-linux-gnu'
    when :loongarch64
      'loongarch64-linux-gnu'
    else
      raise UnsupportedPlatformError, "Unsupported Linux CPU architecture: #{cpu}"
    end
  when :windows
    case cpu
    when :x86_64 then 'x86_64-win32'
    when :arm64 then 'arm64-win32'
    when :i386, :i686 then 'i686-win32'
    else
      raise UnsupportedPlatformError, "Unsupported Windows CPU architecture: #{cpu}"
    end
  when :freebsd
    case cpu
    when :x86_64 then 'x86_64-freebsd'
    when :arm64, :aarch64 then 'aarch64-freebsd'
    else
      raise UnsupportedPlatformError, "Unsupported FreeBSD CPU architecture: #{cpu}"
    end
  else
    raise UnsupportedPlatformError, "Unsupported operating system: #{RbConfig::CONFIG['host_os']}"
  end
end

.qjs_installed?(version: nil, install_dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/http_mimic/downloader.rb', line 101

def qjs_installed?(version: nil, install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  qjs_bin = File.join(target_dir, binary_name_for_platform('qjs'))
  return false unless File.file?(qjs_bin) && File.executable?(qjs_bin)

  if version
    target_version = normalize_version(version)
    v_file = qjs_version_file_path(target_dir)
    return false unless File.file?(v_file)
    return File.read(v_file).strip == target_version
  end

  true
end

.qjs_path(install_dir: nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/http_mimic/downloader.rb', line 116

def qjs_path(install_dir: nil)
  target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
  candidate = File.join(target_dir, binary_name_for_platform('qjs'))
  return candidate if File.file?(candidate) && File.executable?(candidate)

  # Fallback to system PATH
  sys_qjs = `which qjs 2>/dev/null`.strip
  return sys_qjs if !sys_qjs.empty? && File.executable?(sys_qjs)

  nil
end

.qjs_platform_assetObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/http_mimic/downloader.rb', line 128

def qjs_platform_asset
  os  = host_os
  cpu = host_cpu

  case os
  when :macos
    case cpu
    when :arm64 then 'qjs-darwin-arm64'
    when :x86_64 then 'qjs-darwin-x86_64'
    else
      raise UnsupportedPlatformError, "Unsupported macOS CPU architecture for QuickJS: #{cpu}"
    end
  when :linux
    case cpu
    when :x86_64
      'qjs-linux-x86_64'
    when :aarch64, :arm64
      'qjs-linux-aarch64'
    when :arm
      'qjs-linux-armv7'
    when :i386, :i686
      'qjs-linux-x86'
    when :riscv64
      'qjs-linux-riscv64'
    else
      raise UnsupportedPlatformError, "Unsupported Linux CPU architecture for QuickJS: #{cpu}"
    end
  when :windows
    case cpu
    when :x86_64 then 'qjs-windows-x86_64.exe'
    when :i386, :i686 then 'qjs-windows-x86.exe'
    else
      raise UnsupportedPlatformError, "Unsupported Windows CPU architecture for QuickJS: #{cpu}"
    end
  else
    raise UnsupportedPlatformError, "Unsupported operating system for QuickJS: #{RbConfig::CONFIG['host_os']}"
  end
end

.qjs_version_file_path(dir) ⇒ Object



167
168
169
# File 'lib/http_mimic/downloader.rb', line 167

def qjs_version_file_path(dir)
  File.join(dir, '.qjs_version')
end