-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
393 lines (348 loc) · 14.2 KB
/
index.php
File metadata and controls
393 lines (348 loc) · 14.2 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
session_start();
$supportedLangs = ['en', 'ru', 'zh'];
$defaultLang = 'en';
// Устанавливаем язык, если не установлен
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = $defaultLang;
}
// Проверяем, пришёл ли GET-параметр lang и входит ли он в список поддерживаемых
if (isset($_GET['lang']) && in_array($_GET['lang'], $supportedLangs)) {
$_SESSION['lang'] = $_GET['lang'];
}
// Текущий язык
$lang = $_SESSION['lang'];
// Подключаем файл с переводами
$translationsFile = __DIR__ . "/lang.$lang.php";
if (!file_exists($translationsFile)) {
// Если файла с переводами нет, подставим английский
$translationsFile = __DIR__ . "/lang.$defaultLang.php";
}
$t = include $translationsFile;
// Если пришёл POST — обрабатываем форму
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
processForm($t);
exit;
}
// Если GET — отображаем форму
renderForm($t);
/**
* Обработка формы и вывод результата
*/
function processForm(array $t)
{
// Собираем данные из формы
$data = [];
$data['Name'] = $_POST['Name'] ?? '';
$data['GlobalProxy'] = $_POST['GlobalProxy'] ?? 'false';
// Новые поля DNS
$data['RemoteDNSType'] = $_POST['RemoteDNSType'] ?? 'DoH';
$data['RemoteDNSDomain'] = $_POST['RemoteDNSDomain'] ?? '';
$data['RemoteDNSIP'] = $_POST['RemoteDNSIP'] ?? '';
$data['DomesticDNSType'] = $_POST['DomesticDNSType'] ?? 'DoH';
$data['DomesticDNSDomain']= $_POST['DomesticDNSDomain']?? '';
$data['DomesticDNSIP'] = $_POST['DomesticDNSIP'] ?? '';
$data['Geoipurl'] = $_POST['Geoipurl'] ?? '';
$data['Geositeurl'] = $_POST['Geositeurl'] ?? '';
$data['LastUpdated'] = $_POST['LastUpdated'] ?? '';
// Разбираем поле DnsHosts как JSON
$data['DnsHosts'] = parseJsonField($_POST['DnsHosts'] ?? '');
// Поля с массивами
$arrayFields = ['DirectSites', 'DirectIp', 'ProxySites', 'ProxyIp', 'BlockSites', 'BlockIp'];
foreach ($arrayFields as $field) {
$input = $_POST[$field] ?? '';
$lines = explode("\n", trim($input));
$lines = array_map('trim', $lines);
$lines = array_filter($lines); // убрать пустые строки
$data[$field] = array_values($lines);
}
$data['DomainStrategy'] = $_POST['DomainStrategy'] ?? 'IPIfNonMatch';
$data['FakeDNS'] = $_POST['FakeDNS'] ?? 'false';
// Генерация JSON и Base64
$jsonString = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$base64Encoded = base64_encode($jsonString);
$prefixedBase64= 'happ://routing/add/' . $base64Encoded;
// Теперь выводим результат (HTML)
renderResult($t, $jsonString, $prefixedBase64);
}
function renderForm(array $t)
{
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo htmlspecialchars($t['page_title']); ?></title>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.language-switcher {
position: absolute;
right: 20px;
top: 20px;
}
</style>
</head>
<body class="bg-light">
<div class="container mt-5">
<!-- Ссылки для переключения языка -->
<div class="language-switcher">
<?php
global $supportedLangs; // используем глобальный массив
foreach ($supportedLangs as $code) {
echo '<a href="?lang=' . htmlspecialchars($code) . '" class="me-2">' . strtoupper($code) . '</a>';
}
?>
</div>
<h1 class="mb-4"><?php echo htmlspecialchars($t['page_title']); ?></h1>
<form method="post">
<?php
// Перебираем поля формы из массива переводов
foreach ($t['form'] as $key => $field) {
// Массив полей (textarea)
if ($key === 'array_fields') {
foreach ($field as $arrayKey => $arrayField) {
renderTextareaField($arrayKey, $arrayField);
}
continue;
}
// Кнопка "Отправить"
if ($key === 'submit') {
// Не рендерим сейчас, потом ниже сделаем
continue;
}
// Если у поля есть options — значит это select
if (isset($field['options'])) {
renderSelectField($key, $field);
}
// Если это DnsHosts (textarea)
elseif ($key === 'DnsHosts') {
renderTextareaField($key, $field);
}
// Иначе обычное текстовое поле
else {
renderInputField($key, $field);
}
}
?>
<button type="submit" class="btn btn-primary">
<?php echo htmlspecialchars($t['form']['submit']); ?>
</button>
</form>
</div>
<!-- Скрипт для динамического скрытия полей *DNSDomain при выборе DoU -->
<script>
function toggleDNSDomainFields(selectId, domainFieldId) {
const select = document.getElementById(selectId);
const domainField = document.getElementById(domainFieldId).closest('.mb-3');
if (select.value === 'DoU') {
domainField.style.display = 'none';
} else {
domainField.style.display = '';
}
}
document.addEventListener('DOMContentLoaded', function () {
const remoteSelectId = 'RemoteDNSType';
const remoteDomainId = 'RemoteDNSDomain';
const domesticSelectId = 'DomesticDNSType';
const domesticDomainId = 'DomesticDNSDomain';
// Первичная проверка при загрузке
toggleDNSDomainFields(remoteSelectId, remoteDomainId);
toggleDNSDomainFields(domesticSelectId, domesticDomainId);
// При изменении
document.getElementById(remoteSelectId).addEventListener('change', function() {
toggleDNSDomainFields(remoteSelectId, remoteDomainId);
});
document.getElementById(domesticSelectId).addEventListener('change', function() {
toggleDNSDomainFields(domesticSelectId, domesticDomainId);
});
});
</script>
</body>
</html>
<?php
}
function renderResult(array $t, string $jsonString, string $prefixedBase64)
{
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo htmlspecialchars($t['generated_json']); ?></title>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
.copy-button, .deeplink-button, .qr-button {
position: absolute;
top: 10px;
}
.copy-button {
right: 210px;
}
.deeplink-button {
right: 110px;
}
.qr-button {
right: 10px;
}
.pre-container {
position: relative;
}
.language-switcher {
position: absolute;
right: 20px;
top: 20px;
}
</style>
</head>
<body class="bg-light">
<div class="container mt-5">
<!-- Ссылки для переключения языка (как и в форме) -->
<div class="language-switcher">
<?php
global $supportedLangs;
foreach ($supportedLangs as $code) {
echo '<a href="?lang=' . htmlspecialchars($code) . '" class="me-2">' . strtoupper($code) . '</a>';
}
?>
</div>
<h1 class="mb-4"><?php echo htmlspecialchars($t['generated_json']); ?></h1>
<h3><?php echo htmlspecialchars($t['formatted_json']); ?></h3>
<pre class="bg-white p-3 border rounded"><?php echo $jsonString; ?></pre>
<h3><?php echo htmlspecialchars($t['base64_json']); ?></h3>
<div class="pre-container">
<pre id="base64Output" class="bg-white p-3 border rounded"><?php echo $prefixedBase64; ?></pre>
<button class="btn btn-secondary copy-button" onclick="copyToClipboard()">
<?php echo htmlspecialchars($t['copy_to_clipboard']); ?>
</button>
<button class="btn btn-warning deeplink-button" onclick="openDeeplink()">
<?php echo htmlspecialchars($t['deeplink']); ?>
</button>
<button class="btn btn-success qr-button" onclick="showQRCode()">
<?php echo htmlspecialchars($t['qr_code']); ?>
</button>
</div>
<!-- Модальное окно для QR-кода -->
<div class="modal fade" id="qrModal" tabindex="-1" aria-labelledby="qrModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="qrModalLabel"><?php echo htmlspecialchars($t['qr_code']); ?></h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center d-flex justify-content-center">
<div id="qrcode"></div>
</div>
</div>
</div>
</div>
<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="btn btn-primary mt-3">
<?php echo htmlspecialchars($t['back']); ?>
</a>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function copyToClipboard() {
const base64Text = document.getElementById('base64Output').innerText;
navigator.clipboard.writeText(base64Text).then(function() {
alert('<?php echo addslashes($t['copy_success']); ?>');
}, function(err) {
alert('<?php echo addslashes($t['copy_error']); ?> ' + err);
});
}
function showQRCode() {
const base64Text = document.getElementById('base64Output').innerText;
var qrCodeContainer = document.getElementById('qrcode');
qrCodeContainer.innerHTML = '';
new QRCode(qrCodeContainer, {
text: base64Text,
width: 256,
height: 256,
});
var qrModal = new bootstrap.Modal(document.getElementById('qrModal'));
qrModal.show();
}
function openDeeplink() {
const base64Text = document.getElementById('base64Output').innerText;
window.open(base64Text, '_blank');
}
</script>
</body>
</html>
<?php
}
/**
* Парсит JSON-поле (DnsHosts и т.п.), возвращая либо объект, либо пустой объект
*/
function parseJsonField(string $input)
{
if (empty($input)) {
return new stdClass();
}
$decoded = json_decode($input, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Можно вывести ошибку, но для примера вернём пустой объект
return new stdClass();
}
return $decoded;
}
/**
* Рендер обычного текстового поля <input type="text">
*/
function renderInputField(string $key, array $field)
{
?>
<div class="mb-3">
<label for="<?php echo $key; ?>" class="form-label">
<?php echo htmlspecialchars($field['label']); ?>
</label>
<input type="text" class="form-control" name="<?php echo $key; ?>" id="<?php echo $key; ?>"
placeholder="<?php echo htmlspecialchars($field['placeholder']); ?>">
<div class="form-text">
<?php echo htmlspecialchars($field['description']); ?>
</div>
</div>
<?php
}
/**
* Рендер <select>
*/
function renderSelectField(string $key, array $field)
{
?>
<div class="mb-3">
<label for="<?php echo $key; ?>" class="form-label">
<?php echo htmlspecialchars($field['label']); ?>
</label>
<select class="form-select" name="<?php echo $key; ?>" id="<?php echo $key; ?>">
<?php
foreach ($field['options'] as $value => $optionLabel) {
echo '<option value="' . htmlspecialchars($value) . '">'
. htmlspecialchars($optionLabel) . '</option>';
}
?>
</select>
<div class="form-text">
<?php echo htmlspecialchars($field['description']); ?>
</div>
</div>
<?php
}
/**
* Рендер <textarea>
*/
function renderTextareaField(string $key, array $field)
{
?>
<div class="mb-3">
<label for="<?php echo $key; ?>" class="form-label">
<?php echo htmlspecialchars($field['label']); ?>
</label>
<textarea class="form-control" name="<?php echo $key; ?>" id="<?php echo $key; ?>"
rows="3" placeholder="<?php echo htmlspecialchars($field['placeholder']); ?>"></textarea>
<div class="form-text">
<?php echo htmlspecialchars($field['description']); ?>
</div>
</div>
<?php
}