/
worker.go
150 lines (132 loc) · 3.85 KB
/
worker.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
package http
import (
"context"
"fmt"
"github.com/data-preservation-programs/RetrievalBot/pkg/convert"
"github.com/data-preservation-programs/RetrievalBot/pkg/model"
"github.com/data-preservation-programs/RetrievalBot/pkg/net"
"github.com/data-preservation-programs/RetrievalBot/pkg/resolver"
"github.com/data-preservation-programs/RetrievalBot/pkg/task"
"github.com/ipfs/go-cid"
_ "github.com/joho/godotenv/autoload"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/pkg/errors"
net2 "net"
"net/url"
"strconv"
)
type Worker struct{}
func ToURL(ma multiaddr.Multiaddr) (*url.URL, error) {
// host should be either the dns name or the IP
_, host, err := manet.DialArgs(ma)
if err != nil {
return nil, errors.Wrap(err, "failed to get dial args")
}
if ip := net2.ParseIP(host); ip != nil {
if !ip.To4().Equal(ip) {
// raw v6 IPs need `[ip]` encapsulation.
host = fmt.Sprintf("[%s]", host)
}
}
protos := ma.Protocols()
pm := make(map[int]string, len(protos))
for _, p := range protos {
v, err := ma.ValueForProtocol(p.Code)
if err == nil {
pm[p.Code] = v
}
}
scheme := model.HTTP
//nolint:nestif
if _, ok := pm[multiaddr.P_HTTPS]; ok {
scheme = model.HTTPS
} else if _, ok = pm[multiaddr.P_HTTP]; ok {
// /tls/http == /https
if _, ok = pm[multiaddr.P_TLS]; ok {
scheme = model.HTTPS
}
} else if _, ok = pm[multiaddr.P_WSS]; ok {
scheme = model.WSS
} else if _, ok = pm[multiaddr.P_WS]; ok {
scheme = model.WS
// /tls/ws == /wss
if _, ok = pm[multiaddr.P_TLS]; ok {
scheme = model.WSS
}
}
path := ""
if pb, ok := pm[0x300200]; ok {
path, err = url.PathUnescape(pb)
if err != nil {
path = ""
}
}
//nolint:exhaustruct
out := url.URL{
Scheme: string(scheme),
Host: host,
Path: path,
}
return &out, nil
}
func (e Worker) DoWork(tsk task.Task) (*task.RetrievalResult, error) {
ctx := context.Background()
client := net.NewHTTPClient(tsk.Timeout)
host, err := net.InitHost(ctx, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to init host")
}
// First, check if the provider is using boost
protocolProvider := resolver.ProtocolResolver(host, tsk.Timeout)
addrInfo, err := tsk.Provider.GetPeerAddr()
if err != nil {
return nil, errors.Wrap(err, "failed to get peer addr")
}
contentCID := cid.MustParse(tsk.Content.CID)
isBoost, err := protocolProvider.IsBoostProvider(context.Background(), addrInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to check if provider is boost")
}
if !isBoost {
return task.NewErrorRetrievalResult(
task.ProtocolNotSupported,
errors.New("Provider is not using boost")), nil
}
// If so, find the HTTP endpoint
protocols, err := protocolProvider.GetRetrievalProtocols(ctx, addrInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to get retrieval protocols")
}
var urlString string
for _, protocol := range protocols {
if (protocol.Name == string(model.HTTP) || protocol.Name == string(model.HTTPS)) && len(protocol.Addresses) > 0 {
addr, err := convert.AbiToMultiaddr(protocol.Addresses[0])
if err != nil {
return task.NewErrorRetrievalResult(task.ProtocolNotSupported, err), nil
}
url, err := ToURL(addr)
if err != nil {
return task.NewErrorRetrievalResult(
task.ProtocolNotSupported,
errors.Wrap(err, "Cannot convert multiaddr to URL")), nil
}
urlString = url.String()
}
}
if urlString == "" {
return task.NewErrorRetrievalResult(
task.ProtocolNotSupported,
errors.New("No HTTP endpoint found")), nil
}
size := 1024 * 1024
if sizeStr, ok := tsk.Metadata["retrieve_size"]; ok {
size, err = strconv.Atoi(sizeStr)
if err != nil {
return nil, errors.Wrap(err, "failed to convert retrieve_size to int")
}
}
// Finally, retrieve the file
//nolint:wrapcheck
return client.RetrievePiece(ctx, urlString, contentCID, int64(size))
}