Issues (17)

modules/xray.php (1 issue)

1
<?php
2
3
function parseProxyUrl($url, $type = "trojan")
4
{
5
    // Parse the URL into components
6
    $parsedUrl = parse_url($url);
7
8
    // Extract the parameters from the query string
9
    $params = [];
10
    if (isset($parsedUrl["query"])) {
11
        parse_str($parsedUrl["query"], $params);
12
    }
13
14
    // Construct the output object
15
    $output = [
16
        "protocol" => $type,
17
        "username" => isset($parsedUrl["user"]) ? $parsedUrl["user"] : "",
18
        "hostname" => isset($parsedUrl["host"]) ? $parsedUrl["host"] : "",
19
        "port" => isset($parsedUrl["port"]) ? $parsedUrl["port"]: "",
20
        "params" => $params,
21
        "hash" => isset($parsedUrl["fragment"]) ? $parsedUrl["fragment"] : "",
22
    ];
23
24
    return $output;
25
}
26
27
function buildProxyUrl($obj, $type = "trojan")
28
{
29
    $url = $type . "://";
30
    $url .= addUsernameAndPassword($obj);
31
    $url .= $obj["hostname"];
32
    $url .= addPort($obj);
33
    $url .= addParams($obj);
34
    $url .= addHash($obj);
35
    return $url;
36
}
37
38
function addUsernameAndPassword($obj)
39
{
40
    $url = "";
41
    if ($obj["username"] !== "") {
42
        $url .= $obj["username"];
43
        if (isset($obj["pass"]) && $obj["pass"] !== "") {
44
            $url .= ":" . $obj["pass"];
45
        }
46
        $url .= "@";
47
    }
48
    return $url;
49
}
50
51
function addPort($obj)
52
{
53
    $url = "";
54
    if (isset($obj["port"]) && $obj["port"] !== "") {
55
        $url .= ":" . $obj["port"];
56
    }
57
    return $url;
58
}
59
60
function addParams($obj)
61
{
62
    $url = "";
63
    if (!empty($obj["params"])) {
64
        $url .= "?" . http_build_query($obj["params"]);
65
    }
66
    return $url;
67
}
68
69
function addHash($obj)
70
{
71
    $url = "";
72
    if (isset($obj["hash"]) && $obj["hash"] !== "") {
73
        $url .= "#" . $obj["hash"];
74
    }
75
    return $url;
76
}
77
78
function remove_duplicate_xray($input, $type)
79
{
80
    $array = explode("\n", $input);
81
82
    foreach ($array as $item) {
83
        $parts = parseProxyUrl($item, $type);
84
        $part_hash = $parts["hash"];
85
        unset($parts["hash"]);
86
        ksort($parts["params"]);
87
        $part_serialize = base64_encode(serialize($parts));
88
        $result[$part_serialize][] = $part_hash ?? "";
89
    }
90
91
    $finalResult = [];
92
    foreach ($result as $url => $parts) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 82. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
93
        $partAfterHash = $parts[0] ?? "";
94
        $part_serialize = unserialize(base64_decode($url));
95
        $part_serialize["hash"] = $partAfterHash;
96
        $finalResult[] = buildProxyUrl($part_serialize, $type);
97
    }
98
99
    $output = "";
100
    foreach ($finalResult as $config) {
101
        $output .= $output == "" ? $config : "\n" . $config;
102
    }
103
    return $output;
104
}
105
106
107
function get_reality($input)
108
{
109
    $array = explode("\n", $input);
110
    $output = "";
111
    foreach ($array as $item) {
112
        if (stripos($item, "reality")) {
113
            $output .= $output === "" ? $item : "\n$item";
114
        }
115
    }
116
    return $output;
117
}
118