/
find.go
executable file
·162 lines (142 loc) · 5.26 KB
/
find.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
package cmd
import (
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/RaulCalvoLaorden/bntoolkit/dht"
"github.com/RaulCalvoLaorden/bntoolkit/utils"
"github.com/spf13/cobra"
)
var file string
var noadd bool
var typeF int
var timeout int
var waitGroup sync.WaitGroup
//FileInfo : Path plus lenght
type FileInfo struct {
Path []string
Length int
}
var hashesChan chan string
var inicio = 10
var (
builtinAnnounceList = [][]string{
{"udp://tracker.openbittorrent.com:80"},
{"udp://tracker.publicbt.com:80"},
{"udp://tracker.istole.it:6969"},
}
)
// findCmd represents the find command
var findCmd = &cobra.Command{
Use: "find <path to file>",
Short: "Find the file in Bittorrent network",
Long: `Find the file in Bittorrent network using the DHT, a trackers list and the local database. In this command the hashes can be: Possibles, Valid or Downloaded. The first are the ones that could exist because they are valid, the second are the ones that have been found in BitTorrent and the third is that it has peers and can be downloaded.
For example:
bntoolkit find ubuntu-18.04.1-desktop-amd64.iso.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("find called", len(args))
if len(args) != 1 {
cmd.Help()
os.Exit(0)
}
file = strings.Join(args, " ")
db, err := utils.ConnectDb(cfgFile, debug, verbose)
if err != nil {
log.Fatal(err)
}
defer db.Close()
utils.InsertProject(db, debug, verbose, projectName) //if return error exists, so OK
switch typeF {
case 0:
dht.WorkersTorrents(cfgFile, debug, verbose, file, projectName) //create hashes and check in db
dht.ScrapeTrackers(db, debug, verbose, projectName) //scrape trackers
fmt.Println("Possibles (possibles hashes to the file)")
err := utils.SelectPossiblesWhere("possible", db, debug, verbose, projectName) //get only possibles
if err != nil {
log.Println(err)
}
case 1:
dht.WorkersTorrents(cfgFile, debug, verbose, file, projectName) //create hashes and check in db
dht.ScrapeTrackers(db, debug, verbose, projectName) //scrape trackers
dht.SearchDHT(db, debug, verbose, projectName) //search DHT
fmt.Println("Possibles (possibles hashes to the file)")
err := utils.SelectPossiblesWhere("possible", db, debug, verbose, projectName) //get only possibles
if err != nil {
log.Println(err)
}
fmt.Println("Valid (found in DHT)")
err = utils.SelectPossiblesWhere("valid", db, debug, verbose, projectName) //get only valids
if err != nil {
log.Println(err)
}
case 2:
dht.WorkersTorrents(cfgFile, debug, verbose, file, projectName) //create hashes and check in db
dht.ScrapeTrackers(db, debug, verbose, projectName) //scrape trackers
dht.SearchDHT(db, debug, verbose, projectName) //search DHT
fmt.Println("Possibles (possibles hashes to the file)")
err := utils.SelectPossiblesWhere("possible", db, debug, verbose, projectName) //get only possibles
if err != nil {
log.Println(err)
}
fmt.Println("Valid (found in DHT)")
err = utils.SelectPossiblesWhere("valid", db, debug, verbose, projectName) //get only valids
if err != nil {
log.Println(err)
}
fmt.Println("Download valid")
err = utils.DownloadValid(db, timeout, debug, verbose, projectName) //download valid
if err != nil {
log.Println(err)
}
fmt.Println("Downloaded")
err = utils.SelectPossiblesWhere("download", db, debug, verbose, projectName) //get only download
if err != nil {
log.Println(err)
}
case 3:
dht.WorkersTorrents(cfgFile, debug, verbose, file, projectName) //create hashes and check in db
dht.ScrapeTrackers(db, debug, verbose, projectName) //scrape trackers
dht.SearchDHT(db, debug, verbose, projectName) //search DHT
fmt.Println("Possibles (possibles hashes to the file)")
err := utils.SelectPossiblesWhere("possible", db, debug, verbose, projectName) //get only possibles
if err != nil {
log.Println(err)
}
fmt.Println("Valid (found in DHT)")
err = utils.SelectPossiblesWhere("valid", db, debug, verbose, projectName) //get only valids
if err != nil {
log.Println(err)
}
fmt.Println("Download possibles")
err = utils.DownloadPossibles(db, timeout, debug, verbose, projectName) //download possibles
if err != nil {
log.Println(err)
}
fmt.Println("Downloaded")
err = utils.SelectPossiblesWhere("download", db, debug, verbose, projectName) //get only download
if err != nil {
log.Println(err)
}
default:
fmt.Println("Invalid type option")
cmd.Help()
os.Exit(0)
}
},
}
func init() {
rootCmd.AddCommand(findCmd)
findCmd.PersistentFlags().StringVarP(&projectName, "projectName", "p", "default", "ProjectName to database")
findCmd.PersistentFlags().StringVarP(&tracker, "tracker", "", "", "<not implemented> tracker")
findCmd.PersistentFlags().BoolVarP(&noadd, "no-add", "n", false, "<not implemented> no add to database")
findCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 5, "timeout download in minutes")
findCmd.PersistentFlags().IntVarP(&typeF, "mode", "m", 2, `Opciones
0) Trackers
1) Trackers + DHT, Bool valid
2) Trackers + DHT + Download valid, Bool valid and download
3) Try to download all possibles
`)
}