Chore: cleanup code

This commit is contained in:
yaling888
2022-07-01 18:34:36 +08:00
parent e1fe8ce3b2
commit ae6cc1d67d
25 changed files with 159 additions and 189 deletions

View File

@@ -5,6 +5,7 @@ import (
"crypto/md5"
"os"
"path/filepath"
"regexp"
"time"
types "github.com/Dreamacro/clash/constant/provider"
@@ -14,6 +15,8 @@ import (
var (
fileMode os.FileMode = 0o666
dirMode os.FileMode = 0o755
commentRegx = regexp.MustCompile(`(.*#.*\n)`)
)
type parser[V any] func([]byte) (V, error)
@@ -168,6 +171,18 @@ func safeWrite(path string, buf []byte) error {
return os.WriteFile(path, buf, fileMode)
}
func removeComment(buf []byte) []byte {
arr := commentRegx.FindAllSubmatch(buf, -1)
for _, subs := range arr {
sub := subs[0]
if !bytes.HasPrefix(bytes.TrimLeft(sub, " "), []byte("#")) {
continue
}
buf = bytes.Replace(buf, sub, []byte(""), 1)
}
return buf
}
func newFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser parser[V], onUpdate func(V)) *fetcher[V] {
var ticker *time.Ticker
if interval != 0 {

View File

@@ -321,12 +321,13 @@ func proxiesParseAndFilter(filter string, filterReg *regexp.Regexp, forceCertVer
proxies := []C.Proxy{}
for idx, mapping := range schema.Proxies {
if name, ok := mapping["name"]; ok && len(filter) > 0 && !filterReg.MatchString(name.(string)) {
name, ok := mapping["name"].(string)
if ok && len(filter) > 0 && !filterReg.MatchString(name) {
continue
}
if prefixName != "" {
mapping["name"] = prefixName + mapping["name"].(string)
mapping["name"] = prefixName + name
}
proxy, err := adapter.ParseProxy(mapping, forceCertVerify)

View File

@@ -103,7 +103,7 @@ func (h *HTTPVehicle) Read() ([]byte, error) {
return nil, err
}
return buf, nil
return removeComment(buf), nil
}
func NewHTTPVehicle(url string, path string, header http.Header) *HTTPVehicle {