-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
214 lines (191 loc) · 5.85 KB
/
index.php
File metadata and controls
214 lines (191 loc) · 5.85 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
<?php
$mode = 'multi-user'; // multi-user, single-user or ulti-only
// multi-user: each IP can select their repo
// single-user: all users use the same repo
// ulti-only: all users use Ultimate repo
$storagePath = 'sessions/'; // storage directory for config-storage (only used in single- and multi-user modes)
$removeFilesAfterSeconds = 60 * 60 * 24 * 7; // 7 days
//END OF CONFIGURATION
if ($removeFilesAfterSeconds > 0 && !random_int(0, 99)) {
ClientSettings::cleanup($storagePath, $removeFilesAfterSeconds);
}
$clientName = null;
switch ($mode) {
case 'ulti-only':
break;
case 'single-user':
$clientName = 'THEONEANDONLY';
break;
case 'multi-user':
$clientName = $_SERVER['REMOTE_ADDR'];
break;
default:
sendText(['ERROR: Invalid mode configuration.']);
exit();
}
const client2repo = [
'Ultimate' => 'Assembly64',
'Commodore' => 'CommoServe',
];
$settings = new ClientSettings($clientName, $storagePath);
$query = $_GET['query'] ?? '';
if (str_contains($query, '(name:"+")')) {
$settings->set('client-id', 'Ultimate');
sendText(
'You are currently using: ' . client2repo[$settings->get('client-id')],
);
exit();
} elseif (str_contains($query, '(name:"-")')) {
$settings->set('client-id', 'Commodore');
sendText(
'You are currently using: ' . client2repo[$settings->get('client-id')],
);
exit();
} elseif (str_contains($query, '(name:"?")')) {
sendText([
'C64 Ultimate Repo Switcher help:',
" Search for name '+' for Assembly64",
" Search for name '-' for CommoServe",
" Search for name '?' for this help.",
'',
'Default is Assembly64.',
'Settings are stored to your public IP',
'',
'You are currently using: ' . client2repo[$settings->get('client-id')],
]);
exit();
}
$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
$body = file_get_contents('php://input');
$allowedHeaders = [
'Accept-Encoding',
'Host',
'User-Agent',
'Client-Id',
'Connection',
];
$ch = curl_init();
$hostIP = '185.187.254.229';
if (str_contains($uri, '/aql') and !str_contains($uri, '/aql/')) {
$uri = str_replace('/aql', '/aql/0/40', $uri);
}
$fullUrl = 'http://' . $hostIP . $uri;
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HEADER, true);
$headers = [
'Host: hackerswithstyle.se',
'Accept:',
'Content-Length:',
'Content-Type:',
];
$headers[] = 'Client-Id: ' . $settings->get('client-id');
$clientHeaders = (function_exists('apache_request_headers')
? 'apache_request_headers'
: 'getallheaders')();
foreach ($clientHeaders as $key => $value) {
if (in_array($key, $allowedHeaders)) {
$headers[] = $key . ': ' . $value;
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$headerLines = explode("\r\n", $header);
foreach ($headerLines as $headerLine) {
if (
stripos($headerLine, 'Content-Length:') === 0 ||
stripos($headerLine, 'Transfer-Encoding:') === 0 ||
stripos($headerLine, 'Connection:') === 0 ||
stripos($headerLine, 'Content-Encoding:') === 0 ||
stripos($headerLine, 'HTTP/') === 0 ||
trim($headerLine) === ''
) {
continue;
}
header($headerLine);
}
http_response_code($httpCode);
echo $body;
function sendText($rows) {
header('Content-Type: application/json');
$result = [];
foreach ((array) $rows as $row) {
$result[] = ['name' => $row];
}
echo json_encode($result);
}
class ClientSettings {
private $id;
private $path;
private $data = [];
public const DEFAULTS = ['client-id' => 'Ultimate'];
public function __construct($id, $path) {
$this->id = $id;
$this->path = $path;
}
public function set($key, $value) {
if ($this->id === null) {
return;
}
$storageFile = $this->getStoragePath($key);
file_put_contents(
$storageFile,
"<?php\nreturn " . var_export($value, true) . ";\n",
);
$this->data[$key] = $value;
}
public function get($key) {
if ($this->id === null) {
return self::DEFAULTS[$key] ?? null;
}
if (!isset($this->data[$key])) {
$this->load($key);
}
return $this->data[$key];
}
private function load($key) {
$storageFile = $this->getStoragePath($key);
if (file_exists($storageFile)) {
$this->data[$key] = include $storageFile;
} else {
$this->data[$key] = self::DEFAULTS[$key] ?? null;
}
}
public function remove($key) {
if ($this->id === null) {
return;
}
$storageFile = $this->getStoragePath($key);
if (file_exists($storageFile)) {
unlink($storageFile);
}
}
static function cleanup($path, $maxAgeSeconds = 60 * 60 * 24) {
$now = time();
foreach (glob($path . '/*') as $file) {
if (is_file($file)) {
if ($now - filemtime($file) > $maxAgeSeconds) {
unlink($file);
}
}
}
}
private function getStoragePath($key) {
if (!preg_match('/^[a-zA-Z0-9_\-]+$/', $key)) {
throw new Exception('Invalid key');
}
$path = $this->path . '/' . $this->id . '#' . $key . '.php';
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
return $path;
}
}