Passed
Push — main ( e39e98...424964 )
by YeBeKhe
02:37
created

deleteFolder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 9
rs 10
1
<?php
2
header("Content-type: application/json;"); // Set response content type as JSON
3
4
include "modules/get_data.php"; // Include the get_data module
5
include "modules/config.php"; // Include the config module
6
include "modules/ranking.php"; // Include the ranking module
7
include "modules/singbox.php"; // Include the singbox module
8
9
function addHeader ($subscription, $subscriptionName) {
10
    $headerText = "#profile-title: base64:" . base64_encode($subscriptionName) . "
11
#profile-update-interval: 1
12
#subscription-userinfo: upload=0; download=0; total=10737418240000000; expire=2546249531
13
#support-url: https://t.me/v2raycollector
14
#profile-web-page-url: https://github.com/yebekhe/TelegramV2rayCollector
15
16
";
17
18
    return $headerText . $subscription;
19
}
20
21
function deleteFolder($folder) {
22
    if (!is_dir($folder)) {
23
        return;
24
    }
25
    $files = glob($folder . '/*');
26
    foreach ($files as $file) {
27
        is_dir($file) ? deleteFolder($file) : unlink($file);
28
    }
29
    rmdir($folder);
30
}
31
32
function seprate_by_country($configs){
33
    $configsArray = explode("\n", $configs);
34
    $configLocation = "";
35
    $output = [];
36
    foreach ($configsArray as $config) {
37
        $configType = detect_type($config);
38
39
        if ($configType === "vmess") {
40
            $configName = parse_config($config, "vmess", true)['ps'];
41
        } elseif ($configType === "vless" || $configType === "trojan" ){
42
            $configName = parse_config($config, $configType)['hash'];
43
        } elseif ($configType === "ss"){
44
            $configName = parse_config($config, "ss")['name'];
45
        }
46
47
        if (stripos($configName, "RELAY🚩")){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $configName does not seem to be defined for all execution paths leading up to this point.
Loading history...
48
            $configLocation = "RELAY";
49
        } else {
50
            $pattern = '/\b([A-Z]{2})\b[\x{1F1E6}-\x{1F1FF}]{2}/u';
51
            preg_match_all($pattern, $configName, $matches);
52
            $configLocation = $matches[1][0];
53
        }
54
55
        if (!isset($output[$configLocation])){
56
            $output[$configLocation] = [];
57
        }
58
        
59
        if (!in_array($config, $output[$configLocation])) {
60
            $output[$configLocation][] = $config;
61
        }
62
    }
63
    return $output;
64
}
65
66
function process_mix_json($input, $name)
67
{
68
    $mix_data_json = json_encode($input, JSON_PRETTY_PRINT); // Encode input array to JSON with pretty printing
69
    $mix_data_decode = json_decode($mix_data_json); // Decode the JSON into an object or array
70
    usort($mix_data_decode, "compare_time"); // Sort the decoded data using the "compare_time" function
71
    $mix_data_json = json_encode(
72
        $mix_data_decode,
73
        JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
74
    ); // Re-encode the sorted data to JSON with pretty printing and Unicode characters not escaped
75
    $mix_data_json = urldecode($mix_data_json);
76
    $mix_data_json = str_replace("amp;", "", $mix_data_json); // Replace HTML-encoded ampersands with regular ampersands
77
    $mix_data_json = str_replace("\\", "", $mix_data_json); // Remove backslashes from the JSON string
78
    file_put_contents("json/" . $name, $mix_data_json); // Save the JSON data to a file in the "json/" directory with the specified name
79
}
80
81
function fast_fix($input){
82
    $input = urldecode($input);
83
    $input = str_replace("amp;", "", $input);
84
    return $input;
85
}
86
87
function config_array($input){
88
    return array_map(function ($object) {
89
    return $object["config"];
90
}, $input);
91
}
92
93
$raw_url_base =
94
    "https://raw.githubusercontent.com/yebekhe/TelegramV2rayCollector/main"; // Define the base URL for fetching raw data
