Issues (17)

modules/hysteria2.php (1 issue)

1
<?php
2
3
function parseHy2 ($config_str) {
4
    $parsedUrl = parse_url($config_str);
5
6
    // Extract the parameters from the query string
7
    $params = [];
8
    if (isset($parsedUrl["query"])) {
9
        parse_str($parsedUrl["query"], $params);
10
    }
11
12
    // Construct the output object
13
    $output = [
14
        "protocol" => "hy2",
15
        "username" => isset($parsedUrl["user"]) ? $parsedUrl["user"] : "",
16
        "hostname" => isset($parsedUrl["host"]) ? $parsedUrl["host"] : "",
17
        "port" => isset($parsedUrl["port"]) ? $parsedUrl["port"] : "",
18
        "params" => $params,
19
        "hash" => isset($parsedUrl["fragment"]) ? $parsedUrl["fragment"] : "",
20
    ];
21
22
    return $output;
23
24
}
25
26
function buildHy2($obj)
27
{
28
    $url = "hy2://";
29
    $url .= addUsernameAndPassword($obj);
30
    $url .= $obj["hostname"];
31
    $url .= addPort($obj);
32
    $url .= addParams($obj);
33
    $url .= addHash($obj);
34
    return $url;
35
}
36
37
function remove_duplicate_hy2($input)
38
{
39
    $array = explode("\n", $input);
40
41
    foreach ($array as $item) {
42
        $parts = parseHy2($item);
43
        $part_hash = $parts["hash"];
44
        unset($parts["hash"]);
45
        ksort($parts["params"]);
46
        $part_serialize = base64_encode(serialize($parts));
47
        $result[$part_serialize][] = $part_hash ?? "";
48
    }
49
50
    $finalResult = [];
51
    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 41. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
52
        $partAfterHash = $parts[0] ?? "";
53
        $part_serialize = unserialize(base64_decode($url));
54
        $part_serialize["hash"] = $partAfterHash;
55
        $finalResult[] = buildHy2($part_serialize);
56
    }
57
58
    $output = "";
59
    foreach ($finalResult as $config) {
60
        $output .= $output == "" ? $config : "\n" . $config;
61
    }
62
    return $output;
63
}
64