Passed
Push — main ( 953269...2f6d9e )
by YeBeKhe
02:11
created

get_config()   C

Complexity

Conditions 11
Paths 46

Size

Total Lines 80
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 53
c 3
b 0
f 0
nc 46
nop 2
dl 0
loc 80
rs 6.8787

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
include "flag.php";
3
include "ipinfo.php";
4
include "shadowsocks.php";
5
include "vmess.php";
6
include "xray.php";
7
include "ping.php";
8
9
function numberToEmoji($number)
10
{
11
    $map = [
12
        "0" => "0️⃣",
13
        "1" => "1️⃣",
14
        "2" => "2️⃣",
15
        "3" => "3️⃣",
16
        "4" => "4️⃣",
17
        "5" => "5️⃣",
18
        "6" => "6️⃣",
19
        "7" => "7️⃣",
20
        "8" => "8️⃣",
21
        "9" => "9️⃣",
22
    ];
23
24
    $emoji = "";
25
    $digits = str_split($number);
26
27
    foreach ($digits as $digit) {
28
        if (count($digits) === 1) {
29
            $emoji = $map["0"];
30
        }
31
        if (isset($map[$digit])) {
32
            $emoji .= $map[$digit];
33
        }
34
    }
35
36
    return $emoji;
37
}
38
39
function openLink($url)
40
{
41
    $ch = curl_init();
42
    curl_setopt_array($ch, [
43
        CURLOPT_URL => $url,
44
        CURLOPT_RETURNTRANSFER => 1,
45
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
46
        CURLOPT_FOLLOWLOCATION => true,
47
    ]);
48
    return curl_exec($ch);
49
}
50
51
function convert_to_iran_time($utc_timestamp)
52
{
53
    $utc_datetime = new DateTime($utc_timestamp);
54
    $utc_datetime->setTimezone(new DateTimeZone("Asia/Tehran"));
55
    return $utc_datetime->format("Y-m-d H:i:s");
56
}
57
58
function get_config_time($type, $input)
59
{
60
    preg_match_all(
61
        "/" . $type . ':\/\/[^"]+(?:[^<]+<[^<]+)*<time datetime="([^"]+)"/',
62
        $input,
63
        $times
64
    );
65
    return $times;
66
}
67
68
function get_config_items($type, $input)
69
{
70
    preg_match_all("#>" . $type . "://(.*?)<#", $input, $items);
71
    return $items;
72
}
73
74
function is_valid($input)
75
{
76
    if (stripos($input, "…") !== false or stripos($input, "...") !== false) {
77
        return false;
78
    }
79
    return true;
80
}
81
82
function is_reality($input, $type)
83
{
84
    switch ($type) {
85
        case "vmess":
86
            return false;
87
        case "vless":
88
            if (stripos($input, "reality") !== false) {
89
                return true;
90
            } else {
91
                return false;
92
            }
93
        case "trojan":
94
            return false;
95
        case "ss":
96
            return false;
97
    }
98
    return false;
99
}
100
101
function check_pbk($parsedVless)
0 ignored issues
show
Unused Code introduced by
The parameter $parsedVless is not used and could be removed. ( Ignorable by Annotation )

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

