1
|
|
|
<?php |
2
|
|
|
function ParseShadowsocks($config_str) |
3
|
|
|
{ |
4
|
|
|
// Parse the config string as a URL |
5
|
|
|
$url = parse_url($config_str); |
6
|
|
|
|
7
|
|
|
// Extract the encryption method and password from the user info |
8
|
|
|
list($encryption_method, $password) = explode( |
9
|
|
|
":", |
10
|
|
|
base64_decode($url["user"]) |
11
|
|
|
); |
12
|
|
|
|
13
|
|
|
// Extract the server address and port from the host and path |
14
|
|
|
$server_address = $url["host"]; |
15
|
|
|
$server_port = $url["port"]; |
16
|
|
|
|
17
|
|
|
// Extract the name from the fragment (if present) |
18
|
|
|
$name = isset($url["fragment"]) ? urldecode($url["fragment"]) : null; |
19
|
|
|
|
20
|
|
|
// Create an array to hold the server configuration |
21
|
|
|
$server = [ |
22
|
|
|
"encryption_method" => $encryption_method, |
23
|
|
|
"password" => $password, |
24
|
|
|
"server_address" => $server_address, |
25
|
|
|
"server_port" => $server_port, |
26
|
|
|
"name" => $name, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
// Return the server configuration as a JSON string |
30
|
|
|
return $server; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function BuildShadowsocks($server) |
34
|
|
|
{ |
35
|
|
|
// Encode the encryption method and password as a Base64-encoded string |
36
|
|
|
$user = base64_encode( |
37
|
|
|
$server["encryption_method"] . ":" . $server["password"] |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
// Construct the URL from the server address, port, and user info |
41
|
|
|
$url = "ss://$user@{$server["server_address"]}:{$server["server_port"]}"; |
42
|
|
|
|
43
|
|
|
// If the name is present, add it as a fragment to the URL |
44
|
|
|
if (!empty($server["name"])) { |
45
|
|
|
$url .= "#" . urlencode($server["name"]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Return the URL as a string |
49
|
|
|
return $url; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function remove_duplicate_ss($input) |
53
|
|
|
{ |
54
|
|
|
$array = explode("\n", $input); |
55
|
|
|
|
56
|
|
|
foreach ($array as $item) { |
57
|
|
|
$parts = ParseShadowsocks($item); |
58
|
|
|
$part_hash = $parts["name"]; |
59
|
|
|
unset($parts["name"]); |
60
|
|
|
ksort($parts); |
61
|
|
|
$part_serialize = base64_encode(serialize($parts)); |
62
|
|
|
$result[$part_serialize][] = $part_hash ?? ""; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$finalResult = []; |
66
|
|
|
foreach ($result as $url => $parts) { |
|
|
|
|
67
|
|
|
$partAfterHash = $parts[0] ?? ""; |
68
|
|
|
$part_serialize = unserialize(base64_decode($url)); |
69
|
|
|
$part_serialize["name"] = $partAfterHash; |
70
|
|
|
$finalResult[] = BuildShadowsocks($part_serialize); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$output = ""; |
74
|
|
|
foreach ($finalResult as $config) { |
75
|
|
|
$output .= $output == "" ? $config : "\n" . $config; |
76
|
|
|
} |
77
|
|
|
return $output; |
78
|
|
|
} |
79
|
|
|
|