sing-box icon indicating copy to clipboard operation
sing-box copied to clipboard

Idea: Improve for private remote rulesets

Open huihuimoe opened this issue 2 years ago • 0 comments

For Private remote rule sets (e.g., those hosted in private repositories on GitHub/Gitlab), a specific token is required to download them.

When retrieving files from a private repository on GitHub, a request header like Authorization: token <private_access_token> is needed.

In Gitlab, a header like PRIVATE-TOKEN: <private_access_token> is required.

I suggest adding a headers configuration in the remote rule download settings. This allows for the provision of a token during the download process. It could look something like this:

  {
    "type": "remote",
    "format": "source",
    "download_detour": "direct",
    "tag": "some-tag",
    "url": "https://example.com/some/ruleset.json",
+   "headers": {
+     "PRIVATE-TOKEN": "<private_access_token>"
+   }
  }

I've made some simple code modifications to implement this feature, which I've attached here. Please use it if you approve. Thanks!

diff --git a/option/rule_set.go b/option/rule_set.go
index 8e367e6..09da156 100644
--- a/option/rule_set.go
+++ b/option/rule_set.go
@@ -74,9 +74,10 @@ type LocalRuleSet struct {
 }
 
 type RemoteRuleSet struct {
-	URL            string   `json:"url"`
-	DownloadDetour string   `json:"download_detour,omitempty"`
-	UpdateInterval Duration `json:"update_interval,omitempty"`
+	URL            string     `json:"url"`
+	DownloadDetour string     `json:"download_detour,omitempty"`
+	Headers        HTTPHeader `json:"headers,omitempty"`
+	UpdateInterval Duration   `json:"update_interval,omitempty"`
 }
 
 type _HeadlessRule struct {
diff --git a/route/rule_set_remote.go b/route/rule_set_remote.go
index 595e328..283a8c8 100644
--- a/route/rule_set_remote.go
+++ b/route/rule_set_remote.go
@@ -196,6 +196,9 @@ func (s *RemoteRuleSet) fetchOnce(ctx context.Context, startContext adapter.Rule
 	if err != nil {
 		return err
 	}
+	for key, value := range s.options.RemoteOptions.Headers {
+		request.Header[key] = value
+	}
 	if s.lastEtag != "" {
 		request.Header.Set("If-None-Match", s.lastEtag)
 	}

huihuimoe avatar Jan 15 '24 19:01 huihuimoe