2013-10-06 15:01:39 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-22 23:54:11 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/util/dirwatch"
|
2013-10-06 15:01:39 +08:00
|
|
|
"flag"
|
|
|
|
"log"
|
2013-10-14 22:39:12 +08:00
|
|
|
"net"
|
2013-10-13 20:16:21 +08:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2013-10-06 15:01:39 +08:00
|
|
|
"os"
|
2014-03-20 01:30:08 +08:00
|
|
|
"os/signal"
|
2013-10-06 15:01:39 +08:00
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2014-03-20 01:30:08 +08:00
|
|
|
"syscall"
|
2013-10-20 22:07:01 +08:00
|
|
|
"time"
|
2014-04-08 17:39:34 +08:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/fs"
|
2013-10-06 15:01:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-03-20 01:30:08 +08:00
|
|
|
downloadDir string
|
|
|
|
torrentPath string
|
|
|
|
mountDir string
|
|
|
|
disableTrackers = flag.Bool("disableTrackers", false, "disables trackers")
|
|
|
|
testPeer = flag.String("testPeer", "", "the address for a test peer")
|
2014-07-22 23:54:58 +08:00
|
|
|
httpAddr = flag.String("httpAddr", "localhost:0", "HTTP server bind address")
|
2014-06-25 23:37:00 +08:00
|
|
|
readaheadBytes = flag.Int("readaheadBytes", 10*1024*1024, "bytes to readahead in each torrent from the last read piece")
|
2014-03-20 01:30:08 +08:00
|
|
|
testPeerAddr *net.TCPAddr
|
2014-07-24 11:45:49 +08:00
|
|
|
listenAddr = flag.String("listenAddr", ":6882", "incoming connection address")
|
2013-10-06 15:01:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&downloadDir, "downloadDir", "", "location to save torrent data")
|
|
|
|
flag.StringVar(&torrentPath, "torrentPath", func() string {
|
|
|
|
_user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
|
|
|
|
}(), "torrent files in this location describe the contents of the mounted filesystem")
|
|
|
|
flag.StringVar(&mountDir, "mountDir", "", "location the torrent contents are made available")
|
|
|
|
}
|
|
|
|
|
2014-07-24 11:45:49 +08:00
|
|
|
func makeListener() net.Listener {
|
|
|
|
l, err := net.Listen("tcp", *listenAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:30:08 +08:00
|
|
|
func resolveTestPeerAddr() {
|
|
|
|
if *testPeer == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSignalHandlers() {
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
2014-04-08 23:17:53 +08:00
|
|
|
for {
|
|
|
|
<-c
|
|
|
|
err := fuse.Unmount(mountDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 01:30:08 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:18:29 +08:00
|
|
|
func addTestPeer(client *torrent.Client) {
|
|
|
|
for _, t := range client.Torrents() {
|
|
|
|
if testPeerAddr != nil {
|
|
|
|
if err := client.AddPeers(t.InfoHash, []torrent.Peer{{
|
|
|
|
IP: testPeerAddr.IP,
|
|
|
|
Port: testPeerAddr.Port,
|
|
|
|
}}); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 15:01:39 +08:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2014-03-20 01:30:08 +08:00
|
|
|
if flag.NArg() != 0 {
|
|
|
|
os.Stderr.WriteString("one does not simply pass positional args\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
if mountDir == "" {
|
|
|
|
os.Stderr.WriteString("y u no specify mountpoint?\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2013-10-14 22:39:12 +08:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
2014-06-26 15:29:12 +08:00
|
|
|
if *httpAddr != "" {
|
2014-07-22 23:54:58 +08:00
|
|
|
util.LoggedHTTPServe(*httpAddr)
|
2013-10-13 20:16:21 +08:00
|
|
|
}
|
2014-03-20 01:30:08 +08:00
|
|
|
conn, err := fuse.Mount(mountDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fuse.Unmount(mountDir)
|
|
|
|
// TODO: Think about the ramifications of exiting not due to a signal.
|
|
|
|
setSignalHandlers()
|
|
|
|
defer conn.Close()
|
2013-10-13 20:16:21 +08:00
|
|
|
client := &torrent.Client{
|
2014-05-21 15:40:54 +08:00
|
|
|
DataDir: downloadDir,
|
|
|
|
DisableTrackers: *disableTrackers,
|
2014-07-24 11:45:49 +08:00
|
|
|
DownloadStrategy: torrent.NewResponsiveDownloadStrategy(*readaheadBytes),
|
|
|
|
Listener: makeListener(),
|
2013-10-13 20:16:21 +08:00
|
|
|
}
|
2014-06-26 15:29:12 +08:00
|
|
|
http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
client.WriteStatus(w)
|
|
|
|
})
|
2013-10-13 20:16:21 +08:00
|
|
|
client.Start()
|
2014-07-22 23:54:11 +08:00
|
|
|
dw, err := dirwatch.New(torrentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
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.Magnet != "" {
|
|
|
|
err := client.AddMagnet(ev.Magnet)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error adding magnet: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case dirwatch.Removed:
|
|
|
|
err := client.DropTorrent(ev.InfoHash)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error dropping torrent: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-03-20 01:30:08 +08:00
|
|
|
resolveTestPeerAddr()
|
|
|
|
fs := torrentfs.New(client)
|
2013-10-20 22:07:01 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2014-04-08 23:18:29 +08:00
|
|
|
addTestPeer(client)
|
2013-10-20 22:07:01 +08:00
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
}()
|
2014-04-08 23:18:29 +08:00
|
|
|
|
2014-03-20 01:30:08 +08:00
|
|
|
if err := fusefs.Serve(conn, fs); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-08 23:18:55 +08:00
|
|
|
<-conn.Ready
|
|
|
|
if err := conn.MountError; err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-10-06 15:01:39 +08:00
|
|
|
}
|