101
function check_pbk(/** @scrutinizer ignore-unused */ $parsedVless)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
{
103
    if (
104
        is_null($paresdVless["params"]["pbk"]) ||
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $paresdVless does not exist. Did you maybe mean $parsedVless?
Loading history...
105
        $paresdVless["params"]["pbk"] === ""
106
    ) {
107
        return false;
108
    } else {
109
        return true;
110
    }
111
}
112
113
function get_ip($input, $type, $is_reality)
114
{
115
    switch ($type) {
116
        case "vmess":
117
            return get_vmess_ip($input);
118
        case "vless":
119
            return get_vless_ip($input, $is_reality);
120
        case "trojan":
121
            return get_trojan_ip($input);
122
        case "ss":
123
            return get_ss_ip($input);
124
    }
125
}
126
127
function get_address($input, $type)
128
{
129
    switch ($type) {
130
        case "vmess":
131
            return $input["add"];
132
        case "vless":
133
        case "trojan":
134
            return $input["hostname"];
135
        case "ss":
136
            return $input["server_address"];
137
    }
138
}
139
140
function is_number_with_dots($s)
141
{
142
    /*
143
     * Returns true if the given string contains only digits and dots, and false otherwise.
144
     */
145
    for ($i = 0; $i < strlen($s); $i++) {
146
        $c = $s[$i];
147
        if (!ctype_digit($c) && $c != ".") {
148
            return false;
149
        }
150
    }
151
    return true;
152
}
153
154
function is_valid_address($address)
155
{
156
    $ipv4_pattern = '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/';
157
    $ipv6_pattern = '/^[0-9a-fA-F:]+$/'; // matches any valid IPv6 address
158
159
    if (
160
        preg_match($ipv4_pattern, $address) ||
161
        preg_match($ipv6_pattern, $address)
162
    ) {
163
        return true;
164
    } elseif (is_number_with_dots($address) === false) {
165
        if (
166
            substr($address, 0, 8) === "https://" ||
167
            substr($address, 0, 7) === "http://"
168
        ) {
169
            $url = filter_var($address, FILTER_VALIDATE_URL);
170
        } else {
171
            $url = filter_var("https://" . $address, FILTER_VALIDATE_URL);
172
        }
173
        if ($url !== false) {
174
            return true;
175
        } else {
176
            return false;
177
        }
178
    }
179
    return false;
180
}
181
182
function get_vmess_ip($input)
183
{
184
    return !empty($input["sni"])
185
        ? $input["sni"]
186
        : (!empty($input["host"])
187
            ? $input["host"]
188
            : $input["add"]);
189
}
190
191
function get_vless_ip($input, $is_reality)
192
{
193
    return $is_reality
194
        ? $input["hostname"]
195
        : (!empty($input["params"]["sni"])
196
            ? $input["params"]["sni"]
197
            : (!empty($input["params"]["host"])
198
                ? $input["params"]["host"]
199
                : $input["hostname"]));
200
}
201
202
function get_trojan_ip($input)
203
{
204
    return !empty($input["params"]["sni"])
205
        ? $input["params"]["sni"]
206
        : (!empty($input["params"]["host"])
207
            ? $input["params"]["host"]
208
            : $input["hostname"]);
209
}
210
211
function get_ss_ip($input)
212
{
213
    return $input["server_address"];
214
}
215
216
function get_port($input, $type)
217
{
218
    $port = "";
219
    switch ($type) {
220
        case "vmess":
221
            $port = $input["port"];
222
            break;
223
        case "vless":
224
            $port = $input["port"];
225
            break;
226
        case "trojan":
227
            $port = $input["port"];
228
            break;
229
        case "ss":
230
            $port = $input["server_port"];
231
            break;
232
    }
233
    return $port;
234
}
235
236
function get_flag($ip)
237
{
238
    $flag = "";
239
    $ip_info = ip_info($ip);
240
    if (isset($ip_info["country"])) {
241
        $location = $ip_info["country"];
242
        $flag = $location . getFlags($location);
243
    } else {
244
        $flag = "RELAY🚩";
245
    }
246
    return $flag;
247
}
248
249
function get_channels_assets()
250
{
251
    return json_decode(
252
        file_get_contents("modules/channels/channels_assets.json"),
253
        true
254
    );
255
}
256
257
function generate_name($channel, $flag, $is_reality, $number, $type)
258
{
259
    $name = "";
260
    switch ($is_reality) {
261
        case true:
262
            $name =
263
                "رایگان | REALITY | " .
264
                "@" .
265
                $channel .
266
                " | " .
267
                $flag .
268
                " | " .
269
                numberToEmoji($number);
270
            break;
271
        case false:
272
            $name =
273
                "رایگان | " .
274
                $type .
275
                " | @" .
276
                $channel .
277
                " | " .
278
                $flag .
279
                " | " .
280
                numberToEmoji($number);
281
            break;
282
    }
283
    return $name;
284
}
285
286
function parse_config($input, $type, $is_sub = false)
287
{
288
    $parsed_config = [];
289
    switch ($type) {
290
        case "vmess":
291
            $parsed_config = $is_sub
292
                ? decode_vmess($input)
293
                : decode_vmess($type . "://" . $input);
294
            break;
295
        case "vless":
296
        case "trojan":
297
            $parsed_config = $is_sub
298
                ? parseProxyUrl($input, $type)
299
                : parseProxyUrl($type . "://" . $input, $type);
300
            break;
301
        case "ss":
302
            $parsed_config = $is_sub
303
                ? ParseShadowsocks($input)
304
                : ParseShadowsocks($type . "://" . $input);
305
            break;
306
    }
307
    return $parsed_config;
308
}
309
310
function build_config($input, $type)
311
{
312
    $build_config = "";
313
    switch ($type) {
314
        case "vmess":
315
            $build_config = encode_vmess($input);
316
            break;
317
        case "vless":
318
        case "trojan":
319
            $build_config = buildProxyUrl($input, $type);
320
            break;
321
        case "ss":
322
            $build_config = BuildShadowsocks($input);
323
            break;
324
    }
325
    return $build_config;
326
}
327
328
function get_config($channel, $type)
329
{
330
    $name_array = [
331
        "vmess" => "ps",
332
        "vless" => "hash",
333
        "trojan" => "hash",
334
        "ss" => "name",
335
    ];
336
    // Fetch the content of the Telegram channel URL
337
    $get = file_get_contents("https://t.me/s/" . $channel);
338
339
    // Load channels_assets JSON data
340
    $channels_assets = get_channels_assets();
341
342
    $matches = get_config_time($type, $get);
343
    $configs = get_config_items($type, $get);
344
345
    $final_data = [];
346
    if ($channel === "v2raycollectordonate") {
347
        $key_limit = count($configs[1]) - 20;
348
    } else {
349
        $key_limit = count($configs[1]) - 3;
350
    }
351
    $config_number = 1;
352
353
    foreach ($configs[1] as $key => $config) {
354
        if ($key >= $key_limit) {
355
            if (is_valid($config)) {
356
                if (strpos($config, "<br/>") !== false) {
357
                    $config = substr($config, 0, strpos($config, "<br/>"));
358
                }
359
360
                $is_reality = is_reality($config, $type);
361
362
                $the_config = parse_config($config, $type);
363
                $check_pbk = $is_reality ? check_pbk($the_config) : true;
364
365
                $address = get_address($the_config, $type);
366
                if ($check_pbk) {
367
                    if (is_valid_address($address) !== false) {
368
                        $ip = get_ip($the_config, $type, $is_reality);
369
                        $port = get_port($the_config, $type);
370
371
                        @$ping_data = ping($ip, $port);
372
                        if ($ping_data !== "unavailable") {
373
                            $flag = get_flag($ip);
374
375
                            $name_key = $name_array[$type];
376
                            $the_config[$name_key] = generate_name(
377
                                $channel,
378
                                $flag,
379
                                $is_reality,
380
                                $config_number,
381
                                strtoupper($type)
382
                            );
383
384
                            $final_config = build_config($the_config, $type);
385
386
                            $final_data[$key]["channel"]["username"] = $channel;
387
                            $final_data[$key]["channel"]["title"] =
388
                                $channels_assets[$channel]["title"];
389
                            $final_data[$key]["channel"]["logo"] =
390
                                $channels_assets[$channel]["logo"];
391
                            $final_data[$key]["type"] = $is_reality
392
                                ? "reality"
393
                                : $type;
394
                            $final_data[$key]["config"] = $final_config;
395
                            $final_data[$key]["ping"] = $ping_data;
396
                            $final_data[$key]["time"] = convert_to_iran_time(
397
                                $matches[1][$key]
398
                            );
399
                            $config_number++;
400
                        }
401
                    }
402
                }
403
            }
404
        }
405
    }
406
    // Return the final data array
407
    return $final_data;
408
}
409
410
function detect_type($input)
411
{
412
    $type = "";
413
    if (substr($input, 0, 8) === "vmess://") {
414
        $type = "vmess";
415
    } elseif (substr($input, 0, 8) === "vless://") {
416
        $type = "vless";
417
    } elseif (substr($input, 0, 9) === "trojan://") {
418
        $type = "trojan";
419
    } elseif (substr($input, 0, 5) === "ss://") {
420
        $type = "ss";
421
    }
422
423
    return $type;
424
}
425
426
function process_subscription($input, $channel)
427
{
428
    $name_array = [
429
        "vmess" => "ps",
430
        "vless" => "hash",
431
        "trojan" => "hash",
432
        "ss" => "name",
433
    ];
434
435
    $final_data = [];
436
    $configs = explode("\n", $input);
437
    $array_helper_vmess = 0;
438
    $array_helper_vless = 0;
439
    $array_helper_ss = 0;
440
    $array_helper_trojan = 0;
441
    $config_number = 1;
442
    $i = 0;
443
    $channel = $channel . " | Donated";
444
    foreach ($configs as $config) {
445
        $type = detect_type($config);
446
        $is_reality = is_reality($config, $type);
447
448
        $the_config = parse_config($config, $type, true);
449
        $check_pbk = $is_reality ? check_pbk($the_config) : true;
450
451
        $address = get_address($the_config, $type);
452
        if ($check_pbk) {
453
            if (is_valid_address($address) !== false) {
454
                $ip = get_ip($the_config, $type, $is_reality);
455
                $port = get_port($the_config, $type);
456
457
                @$ping_data = ping($ip, $port);
458
                if ($ping_data !== "unavailable") {
459
                    $flag = get_flag($ip);
460
461
                    $name_key = $name_array[$type];
462
                    $the_config[$name_key] = generate_name(
463
                        $channel,
464
                        $flag,
465
                        $is_reality,
466
                        $config_number,
467
                        strtoupper($type)
468
                    );
469
                    $final_config = build_config($the_config, $type);
470
471
                    $key = ${"array_helper_$type"};
472
473
                    $final_data[$type][$key]["channel"]["username"] = $channel;
474
                    $final_data[$type][$key]["channel"]["title"] = $channel;
475
                    $final_data[$type][$key]["channel"]["logo"] = "null";
476
                    $final_data[$type][$key]["type"] = $is_reality
477
                        ? "reality"
478
                        : $type;
479
                    $final_data[$type][$key]["config"] = $final_config;
480
                    $final_data[$type][$key]["ping"] = $ping_data;
481
                    $final_data[$type][$key]["time"] = tehran_time();
482
483
                    $key++;
484
                    ${"array_helper_$type"} = $key;
485
                    $config_number++;
486
                }
487
            }
488
        }
489
    }
490
    $i++;
491
    return $final_data;
492
}
493