Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: subfile as sublink fallback #726

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ func newControlPlane(log *logrus.Logger, bpf interface{}, dnsCache map[string]*c
tagToNodeList[tag] = append(tagToNodeList[tag], nodes...)
}
}

// Delete all files in persist.d that are not in tagToNodeList
files, err := os.ReadDir(filepath.Join(filepath.Dir(cfgFile), "persist.d"))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
for _, file := range files {
tag := strings.TrimSuffix(file.Name(), ".sub")
if _, ok := tagToNodeList[tag]; !ok {
err := os.Remove(filepath.Join(filepath.Dir(cfgFile), "persist.d", file.Name()))
if err != nil {
return nil, err
}
}
}

if len(tagToNodeList) == 0 {
if resolvingfailed {
log.Warnln("No node found because all subscription resolving failed.")
Expand Down
44 changes: 44 additions & 0 deletions common/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,23 @@ func ResolveSubscription(log *logrus.Logger, client *http.Client, configDir stri
req *http.Request
resp *http.Response
)

persistToFile := false

switch u.Scheme {
case "file":
b, err = ResolveFile(u, configDir)
if err != nil {
return "", nil, err
}
goto resolve
case "http-file", "https-file":
if len(tag) == 0 {
return "", nil, fmt.Errorf("tag is required for http-file/https-file subscription")
}
persistToFile = true
subscription = strings.Replace(subscription, "-file", "", 1)
break
default:
}
req, err = http.NewRequest("GET", subscription, nil)
Expand All @@ -170,13 +180,47 @@ func ResolveSubscription(log *logrus.Logger, client *http.Client, configDir stri
req.Header.Set("User-Agent", fmt.Sprintf("dae/%v (like v2rayA/1.0 WebRequestHelper) (like v2rayN/1.0 WebRequestHelper)", config.Version))
resp, err = client.Do(req)
if err != nil {
if persistToFile {
log.Warnln("failed to fetch subscription, try to read from file")
u.Host = "persist.d/" + tag + ".sub"
u.Path = ""
b, err = ResolveFile(u, configDir)

if err != nil {
return "", nil, err
}
goto resolve
}

return "", nil, err
}
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}

if persistToFile {
path := filepath.Join(configDir, "persist.d")
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, 0700)
if err != nil {
return "", nil, err
}
}

path = filepath.Join(path, tag+".sub")
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return "", nil, err
}
defer file.Close()

_, err = file.Write(b)
if err != nil {
return "", nil, err
}
}
resolve:
if nodes, err = ResolveSubscriptionAsSIP008(log, b); err == nil {
return tag, nodes, nil
Expand Down
124 changes: 0 additions & 124 deletions docs/en/user-guide/persistent-subscription.md

This file was deleted.

5 changes: 5 additions & 0 deletions example.dae
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ subscription {
another_sub: 'https://example.com/another_sub'
'https://example.com/no_tag_link'
'file://relative/path/to/mysub.sub' # Put subscription content in /etc/dae/relative/path/to/mysub.sub

# Subscriptions with '-file' will be saved to 'config_dir/persist.d/your_sub_tag.sub'.
# This file will serve as a fallback when fetching the subscription via a link fails.
# It will be updated automatically once the fetch is successful.
persist_sub: 'https-file://www.example.com/persist_sub/link'
}

# Nodes defined here will be merged as a part of the global node pool.
Expand Down
Loading