16 _ "github.com/anacrolix/envpprof"
18 "github.com/anacrolix/torrent/util/dirwatch"
21 fusefs "bazil.org/fuse/fs"
22 "github.com/anacrolix/torrent"
23 "github.com/anacrolix/torrent/fs"
27 torrentPath = flag.String("torrentPath", func() string {
28 _user, err := user.Current()
32 return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
33 }(), "torrent files in this location describe the contents of the mounted filesystem")
34 downloadDir = flag.String("downloadDir", "", "location to save torrent data")
35 mountDir = flag.String("mountDir", "", "location the torrent contents are made available")
37 disableTrackers = flag.Bool("disableTrackers", false, "disables trackers")
38 testPeer = flag.String("testPeer", "", "the address for a test peer")
39 readaheadBytes = flag.Int64("readaheadBytes", 10*1024*1024, "bytes to readahead in each torrent from the last read piece")
40 listenAddr = flag.String("listenAddr", ":6882", "incoming connection address")
42 testPeerAddr *net.TCPAddr
45 func resolveTestPeerAddr() {
50 testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer)
56 func exitSignalHandlers(fs *torrentfs.TorrentFS) {
57 c := make(chan os.Signal)
58 signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
62 err := fuse.Unmount(*mountDir)
69 func addTestPeer(client *torrent.Client) {
70 for _, t := range client.Torrents() {
71 if testPeerAddr != nil {
72 if err := t.AddPeers([]torrent.Peer{{
74 Port: testPeerAddr.Port,
85 os.Stderr.WriteString("one does not simply pass positional args\n")
89 os.Stderr.WriteString("y u no specify mountpoint?\n")
92 log.SetFlags(log.LstdFlags | log.Lshortfile)
93 conn, err := fuse.Mount(*mountDir)
97 defer fuse.Unmount(*mountDir)
98 // TODO: Think about the ramifications of exiting not due to a signal.
100 client, err := torrent.NewClient(&torrent.Config{
101 DataDir: *downloadDir,
102 DisableTrackers: *disableTrackers,
103 // DownloadStrategy: torrent.NewResponsiveDownloadStrategy(*readaheadBytes),
104 ListenAddr: *listenAddr,
105 NoUpload: true, // Ensure that uploads are responsive.
107 http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
108 client.WriteStatus(w)
110 dw, err := dirwatch.New(*torrentPath)
115 for ev := range dw.Events {
118 if ev.TorrentFilePath != "" {
119 _, err := client.AddTorrentFromFile(ev.TorrentFilePath)
121 log.Printf("error adding torrent to client: %s", err)
123 } else if ev.MagnetURI != "" {
124 _, err := client.AddMagnet(ev.MagnetURI)
126 log.Printf("error adding magnet: %s", err)
129 case dirwatch.Removed:
130 for _, t := range client.Torrents() {
131 if t.InfoHash != ev.InfoHash {
140 resolveTestPeerAddr()
141 fs := torrentfs.New(client)
142 go exitSignalHandlers(fs)
146 time.Sleep(10 * time.Second)
150 if err := fusefs.Serve(conn, fs); err != nil {
154 if err := conn.MountError; err != nil {