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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package fetcher
import (
"context"
"encoding/base64"
"io"
"log"
"net/http"
"strings"
"time"
"gitrepo.ru/neonxp/idecnode/pkg/config"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
type Fetcher struct {
idec *idec.IDEC
config *config.Config
client *http.Client
}
func New(i *idec.IDEC, cfg *config.Config) *Fetcher {
return &Fetcher{
idec: i,
config: cfg,
client: &http.Client{
Timeout: 60 * time.Second,
},
}
}
func (f *Fetcher) Run(ctx context.Context) error {
for _, node := range f.config.Fetch {
messagesToDownloads := []string{}
log.Println("fetching", node)
for _, echoID := range node.Echos {
missed, err := f.getMissedEchoMessages(node, echoID)
if err != nil {
return err
}
messagesToDownloads = append(messagesToDownloads, missed...)
}
if err := f.downloadMessages(node, messagesToDownloads); err != nil {
return err
}
if err := f.downloadBlacklist(node); err != nil {
return err
}
}
log.Println("finished")
return nil
}
func (f *Fetcher) downloadMessages(node config.Node, messagesToDownloads []string) error {
var slice []string
for {
limit := min(20, len(messagesToDownloads))
if limit == 0 {
return nil
}
slice, messagesToDownloads = messagesToDownloads[:limit-1], messagesToDownloads[limit:]
if err := f.downloadMessagesChunk(node, slice); err != nil {
return err
}
}
}
func (f *Fetcher) getMissedEchoMessages(node config.Node, echoID string) ([]string, error) {
missed := []string{}
messages, _, err := f.idec.GetMessageIDsByEcho(echoID, 0, 0)
if err != nil {
return nil, err
}
messagesIndex := map[string]struct{}{}
for _, msgID := range messages {
messagesIndex[msgID] = struct{}{}
}
p := formatCommand(node, "u/e", echoID)
resp, err := f.client.Get(p)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.Contains(line, ".") {
// echo name
continue
}
if line == "" {
continue
}
if _, exist := messagesIndex[line]; !exist {
missed = append(missed, line)
}
}
return missed, nil
}
func (f *Fetcher) downloadMessagesChunk(node config.Node, messages []string) error {
p := formatCommand(node, "u/m", messages...)
resp, err := f.client.Get(p)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if line == "" {
continue
}
p := strings.Split(line, ":")
rawMessage, err := base64.StdEncoding.DecodeString(p[1])
if err != nil {
return err
}
if err := f.idec.SaveBundleMessage(p[0], string(rawMessage)); err != nil {
return err
}
}
return nil
}
func (f *Fetcher) downloadBlacklist(node config.Node) error {
p := formatCommand(node, "blacklist.txt")
resp, err := f.client.Get(p)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
return f.idec.MergeBlacklist(lines)
}
func formatCommand(node config.Node, method string, args ...string) string {
segments := []string{node.Addr, method}
segments = append(segments, args...)
p := strings.Join(segments, "/")
return p
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
|