Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sub file as sublink fallback
Browse files Browse the repository at this point in the history
st0nie committed Jan 8, 2025
1 parent 3f4cceb commit 87be4b0
Showing 3 changed files with 49 additions and 124 deletions.
44 changes: 44 additions & 0 deletions common/subscription/subscription.go
Original file line number Diff line number Diff line change
@@ -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)
@@ -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
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
@@ -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.

0 comments on commit 87be4b0

Please sign in to comment.