forked from gitwillsky/btsearcher
/
btengine.go
98 lines (80 loc) · 1.88 KB
/
btengine.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
package main
import (
"github.com/gitwillsky/btsearcher/dht"
"github.com/gitwillsky/btsearcher/torrent"
"github.com/gitwillsky/btsearcher/models"
"time"
"strings"
"errors"
"github.com/gitwillsky/btsearcher/filter"
"github.com/astaxie/beego"
)
// insert metainfo to database.
func Insert(infoHash string, ip string) error {
t := &models.Torrent{
LastDownloadIp: ip,
LastDownloadAt: time.Now(),
InfoHash: strings.ToLower(infoHash),
}
if t.IsExists() {
return errors.New("Torrent already in database;")
}
// get torrent file information.
torrentFile := torrent.New(infoHash)
m, err := torrentFile.GetTorrentMetaInfo()
if err != nil {
return err
}
t.Name = m.Info.Name
t.Enable = true
if len(t.Name) < 2 || len([]byte(t.Name)) < 2 {
return errors.New("Torrent info name length lt 2!")
} else if filter.IsIllegalWords(t.Name) {
// 非法资源
t.Enable = false
}
t.FileLen = m.Info.Length
t.Md5 = m.Info.Md5sum
t.Magnet = "magnet:?xt=urn:btih:" + t.InfoHash
t.CreateDate = time.Unix(m.CreateDate, 0)
// file list
for _, v := range m.Info.Files {
// 非法资源
if filter.IsIllegalWords(v.Path[0]) {
t.Enable = false
}
file := &models.File{
Name:v.Path[0],
Length:v.Length,
Md5:v.Md5sum,
}
t.FileList = append(t.FileList, file)
}
return t.Create()
}
func excute(output chan string, port int) {
myDHTNode := dht.NewDHTNode(output, port)
myDHTNode.Run()
}
func main() {
output := make(chan string)
portA, _ := beego.AppConfig.Int("UDPPort")
excute(output, portA)
for {
select {
case dat := <-output:
s := strings.Split(dat, ":")
infohash := s[0]
ip := s[1]
go func(info string, ip string) {
Insert(info, ip)
/*if err := Insert(info, ip); err != nil {
fmt.Printf("%s insert failed: %s \n", info, err.Error())
}else {
fmt.Printf("%s insert success! \n", info)
}
*/
}(infohash, ip)
}
}
}