3 Commits

2 changed files with 22 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"strconv"
"strings"
"unicode/utf8"
P "github.com/bestnite/sub2clash/model/proxy"
)
@ -32,8 +33,13 @@ func ParsePort(portStr string) (int, error) {
return port, nil
}
// isLikelyBase64 不严格判断是否是合法的 Base64, 很多分享链接不符合 Base64 规范
func isLikelyBase64(s string) bool {
if len(s)%4 == 0 && strings.HasSuffix(s, "=") && !strings.Contains(strings.TrimSuffix(s, "="), "=") {
if strings.TrimSpace(s) == "" {
return false
}
if !strings.Contains(strings.TrimSuffix(s, "="), "=") {
s = strings.TrimSuffix(s, "=")
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for _, c := range s {
@ -41,17 +47,25 @@ func isLikelyBase64(s string) bool {
return false
}
}
return true
}
return false
decoded, err := DecodeBase64(s)
if err != nil {
return false
}
if !utf8.ValidString(decoded) {
return false
}
return true
}
func DecodeBase64(s string) (string, error) {
s = strings.TrimSpace(s)
if strings.Contains(s, "-") || strings.Contains(s, "_") {
s = strings.Replace(s, "-", "+", -1)
s = strings.Replace(s, "_", "/", -1)
s = strings.ReplaceAll(s, "-", "+")
s = strings.ReplaceAll(s, "_", "/")
}
if len(s)%4 != 0 {
s += strings.Repeat("=", 4-len(s)%4)

View File

@ -55,7 +55,7 @@
<label for="endpoint">客户端类型:</label>
<select class="form-control" id="endpoint" name="endpoint">
<option value="clash">Clash</option>
<option value="meta">Clash.Meta</option>
<option value="meta" selected>Clash.Meta</option>
</select>
</div>
<!-- Template -->
@ -75,9 +75,9 @@
</div>
<!-- User Agent -->
<div class="form-group mb-3">
<label for="user-agent">ua标识:</label>
<label for="user-agent">UA 标识:</label>
<textarea class="form-control" id="user-agent" name="user-agent"
placeholder="用于获取订阅的http请求中的user-agent标识(可选)" rows="3"></textarea>
placeholder="用于获取订阅的 http 请求中的 User-Agent 标识可选" rows="3"></textarea>
</div>
<!-- Refresh -->
<div class="form-check mb-3">