refactor: tag getter

This commit is contained in:
mzz2017 2023-02-12 11:33:12 +08:00
parent a2bc4793a3
commit c43b6887d7
3 changed files with 16 additions and 27 deletions

View File

@ -133,20 +133,9 @@ func resolveFile(u *url.URL, configDir string) (b []byte, err error) {
func ResolveSubscription(log *logrus.Logger, configDir string, subscription string) (tag string, nodes []string, err error) {
/// Get tag.
iColon := strings.Index(subscription, ":")
if iColon == -1 {
goto parseUrl
}
// If first colon is like "://" in "scheme://linkbody", no tag is present.
if strings.HasPrefix(subscription[iColon:], "://") {
goto parseUrl
}
// Else tag is the part before colon.
tag = subscription[:iColon]
subscription = subscription[iColon+1:]
tag, subscription = common.GetTagFromLinkLikePlaintext(subscription)
/// Parse url.
parseUrl:
u, err := url.Parse(subscription)
if err != nil {
return tag, nil, fmt.Errorf("failed to parse subscription \"%v\": %w", subscription, err)

View File

@ -340,3 +340,16 @@ func MapKeys(m interface{}) (keys []string, err error) {
}
return keys, nil
}
func GetTagFromLinkLikePlaintext(link string) (tag string, afterTag string) {
iColon := strings.Index(link, ":")
if iColon == -1 {
return "", link
}
// If first colon is like "://" in "scheme://linkbody", no tag is present.
if strings.HasPrefix(link[iColon:], "://") {
return "", link
}
// Else tag is the part before colon.
return link[:iColon], link[iColon+1:]
}

View File

@ -7,8 +7,8 @@ package dialer
import (
"fmt"
"github.com/v2rayA/dae/common"
"net/url"
"strings"
)
type FromLinkCreator func(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error)
@ -21,20 +21,7 @@ func FromLinkRegister(name string, creator FromLinkCreator) {
func NewFromLink(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error) {
/// Get overwritten name.
var overwrittenName string
iColon := strings.Index(link, ":")
if iColon == -1 {
goto parseUrl
}
// If first colon is like "://" in "scheme://linkbody", no tag is present.
if strings.HasPrefix(link[iColon:], "://") {
goto parseUrl
}
// Else tag is the part before colon.
overwrittenName = link[:iColon]
link = link[iColon+1:]
parseUrl:
overwrittenName, link := common.GetTagFromLinkLikePlaintext(link)
u, err := url.Parse(link)
if err != nil {
return nil, err