-
-
Notifications
You must be signed in to change notification settings - Fork 621
/
main.go
151 lines (140 loc) · 3.32 KB
/
main.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
// Mounts a FUSE filesystem backed by torrents and magnet links.
package main
import (
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"os/user"
"path/filepath"
"syscall"
"time"
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/tagflag"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/fs"
"github.com/anacrolix/torrent/util/dirwatch"
)
var (
args = struct {
MetainfoDir string `help:"torrent files in this location describe the contents of the mounted filesystem"`
DownloadDir string `help:"location to save torrent data"`
MountDir string `help:"location the torrent contents are made available"`
DisableTrackers bool
TestPeer *net.TCPAddr
ReadaheadBytes tagflag.Bytes
ListenAddr *net.TCPAddr
}{
MetainfoDir: func() string {
_user, err := user.Current()
if err != nil {
log.Fatal(err)
}
return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
}(),
ReadaheadBytes: 10 << 20,
ListenAddr: &net.TCPAddr{},
}
)
func exitSignalHandlers(fs *torrentfs.TorrentFS) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for {
<-c
fs.Destroy()
err := fuse.Unmount(args.MountDir)
if err != nil {
log.Print(err)
}
}
}
func addTestPeer(client *torrent.Client) {
for _, t := range client.Torrents() {
t.AddPeers([]torrent.PeerInfo{{
Addr: args.TestPeer,
}})
}
}
func main() {
os.Exit(mainExitCode())
}
func mainExitCode() int {
tagflag.Parse(&args)
if args.MountDir == "" {
os.Stderr.WriteString("y u no specify mountpoint?\n")
return 2
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
conn, err := fuse.Mount(args.MountDir)
if err != nil {
log.Fatal(err)
}
defer fuse.Unmount(args.MountDir)
// TODO: Think about the ramifications of exiting not due to a signal.
defer conn.Close()
cfg := torrent.NewDefaultClientConfig()
cfg.DataDir = args.DownloadDir
cfg.DisableTrackers = args.DisableTrackers
cfg.NoUpload = true // Ensure that downloads are responsive.
cfg.SetListenAddr(args.ListenAddr.String())
client, err := torrent.NewClient(cfg)
if err != nil {
log.Print(err)
return 1
}
// This is naturally exported via GOPPROF=http.
http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w)
})
dw, err := dirwatch.New(args.MetainfoDir)
if err != nil {
log.Printf("error watching torrent dir: %s", err)
return 1
}
go func() {
for ev := range dw.Events {
switch ev.Change {
case dirwatch.Added:
if ev.TorrentFilePath != "" {
_, err := client.AddTorrentFromFile(ev.TorrentFilePath)
if err != nil {
log.Printf("error adding torrent to client: %s", err)
}
} else if ev.MagnetURI != "" {
_, err := client.AddMagnet(ev.MagnetURI)
if err != nil {
log.Printf("error adding magnet: %s", err)
}
}
case dirwatch.Removed:
T, ok := client.Torrent(ev.InfoHash)
if !ok {
break
}
T.Drop()
}
}
}()
fs := torrentfs.New(client)
go exitSignalHandlers(fs)
if args.TestPeer != nil {
go func() {
for {
addTestPeer(client)
time.Sleep(10 * time.Second)
}
}()
}
if err := fusefs.Serve(conn, fs); err != nil {
log.Fatal(err)
}
<-conn.Ready
if err := conn.MountError; err != nil {
log.Fatal(err)
}
return 0
}