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
/
Copy pathsearchnmdc.go
193 lines (178 loc) · 4.05 KB
/
searchnmdc.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
package dctk
import (
"bytes"
"fmt"
"net"
"strings"
"github.com/aler9/go-dc/nmdc"
godctiger "github.com/aler9/go-dc/tiger"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/tiger"
)
func (c *Client) handleNmdcSearchResult(isActive bool, msg *nmdc.SR) {
peer := c.peerByNick(msg.From)
if peer == nil {
return
}
sr := &SearchResult{
IsActive: isActive,
Peer: peer,
Path: strings.Join(msg.Path, "/"),
SlotAvail: uint(msg.FreeSlots),
Size: msg.Size,
TTH: (*tiger.Hash)(msg.TTH),
IsDir: msg.IsDir,
}
c.handleSearchResult(sr)
}
func (c *Client) handleNmdcSearchOutgoingRequest(conf SearchConf) error {
if conf.MaxSize != 0 && conf.MinSize != 0 {
return fmt.Errorf("max size and min size cannot be used together in NMDC")
}
c.hubConn.conn.Write(&nmdc.Search{
DataType: func() nmdc.DataType {
switch conf.Type {
case SearchAny:
return nmdc.DataTypeAny
case SearchDirectory:
return nmdc.DataTypeFolders
}
return nmdc.DataTypeTTH
}(),
SizeRestricted: (conf.MaxSize != 0) || (conf.MinSize != 0),
IsMaxSize: (conf.MaxSize != 0),
Size: func() uint64 {
if conf.MaxSize != 0 {
return conf.MaxSize
}
return conf.MinSize
}(),
Pattern: func() string {
if conf.Type != SearchTTH {
return conf.Query
}
return ""
}(),
TTH: func() *godctiger.Hash {
if conf.Type == SearchTTH {
ptr := godctiger.Hash(conf.TTH)
return &ptr
}
return nil
}(),
Address: func() string {
if !c.conf.IsPassive {
return fmt.Sprintf("%s:%d", c.ip, c.conf.UDPPort)
}
return ""
}(),
User: func() string {
if c.conf.IsPassive {
return c.conf.Nick
}
return ""
}(),
})
return nil
}
func (c *Client) handleNmdcSearchIncomingRequest(req *nmdc.Search) {
results, err := func() ([]interface{}, error) {
// we do not support search by type
if _, ok := map[nmdc.DataType]struct{}{
nmdc.DataTypeAny: {},
nmdc.DataTypeFolders: {},
nmdc.DataTypeTTH: {},
}[req.DataType]; !ok {
return nil, fmt.Errorf("unsupported search type: %v", req.DataType)
}
sr := &searchIncomingRequest{
isActive: req.Address != "",
stype: func() SearchType {
switch req.DataType {
case nmdc.DataTypeAny:
return SearchAny
case nmdc.DataTypeFolders:
return SearchDirectory
}
return SearchTTH
}(),
minSize: func() uint64 {
if req.SizeRestricted && !req.IsMaxSize {
return req.Size
}
return 0
}(),
maxSize: func() uint64 {
if req.SizeRestricted && req.IsMaxSize {
return req.Size
}
return 0
}(),
}
if req.DataType == nmdc.DataTypeTTH {
sr.tth = tiger.Hash(*req.TTH)
} else {
sr.query = req.Pattern
}
return c.handleSearchIncomingRequest(sr)
}()
if err != nil {
log.Log(c.conf.LogLevel, log.LevelDebug, "[search] error: %s", err)
return
}
msgs := make([]*nmdc.SR, len(results))
for i, res := range results {
msgs[i] = &nmdc.SR{
Path: strings.Split(func() string {
if f, ok := res.(*shareFile); ok {
return f.aliasPath
}
return res.(*shareDirectory).aliasPath
}(), "\\"),
IsDir: func() bool {
_, ok := res.(*shareDirectory)
return ok
}(),
Size: func() uint64 {
if f, ok := res.(*shareFile); ok {
return f.size
}
return 0
}(),
TTH: func() *godctiger.Hash {
if f, ok := res.(*shareFile); ok {
ptr := godctiger.Hash(f.tth)
return &ptr
}
return nil
}(),
From: c.conf.Nick,
FreeSlots: int(c.uploadSlotAvail),
TotalSlots: int(c.conf.UploadMaxParallel),
HubAddress: fmt.Sprintf("%s:%d", c.hubSolvedIP, c.hubPort),
}
}
// if request was active, send to peer
if req.Address != "" {
go func() {
conn, err := net.Dial("udp", req.Address)
if err != nil {
return
}
defer conn.Close()
for _, msg := range msgs {
var buf bytes.Buffer
buf.WriteString("$SR ")
msg.MarshalNMDC(nil, &buf)
buf.WriteByte('|')
conn.Write(buf.Bytes())
}
}()
// send to hub
} else {
for _, msg := range msgs {
msg.To = req.User
c.hubConn.conn.Write(msg)
}
}
}