forked from SagerNet/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.go
117 lines (107 loc) · 2.68 KB
/
subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package serenity
import (
"time"
"github.com/sagernet/serenity/libsubscription"
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
)
type SubscriptionClient struct {
logger logger.Logger
ticker *time.Ticker
subscriptions []*SubscriptionOptions
close chan struct{}
}
func NewSubscriptionClient(logger logger.Logger, subscriptions []*SubscriptionOptions) *SubscriptionClient {
var interval time.Duration
for _, subscription := range subscriptions {
if interval == 0 || time.Duration(subscription.UpdateInterval) < interval {
interval = time.Duration(subscription.UpdateInterval)
}
}
if interval == 0 {
interval = 5 * time.Minute
}
return &SubscriptionClient{
logger: logger,
ticker: time.NewTicker(interval),
subscriptions: subscriptions,
close: make(chan struct{}),
}
}
func (c *SubscriptionClient) Subscriptions() []*SubscriptionOptions {
return common.Filter(c.subscriptions, func(it *SubscriptionOptions) bool {
return it.LastUpdate != time.Time{}
})
}
func (c *SubscriptionClient) Start() error {
err := c.Update()
if err != nil {
return err
}
go c.loopUpdate()
return nil
}
func (c *SubscriptionClient) Close() error {
c.ticker.Stop()
select {
case <-c.close:
default:
close(c.close)
}
return nil
}
func (c *SubscriptionClient) loopUpdate() {
for {
select {
case <-c.ticker.C:
case <-c.close:
return
}
err := c.Update()
if err != nil {
c.logger.Error(err)
}
}
}
func (c *SubscriptionClient) Update() error {
httpClient := libbox.NewHTTPClient()
httpClient.ModernTLS()
defer httpClient.Close()
for i, subscription := range c.subscriptions {
err := c.update(httpClient, subscription)
if err != nil {
return E.Cause(err, "update subscription[", i, "]: ", subscription.URL)
}
c.logger.Info("updated subscription[", i, "]: ", len(subscription.ServerCache), " servers")
}
return nil
}
func (c *SubscriptionClient) update(httpClient libbox.HTTPClient, subscription *SubscriptionOptions) error {
request := httpClient.NewRequest()
err := request.SetURL(subscription.URL)
if err != nil {
return err
}
if subscription.UserAgent != "" {
request.SetUserAgent(subscription.UserAgent)
} else {
request.SetUserAgent("ClashForAndroid/serenity")
}
response, err := request.Execute()
if err != nil {
return err
}
content, err := response.GetContentString()
if err != nil {
return err
}
servers, err := libsubscription.ParseSubscription(content)
if err != nil {
return err
}
subscription.LastUpdate = time.Now()
subscription.ServerCache = servers
return nil
}