This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
searchadc.go
207 lines (175 loc) · 4.51 KB
/
searchadc.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
201
202
203
204
205
206
207
package dctk
import (
"bytes"
"fmt"
"net"
"strings"
"github.com/aler9/go-dc/adc"
godctiger "github.com/aler9/go-dc/tiger"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/protoadc"
"github.com/aler9/dctk/pkg/tiger"
)
func (c *Client) handleAdcSearchResult(isActive bool, peer *Peer, msg *adc.SearchResult) {
sr := &SearchResult{
IsActive: isActive,
Peer: peer,
}
sr.Path = msg.Path
sr.Size = uint64(msg.Size)
sr.SlotAvail = uint(msg.Slots)
if msg.TTH != nil {
if tiger.Hash(*msg.TTH) == dirTTH {
sr.IsDir = true
} else {
sr.TTH = (*tiger.Hash)(msg.TTH)
}
}
if sr.IsDir {
sr.Path = strings.TrimSuffix(sr.Path, "/")
}
c.handleSearchResult(sr)
}
func (c *Client) handleAdcSearchOutgoingRequest(conf SearchConf) error {
req := &adc.SearchRequest{
// always add token even if we're not using it
Token: protoadc.AdcRandomToken(),
}
switch conf.Type {
case SearchAny:
req.And = append(req.And, conf.Query)
case SearchDirectory:
req.Type = adc.FileTypeDir
req.And = append(req.And, conf.Query)
case SearchTTH:
req.TTH = (*godctiger.Hash)(&conf.TTH)
}
// MaxSize and MinSize are used only for files. They can be used for
// directories too in ADC, but we want to minimize differences with NMDC.
if conf.Type == SearchAny || conf.Type == SearchTTH {
if conf.MaxSize != 0 {
req.Le = int64(conf.MaxSize)
}
if conf.MinSize != 0 {
req.Ge = int64(conf.MinSize)
}
}
var features []adc.FeatureSel
// if we're passive, require that the receiver is active
if c.conf.IsPassive {
features = append(features, adc.FeatureSel{adc.FeaTCP4, true}) //nolint:govet
}
if len(features) > 0 {
c.hubConn.conn.Write(&protoadc.AdcFSearchRequest{ //nolint:govet
&adc.FeaturePacket{ID: c.adcSessionID, Sel: features},
req,
})
} else {
c.hubConn.conn.Write(&protoadc.AdcBSearchRequest{ //nolint:govet
&adc.BroadcastPacket{ID: c.adcSessionID},
req,
})
}
return nil
}
func (c *Client) handleAdcSearchIncomingRequest(id adc.SID, req *adc.SearchRequest) {
var peer *Peer
results, err := func() ([]interface{}, error) {
peer = c.peerBySessionID(id)
if peer == nil {
return nil, fmt.Errorf("search author not found")
}
if req.Group != adc.ExtNone {
return nil, fmt.Errorf("search by type is not supported")
}
if len(req.Ext) > 0 {
return nil, fmt.Errorf("search by extension is not supported")
}
if len(req.Not) > 0 {
return nil, fmt.Errorf("search by OR is not supported")
}
if req.Eq != 0 {
return nil, fmt.Errorf("search by exact size is not supported")
}
if req.Type == adc.FileTypeFile {
return nil, fmt.Errorf("file-only search is not supported")
}
if len(req.And) == 0 && req.TTH == nil {
return nil, fmt.Errorf("AN or TR are required")
}
sr := &searchIncomingRequest{
isActive: !peer.IsPassive,
stype: func() SearchType {
if req.TTH != nil {
return SearchTTH
}
if req.Type == adc.FileTypeDir {
return SearchDirectory
}
return SearchAny
}(),
minSize: uint64(req.Ge),
maxSize: uint64(req.Le),
}
if req.TTH != nil {
sr.tth = tiger.Hash(*req.TTH)
} else {
sr.query = req.And[0]
}
return c.handleSearchIncomingRequest(sr)
}()
if err != nil {
log.Log(c.conf.LogLevel, log.LevelDebug, "[search] error: %s", err)
return
}
msgs := make([]*adc.SearchResult, len(results))
for i, res := range results {
msg := &adc.SearchResult{
Slots: int(c.conf.UploadMaxParallel),
}
switch o := res.(type) {
case *shareFile:
msg.Path = o.aliasPath
msg.TTH = (*godctiger.Hash)(&o.tth)
msg.Size = int64(o.size)
case *shareDirectory:
// if directory, add a trailing slash
msg.Path = o.aliasPath + "/"
msg.TTH = (*godctiger.Hash)(&dirTTH)
msg.Size = int64(o.size)
}
// add token if sent by author
msg.Token = req.Token
msgs[i] = msg
}
// send to peer
if !peer.IsPassive {
go func() {
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", peer.IP, peer.adcUDPPort))
if err != nil {
return
}
defer conn.Close()
for _, msg := range msgs {
amsg := &protoadc.AdcUSearchResult{ //nolint:govet
&adc.UDPPacket{ID: peer.adcClientID},
msg,
}
amsg.Pkt.SetMessage(amsg.Msg)
var buf bytes.Buffer
if err := amsg.Pkt.MarshalPacketADC(&buf); err != nil {
panic(err)
}
conn.Write(buf.Bytes())
}
}()
// send to hub
} else {
for _, msg := range msgs {
c.hubConn.conn.Write(&protoadc.AdcDSearchResult{ //nolint:govet
&adc.DirectPacket{ID: c.adcSessionID, To: peer.adcSessionID},
msg,
})
}
}
}