2013-09-26 17:49:15 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2013-09-29 06:11:24 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
2013-09-26 17:49:15 +08:00
|
|
|
"crypto"
|
2013-09-29 06:11:24 +08:00
|
|
|
"crypto/rand"
|
2013-09-26 17:49:15 +08:00
|
|
|
"errors"
|
|
|
|
metainfo "github.com/nsf/libtorgo/torrent"
|
|
|
|
"io"
|
|
|
|
"launchpad.net/gommap"
|
2013-09-29 06:11:24 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
2013-09-26 17:49:15 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PieceHash = crypto.SHA1
|
|
|
|
)
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
type InfoHash [20]byte
|
2013-09-26 17:49:15 +08:00
|
|
|
|
|
|
|
type pieceSum [20]byte
|
|
|
|
|
|
|
|
func copyHashSum(dst, src []byte) {
|
|
|
|
if len(dst) != len(src) || copy(dst, src) != len(dst) {
|
|
|
|
panic("hash sum sizes differ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
func BytesInfoHash(b []byte) (ih InfoHash) {
|
2013-09-26 17:49:15 +08:00
|
|
|
if len(b) != len(ih) {
|
|
|
|
panic("bad infohash bytes")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type pieceState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
pieceStateUnknown = iota
|
|
|
|
pieceStateComplete
|
|
|
|
pieceStateIncomplete
|
|
|
|
)
|
|
|
|
|
|
|
|
type piece struct {
|
|
|
|
State pieceState
|
|
|
|
Hash pieceSum
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
type connection struct {
|
|
|
|
Socket net.Conn
|
|
|
|
post chan peer_protocol.Message
|
|
|
|
|
|
|
|
Interested bool
|
|
|
|
Choked bool
|
|
|
|
Requests []peer_protocol.Request
|
|
|
|
|
|
|
|
PeerId [20]byte
|
|
|
|
PeerInterested bool
|
|
|
|
PeerChoked bool
|
|
|
|
PeerRequests []peer_protocol.Request
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:49:15 +08:00
|
|
|
type torrent struct {
|
2013-09-29 06:11:24 +08:00
|
|
|
InfoHash InfoHash
|
2013-09-26 17:49:15 +08:00
|
|
|
Pieces []piece
|
|
|
|
Data MMapSpan
|
|
|
|
MetaInfo *metainfo.MetaInfo
|
2013-09-29 06:11:24 +08:00
|
|
|
Conns []connection
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type Peer struct {
|
|
|
|
Id [20]byte
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t torrent) PieceSize(piece int) (size int64) {
|
|
|
|
if piece == len(t.Pieces)-1 {
|
|
|
|
size = t.Data.Size() % t.MetaInfo.PieceLength
|
|
|
|
}
|
|
|
|
if size == 0 {
|
|
|
|
size = t.MetaInfo.PieceLength
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-29 12:18:43 +08:00
|
|
|
func (t *torrent) PieceReader(piece int) io.Reader {
|
2013-09-26 17:49:15 +08:00
|
|
|
return io.NewSectionReader(t.Data, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
|
|
|
|
}
|
|
|
|
|
2013-09-29 12:18:43 +08:00
|
|
|
func (t *torrent) HashPiece(piece int) (ps pieceSum) {
|
2013-09-26 17:49:15 +08:00
|
|
|
hash := PieceHash.New()
|
|
|
|
n, err := io.Copy(hash, t.PieceReader(piece))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if n != t.PieceSize(piece) {
|
|
|
|
panic("hashed wrong number of bytes")
|
|
|
|
}
|
|
|
|
copyHashSum(ps[:], hash.Sum(nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
2013-09-29 06:11:24 +08:00
|
|
|
DataDir string
|
|
|
|
HalfOpenLimit int
|
|
|
|
PeerId [20]byte
|
|
|
|
|
|
|
|
halfOpen int
|
|
|
|
torrents map[InfoHash]*torrent
|
2013-09-26 17:49:15 +08:00
|
|
|
|
|
|
|
noTorrents chan struct{}
|
|
|
|
addTorrent chan *torrent
|
2013-09-29 06:11:24 +08:00
|
|
|
torrentFinished chan InfoHash
|
2013-09-26 17:49:15 +08:00
|
|
|
actorTask chan func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(dataDir string) *client {
|
|
|
|
c := &client{
|
2013-09-29 06:11:24 +08:00
|
|
|
DataDir: dataDir,
|
|
|
|
HalfOpenLimit: 10,
|
|
|
|
|
|
|
|
torrents: make(map[InfoHash]*torrent),
|
2013-09-26 17:49:15 +08:00
|
|
|
|
|
|
|
noTorrents: make(chan struct{}),
|
|
|
|
addTorrent: make(chan *torrent),
|
2013-09-29 06:11:24 +08:00
|
|
|
torrentFinished: make(chan InfoHash),
|
2013-09-26 17:49:15 +08:00
|
|
|
actorTask: make(chan func()),
|
|
|
|
}
|
2013-09-29 06:11:24 +08:00
|
|
|
_, err := rand.Read(c.PeerId[:])
|
|
|
|
if err != nil {
|
|
|
|
panic("error generating peer id")
|
|
|
|
}
|
2013-09-26 17:49:15 +08:00
|
|
|
go c.run()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func mmapTorrentData(metaInfo *metainfo.MetaInfo, location string) (mms MMapSpan, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
mms.Close()
|
|
|
|
mms = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for _, miFile := range metaInfo.Files {
|
|
|
|
fileName := filepath.Join(append([]string{location, metaInfo.Name}, miFile.Path...)...)
|
2013-09-29 02:03:04 +08:00
|
|
|
err = os.MkdirAll(filepath.Dir(fileName), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-09-26 17:49:15 +08:00
|
|
|
var file *os.File
|
2013-09-29 02:03:04 +08:00
|
|
|
file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = file.Stat()
|
2013-09-26 17:49:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-09-29 02:03:04 +08:00
|
|
|
if fi.Size() < miFile.Length {
|
|
|
|
err = file.Truncate(miFile.Length)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 17:49:15 +08:00
|
|
|
var mMap gommap.MMap
|
|
|
|
mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-09-29 02:03:04 +08:00
|
|
|
if int64(len(mMap)) != miFile.Length {
|
|
|
|
panic("mmap has wrong length")
|
|
|
|
}
|
2013-09-26 17:49:15 +08:00
|
|
|
mms = append(mms, MMap{mMap})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
func (me *client) torrent(ih InfoHash) *torrent {
|
|
|
|
for _, t := range me.torrents {
|
|
|
|
if t.InfoHash == ih {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) initiateConn(peer Peer, torrent *torrent) {
|
|
|
|
if peer.Id == me.PeerId {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
me.halfOpen++
|
|
|
|
go func() {
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: peer.IP,
|
|
|
|
Port: peer.Port,
|
|
|
|
})
|
|
|
|
me.withContext(func() {
|
|
|
|
me.halfOpen--
|
|
|
|
me.openNewConns()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error connecting to peer: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
me.runConnection(conn, torrent, peer.Id)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) runConnection(conn net.Conn, torrent *torrent, peerId [20]byte) {
|
|
|
|
log.Fatalf("connected to %s", conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) openNewConns() {
|
|
|
|
for _, t := range me.torrents {
|
|
|
|
for len(t.Peers) != 0 {
|
|
|
|
if me.halfOpen >= me.HalfOpenLimit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := t.Peers[0]
|
|
|
|
t.Peers = t.Peers[1:]
|
|
|
|
me.initiateConn(p, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) AddPeers(infoHash InfoHash, peers []Peer) (err error) {
|
|
|
|
me.withContext(func() {
|
|
|
|
t := me.torrent(infoHash)
|
|
|
|
if t == nil {
|
|
|
|
err = errors.New("no such torrent")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Peers = append(t.Peers, peers...)
|
|
|
|
me.openNewConns()
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:49:15 +08:00
|
|
|
func (me *client) AddTorrent(metaInfo *metainfo.MetaInfo) error {
|
|
|
|
torrent := &torrent{
|
|
|
|
InfoHash: BytesInfoHash(metaInfo.InfoHash),
|
|
|
|
}
|
|
|
|
for offset := 0; offset < len(metaInfo.Pieces); offset += PieceHash.Size() {
|
|
|
|
hash := metaInfo.Pieces[offset : offset+PieceHash.Size()]
|
|
|
|
if len(hash) != PieceHash.Size() {
|
|
|
|
return errors.New("bad piece hash in metainfo")
|
|
|
|
}
|
|
|
|
piece := piece{}
|
|
|
|
copyHashSum(piece.Hash[:], hash)
|
|
|
|
torrent.Pieces = append(torrent.Pieces, piece)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
torrent.Data, err = mmapTorrentData(metaInfo, me.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-29 02:03:36 +08:00
|
|
|
torrent.MetaInfo = metaInfo
|
2013-09-26 17:49:15 +08:00
|
|
|
me.addTorrent <- torrent
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) WaitAll() {
|
|
|
|
<-me.noTorrents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) withContext(f func()) {
|
|
|
|
me.actorTask <- f
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
func (me *client) pieceHashed(ih InfoHash, piece int, correct bool) {
|
2013-09-26 17:49:15 +08:00
|
|
|
torrent := me.torrents[ih]
|
|
|
|
torrent.Pieces[piece].State = func() pieceState {
|
|
|
|
if correct {
|
|
|
|
return pieceStateComplete
|
|
|
|
} else {
|
|
|
|
return pieceStateIncomplete
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for _, piece := range torrent.Pieces {
|
|
|
|
if piece.State == pieceStateUnknown {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 02:04:46 +08:00
|
|
|
go func() {
|
|
|
|
me.torrentFinished <- ih
|
|
|
|
}()
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *client) run() {
|
|
|
|
for {
|
|
|
|
noTorrents := me.noTorrents
|
|
|
|
if len(me.torrents) != 0 {
|
|
|
|
noTorrents = nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case noTorrents <- struct{}{}:
|
|
|
|
case torrent := <-me.addTorrent:
|
|
|
|
if _, ok := me.torrents[torrent.InfoHash]; ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
me.torrents[torrent.InfoHash] = torrent
|
|
|
|
for i := range torrent.Pieces {
|
|
|
|
go func(piece int) {
|
|
|
|
sum := torrent.HashPiece(piece)
|
|
|
|
me.withContext(func() {
|
|
|
|
me.pieceHashed(torrent.InfoHash, piece, sum == torrent.Pieces[piece].Hash)
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
case infoHash := <-me.torrentFinished:
|
|
|
|
delete(me.torrents, infoHash)
|
2013-09-29 02:05:09 +08:00
|
|
|
case task := <-me.actorTask:
|
|
|
|
task()
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|