diff --git a/backend/api/profile.go b/backend/api/profile.go index 2868d02..6bb1f39 100644 --- a/backend/api/profile.go +++ b/backend/api/profile.go @@ -109,16 +109,6 @@ func postProfile(w http.ResponseWriter, r *http.Request) { // 尝试clash解析 成功返回 bodyData := []byte(body.Data) - _, err := config.UnmarshalRawConfig(bodyData) - if err == nil { - err = ResolveConfig(false, false, "", "", tools.Dec(15), 41, bodyData) - if err == nil { - render.NoContent(w, r) - return - } - } - - builder := strings.Builder{} urls := make([]string, 0) // 按行读取文件 @@ -136,12 +126,10 @@ func postProfile(w http.ResponseWriter, r *http.Request) { sub = strings.Split(sub, " ")[0] if strings.HasPrefix(sub, "http") { urls = append(urls, sub) - } else if strings.Contains(sub, "://") { - builder.WriteString(sub + "\n") - } else { } } + // 获取url订阅中的内容 for _, url := range urls { content, fileName := tools.ConcurrentHttpGet(url, nil) err := ResolveConfig(false, false, "", url, fileName, 31, content) @@ -151,45 +139,9 @@ func postProfile(w http.ResponseWriter, r *http.Request) { } } - if builder.Len() > 0 || len(urls) == 0 { - var all []byte - if builder.Len() > 0 { - all = []byte(builder.String()) - } else { - all = bodyData - } - - ray, base64Error := convert.ConvertsV2Ray(all) - if base64Error == nil && len(ray) > 0 { - ray = resolve.MapsToProxies(ray) - rails := spider.SortAddIndex(ray) - if len(rails) == 0 { - render.NoContent(w, r) - return - } - if len(rails) > 511 { - rails = rails[0:512] - } - proxies := make(map[string]any) - proxies["proxies"] = rails - all, _ = yaml.Marshal(proxies) - - suffix := "txt" - kind := 2 - snowflakeId := tools.SnowflakeId() - profile := resolve.Profile{} - profile.Id = fmt.Sprintf("%s%d", constant.PrefixProfile, snowflakeId) - profile.Type = kind - profile.Title = fmt.Sprintf("%d", snowflakeId) - profile.Order = snowflakeId - profile.Path = "uploads/" + profile.Id + "." + suffix - - fileSaveError := saveProfile2Local(profile.Path, all) - if fileSaveError == nil { - marshal, _ := json.Marshal(profile) - _ = cache.Put(profile.Id, marshal) - } - } + // 解析直接输入的内容 + if len(urls) == 0 { + _ = ResolveConfig(false, false, "", "", tools.Dec(15), 41, bodyData) } render.NoContent(w, r) diff --git a/backend/meta/meta.go b/backend/meta/meta.go index 2794c98..49f0c8d 100644 --- a/backend/meta/meta.go +++ b/backend/meta/meta.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "github.com/metacubex/bbolt" - ctp "github.com/metacubex/mihomo/component/http" "github.com/metacubex/mihomo/component/profile/cachefile" "github.com/metacubex/mihomo/config" C "github.com/metacubex/mihomo/constant" @@ -191,13 +190,15 @@ func StartCore(profile resolve.Profile, reload bool) { rawCfg.Tun.Enable = false } + // 设置UA + tools.SetUA(rawCfg.GlobalUA) + NowConfig, err = config.ParseRawConfig(rawCfg) if err != nil { log.Warnln("Parse config error: %s", err.Error()) return } - ctp.SetUA("Clash.Meta/1.18.10") if !reload { freePort, err := tools.GetFreeWithPort(10000) if err != nil { diff --git a/backend/spider/fuzzy.go b/backend/spider/fuzzy.go index d64d986..0f1195c 100644 --- a/backend/spider/fuzzy.go +++ b/backend/spider/fuzzy.go @@ -146,12 +146,12 @@ func ComputeFuzzy(content []byte, headers map[string]string) []map[string]any { ok = collect.Get() } else { all := GetBytes(url, headers) - if all == nil || len(all) < 16 { + if all == nil || len(all) < 32 { return } isGo := true rawCfgInner, err := config.UnmarshalRawConfig(all) - if err == nil && rawCfgInner.Proxy != nil { + if err == nil && len(rawCfgInner.Proxy) > 0 { ok = rawCfgInner.Proxy isGo = false } @@ -162,6 +162,13 @@ func ComputeFuzzy(content []byte, headers map[string]string) []map[string]any { isGo = false } } + if isGo { + sing, err := convert.ConvertsSingBox(all) + if err == nil && sing != nil { + ok = sing + isGo = false + } + } if isGo { builder := strings.Builder{} for _, link := range grepShareLink(all) { diff --git a/backend/tools/httpclient.go b/backend/tools/httpclient.go index db1cb95..db2af07 100644 --- a/backend/tools/httpclient.go +++ b/backend/tools/httpclient.go @@ -3,7 +3,7 @@ package tools import ( "crypto/tls" "fmt" - ctp "github.com/metacubex/mihomo/component/http" + C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/hub/executor" "github.com/metacubex/mihomo/log" "golang.org/x/net/context" @@ -16,6 +16,8 @@ import ( "time" ) +var UA = "clash-verge/v2.0.2" + var dialerBaidu = &net.Dialer{ Resolver: &net.Resolver{ PreferGo: true, @@ -32,6 +34,14 @@ var dialBaiduContext = func(ctx context.Context, network, addr string) (net.Conn return dialerBaidu.DialContext(ctx, network, addr) } +func SetUA(ua string) { + if ua != "clash.meta/"+C.Version { + UA = ua + } else { + UA = "clash-verge/v2.0.2" + } +} + // GetFileName 获取响应头中的文件名 func GetFileName(header http.Header) (fileName string) { disposition := header.Get("content-disposition") @@ -72,7 +82,7 @@ func HttpGetByProxy(requestUrl string, headers map[string]string) ([]byte, strin } req.Header.Set("Accept-Encoding", "utf-8") req.Header.Set("Accept", "*/*") - req.Header.Set("User-Agent", ctp.UA()) + req.Header.Set("User-Agent", UA) if headers != nil && len(headers) > 0 { for k, v := range headers { req.Header.Set(k, v) @@ -136,9 +146,9 @@ func HttpGetWithTimeout(requestUrl string, outTime time.Duration, needDail bool, log.Warnln("HttpGetWithTimeout http.NewRequest %s %v", requestUrl, err) return nil, "", err } - req.Header.Set("Accept-Encoding", "utf-8") // 设置响应内容编码为utf-8 - req.Header.Set("Accept", "*/*") // 设置响应内容类型为全部 - req.Header.Set("User-Agent", ctp.UA()) // 设置用户代理为C.UA + req.Header.Set("Accept-Encoding", "utf-8") + req.Header.Set("Accept", "*/*") + req.Header.Set("User-Agent", UA) if headers != nil && len(headers) > 0 { for k, v := range headers { req.Header.Set(k, v)