mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
feat(import): let the client say whether it imports a server or a subscription
Import guessed from the URL scheme. Commit3fd0e882added 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 before3fd0e882. 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.
This commit is contained in:
3 files changed
+57
-15
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user