95
96
$mix_data = []; // Initialize an empty array for mix data
97
$vmess_data = []; // Initialize an empty array for vmess data
98
$trojan_data = []; // Initialize an empty array for trojan data
99
$vless_data = []; // Initialize an empty array for vless data
100
$shadowsocks_data = []; // Initialize an empty array for shadowsocks data
101
$tuic_data = []; // Initialize an empty array for tuic data
102
103
//Get data from channels
104
foreach ($Types as $key => $type_array) {
105
    $count = count($type_array);
106
    for ($type_count = $count - 1; $type_count >= 0; $type_count--) {
107
        $current_type = $type_array[$type_count];
108
        switch ($current_type) {
109
            case "vmess":
110
                // Merge the results of `get_config` function with $vmess_data array
111
                $vmess_data = array_merge(
112
                    $vmess_data,
113
                    /** @scrutinizer ignore-call */ 
114
                    get_config($key, $current_type)
115
                );
116
                break;
117
            case "vless":
118
                // Merge the results of `get_config` function with $vless_data array
119
                $vless_data = array_merge(
120
                    $vless_data,
121
                    /** @scrutinizer ignore-call */
122
                    get_config($key, $current_type)
123
                );
124
                break;
125
            case "trojan":
126
                // Merge the results of `get_config` function with $trojan_data array
127
                $trojan_data = array_merge(
128
                    $trojan_data,
129
                    /** @scrutinizer ignore-call */
130
                    get_config($key, $current_type)
131
                );
132
                break;
133
            case "ss":
134
                // Merge the results of `get_config` function with $shadowsocks_data array
135
                $shadowsocks_data = array_merge(
136
                    $shadowsocks_data,
137
                    /** @scrutinizer ignore-call */
138
                    get_config($key, $current_type)
139
                );
140
                break;
141
            case "tuic":
142
                // Merge the results of `get_config` function with $tuic_data array
143
                $tuic_data = array_merge(
144
                    $tuic_data,
145
                    /** @scrutinizer ignore-call */
146
                    get_config($key, $current_type)
147
                );
148
                break;
149
            default:
150
                // Do nothing if unknown type is encountered
151
                break;
152
        }
153
    }
154
}
155
156
$donated_vmess_data = []; // Initialize an empty array for vmess data
157
$donated_trojan_data = []; // Initialize an empty array for trojan data
158
$donated_vless_data = []; // Initialize an empty array for vless data
159
$donated_shadowsocks_data = []; // Initialize an empty array for shadowsocks data
160
$donated_tuic_data = []; // Initialize an empty array for tuic data
161
162
$base_donated_url = "https://yebekhe.000webhostapp.com/donate/donated_servers/";
163
164
$processed_subscription = [];
165
$usernames = [];
166
foreach ($donated_subscription as $url){
167
    $max_attempts = 3;
168
    $attempts = 0;
169
    while ($attempts < $max_attempts) {
170
        try {
171
            $usernames = json_decode(file_get_contents($url), true);
172
            break; // Success, so break out of the loop
173
        } catch (Exception $e) {
174
            // Handle the error here, e.g. by logging it
175
            $attempts++;
176
            if ($attempts == $max_attempts) {
177
             // Reached max attempts, so throw an exception to indicate failure
178
                throw new Exception('Failed to retrieve data after ' . $max_attempts . ' attempts.');
179
            }
180
        sleep(1); // Wait for 1 second before retrying
181
        }
182
    }
183
    foreach ($usernames as $username){
184
        $subscription_data = file_get_contents($base_donated_url . $username);
185
        $processed_subscription = /** @scrutinizer ignore-call */ process_subscription($subscription_data, $username);
186
        foreach ($processed_subscription as $donated_type => $donated_data){
187
            switch ($donated_type){
188
                case "vmess" :
189
                    $donated_vmess_data = array_merge(
190
                        $donated_vmess_data,
191
                        $donated_data
192
                    );
193
                    break;
194
                case "vless" :
195
                    $donated_vless_data = array_merge(
196
                        $donated_vless_data,
197
                        $donated_data
198
                    );
199
                    break;
200
                case "ss" :
201
                    $donated_shadowsocks_data = array_merge(
202
                        $donated_shadowsocks_data,
203
                        $donated_data
204
                    );
205
                    break;
206
                case "trojan" :
207
                    $donated_trojan_data = array_merge(
208
                        $donated_trojan_data,
209
                        $donated_data
210
                    );
211
                    break;
212
                case "tuic" :
213
                    $donated_tuic_data = array_merge(
214
                        $donated_tuic_data,
215
                        $donated_data
216
                    );
217
                    break;
218
                default:
219
                    // Do nothing if unknown type is encountered
220
                    break;
221
            }
222
        }
223
    }
224
}
225
226
$string_donated_vmess = $donated_vmess_data !== [] ? remove_duplicate_vmess(implode("\n", config_array($donated_vmess_data))) : "";
227
$string_donated_vless = $donated_vless_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_vless_data))), "vless") : "";
228
$string_donated_trojan = $donated_trojan_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_trojan_data))), "trojan") : "";
229
$string_donated_shadowsocks = $donated_shadowsocks_data !== [] ? remove_duplicate_ss(fast_fix(implode("\n", config_array($donated_shadowsocks_data)))) : "";
230
$string_donated_tuic = $donated_tuic_data !== [] ? remove_duplicate_ss(fast_fix(implode("\n", config_array($donated_tuic_data)))) : "";
231
$string_donated_reality = get_reality($string_donated_vless);
232
233
$donated_mix =
234
    $string_donated_vmess .
