From 023a96a6d3424f84693d45dc7ba6ef0f612de3f7 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Tue, 24 Jan 2023 16:34:52 +0800 Subject: [PATCH] make ConvertsV2Ray more robust (#349) * make ConvertsV2Ray more robust * add log * fix --- adapter/outboundgroup/groupbase.go | 1 + adapter/provider/provider.go | 1 + common/convert/converter.go | 13 +++++++++++-- common/convert/v.go | 10 +++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/adapter/outboundgroup/groupbase.go b/adapter/outboundgroup/groupbase.go index fee24bd6a..0a4217938 100644 --- a/adapter/outboundgroup/groupbase.go +++ b/adapter/outboundgroup/groupbase.go @@ -165,6 +165,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { for i := range gb.excludeTypeArray { if strings.EqualFold(mType, gb.excludeTypeArray[i]) { flag = true + break } } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 7d7ba9771..e8bd7ed1f 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -301,6 +301,7 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray for i := range excludeTypeArray { if strings.EqualFold(pType, excludeTypeArray[i]) { flag = true + break } } diff --git a/common/convert/converter.go b/common/convert/converter.go index 891f43bbc..d58e0bab3 100644 --- a/common/convert/converter.go +++ b/common/convert/converter.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/Dreamacro/clash/log" "net/url" "strconv" "strings" @@ -120,7 +121,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) { } query := urlVLess.Query() vless := make(map[string]any, 20) - handleVShareLink(names, urlVLess, scheme, vless) + err = handleVShareLink(names, urlVLess, scheme, vless) + if err != nil { + log.Warnln("error:%s line:%s", err.Error(), line) + continue + } if flow := query.Get("flow"); flow != "" { vless["flow"] = strings.ToLower(flow) } @@ -138,7 +143,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) { } query := urlVMess.Query() vmess := make(map[string]any, 20) - handleVShareLink(names, urlVMess, scheme, vmess) + err = handleVShareLink(names, urlVMess, scheme, vmess) + if err != nil { + log.Warnln("error:%s line:%s", err.Error(), line) + continue + } vmess["alterId"] = 0 vmess["cipher"] = "auto" if encryption := query.Get("encryption"); encryption != "" { diff --git a/common/convert/v.go b/common/convert/v.go index 29be4a9f2..02c7b707a 100644 --- a/common/convert/v.go +++ b/common/convert/v.go @@ -1,15 +1,22 @@ package convert import ( + "errors" "net/url" "strings" ) -func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy map[string]any) { +func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy map[string]any) error { // Xray VMessAEAD / VLESS share link standard // https://github.com/XTLS/Xray-core/discussions/716 query := url.Query() proxy["name"] = uniqueName(names, url.Fragment) + if url.Hostname() == "" { + return errors.New("url.Hostname() is empty") + } + if url.Port() == "" { + return errors.New("url.Port() is empty") + } proxy["type"] = scheme proxy["server"] = url.Hostname() proxy["port"] = url.Port() @@ -94,4 +101,5 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m grpcOpts["grpc-service-name"] = query.Get("serviceName") proxy["grpc-opts"] = grpcOpts } + return nil }