forked from aler9/dctk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_adc.go
230 lines (202 loc) · 5.56 KB
/
search_adc.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package dctoolkit
import (
"fmt"
"net"
"strings"
)
const (
adcSearchFile = "1"
adcSearchDirectory = "2"
)
func (c *Client) handleAdcSearchResult(isActive bool, peer *Peer, msg *msgAdcKeySearchResult) {
sr := &SearchResult{
IsActive: isActive,
Peer: peer,
}
for key, val := range msg.Fields {
switch key {
case adcFieldFilePath:
sr.Path = val
case adcFieldSize:
sr.Size = atoui64(val)
case adcFieldFileTTH:
if val == dirTTH {
sr.IsDir = true
} else {
tth, err := TigerHashFromBase32(val)
if err != nil {
return
}
sr.TTH = tth
}
case adcFieldUploadSlotCount:
sr.SlotAvail = atoui(val)
}
}
if sr.IsDir == true {
sr.Path = strings.TrimSuffix(sr.Path, "/")
}
c.handleSearchResult(sr)
}
func (c *Client) handleAdcSearchOutgoingRequest(conf SearchConf) error {
fields := make(map[string]string)
// always add token even if we're not using it
fields[adcFieldToken] = adcRandomToken()
switch conf.Type {
case SearchAny:
fields[adcFieldQueryAnd] = conf.Query
case SearchDirectory:
fields[adcFieldIsFileOrDir] = adcSearchDirectory
fields[adcFieldQueryAnd] = conf.Query
case SearchTTH:
fields[adcFieldFileTTH] = conf.TTH.String()
}
// 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 {
fields[adcFieldMaxSize] = numtoa(conf.MaxSize)
}
if conf.MinSize != 0 {
fields[adcFieldMinSize] = numtoa(conf.MinSize)
}
}
requiredFeatures := make(map[string]struct{})
// if we're passive, require that the recipient is active
if c.conf.IsPassive == true {
requiredFeatures["TCP4"] = struct{}{}
}
if len(requiredFeatures) > 0 {
c.connHub.conn.Write(&msgAdcFSearchRequest{
msgAdcTypeF{SessionId: c.sessionId, RequiredFeatures: requiredFeatures},
msgAdcKeySearchRequest{fields},
})
} else {
c.connHub.conn.Write(&msgAdcBSearchRequest{
msgAdcTypeB{c.sessionId},
msgAdcKeySearchRequest{fields},
})
}
return nil
}
func (c *Client) handleAdcSearchIncomingRequest(authorSessionId string, req *msgAdcKeySearchRequest) {
var peer *Peer
results, err := func() ([]interface{}, error) {
peer = c.peerBySessionId(authorSessionId)
if peer == nil {
return nil, fmt.Errorf("search author not found")
}
if _, ok := req.Fields[adcFieldFileGroup]; ok {
return nil, fmt.Errorf("search by type is not supported")
}
if _, ok := req.Fields[adcFieldFileExcludeExtens]; ok {
return nil, fmt.Errorf("search by type is not supported")
}
if _, ok := req.Fields[adcFieldFileQueryOr]; ok {
return nil, fmt.Errorf("search by query OR is not supported")
}
if _, ok := req.Fields[adcFieldFileExactSize]; ok {
return nil, fmt.Errorf("search by exact size is not supported")
}
if _, ok := req.Fields[adcFieldFileExtension]; ok {
return nil, fmt.Errorf("search by extension is not supported")
}
if _, ok := req.Fields[adcFieldIsFileOrDir]; ok {
if req.Fields[adcFieldIsFileOrDir] != adcSearchDirectory {
return nil, fmt.Errorf("search file only is not supported")
}
}
if _, ok := req.Fields[adcFieldQueryAnd]; !ok {
if _, ok := req.Fields[adcFieldFileTTH]; !ok {
return nil, fmt.Errorf("AN or TR is required")
}
}
sr := &searchIncomingRequest{
isActive: (peer.IsPassive == false),
stype: func() SearchType {
if _, ok := req.Fields[adcFieldFileTTH]; ok {
return SearchTTH
}
if _, ok := req.Fields[adcFieldIsFileOrDir]; ok {
if req.Fields[adcFieldIsFileOrDir] == adcSearchDirectory {
return SearchDirectory
}
}
return SearchAny
}(),
minSize: func() uint64 {
if val, ok := req.Fields[adcFieldMinSize]; ok {
return atoui64(val)
}
return 0
}(),
maxSize: func() uint64 {
if val, ok := req.Fields[adcFieldMaxSize]; ok {
return atoui64(val)
}
return 0
}(),
}
if _, ok := req.Fields[adcFieldFileTTH]; ok {
var err error
sr.tth, err = TigerHashFromBase32(req.Fields[adcFieldFileTTH])
if err != nil {
return nil, fmt.Errorf("invalid TTH: %v", req.Fields[adcFieldFileTTH])
}
} else {
sr.query = req.Fields[adcFieldQueryAnd]
}
return c.handleSearchIncomingRequest(sr)
}()
if err != nil {
dolog(LevelDebug, "[search] error: %s", err)
return
}
var msgs []*msgAdcKeySearchResult
for _, res := range results {
fields := map[string]string{
adcFieldUploadSlotCount: numtoa(c.conf.UploadMaxParallel),
}
switch o := res.(type) {
case *shareFile:
fields[adcFieldFilePath] = o.aliasPath
fields[adcFieldFileTTH] = o.tth.String()
fields[adcFieldSize] = numtoa(o.size)
case *shareDirectory:
// if directory, add a trailing slash
fields[adcFieldFilePath] = o.aliasPath + "/"
fields[adcFieldFileTTH] = dirTTH
fields[adcFieldSize] = numtoa(o.size)
}
// add token if sent by author
if val, ok := req.Fields[adcFieldToken]; ok {
fields[adcFieldToken] = val
}
msgs = append(msgs, &msgAdcKeySearchResult{Fields: fields})
}
// send to peer
if peer.IsPassive == false {
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 {
encmsg := &msgAdcUSearchResult{
msgAdcTypeU{peer.adcClientId},
*msg,
}
conn.Write([]byte(encmsg.AdcTypeEncode(encmsg.AdcKeyEncode())))
}
}()
// send to hub
} else {
for _, msg := range msgs {
c.connHub.conn.Write(&msgAdcDSearchResult{
msgAdcTypeD{c.sessionId, peer.adcSessionId},
*msg,
})
}
}
}