write a simple wrapper of symfony http client;force the client to use ipv4
This commit is contained in:
@@ -9,13 +9,13 @@ use OCA\NCDownloader\Search\Sites\bitSearch;
|
|||||||
use OCA\NCDownloader\Search\Sites\sliderkz;
|
use OCA\NCDownloader\Search\Sites\sliderkz;
|
||||||
use OCA\NCDownloader\Search\Sites\TPB;
|
use OCA\NCDownloader\Search\Sites\TPB;
|
||||||
use OCA\NCDownloader\Tools\Aria2;
|
use OCA\NCDownloader\Tools\Aria2;
|
||||||
|
use OCA\NCDownloader\Tools\Client;
|
||||||
use OCA\NCDownloader\Tools\Helper;
|
use OCA\NCDownloader\Tools\Helper;
|
||||||
use OCA\NCDownloader\Tools\Settings;
|
use OCA\NCDownloader\Tools\Settings;
|
||||||
use OCA\NCDownloader\Tools\Youtube;
|
use OCA\NCDownloader\Tools\Youtube;
|
||||||
use OCP\AppFramework\App;
|
use OCP\AppFramework\App;
|
||||||
use OCP\IContainer;
|
use OCP\IContainer;
|
||||||
use Symfony\Component\DomCrawler\Crawler;
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
use Symfony\Component\HttpClient\HttpClient;
|
|
||||||
|
|
||||||
class Application extends App
|
class Application extends App
|
||||||
{
|
{
|
||||||
@@ -82,17 +82,15 @@ class Application extends App
|
|||||||
$container->query('Youtube')
|
$container->query('Youtube')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
$container->registerService('httpClient', function () {
|
||||||
|
$options = [
|
||||||
|
'ipv4' => true,
|
||||||
|
];
|
||||||
|
return Client::create($options);
|
||||||
|
});
|
||||||
$container->registerService('crawler', function () {
|
$container->registerService('crawler', function () {
|
||||||
return new Crawler();
|
return new Crawler();
|
||||||
});
|
});
|
||||||
$container->registerService('httpClient', function () {
|
|
||||||
$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36';
|
|
||||||
return HttpClient::create([
|
|
||||||
'headers' => [
|
|
||||||
'User-Agent' => $agent,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
$container->registerService(TPB::class, function (IContainer $container) {
|
$container->registerService(TPB::class, function (IContainer $container) {
|
||||||
$crawler = $container->query('crawler');
|
$crawler = $container->query('crawler');
|
||||||
$client = $container->query('httpClient');
|
$client = $container->query('httpClient');
|
||||||
|
|||||||
54
lib/Tools/Client.php
Normal file
54
lib/Tools/Client.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\NCDownloader\Tools;
|
||||||
|
|
||||||
|
//require __DIR__ . "/../../vendor/autoload.php";
|
||||||
|
use Symfony\Component\HttpClient\HttpClient;
|
||||||
|
|
||||||
|
final class Client
|
||||||
|
{
|
||||||
|
public function __construct(?array $options = null)
|
||||||
|
{
|
||||||
|
$this->client = HttpClient::create($this->configure($options));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function create(?array $options = null)
|
||||||
|
{
|
||||||
|
return new self($options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function defaultUserAgent(): string
|
||||||
|
{
|
||||||
|
return "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36";
|
||||||
|
}
|
||||||
|
|
||||||
|
private function defaultOptions(): array
|
||||||
|
{
|
||||||
|
$settings = [
|
||||||
|
'headers' => [
|
||||||
|
],
|
||||||
|
'extra' => ['curl' => null],
|
||||||
|
];
|
||||||
|
return $settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function configure(array $options): array
|
||||||
|
{
|
||||||
|
|
||||||
|
extract($options);
|
||||||
|
$settings = $this->defaultOptions();
|
||||||
|
$settings['extra']['curl'] = $curl ?? [];
|
||||||
|
$settings['headers'] = $headers ?? [];
|
||||||
|
|
||||||
|
if ($ipv4 || $force_ipv4) {
|
||||||
|
$settings['extra']['curl'] = [CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4];
|
||||||
|
}
|
||||||
|
$settings['headers']['User-Agent'] = $useragent ?? $this->defaultUserAgent();
|
||||||
|
|
||||||
|
return $settings;
|
||||||
|
}
|
||||||
|
public function request(string $url, $method, ?array $options = [])
|
||||||
|
{
|
||||||
|
return $this->client->request($url, $method, $options);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user