This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
CFCurlImpl.php
228 lines (191 loc) · 7.29 KB
/
CFCurlImpl.php
1
2
3
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
166
167
168
169
170
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace CloudflareBypass;
use CloudflareBypass\Model\UAM\UAMPageAttributes;
use CloudflareBypass\Model\UAMOptions;
/**
* Class CFCurlImpl
* - Implementation of CFCurl
*
* @package CloudflareBypass
*/
class CFCurlImpl implements CFCurl
{
/**
* SSL ciphers to use when establishing SSL connection.
* NOTE: Removes problematic ciphers which cause the captcha page to show up.
*
* @var array
*/
const DEFAULT_CIPHERS =
[
"ECDHE+AESGCM",
"ECDHE+CHACHA20",
"DHE+AESGCM",
"DHE+CHACHA20",
"ECDH+AESGCM",
"DH+AESGCM",
"ECDH+AES",
"DH+AES",
"RSA+AESGCM",
"RSA+AES",
"!aNULL",
"!eNULL",
"!MD5",
"!DSS",
"!ECDHE+SHA",
"!AES128-SHA",
"!DHE"
];
/**
* Headers required to bypass CloudFlare.
*
* @var array
*/
const DEFAULT_HEADERS =
[
"Upgrade-Insecure-Requests",
"User-Agent",
"Accept",
"Accept-Language"
];
/**
* TRUE if clearance is found.
*
* @var bool
*/
private $clearance;
/**
* UAM page
*
* @var UAMPageImpl
*/
private $uamPage;
/**
* Logger
*
* @var LoggerImpl
*/
private $logger;
public function __construct()
{
$this->uamPage = new UAMPageImpl();
$this->logger = new LoggerImpl();
}
public function exec($curlHandle, UAMOptions $uamOptions, int $retry = 0, bool $keepHandle = false): string
{
if (!$keepHandle) {
curl_setopt($curlHandle, CURLOPT_COOKIELIST, "");
}
$page = curl_exec($curlHandle);
$info = curl_getinfo($curlHandle);
if (UAMPage::isUAMPage($info['http_code'], $page)) {
$this->clearance = false;
$cloneCurlHandle = $keepHandle ? $curlHandle : $this->cloneCurlHandle($curlHandle);
$this->bypassIUAMPage($cloneCurlHandle, $page, $info, $retry, $uamOptions, $keepHandle);
if (!$keepHandle) {
$this->copyCookies($cloneCurlHandle, $curlHandle);
$page = curl_exec($curlHandle);
}
}
if ($info['http_code'] === 403 && strpos($page, "captcha")) {
$this->logger->error(sprintf("Captcha (retry: %s) -> not supported!", $retry));
throw new \ErrorException("Captcha page is not supported!");
}
return $page;
}
public function bypassIUAMPage($curlHandle, string $page, array $info, int $retry, UAMOptions $uamOptions, bool $keepHandle): string
{
if (!$keepHandle) {
$this->logger->enable($uamOptions->isVerbose());
if (!$this->checkForCorrectHeaders($info['request_header'])) {
throw new \ErrorException("cURL handle does not contain correct headers (user-agent, accept, accept-language, upgrade-insecure-requests)");
}
}
$scheme = parse_url($info['url'], PHP_URL_SCHEME);
$host = parse_url($info['url'], PHP_URL_HOST);
$time = microtime(true) * 1000000;
if ($scheme === "https") {
$this->logger->info(sprintf("UAM (retry: %s) -> applying HTTPS settings", $retry));
curl_setopt($curlHandle, CURLOPT_SSL_CIPHER_LIST, implode(":", self::DEFAULT_CIPHERS));
if (strpos($info['request_header'], 'HTTP/2') !== false) {
$headers = array_filter(array_slice(preg_split("/\r\n|\n/", $info['request_header']), 1));
$headers[] = "sec-fetch-mode: navigate";
$headers[] = "sec-fetch-site: none";
$headers[] = "sec-fetch-user: ?1";
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
}
$page = curl_exec($curlHandle);
}
$this->logger->info(sprintf("UAM (retry: %s) -> waiting for %s seconds!", $retry, $uamOptions->getDelay()));
usleep(max(($uamOptions->getDelay() * 1000000) - ((microtime(true) * 1000000) - $time), 0));
$this->logger->info(sprintf("UAM (retry: %s) -> getting form params...", $retry));
$pageAttributes = new UAMPageAttributes($scheme, $host, $page);
$formParams = $pageAttributes->getFormParams();
$this->logger->info(sprintf("UAM (retry: %s) -> (r param: %s)", $retry, $formParams->getR()));
$this->logger->info(sprintf("UAM (retry: %s) -> (jschl_vc param: %s)", $retry, $formParams->getJschlVc()));
$this->logger->info(sprintf("UAM (retry: %s) -> (pass param: %s)", $retry, $formParams->getPass()));
$this->logger->info(sprintf("UAM (retry: %s) -> (jschl_answer param: %s)", $retry, $formParams->getJschlAnswer()));
curl_setopt($curlHandle, CURLOPT_URL, $this->uamPage->getClearanceUrl($pageAttributes));
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $pageAttributes->getFormParams()->getQueryString());
$clearancePage = $this->exec($curlHandle, $uamOptions, $retry + 1, true);
$clearanceCookies = curl_getinfo($curlHandle, CURLINFO_COOKIELIST);
if (!$this->clearance) {
foreach ($clearanceCookies as $clearanceCookie) {
if (strpos($clearanceCookie, "cf_clearance") !== false) {
$this->logger->info(sprintf("UAM (retry: %s) -> clearance cookie found!", $retry));
$this->clearance = true;
break;
}
}
$this->logger->info(print_r($clearanceCookies, true));
if (!$this->clearance) {
$this->logger->error(sprintf("UAM (retry: %s) -> clearance cookie missing!", $retry));
throw new \ErrorException("CF clearance could not be found!");
}
}
return $clearancePage;
}
/**
* Clones cURL handle.
*
* @param mixed $curlHandle cURL handle
* @return mixed new cURL handle
*/
private function cloneCurlHandle($curlHandle)
{
$cloneCurlHandle = curl_copy_handle($curlHandle);
curl_setopt($cloneCurlHandle, CURLINFO_HEADER_OUT, true);
$this->copyCookies($curlHandle, $cloneCurlHandle);
return $cloneCurlHandle;
}
/**
* Checks if cURL handle has correct headers to bypass CloudFlare.
*
* @param string $headers Page request headers.
* @return bool TRUE if cURL handle contains correct headers
*/
private function checkForCorrectHeaders(string $headers): bool
{
foreach (self::DEFAULT_HEADERS as $header) {
if (stripos($headers, $header.':') === false) {
return false;
}
}
return true;
}
/**
* Copy cookies from one cURL handle to another.
*
* @param mixed $curlHandleFrom cURL handle to copy cookies from.
* @param mixed $curlHandleTo cURL handle to copy cookies to.
*/
private function copyCookies($curlHandleFrom, $curlHandleTo)
{
$cookies = curl_getinfo($curlHandleFrom, CURLINFO_COOKIELIST);
foreach ($cookies as $cookie) {
curl_setopt($curlHandleTo, CURLOPT_COOKIELIST, $cookie);
}
}
}