235
    "\n" .
236
    $string_donated_vless .
237
    "\n" .
238
    $string_donated_trojan .
239
    "\n" .
240
    $string_donated_shadowsocks .
241
    "\n" .
242
    $string_donated_tuic;
243
$donated_array = explode("\n", $donated_mix);
244
245
foreach ($donated_array as $key => $donated_config){
246
    if ($donated_config === ""){
247
        unset($donated_array[$key]);
248
    }
249
}
250
251
$donated_mix = implode("\n", $donated_array);
252
253
file_put_contents("sub/normal/donated", addHeader($donated_mix, "TVC | DONATED"));
254
file_put_contents("sub/base64/donated", base64_encode(addHeader($donated_mix, "TVC | DONATED")));
255
256
// Extract the "config" value from each object in $type_data and store it in $type_array
257
$vmess_array = config_array($vmess_data);
258
$vless_array = config_array($vless_data);
259
$trojan_array = config_array($trojan_data);
260
$shadowsocks_array = config_array($shadowsocks_data);
261
$tuic_array = config_array($tuic_data);
262
263
$fixed_string_vmess = remove_duplicate_vmess(implode("\n", $vmess_array));
264
$fixed_string_vmess_array = explode("\n", $fixed_string_vmess);
265
$json_vmess_array = [];
266
267
// Iterate over $vmess_data and $fixed_string_vmess_array to find matching configurations
268
foreach ($vmess_data as $vmess_config_data) {
269
    foreach ($fixed_string_vmess_array as $vmess_config) {
270
        if (
271
            decode_vmess($vmess_config)["ps"] ===
272
            decode_vmess($vmess_config_data["config"])["ps"]
273
        ) {
274
            // Add matching configuration to $json_vmess_array
275
            $json_vmess_array[] = $vmess_config_data;
276
        }
277
    }
278
}
279
280
$string_vless = fast_fix(implode("\n", $vless_array));
281
$fixed_string_vless = remove_duplicate_xray($string_vless, "vless");
282
$fixed_string_vless_array = explode("\n", $fixed_string_vless);
283
$json_vless_array = [];
284
285
// Iterate over $vless_data and $fixed_string_vless_array to find matching configurations
286
foreach ($vless_data as $vless_config_data) {
287
    foreach ($fixed_string_vless_array as $vless_config) {
288
        if (
289
            parseProxyUrl($vless_config, "vless")["hash"] ===
290
            parseProxyUrl($vless_config_data["config"], "vless")["hash"]
291
        ) {
292
            // Add matching configuration to $json_vless_array
293
            $json_vless_array[] = $vless_config_data;
294
        }
295
    }
296
}
297
298
$string_trojan = fast_fix(implode("\n", $trojan_array));
299
$fixed_string_trojan = remove_duplicate_xray($string_trojan, "trojan");
300
$fixed_string_trojan_array = explode("\n", $fixed_string_trojan);
301
$json_trojan_array = [];
302
303
// Iterate over $trojan_data and $fixed_string_trojan_array to find matching configurations
304
foreach ($trojan_data as $trojan_config_data) {
305
    foreach ($fixed_string_trojan_array as $key => $trojan_config) {
306
        if (
307
            parseProxyUrl($trojan_config)["hash"] ===
308
            parseProxyUrl($trojan_config_data["config"])["hash"]
309
        ) {
310
            // Add matching configuration to $json_trojan_array
311
            $json_trojan_array[$key] = $trojan_config_data;
312
        }
313
    }
314
}
315
316
$string_shadowsocks = fast_fix(implode("\n", $shadowsocks_array));
317
$fixed_string_shadowsocks = remove_duplicate_ss($string_shadowsocks);
318
$fixed_string_shadowsocks_array = explode("\n", $fixed_string_shadowsocks);
319
$json_shadowsocks_array = [];
320
321
// Iterate over $shadowsocks_data and $fixed_string_shadowsocks_array to find matching configurations
322
foreach ($shadowsocks_data as $shadowsocks_config_data) {
323
    foreach ($fixed_string_shadowsocks_array as $shadowsocks_config) {
324
        if (
325
            ParseShadowsocks($shadowsocks_config)["name"] ===
326
            ParseShadowsocks($shadowsocks_config_data["config"])["name"]
327
        ) {
328
            // Add matching configuration to $json_shadowsocks_array
329
            $json_shadowsocks_array[] = $shadowsocks_config_data;
330
        }
331
    }
332
}
333
334
$string_tuic = fast_fix(implode("\n", $tuic_array));
335
$fixed_string_tuic = remove_duplicate_tuic($string_tuic);
336
$fixed_string_tuic_array = explode("\n", $fixed_string_tuic);
337
$json_tuic_array = [];
338
339
// Iterate over $tuic_data and $fixed_string_tuic_array to find matching configurations
340
foreach ($tuic_data as $tuic_config_data) {
341
    foreach ($fixed_string_tuic_array as $key => $tuic_config) {
342
        if (
343
            parseTuic($tuic_config)["hash"] ===
344
            parseTuic($tuic_config_data["config"])["hash"]
345
        ) {
346
            // Add matching configuration to $json_tuic_array
347
            $json_tuic_array[$key] = $tuic_config_data;
348
        }
349
    }
350
}
351
352
$fixed_string_reality = get_reality($fixed_string_vless);
353
354
$mix =
355
    $fixed_string_vmess .
