From 8b5c57209f29760cab74b49b0a8ba27d7ca76a0b Mon Sep 17 00:00:00 2001 From: n3t1zen Date: Thu, 26 Feb 2026 12:01:01 +0800 Subject: [PATCH] feat: enhance subscription link handling with caching support --- cmd/sing-box/cmd_run.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index 7f46da00..1199a64b 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -51,15 +51,28 @@ func readConfigAt(path string) (*OptionsEntry, error) { err error ) subscriptionLink := os.Getenv("SING_SUBSCRIPTION_LINK") + subscriptionCache := os.Getenv("SING_SUBSCRIPTION_CACHE") if path == "stdin" { configContent, err = io.ReadAll(os.Stdin) } else if path == "env" && subscriptionLink != "" { configContentResponse, err := http.Get(subscriptionLink) - if err != nil { - return nil, E.Cause(err, "failed to GET subscription link") + if err != nil || configContentResponse.StatusCode != 200 { + if subscriptionCache != "" { + log.Info("using previous subscription cache") + configContent, err = os.ReadFile(subscriptionCache) + } else { + return nil, E.Cause(err, "failed to GET subscription link") + } + } else { + defer configContentResponse.Body.Close() + configContent, err = io.ReadAll(configContentResponse.Body) + if err == nil && subscriptionCache != "" { + _err := os.WriteFile(subscriptionCache, configContent, 0600) + if _err != nil { + log.Error(E.Cause(_err, "failed to cache subscription file")) + } + } } - defer configContentResponse.Body.Close() - configContent, err = io.ReadAll(configContentResponse.Body) } else { configContent, err = os.ReadFile(path) }