-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload
111 lines (92 loc) · 2.33 KB
/
upload
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
package main
import (
"fmt"
"github.com/anacrolix/dht/v2"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
"log"
"net"
"net/http"
"time"
)
func newClient() (*torrent.Client, error) {
clientConfig := torrent.NewDefaultClientConfig()
clientConfig.DisableTrackers = true
clientConfig.DisableTCP = false
clientConfig.DisablePEX = true
clientConfig.DisableUTP = true
clientConfig.DisableIPv4 = false
clientConfig.DisableIPv6 = true
clientConfig.DisableAcceptRateLimiting = true
clientConfig.NoDHT = false
ua, err := net.ResolveUDPAddr("udp", net.JoinHostPort("127.0.0.1", "6081"))
if err != nil {
fmt.Println("error resolving ", err)
panic(err)
}
clientConfig.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) { return []dht.Addr{dht.NewAddr(ua)}, nil }
}
clientConfig.Debug = true
clientConfig.Seed = true
//clientConfig.PublicIp4 = flags.PublicIP
//clientConfig.PublicIp6 = flags.PublicIP
//clientConfig.DisablePEX = true
clientConfig.DisableWebtorrent = true
clientConfig.DisableWebseeds = true
client, err := torrent.NewClient(clientConfig)
clientConfig.DataDir = "/Users/rain/Movies/film"
return client, err
}
func main() {
filePath := "/Users/rain/Movies/film"
log.SetFlags(log.Flags() | log.Lshortfile)
cl, err := newClient()
if err != nil {
log.Fatalln(err)
return
}
mi := metainfo.MetaInfo{
}
info := metainfo.Info{
PieceLength: 256 * 1024,
}
err = info.BuildFromFilePath(filePath)
if err != nil {
log.Fatal(err)
}
mi.InfoBytes, err = bencode.Marshal(info)
if err != nil {
log.Fatal(err)
}
m := mi.Magnet(nil, &info)
m.DisplayName = "dianying"
log.Println("info id:", m.String())
t, err := cl.AddTorrent(&mi)
if err != nil {
log.Fatalln(err)
}
t.DownloadAll()
log.Println("seed:", t.Seeding())
http.HandleFunc("/torrent", func(w http.ResponseWriter, r *http.Request) {
log.Println("info id:", m.String())
cl.WriteStatus(w)
})
http.HandleFunc("/dht", func(w http.ResponseWriter, r *http.Request) {
for _, ds := range cl.DhtServers() {
ds.WriteStatus(w)
}
})
go func() {
http.ListenAndServe(":8090", nil)
select {
}
}()
d := time.Duration(time.Second*2)
tt := time.NewTicker(d)
for {
<- tt.C
log.Println(t.Seeding(), t.BytesCompleted())
}
}