Passed
Push — main ( df14b3...e950a0 )
by YeBeKhe
03:47 queued 01:43
created

addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
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=455727941; download=6174315083; total=107374182400000000; expire=" . create_tehran_timestamp_tomorrow() ."
13
#support-url: https://t.me/v2raycollector
14
#profile-web-page-url: https://github.com/yebekhe/TelegramV2rayCollector
15
";
16
17
    return $headerText . $subscription;
18
}
19
20
function deleteFolder($folder) {
21
    if (!is_dir($folder)) {
22
        return;
23
    }
24
    $files = glob($folder . '/*');
25
    foreach ($files as $file) {
26
        is_dir($file) ? deleteFolder($file) : unlink($file);
27
    }
28
    rmdir($folder);
29
}
30
31
function seprate_by_country($configs){
32
    $configsArray = explode("\n", $configs);
33
    $configLocation = "";
34
    $output = [];
35
    foreach ($configsArray as $config) {
36
        $configType = detect_type($config);
37
38
        if ($configType === "vmess") {
39
            $configName = parse_config($config, "vmess", true)['ps'];
40
        } elseif ($configType === "vless" || $configType === "trojan" ){
41
            $configName = parse_config($config, $configType)['hash'];
42
        } elseif ($configType === "ss"){
43
            $configName = parse_config($config, "ss")['name'];
44
        }
45
46
        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...
47
            $configLocation = "RELAY";
48
        } else {
49
            $pattern = '/\b([A-Z]{2})\b[\x{1F1E6}-\x{1F1FF}]{2}/u';
50
            preg_match_all($pattern, $configName, $matches);
51
            $configLocation = $matches[1][0];
52
        }
53
54
        if (!isset($output[$configLocation])){
55
            $output[$configLocation] = [];
56
        }
57
        
58
        if (!in_array($config, $output[$configLocation])) {
59
            $output[$configLocation][] = $config;
60
        }
61
    }
62
    return $output;
63
}
64
65
function process_mix_json($input, $name)
66
{
67
    $mix_data_json = json_encode($input, JSON_PRETTY_PRINT); // Encode input array to JSON with pretty printing
68
    $mix_data_decode = json_decode($mix_data_json); // Decode the JSON into an object or array
69
    usort($mix_data_decode, "compare_time"); // Sort the decoded data using the "compare_time" function
70
    $mix_data_json = json_encode(
71
        $mix_data_decode,
72
        JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
73
    ); // Re-encode the sorted data to JSON with pretty printing and Unicode characters not escaped
74
    $mix_data_json = urldecode($mix_data_json);
75
    $mix_data_json = str_replace("amp;", "", $mix_data_json); // Replace HTML-encoded ampersands with regular ampersands
76
    $mix_data_json = str_replace("\\", "", $mix_data_json); // Remove backslashes from the JSON string
77
    file_put_contents("json/" . $name, $mix_data_json); // Save the JSON data to a file in the "json/" directory with the specified name
78
}
79
80
function fast_fix($input){
81
    $input = urldecode($input);
82
    $input = str_replace("amp;", "", $input);
83
    return $input;
84
}
85
86
function config_array($input){
87
    return array_map(function ($object) {
88
    return $object["config"];
89
}, $input);
90
}
91
92
$raw_url_base =
93
    "https://raw.githubusercontent.com/yebekhe/TelegramV2rayCollector/main"; // Define the base URL for fetching raw data
