-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
175 lines (140 loc) · 4.98 KB
/
proxy.php
File metadata and controls
175 lines (140 loc) · 4.98 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
<?php
require_once 'db.php';
// Generate the proxy script for a user
function generateProxyScript($apiToken) {
$script = <<<'PHP'
<?php
// ImgurUK Contributor Proxy Script
// This script proxies imgur content for the ImgurUK network
define('API_TOKEN', '%%TOKEN%%');
define('ALLOWED_REFERER', 'imguruk.com');
// Health check endpoint
if (isset($_GET['health']) && $_GET['health'] === 'check') {
$authToken = $_SERVER['HTTP_X_API_TOKEN'] ?? '';
if ($authToken === API_TOKEN) {
header('Content-Type: application/json');
echo json_encode([
'status' => 'healthy',
'timestamp' => time(),
'version' => '1.0'
]);
exit;
} else {
http_response_code(403);
exit;
}
}
// Validate the request
$referer = $_SERVER['HTTP_REFERER'] ?? '';
$authToken = $_SERVER['HTTP_X_API_TOKEN'] ?? '';
// Check referer contains imguruk.com
if (strpos($referer, ALLOWED_REFERER) === false) {
http_response_code(403);
exit;
}
// Validate API token
if ($authToken !== API_TOKEN) {
http_response_code(403);
exit;
}
// Get requested file
$uri = $_SERVER['REQUEST_URI'];
$pathInfo = pathinfo($uri);
// Only allow image files
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'];
$extension = strtolower($pathInfo['extension'] ?? '');
if (!in_array($extension, $allowedExtensions)) {
http_response_code(404);
exit;
}
// Build imgur URL
$filename = basename($uri);
$imgurUrl = 'https://i.imgur.com/' . $filename;
// Fetch from imgur with proper user agent
$ch = curl_init($imgurUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
if ($httpCode !== 200) {
http_response_code($httpCode);
curl_close($ch);
exit;
}
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
curl_close($ch);
// Parse and send Content-Type header
if (preg_match('/Content-Type:\s*([^\r\n]+)/i', $headers, $matches)) {
header('Content-Type: ' . trim($matches[1]));
}
// Output the content
echo $body;
PHP;
return str_replace('%%TOKEN%%', $apiToken, $script);
}
// Get user's proxies
function getUserProxies($userId) {
$db = getDB();
$stmt = $db->prepare('SELECT id, proxy_url, is_active, last_used, created_at FROM user_proxies WHERE user_id = :user_id ORDER BY created_at DESC');
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$proxies = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$proxies[] = $row;
}
return $proxies;
}
// Add new proxy
function addProxy($userId, $proxyUrl) {
// Validate URL
if (!filter_var($proxyUrl, FILTER_VALIDATE_URL)) {
return ['success' => false, 'error' => 'Invalid URL'];
}
// Check if URL contains imguruk.com (prevent loops)
if (strpos($proxyUrl, 'imguruk.com') !== false) {
return ['success' => false, 'error' => 'Cannot use imguruk.com as proxy'];
}
$db = getDB();
$stmt = $db->prepare('INSERT INTO user_proxies (user_id, proxy_url) VALUES (:user_id, :proxy_url)');
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$stmt->bindValue(':proxy_url', $proxyUrl, SQLITE3_TEXT);
try {
$stmt->execute();
return ['success' => true, 'message' => 'Proxy added successfully'];
} catch (Exception $e) {
return ['success' => false, 'error' => 'Failed to add proxy'];
}
}
// Toggle proxy active status
function toggleProxy($proxyId, $userId) {
$db = getDB();
// Verify ownership
$stmt = $db->prepare('SELECT is_active FROM user_proxies WHERE id = :id AND user_id = :user_id');
$stmt->bindValue(':id', $proxyId, SQLITE3_INTEGER);
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$proxy = $result->fetchArray(SQLITE3_ASSOC);
if (!$proxy) {
return ['success' => false, 'error' => 'Proxy not found'];
}
$newStatus = $proxy['is_active'] ? 0 : 1;
$updateStmt = $db->prepare('UPDATE user_proxies SET is_active = :status WHERE id = :id');
$updateStmt->bindValue(':status', $newStatus, SQLITE3_INTEGER);
$updateStmt->bindValue(':id', $proxyId, SQLITE3_INTEGER);
$updateStmt->execute();
return ['success' => true, 'is_active' => $newStatus];
}
// Delete proxy
function deleteProxy($proxyId, $userId) {
$db = getDB();
$stmt = $db->prepare('DELETE FROM user_proxies WHERE id = :id AND user_id = :user_id');
$stmt->bindValue(':id', $proxyId, SQLITE3_INTEGER);
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$stmt->execute();
return ['success' => true, 'message' => 'Proxy deleted'];
}