From 9bb5ffbaef5fce6fd09a0b2d7ca14744f5bdaffc Mon Sep 17 00:00:00 2001 From: Zakk Date: Fri, 18 Sep 2026 02:53:30 +1000 Subject: [PATCH] feat(import): let the client say whether it imports a server or a subscription Import guessed from the URL scheme. Commit 3fd0e882 added http and https to the server prefixes so that http proxy nodes were not stored as subscriptions, and from then on every http(s) subscription address was parsed as a proxy node and failed with "ResolveURL failed: invalid parameters". POST /api/import now takes "kind": "server" or "subscription". Import is split into ImportServer and ImportSubscription; the old scheme-guessing Import remains for requests without kind, with http and https back on the subscription side as before 3fd0e882. The v4 migration calls the explicit functions since it knows which list each address came from. The GUI side (a server link / subscription address toggle that sends kind) is in the gui branch. --- service/pre_database.go | 4 +-- service/server/controller/import.go | 18 +++++++++-- service/server/service/import.go | 50 ++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/service/pre_database.go b/service/pre_database.go index 074159f9..73b64a58 100644 --- a/service/pre_database.go +++ b/service/pre_database.go @@ -126,7 +126,7 @@ func initConfigure() { subs := configurev4.GetSubscriptionsV2() for _, sub := range subs { log.Info("Importing subscription: %v", sub.Address) - if e := service.Import(sub.Address, nil); e != nil { + if e := service.ImportSubscription(sub.Address); e != nil { log.Warn("Failed to migrate subscription: %v", sub.Address) } } @@ -135,7 +135,7 @@ func initConfigure() { TYPE: configurev4.ServerType, ID: iSvr + 1, }); e == nil { - if e := service.Import(addr, nil); e != nil { + if e := service.ImportServer(addr, nil); e != nil { log.Warn("Failed to migrate server: %v", addr) } } diff --git a/service/server/controller/import.go b/service/server/controller/import.go index 4e9dc446..7513d950 100644 --- a/service/server/controller/import.go +++ b/service/server/controller/import.go @@ -12,7 +12,10 @@ import ( func PostImport(ctx *gin.Context) { var body struct { - URL string `json:"url"` + URL string `json:"url"` + // Kind is "server" or "subscription". Empty means the client did not + // say, and the scheme of URL decides. + Kind string `json:"kind"` Which interface{} `json:"which"` } if err := ctx.ShouldBindJSON(&body); err != nil { @@ -30,7 +33,18 @@ func PostImport(ctx *gin.Context) { } } - err := service.Import(body.URL, which) + var err error + switch body.Kind { + case "server": + err = service.ImportServer(body.URL, which) + case "subscription": + err = service.ImportSubscription(body.URL) + case "": + err = service.Import(body.URL, which) + default: + common.ResponseError(ctx, logError(fmt.Sprintf("bad request: unknown kind %q", body.Kind))) + return + } if err != nil { common.ResponseError(ctx, logError(err)) return diff --git a/service/server/service/import.go b/service/server/service/import.go index b84f3c1b..9329691f 100644 --- a/service/server/service/import.go +++ b/service/server/service/import.go @@ -27,8 +27,36 @@ func PluginManagerValidateLink(url string) bool { } } +// Import guesses whether url is a server link or a subscription address +// from its scheme. Callers that know which one they hold should use +// ImportServer or ImportSubscription instead; this wrapper only exists for +// clients that predate the "kind" field of POST /api/import. func Import(url string, which *configure.Which) (err error) { log.Trace("Import: received url=%v, which=%+v", url, which) + url = strings.TrimSpace(url) + if lines := strings.Split(url, "\n"); len(lines) >= 2 || strings.HasPrefix(url, "{") { + return ImportServer(url, which) + } + // "http" and "https" are deliberately absent: a bare http(s) URL has + // always meant a subscription here, and single HTTP proxy nodes are + // written as http-proxy:// or https-proxy://. + supportedPrefix := []string{"vmess", "vless", "ss", "ssr", "trojan", "trojan-go", "http-proxy", + "https-proxy", "socks5", "http2", "juicity", "tuic", "hysteria", "hysteria2", "anytls", + "shadowsocks", "shadowsocksr", "hy1", "hy2", "mcore", "mcp", "plugin", "wireguard"} + for i := range supportedPrefix { + supportedPrefix[i] += "://" + } + urlLower := strings.ToLower(url) + if PluginManagerValidateLink(url) || common.HasAnyPrefix(urlLower, supportedPrefix) { + return ImportServer(url, which) + } + return ImportSubscription(url) +} + +// ImportServer imports one server link, or several separated by newlines, +// or a JSON config. A non-nil which with ID > 0 replaces that server instead +// of appending. +func ImportServer(url string, which *configure.Which) (err error) { resolv.CheckResolvConf() url = strings.TrimSpace(url) if lines := strings.Split(url, "\n"); len(lines) >= 2 || strings.HasPrefix(url, "{") { @@ -43,15 +71,7 @@ func Import(url string, which *configure.Which) (err error) { } return nil } - supportedPrefix := []string{"vmess", "vless", "ss", "ssr", "trojan", "trojan-go", "http", "https", "http-proxy", - "https-proxy", "socks5", "http2", "juicity", "tuic", "hysteria", "hysteria2", "anytls", - "shadowsocks", "shadowsocksr", "hy1", "hy2", "mcore", "mcp", "plugin", "wireguard"} - for i := range supportedPrefix { - supportedPrefix[i] += "://" - } - urlLower := strings.ToLower(url) - if PluginManagerValidateLink(url) || common.HasAnyPrefix(urlLower, supportedPrefix) { - log.Trace("Import: url matches supported prefixes or is valid for PluginManager") + { var obj serverObj.ServerObj obj, err = ResolveURL(url) if err != nil { @@ -98,8 +118,16 @@ func Import(url string, which *configure.Which) (err error) { log.Info("Import: appending a new server") err = configure.AppendServers([]*configure.ServerRaw{{ServerObj: obj}}) } - } else { - // subscription + } + return +} + +// ImportSubscription fetches url (or the base64 payload of a sub:// link), +// parses the server list it returns and stores it as a new subscription. +func ImportSubscription(url string) (err error) { + resolv.CheckResolvConf() + url = strings.TrimSpace(url) + { source := url if u, err := url2.Parse(source); err == nil { switch strings.ToLower(u.Scheme) {