This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.go
More file actions
89 lines (78 loc) · 2.35 KB
/
clean.go
File metadata and controls
89 lines (78 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package singproxy
import (
"fmt"
"net/url"
"strings"
)
func cleanBruteForce(cleanedURL string) string {
schemeEnd := strings.Index(cleanedURL, "://")
if schemeEnd != -1 {
scheme := cleanedURL[:schemeEnd]
rest := cleanedURL[schemeEnd+3:]
switch scheme {
case "ss":
// Handles ss://user@host:port@comment format
parts := strings.Split(cleanedURL, "@")
if len(parts) > 2 {
realHostAndPort := parts[len(parts)-2]
userInfoPart := strings.Join(parts[:len(parts)-2], "@")
cleanedURL = fmt.Sprintf("%s@%s", userInfoPart, realHostAndPort)
}
case "trojan", "hysteria2":
if atIndex := strings.LastIndex(rest, "@"); atIndex > 0 {
userInfo := rest[:atIndex]
hostInfo := rest[atIndex+1:]
safeUserInfo := strings.ReplaceAll(userInfo, "-->", "--")
safeUserInfo = strings.ReplaceAll(safeUserInfo, " ", "%20")
cleanedURL = fmt.Sprintf("%s://%s@%s", scheme, safeUserInfo, hostInfo)
}
case "vless":
if atIndex := strings.LastIndex(rest, "@"); atIndex != -1 {
hostPartStart := atIndex + 1
hostAndQuery := rest[hostPartStart:]
queryIndex := strings.Index(hostAndQuery, "?")
hostPart := hostAndQuery
if queryIndex != -1 {
hostPart = hostAndQuery[:queryIndex]
}
if strings.Contains(hostPart, "---") {
cleanHost := strings.Split(hostPart, "---")[0]
cleanedURL = cleanedURL[:schemeEnd+3+atIndex+1] + cleanHost + cleanedURL[schemeEnd+3+atIndex+1+len(hostPart):]
}
}
case "vmess":
payload := cleanedURL[schemeEnd+3:]
cleanedPayload := nonBase64Chars.ReplaceAllString(payload, "")
cleanedURL = "vmess://" + cleanedPayload
}
}
return cleanedURL
}
func cleanProxyURL(raw string) string {
raw = strings.ReplaceAll(raw, "\u0026amp;", "&")
raw = strings.ReplaceAll(raw, "\u0026", "&")
raw = strings.ReplaceAll(raw, "&", "&")
raw = strings.ReplaceAll(raw, "&", "&")
noFragment := raw
if i := strings.Index(raw, "#"); i != -1 {
noFragment = raw[:i]
}
base, rawQuery := noFragment, ""
if i := strings.Index(noFragment, "?"); i != -1 {
base = noFragment[:i]
rawQuery = noFragment[i+1:]
}
queryVals, err := url.ParseQuery(rawQuery)
if err != nil {
return raw
}
queryVals.Del("ps")
queryVals.Del("tag")
queryVals.Del("label")
queryVals.Del("remarks")
queryStr := queryVals.Encode()
if queryStr != "" {
return base + "?" + queryStr
}
return cleanBruteForce(base)
}