356
    "\n" .
357
    $fixed_string_vless .
358
    "\n" .
359
    $fixed_string_trojan .
360
    "\n" .
361
    $fixed_string_shadowsocks .
362
    "\n" .
363
    $fixed_string_tuic .
364
    "\n" .
365
    $donated_mix;
366
367
$mix_data = array_merge(
368
    $vmess_data,
369
    $vless_data,
370
    $trojan_data,
371
    $shadowsocks_data,
372
    $tuic_data,
373
);
374
375
$mix_data_deduplicate = array_merge(
376
    $json_vmess_array,
377
    $json_vless_array,
378
    $json_trojan_array,
379
    $json_shadowsocks_array,
380
    $json_tuic_array
381
);
382
383
$subscription_types = [
384
    "mix" => base64_encode(addHeader($mix, "TVC | MIX")),
385
    "vmess" => base64_encode(addHeader($fixed_string_vmess, "TVC | VMESS")),
386
    "vless" => base64_encode(addHeader($fixed_string_vless, "TVC | VLESS")),
387
    "reality" => base64_encode(addHeader($fixed_string_reality, "TVC | REALITY")),
388
    "trojan" => base64_encode(addHeader($fixed_string_trojan, "TVC | TROJAN")),
389
    "shadowsocks" => base64_encode(addHeader($fixed_string_shadowsocks, "TVC | SHADOWSOCKS")),
390
    "tuic" => base64_encode(addHeader($fixed_string_tuic, "TVC | tuic")),
391
];
392
393
// Write subscription data to files
394
foreach ($subscription_types as $subscription_type => $subscription_data) {
395
    file_put_contents(
396
        "sub/normal/" . $subscription_type,
397
        base64_decode($subscription_data)
398
    );
399
    file_put_contents(
400
        "sub/base64/" . $subscription_type,
401
        $subscription_data
402
    );
403
}
404
405
$countryBased = seprate_by_country($mix);
406
deleteFolder("country");
407
mkdir("country");
408
foreach ($countryBased as $country => $configsArray) {
409
    if (!is_dir("country/". $country)) {
410
        mkdir("country/". $country);
411
    }
412
    $configsSub = implode("\n", $configsArray);
413
    file_put_contents("country/". $country . "/normal", $configsSub);
414
    file_put_contents("country/". $country . "/base64", base64_encode($configsSub));
415
}
416
417
process_mix_json($mix_data, "configs.json");
418
process_mix_json($mix_data_deduplicate, "configs_deduplicate.json");
419
420
$singboxTypes = [
421
    "mix" => $mix,
422
    "vmess" => $fixed_string_vmess,
423
    "vless" => $fixed_string_vless,
424
    "trojan" => $fixed_string_trojan,
425
    "shadowsocks" => $fixed_string_shadowsocks,
426
    "tuic" => $fixed_string_tuic,
427
];
428
429
foreach ($singboxTypes as $singboxType => $subContents) {
430
    file_put_contents("singbox/nekobox/118/" . $singboxType . ".json", GenerateConfig($subContents, "nnew", $singboxType));
431
    file_put_contents("singbox/nekobox/117/" . $singboxType . ".json", GenerateConfig($subContents, "nold", $singboxType));
432
    file_put_contents("singbox/sfasfi/" . $singboxType . ".json", GenerateConfig($subContents, "sfia", $singboxType));
433
    file_put_contents("singbox/nekobox/118/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nnew", $singboxType));