94
95
$mix_data = []; // Initialize an empty array for mix data
96
$vmess_data = []; // Initialize an empty array for vmess data
97
$trojan_data = []; // Initialize an empty array for trojan data
98
$vless_data = []; // Initialize an empty array for vless data
99
$shadowsocks_data = []; // Initialize an empty array for shadowsocks data
100
101
//Get data from channels
102
foreach ($Types as $key => $type_array) {
103
    $count = count($type_array);
104
    for ($type_count = $count - 1; $type_count >= 0; $type_count--) {
105
        $current_type = $type_array[$type_count];
106
        switch ($current_type) {
107
            case "vmess":
108
                // Merge the results of `get_config` function with $vmess_data array
109
                $vmess_data = array_merge(
110
                    $vmess_data,
111
                    /** @scrutinizer ignore-call */ 
112
                    get_config($key, $current_type)
113
                );
114
                break;
115
            case "vless":
116
                // Merge the results of `get_config` function with $vless_data array
117
                $vless_data = array_merge(
118
                    $vless_data,
119
                    /** @scrutinizer ignore-call */
120
                    get_config($key, $current_type)
121
                );
122
                break;
123
            case "trojan":
124
                // Merge the results of `get_config` function with $trojan_data array
125
                $trojan_data = array_merge(
126
                    $trojan_data,
127
                    /** @scrutinizer ignore-call */
128
                    get_config($key, $current_type)
129
                );
130
                break;
131
            case "ss":
132
                // Merge the results of `get_config` function with $shadowsocks_data array
133
                $shadowsocks_data = array_merge(
134
                    $shadowsocks_data,
135
                    /** @scrutinizer ignore-call */
136
                    get_config($key, $current_type)
137
                );
138
                break;
139
            default:
140
                // Do nothing if unknown type is encountered
141
                break;
142
        }
143
    }
144
}
145
146
$donated_vmess_data = []; // Initialize an empty array for vmess data
147
$donated_trojan_data = []; // Initialize an empty array for trojan data
148
$donated_vless_data = []; // Initialize an empty array for vless data
149
$donated_shadowsocks_data = []; // Initialize an empty array for shadowsocks data
150
151
$base_donated_url = "https://yebekhe.000webhostapp.com/donate/donated_servers/";
152
153
$processed_subscription = [];
154
$usernames = [];
155
foreach ($donated_subscription as $url){
156
    $max_attempts = 3;
157
    $attempts = 0;
158
    while ($attempts < $max_attempts) {
159
        try {
160
            $usernames = json_decode(file_get_contents($url), true);
161
            break; // Success, so break out of the loop
162
        } catch (Exception $e) {
163
            // Handle the error here, e.g. by logging it
164
            $attempts++;
165
            if ($attempts == $max_attempts) {
166
             // Reached max attempts, so throw an exception to indicate failure
167
                throw new Exception('Failed to retrieve data after ' . $max_attempts . ' attempts.');
168
            }
169
        sleep(1); // Wait for 1 second before retrying
170
        }
171
    }
172
    foreach ($usernames as $username){
173
        $subscription_data = file_get_contents($base_donated_url . $username);
174
        $processed_subscription = /** @scrutinizer ignore-call */ process_subscription($subscription_data, $username);
175
        foreach ($processed_subscription as $donated_type => $donated_data){
176
            switch ($donated_type){
177
                case "vmess" :
178
                    $donated_vmess_data = array_merge(
179
                        $donated_vmess_data,
180
                        $donated_data
181
                    );
182
                    break;
183
                case "vless" :
184
                    $donated_vless_data = array_merge(
185
                        $donated_vless_data,
186
                        $donated_data
187
                    );
188
                    break;
189
                case "ss" :
190
                    $donated_shadowsocks_data = array_merge(
191
                        $donated_shadowsocks_data,
192
                        $donated_data
193
                    );
194
                    break;
195
                case "trojan" :
196
                    $donated_trojan_data = array_merge(
197
                        $donated_trojan_data,
198
                        $donated_data
199
                    );
200
                    break;
201
            }
202
        }
203
    }
204
}
205
206
$string_donated_vmess = $donated_vmess_data !== [] ? remove_duplicate_vmess(implode("\n", config_array($donated_vmess_data))) : "";
207
$string_donated_vless = $donated_vless_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_vless_data))), "vless") : "";
208
$string_donated_trojan = $donated_vless_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_trojan_data))), "trojan") : "";
209
$string_donated_shadowsocks = $donated_vless_data !== [] ? remove_duplicate_ss(fast_fix(implode("\n", config_array($donated_shadowsocks_data)))) : "";
210
$string_donated_reality = get_reality($string_donated_vless);
211
212
$donated_mix =
213
    $string_donated_vmess .
214
    "\n" .
215
    $string_donated_vless .
216
    "\n" .
217
    $string_donated_trojan .
218
    "\n" .
219
    $string_donated_shadowsocks;
220
$donated_array = explode("\n", $donated_mix);
221
222
foreach ($donated_array as $key => $donated_config){
223
    if ($donated_config === ""){
224
        unset($donated_array[$key]);
225
    }
226
}
227
228
$donated_mix = implode("\n", $donated_array);
229
230
file_put_contents("sub/normal/donated", addHeader($donated_mix));
0 ignored issues
show
Bug introduced by
The call to addHeader() has too few arguments starting with subscriptionName. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

