-
Notifications
You must be signed in to change notification settings - Fork 5
/
sender.go
200 lines (181 loc) · 4.83 KB
/
sender.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package httpsender
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/ipni/go-libipni/announce/message"
"github.com/libp2p/go-libp2p/core/peer"
)
const DefaultAnnouncePath = "/announce"
// Sender sends announce messages over HTTP.
type Sender struct {
announceURLs []string
client *http.Client
extraData []byte
peerID peer.ID
userAgent string
}
// New creates a new Sender that sends advertisement announcement messages over
// HTTP. Announcements are sent directly to the specified URLs. The specified
// peerID is added to the multiaddrs contained in the announcements, which is
// how the publisher ID is communicated over HTTP.
func New(announceURLs []*url.URL, peerID peer.ID, options ...Option) (*Sender, error) {
if len(announceURLs) == 0 {
return nil, errors.New("no announce urls")
}
err := peerID.Validate()
if err != nil {
return nil, err
}
opts, err := getOpts(options)
if err != nil {
return nil, err
}
client := opts.client
if client == nil {
client = &http.Client{
Timeout: opts.timeout,
}
}
urls := make([]string, 0, len(announceURLs))
seen := make(map[string]struct{}, len(announceURLs))
for _, u := range announceURLs {
if u.Path == "" {
u.Path = DefaultAnnouncePath
}
ustr := u.String()
if _, ok := seen[ustr]; ok {
// Skip duplicate.
continue
}
seen[ustr] = struct{}{}
urls = append(urls, ustr)
}
return &Sender{
announceURLs: urls,
extraData: opts.extraData,
client: client,
peerID: peerID,
userAgent: opts.userAgent,
}, nil
}
// Close closes idle HTTP connections.
func (s *Sender) Close() error {
s.client.CloseIdleConnections()
return nil
}
// Send sends the Message to the announce URLs.
func (s *Sender) Send(ctx context.Context, msg message.Message) error {
err := s.addIDToAddrs(&msg)
if err != nil {
return fmt.Errorf("cannot add p2p id to message addrs: %w", err)
}
if len(s.extraData) != 0 {
msg.ExtraData = s.extraData
}
buf := bytes.NewBuffer(nil)
if err = msg.MarshalCBOR(buf); err != nil {
return fmt.Errorf("cannot cbor encode announce message: %w", err)
}
return s.sendData(ctx, buf, false)
}
func (s *Sender) SendJson(ctx context.Context, msg message.Message) error {
err := s.addIDToAddrs(&msg)
if err != nil {
return fmt.Errorf("cannot add p2p id to message addrs: %w", err)
}
if len(s.extraData) != 0 {
msg.ExtraData = s.extraData
}
buf := new(bytes.Buffer)
if err = json.NewEncoder(buf).Encode(msg); err != nil {
return fmt.Errorf("cannot json encode announce message: %w", err)
}
return s.sendData(ctx, buf, true)
}
// addIDToAddrs adds the peerID to each of the multiaddrs in the message. This
// is necessay to communicate the publisher ID when sending an announce over
// HTTP.
func (s *Sender) addIDToAddrs(msg *message.Message) error {
if len(msg.Addrs) == 0 {
return nil
}
addrs, err := msg.GetAddrs()
if err != nil {
return fmt.Errorf("cannot get addrs from message: %s", err)
}
ai := peer.AddrInfo{
ID: s.peerID,
Addrs: addrs,
}
p2pAddrs, err := peer.AddrInfoToP2pAddrs(&ai)
if err != nil {
return err
}
msg.SetAddrs(p2pAddrs)
return nil
}
func (s *Sender) sendData(ctx context.Context, buf *bytes.Buffer, js bool) error {
if len(s.announceURLs) < 2 {
u := s.announceURLs[0]
err := s.sendAnnounce(ctx, u, buf, js)
if err != nil {
return fmt.Errorf("failed to send http announce message to %s: %w", u, err)
}
return nil
}
errChan := make(chan error)
data := buf.Bytes()
for _, u := range s.announceURLs {
// Send HTTP announce to indexers concurrently. If context is canceled,
// then requests will be canceled.
go func(announceURL string) {
err := s.sendAnnounce(ctx, announceURL, bytes.NewBuffer(data), js)
if err != nil {
errChan <- fmt.Errorf("failed to send http announce to %s: %w", announceURL, err)
return
}
errChan <- nil
}(u)
}
var errs error
for i := 0; i < len(s.announceURLs); i++ {
err := <-errChan
if err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
func (s *Sender) sendAnnounce(ctx context.Context, announceURL string, buf *bytes.Buffer, js bool) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, announceURL, buf)
if err != nil {
return err
}
req.Header.Set("User-Agent", s.userAgent)
if js {
req.Header.Set("Content-Type", "application/json")
} else {
req.Header.Set("Content-Type", "application/octet-stream")
}
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%d %s: %s", resp.StatusCode, http.StatusText(resp.StatusCode), strings.TrimSpace(string(body)))
}
return nil
}