Skip to content

Commit

Permalink
Make remote rule sets support multiple URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
moonfruit committed Apr 2, 2024
1 parent cc8ae72 commit 5dd0208
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions option/rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type LocalRuleSet struct {
}

type RemoteRuleSet struct {
URL string `json:"url"`
DownloadDetour string `json:"download_detour,omitempty"`
UpdateInterval Duration `json:"update_interval,omitempty"`
URL Listable[string] `json:"url"`
DownloadDetour string `json:"download_detour,omitempty"`
UpdateInterval Duration `json:"update_interval,omitempty"`
}

type _HeadlessRule struct {
Expand Down
3 changes: 3 additions & 0 deletions route/rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func NewRuleSet(ctx context.Context, router adapter.Router, logger logger.Contex
case C.RuleSetTypeLocal:
return NewLocalRuleSet(router, options)
case C.RuleSetTypeRemote:
if len(options.RemoteOptions.URL) == 0 {
return nil, E.New("missing urls")
}
return NewRemoteRuleSet(ctx, router, logger, options), nil
default:
return nil, E.New("unknown rule set type: ", options.Type)
Expand Down
15 changes: 13 additions & 2 deletions route/rule_set_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ func (s *RemoteRuleSet) loopUpdate() {
}

func (s *RemoteRuleSet) fetchOnce(ctx context.Context, startContext adapter.RuleSetStartContext) error {
s.logger.Debug("updating rule-set ", s.options.Tag, " from URL: ", s.options.RemoteOptions.URL)
var err error
for _, url := range s.options.RemoteOptions.URL {
err = s.fetchURL(ctx, url, startContext)
if err == nil {
return nil
}
}
return err
}

func (s *RemoteRuleSet) fetchURL(ctx context.Context, url string, startContext adapter.RuleSetStartContext) error {
s.logger.Debug("updating rule-set ", s.options.Tag, " from URL: ", url)
var httpClient *http.Client
if startContext != nil {
httpClient = startContext.HTTPClient(s.options.RemoteOptions.DownloadDetour, s.dialer)
Expand All @@ -199,7 +210,7 @@ func (s *RemoteRuleSet) fetchOnce(ctx context.Context, startContext adapter.Rule
},
}
}
request, err := http.NewRequest("GET", s.options.RemoteOptions.URL, nil)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 5dd0208

Please sign in to comment.