230
file_put_contents("sub/normal/donated", /** @scrutinizer ignore-call */ addHeader($donated_mix));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
231
file_put_contents("sub/base64/donated", base64_encode(addHeader($donated_mix)));
232
233
// Extract the "config" value from each object in $type_data and store it in $type_array
234
$vmess_array = config_array($vmess_data);
235
$vless_array = config_array($vless_data);
236
$trojan_array = config_array($trojan_data);
237
$shadowsocks_array = config_array($shadowsocks_data);
238
239
$fixed_string_vmess = remove_duplicate_vmess(implode("\n", $vmess_array));
240
$fixed_string_vmess_array = explode("\n", $fixed_string_vmess);
241
$json_vmess_array = [];
242
243
// Iterate over $vmess_data and $fixed_string_vmess_array to find matching configurations
244
foreach ($vmess_data as $vmess_config_data) {
245
    foreach ($fixed_string_vmess_array as $vmess_config) {
246
        if (
247
            decode_vmess($vmess_config)["ps"] ===
248
            decode_vmess($vmess_config_data["config"])["ps"]
249
        ) {
250
            // Add matching configuration to $json_vmess_array
251
            $json_vmess_array[] = $vmess_config_data;
252
        }
253
    }
254
}
255
256
$string_vless = fast_fix(implode("\n", $vless_array));
257
$fixed_string_vless = remove_duplicate_xray($string_vless, "vless");
258
$fixed_string_vless_array = explode("\n", $fixed_string_vless);
259
$json_vless_array = [];
260
261
// Iterate over $vless_data and $fixed_string_vless_array to find matching configurations
262
foreach ($vless_data as $vless_config_data) {
263
    foreach ($fixed_string_vless_array as $vless_config) {
264
        if (
265
            parseProxyUrl($vless_config, "vless")["hash"] ===
266
            parseProxyUrl($vless_config_data["config"], "vless")["hash"]
267
        ) {
268
            // Add matching configuration to $json_vless_array
269
            $json_vless_array[] = $vless_config_data;
270
        }
271
    }
272
}
273
274
$string_trojan = fast_fix(implode("\n", $trojan_array));
275
$fixed_string_trojan = remove_duplicate_xray($string_trojan, "trojan");
276
$fixed_string_trojan_array = explode("\n", $fixed_string_trojan);
277
$json_trojan_array = [];
278
279
// Iterate over $trojan_data and $fixed_string_trojan_array to find matching configurations
280
foreach ($trojan_data as $trojan_config_data) {
281
    foreach ($fixed_string_trojan_array as $key => $trojan_config) {
282
        if (
283
            parseProxyUrl($trojan_config)["hash"] ===
284
            parseProxyUrl($trojan_config_data["config"])["hash"]
285
        ) {
286
            // Add matching configuration to $json_trojan_array
287
            $json_trojan_array[$key] = $trojan_config_data;
288
        }
289
    }
290
}
291
292
$string_shadowsocks = fast_fix(implode("\n", $shadowsocks_array));
293
$fixed_string_shadowsocks = remove_duplicate_ss($string_shadowsocks);
294
$fixed_string_shadowsocks_array = explode("\n", $fixed_string_shadowsocks);
295
$json_shadowsocks_array = [];
296
297
// Iterate over $shadowsocks_data and $fixed_string_shadowsocks_array to find matching configurations
298
foreach ($shadowsocks_data as $shadowsocks_config_data) {
299
    foreach ($fixed_string_shadowsocks_array as $shadowsocks_config) {
300
        if (
301
            ParseShadowsocks($shadowsocks_config)["name"] ===
302
            ParseShadowsocks($shadowsocks_config_data["config"])["name"]
303
        ) {
304
            // Add matching configuration to $json_shadowsocks_array
305
            $json_shadowsocks_array[] = $shadowsocks_config_data;
306
        }
307
    }
308
}
309
310
$fixed_string_reality = get_reality($fixed_string_vless);
311
312
$mix =
313
    $fixed_string_vmess .
314
    "\n" .
315
    $fixed_string_vless .
316
    "\n" .
317
    $fixed_string_trojan .
318
    "\n" .
319
    $fixed_string_shadowsocks .
320
    "\n" .
321
    $donated_mix;
