forked from fanpei91/torsniff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2pspider.go
270 lines (249 loc) · 5.59 KB
/
p2pspider.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/fanpei91/bencode"
"github.com/fanpei91/godht"
"github.com/fanpei91/metawire"
)
const (
Dir = "torrents"
)
func home() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
return os.Getenv(env)
}
type tfile struct {
name string
length int64
}
func (tf *tfile) String() string {
return fmt.Sprintf("name: %s\n, size: %d\n", tf.name, tf.length)
}
type torrent struct {
infohashHex string
name string
length int64
files []*tfile
}
func (t *torrent) String() string {
return fmt.Sprintf(
"link: %s\nname: %s\nsize: %d\nfile: %d\n",
fmt.Sprintf("magnet:?xt=urn:btih:%s", t.infohashHex),
t.name,
t.length,
len(t.files),
)
}
func newTorrent(meta []byte, infohashHex string) (*torrent, error) {
dict, err := bencode.Decode(bytes.NewBuffer(meta))
if err != nil {
return nil, err
}
t := &torrent{infohashHex: infohashHex}
if name, ok := dict["name.utf-8"].(string); ok {
t.name = name
} else if name, ok := dict["name"].(string); ok {
t.name = name
}
if length, ok := dict["length"].(int64); ok {
t.length = length
}
var total int64
if files, ok := dict["files"].([]interface{}); ok {
for _, file := range files {
var filename string
var filelength int64
if f, ok := file.(map[string]interface{}); ok {
if path, ok := f["path.utf-8"].([]string); ok {
filename = strings.Join(path, "/")
} else if path, ok := f["path"].([]string); ok {
filename = strings.Join(path, "/")
}
if length, ok := f["length"].(int64); ok {
filelength = length
total += filelength
}
t.files = append(t.files, &tfile{name: filename, length: filelength})
}
}
}
if t.length == 0 {
t.length = total
}
return t, nil
}
type blacklist struct {
container sync.Map
expiredAfter time.Duration
}
func newBalckList(expiredAfter time.Duration) *blacklist {
b := &blacklist{expiredAfter: expiredAfter}
go b.sweep()
return b
}
func (b *blacklist) in(addr *net.TCPAddr) bool {
key := addr.String()
v, ok := b.container.Load(key)
if !ok {
return false
}
c := v.(time.Time)
if c.Sub(time.Now()) > b.expiredAfter {
b.container.Delete(key)
return false
}
return true
}
func (b *blacklist) add(addr *net.TCPAddr) {
b.container.Store(addr.String(), time.Now())
}
func (b *blacklist) sweep() {
for range time.Tick(10 * time.Second) {
now := time.Now()
b.container.Range(func(k, v interface{}) bool {
c := v.(time.Time)
if c.Sub(now) > b.expiredAfter {
b.container.Delete(k)
}
return true
})
}
}
type p2pspider struct {
laddr string
maxFriends int
maxPeers int
secret string
timeout time.Duration
blacklist *blacklist
dir string
}
func (p *p2pspider) run() {
tokens := make(chan struct{}, p.maxPeers)
dht, err := godht.New(
p.laddr,
godht.MaxFriendsPerSec(p.maxFriends),
godht.Secret(p.secret),
)
if err != nil {
panic(err)
}
log.Println("running, wait a few minutes...")
for ac := range dht.Announce {
tokens <- struct{}{}
go p.work(ac, tokens)
}
}
func (p *p2pspider) work(ac *godht.Announce, tokens chan struct{}) {
defer func() {
<-tokens
}()
if p.isExist(ac.InfohashHex) {
return
}
if p.blacklist.in(ac.Peer) {
return
}
peer := metawire.New(
string(ac.Infohash),
ac.Peer.String(),
metawire.Timeout(p.timeout),
)
data, err := peer.Fetch()
if err != nil {
p.blacklist.add(ac.Peer)
return
}
_, err = p.save(ac.InfohashHex, data)
if err != nil {
return
}
t, err := newTorrent(data, ac.InfohashHex)
if err != nil {
return
}
log.Println(t)
}
func (p *p2pspider) isExist(infohashHex string) bool {
name, _ := p.pathname(infohashHex)
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func (p *p2pspider) save(infohashHex string, data []byte) (string, error) {
name, dir := p.pathname(infohashHex)
if err := os.MkdirAll(dir, 0755); err != nil {
return "", err
}
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return "", err
}
defer f.Close()
d, err := bencode.Decode(bytes.NewBuffer(data))
if err != nil {
return "", err
}
_, err = f.Write(bencode.Encode(map[string]interface{}{
"info": d,
}))
if err != nil {
return "", err
}
return name, nil
}
func (p *p2pspider) pathname(infohashHex string) (name string, dir string) {
dir = path.Join(p.dir, infohashHex[:2], infohashHex[len(infohashHex)-2:])
name = path.Join(dir, infohashHex+".torrent")
return
}
func main() {
addr := flag.String("a", "0.0.0.0", "listen on given address")
port := flag.Int("p", 6881, "listen on given port")
maxFriends := flag.Int("f", 500, "max friends to make with per second")
peers := flag.Int("e", 400, "max peers(TCP) to connenct to download torrent file")
timeout := flag.Duration("t", 10*time.Second, "max time allowed for downloading torrent file")
secret := flag.String("s", "$p2pspider$", "token secret")
dir := flag.String("d", path.Join(home(), Dir), "the directory to store the torrent file")
verbose := flag.Bool("v", true, "run in verbose mode")
flag.Parse()
absDir, err := filepath.Abs(*dir)
if err != nil {
panic(err)
}
log.SetFlags(0)
if *verbose {
log.SetOutput(os.Stdout)
} else {
log.SetOutput(ioutil.Discard)
}
p := &p2pspider{
laddr: fmt.Sprintf("%s:%d", *addr, *port),
timeout: *timeout,
maxFriends: *maxFriends,
maxPeers: *peers,
secret: *secret,
dir: absDir,
blacklist: newBalckList(10 * time.Minute),
}
p.run()
}