434
    file_put_contents("singbox/nekobox/117/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nold", $singboxType));
435
    file_put_contents("singbox/sfasfi/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "sfia", $singboxType));
436
}
437
438
$the_string_reality_singbox = $fixed_string_reality . "\n" . $string_donated_reality ;
439
$string_reality_singbox = remove_duplicate_xray($the_string_reality_singbox, "vless");
440
441
file_put_contents("singbox/nekobox/117/reality.json", GenerateConfig($string_reality_singbox, "nold", "reality"));
442
file_put_contents("singbox/nekobox/118/reality.json", GenerateConfig($string_reality_singbox, "nnew", "reality"));
443
file_put_contents("singbox/sfasfi/reality.json", GenerateConfig($string_reality_singbox,"sfia", "reality"));
444
file_put_contents("singbox/nekobox/117/realityLite.json", GenerateConfigLite($string_reality_singbox, "nold", "reality"));
445
file_put_contents("singbox/nekobox/118/realityLite.json", GenerateConfigLite($string_reality_singbox, "nnew", "reality"));
446
file_put_contents("singbox/sfasfi/realityLite.json", GenerateConfigLite($string_reality_singbox,"sfia", "reality"));
447
448
$data = [
449
    [
450
        "data" => $vmess_data,
451
        "filename" => "channel_ranking_vmess.json",
452
        "type" => "vmess",
453
    ],
454
    [
455
        "data" => $vless_data,
456
        "filename" => "channel_ranking_vless.json",
457
        "type" => "vless",
458
    ],
459
    [
460
        "data" => $trojan_data,
461
        "filename" => "channel_ranking_trojan.json",
462
        "type" => "trojan",
463
    ],
464
    [
465
        "data" => $shadowsocks_data,
466
        "filename" => "channel_ranking_ss.json",
467
        "type" => "ss",
468
    ],
469
    [
470
        "data" => $tuic_data,
471
        "filename" => "channel_ranking_tuic.json",
472
        "type" => "tuic",
473
    ],
474
];
475
476
// Process each item in the data array
477
foreach ($data as $item) {
478
    // Calculate ranking for the specific type of data
479
    $channel_ranking = ranking($item["data"], $item["type"]);
480
481
    // Convert the ranking to JSON format
482
    $json_content = json_encode($channel_ranking, JSON_PRETTY_PRINT);
483
484
    // Write the JSON content to a file in the "ranking" directory
485
    file_put_contents("ranking/{$item["filename"]}", $json_content);
486
}
487