mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
code optimize
This commit is contained in:
parent
ced15857f3
commit
ac3655693c
@ -30,7 +30,7 @@ type Subscription struct {
|
|||||||
logFactory log.Factory
|
logFactory log.Factory
|
||||||
|
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
downloadDetour string `json:"download_detour,omitempty"`
|
downloadDetour string
|
||||||
dialerOptions option.DialerOptions
|
dialerOptions option.DialerOptions
|
||||||
providers []*subscriptionProvider
|
providers []*subscriptionProvider
|
||||||
|
|
||||||
@ -39,10 +39,10 @@ type Subscription struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type subscriptionProvider struct {
|
type subscriptionProvider struct {
|
||||||
Tag string `json:"tag,omitempty"`
|
tag string
|
||||||
URL string `json:"url"`
|
url string
|
||||||
Exclude *regexp.Regexp `json:"exclude,omitempty"`
|
exclude *regexp.Regexp
|
||||||
Include *regexp.Regexp `json:"include,omitempty"`
|
include *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSubscription creates a new subscription service.
|
// NewSubscription creates a new subscription service.
|
||||||
@ -80,10 +80,10 @@ func NewSubscription(ctx context.Context, router adapter.Router, logger log.Cont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
providers = append(providers, &subscriptionProvider{
|
providers = append(providers, &subscriptionProvider{
|
||||||
Tag: p.Tag,
|
tag: p.Tag,
|
||||||
URL: p.URL,
|
url: p.URL,
|
||||||
Exclude: exclude,
|
exclude: exclude,
|
||||||
Include: include,
|
include: include,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
interval := time.Duration(options.SubscriptionOptions.Interval)
|
interval := time.Duration(options.SubscriptionOptions.Interval)
|
||||||
@ -151,8 +151,8 @@ func (s *Subscription) fetch() error {
|
|||||||
}
|
}
|
||||||
for i, provider := range s.providers {
|
for i, provider := range s.providers {
|
||||||
var tag string
|
var tag string
|
||||||
if provider.Tag != "" {
|
if provider.tag != "" {
|
||||||
tag = provider.Tag
|
tag = provider.tag
|
||||||
} else {
|
} else {
|
||||||
tag = F.ToString(i)
|
tag = F.ToString(i)
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ func (s *Subscription) fetch() error {
|
|||||||
s.logger.Info(len(links), " links found from provider [", tag, "]")
|
s.logger.Info(len(links), " links found from provider [", tag, "]")
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
opt := link.Options()
|
opt := link.Options()
|
||||||
if !applyFilter(opt.Tag, provider) {
|
if !selectedByTag(opt.Tag, provider) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.applyOptions(opt, provider)
|
s.applyOptions(opt, provider)
|
||||||
@ -184,18 +184,18 @@ func (s *Subscription) fetch() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyFilter(tag string, provider *subscriptionProvider) bool {
|
func selectedByTag(tag string, provider *subscriptionProvider) bool {
|
||||||
if provider.Exclude != nil && provider.Exclude.MatchString(tag) {
|
if provider.exclude != nil && provider.exclude.MatchString(tag) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if provider.Include == nil {
|
if provider.include == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return provider.Include.MatchString(tag)
|
return provider.include.MatchString(tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Subscription) applyOptions(options *option.Outbound, provider *subscriptionProvider) error {
|
func (s *Subscription) applyOptions(options *option.Outbound, provider *subscriptionProvider) error {
|
||||||
options.Tag = s.tag + "." + provider.Tag + "." + options.Tag
|
options.Tag = s.tag + "." + provider.tag + "." + options.Tag
|
||||||
switch options.Type {
|
switch options.Type {
|
||||||
case C.TypeSocks:
|
case C.TypeSocks:
|
||||||
options.SocksOptions.DialerOptions = s.dialerOptions
|
options.SocksOptions.DialerOptions = s.dialerOptions
|
||||||
@ -224,7 +224,7 @@ func (s *Subscription) applyOptions(options *option.Outbound, provider *subscrip
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Subscription) fetchProvider(client *http.Client, provider *subscriptionProvider) ([]link.Link, error) {
|
func (s *Subscription) fetchProvider(client *http.Client, provider *subscriptionProvider) ([]link.Link, error) {
|
||||||
req, err := http.NewRequestWithContext(s.ctx, http.MethodGet, provider.URL, nil)
|
req, err := http.NewRequestWithContext(s.ctx, http.MethodGet, provider.url, nil)
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user