Skip to content

Commit

Permalink
refactor: ua set
Browse files Browse the repository at this point in the history
  • Loading branch information
snakem982 committed Dec 7, 2024
1 parent caea31b commit 0b20e08
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 61 deletions.
56 changes: 4 additions & 52 deletions backend/api/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

// 按行读取文件
Expand All @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions backend/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions backend/spider/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
20 changes: 15 additions & 5 deletions backend/tools/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,6 +16,8 @@ import (
"time"
)

var UA = "clash-verge/v2.0.2"

var dialerBaidu = &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0b20e08

Please sign in to comment.