322
323
$mix_data = array_merge(
324
    $vmess_data,
325
    $vless_data,
326
    $trojan_data,
327
    $shadowsocks_data
328
);
329
330
$mix_data_deduplicate = array_merge(
331
    $json_vmess_array,
332
    $json_vless_array,
333
    $json_trojan_array,
334
    $json_shadowsocks_array
335
);
336
337
$subscription_types = [
338
    "mix" => base64_encode($mix),
339
    "vmess" => base64_encode($fixed_string_vmess),
340
    "vless" => base64_encode($fixed_string_vless),
341
    "reality" => base64_encode($fixed_string_reality),
342
    "trojan" => base64_encode($fixed_string_trojan),
343
    "shadowsocks" => base64_encode($fixed_string_shadowsocks),
344
];
345
346
// Write subscription data to files
347
foreach ($subscription_types as $subscription_type => $subscription_data) {
348
    file_put_contents(
349
        "sub/normal/" . $subscription_type,
350
        base64_decode(addHeader($subscription_data))
351
    );
352
    file_put_contents(
353
        "sub/base64/" . $subscription_type,
354
        addHeader($subscription_data)
355
    );
356
}
357
358
$countryBased = seprate_by_country($mix);
359
deleteFolder("country");
360
mkdir("country");
361
foreach ($countryBased as $country => $configsArray) {
362
    if (!is_dir("country/". $country)) {
363
        mkdir("country/". $country);
364
    }
365
    $configsSub = implode("\n", $configsArray);
366
    file_put_contents("country/". $country . "/normal", $configsSub);
367
    file_put_contents("country/". $country . "/base64", base64_encode($configsSub));
368
}
369
370
process_mix_json($mix_data, "configs.json");
371
process_mix_json($mix_data_deduplicate, "configs_deduplicate.json");
372
373
$singboxTypes = [
374
    "mix" => $mix,
375
    "vmess" => $fixed_string_vmess,
376
    "vless" => $fixed_string_vless,
377
    "trojan" => $fixed_string_trojan,
378
    "shadowsocks" => $fixed_string_shadowsocks,
379
];
380
381
foreach ($singboxTypes as $singboxType => $subContents) {
382
    file_put_contents("singbox/nekobox/118/" . $singboxType . ".json", GenerateConfig($subContents, "nnew"));
383
    file_put_contents("singbox/nekobox/117/" . $singboxType . ".json", GenerateConfig($subContents, "nold"));
384
    file_put_contents("singbox/sfasfi/" . $singboxType . ".json", GenerateConfig($subContents, "sfia"));
385
    file_put_contents("singbox/nekobox/118/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nnew"));
386
    file_put_contents("singbox/nekobox/117/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nold"));
387
    file_put_contents("singbox/sfasfi/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "sfia"));
388
}
389
390
$the_string_reality_singbox = $fixed_string_reality . "\n" . $string_donated_reality ;
391
$string_reality_singbox = remove_duplicate_xray($the_string_reality_singbox, "vless");
392
393
file_put_contents("singbox/nekobox/117/reality.json", GenerateConfig($string_reality_singbox, "nold"));
394
file_put_contents("singbox/nekobox/118/reality.json", GenerateConfig($string_reality_singbox, "nnew"));
395
file_put_contents("singbox/sfasfi/reality.json", GenerateConfig($string_reality_singbox,"sfia"));
396
file_put_contents("singbox/nekobox/117/realityLite.json", GenerateConfigLite($string_reality_singbox, "nold"));
397
file_put_contents("singbox/nekobox/118/realityLite.json", GenerateConfigLite($string_reality_singbox, "nnew"));
398
file_put_contents("singbox/sfasfi/realityLite.json", GenerateConfigLite($string_reality_singbox,"sfia"));
399
400
$data = [
401
    [
402
        "data" => $vmess_data,
403
        "filename" => "channel_ranking_vmess.json",
404
        "type" => "vmess",
405
    ],
406
    [
407
        "data" => $vless_data,
408
        "filename" => "channel_ranking_vless.json",
409
        "type" => "vless",
410
    ],
411
    [
412
        "data" => $trojan_data,
413
        "filename" => "channel_ranking_trojan.json",
414
        "type" => "trojan",
415
    ],
416
    [
417
        "data" => $shadowsocks_data,
418
        "filename" => "channel_ranking_ss.json",
419
        "type" => "ss",
420
    ],
421
];
422
423
// Process each item in the data array
424
foreach ($data as $item) {
425
    // Calculate ranking for the specific type of data
426
    $channel_ranking = ranking($item["data"], $item["type"]);
427
428
    // Convert the ranking to JSON format
429
    $json_content = json_encode($channel_ranking, JSON_PRETTY_PRINT);
430
431
    // Write the JSON content to a file in the "ranking" directory
432
    file_put_contents("ranking/{$item["filename"]}", $json_content);
433
}
434