Compare commits
1 Commits
master
...
interfaces
Author | SHA1 | Date | |
---|---|---|---|
ffeb6c3a57 |
28
.github/workflows/go.yml
vendored
28
.github/workflows/go.yml
vendored
@ -1,28 +0,0 @@
|
||||
name: Go
|
||||
on: push
|
||||
env:
|
||||
GO111MODULE: on
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-22.04
|
||||
name: Go ${{ matrix.go }}
|
||||
strategy:
|
||||
matrix:
|
||||
go:
|
||||
- '1.11'
|
||||
- '1.12'
|
||||
- '1.13'
|
||||
- '1.14'
|
||||
- '1.15'
|
||||
- '1.16'
|
||||
- '1.17'
|
||||
- '1.18'
|
||||
- '1.19'
|
||||
- '1.20'
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
- run: go test -race -cover ./...
|
12
.gitignore
vendored
12
.gitignore
vendored
@ -33,16 +33,8 @@ test_*
|
||||
# log
|
||||
*.log
|
||||
|
||||
vendor/
|
||||
|
||||
# VS Code
|
||||
.vscode/
|
||||
debug
|
||||
debug_test
|
||||
|
||||
# Mac
|
||||
.DS_Store
|
||||
|
||||
# hidden files
|
||||
.*
|
||||
_*
|
||||
# VS Code
|
||||
.vscode/
|
||||
|
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@ -0,0 +1,13 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.9.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- 1.14.x
|
||||
- 1.15.x
|
||||
env:
|
||||
- GO111MODULE=on
|
||||
script:
|
||||
- go test -race ./...
|
228
README.md
228
README.md
@ -1,23 +1,24 @@
|
||||
# BT - Another Implementation For Golang [](https://github.com/xgfone/go-bt/actions/workflows/go.yml) [](https://pkg.go.dev/github.com/xgfone/go-bt) [](https://raw.githubusercontent.com/xgfone/go-bt/master/LICENSE)
|
||||
# BT - Another Implementation For Golang [](https://travis-ci.com/github/xgfone/bt) [](https://pkg.go.dev/github.com/xgfone/bt) [](https://raw.githubusercontent.com/xgfone/bt/master/LICENSE)
|
||||
|
||||
A pure golang implementation of [BitTorrent](http://bittorrent.org/beps/bep_0000.html) library, which is inspired by [dht](https://github.com/shiyanhui/dht) and [torrent](https://github.com/anacrolix/torrent).
|
||||
|
||||
## Install
|
||||
|
||||
## Install
|
||||
```shell
|
||||
$ go get -u github.com/xgfone/go-bt
|
||||
$ go get -u github.com/xgfone/bt
|
||||
```
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
- Support `Go1.11+`.
|
||||
- Support `Go1.9+`.
|
||||
- Support IPv4/IPv6.
|
||||
- Multi-BEPs implementation.
|
||||
- Pure Go implementation without `CGO`.
|
||||
- Only library without any denpendencies. For the command tools, see [bttools](https://github.com/xgfone/bttools).
|
||||
|
||||
## The Implemented Specifications
|
||||
|
||||
## The Implemented Specifications
|
||||
- [x] [**BEP 03:** The BitTorrent Protocol Specification](http://bittorrent.org/beps/bep_0003.html)
|
||||
- [x] [**BEP 05:** DHT Protocol](http://bittorrent.org/beps/bep_0005.html)
|
||||
- [x] [**BEP 06:** Fast Extension](http://bittorrent.org/beps/bep_0006.html)
|
||||
@ -36,6 +37,219 @@ $ go get -u github.com/xgfone/go-bt
|
||||
- [ ] [**BEP 44:** Storing arbitrary data in the DHT](http://bittorrent.org/beps/bep_0044.html)
|
||||
- [x] [**BEP 48:** Tracker Protocol Extension: Scrape](http://bittorrent.org/beps/bep_0048.html)
|
||||
|
||||
## Example
|
||||
|
||||
See [godoc](https://pkg.go.dev/github.com/xgfone/go-bt) or [bttools](https://github.com/xgfone/bttools).
|
||||
## Example
|
||||
See [godoc](https://pkg.go.dev/github.com/xgfone/bt) or [bttools](https://github.com/xgfone/bttools).
|
||||
|
||||
### Example 1: Download the file from the remote peer
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/bt/downloader"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
pp "github.com/xgfone/bt/peerprotocol"
|
||||
"github.com/xgfone/bt/tracker"
|
||||
)
|
||||
|
||||
var peeraddr string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&peeraddr, "peeraddr", "", "The address of the peer storing the file.")
|
||||
}
|
||||
|
||||
func getPeersFromTrackers(id, infohash metainfo.Hash, trackers []string) (peers []string) {
|
||||
c, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
|
||||
resp := tracker.GetPeers(c, id, infohash, trackers)
|
||||
for r := range resp {
|
||||
for _, addr := range r.Resp.Addresses {
|
||||
addrs := addr.String()
|
||||
nonexist := true
|
||||
for _, peer := range peers {
|
||||
if peer == addrs {
|
||||
nonexist = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if nonexist {
|
||||
peers = append(peers, addrs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
torrentfile := os.Args[1]
|
||||
mi, err := metainfo.LoadFromFile(torrentfile)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
id := metainfo.NewRandomHash()
|
||||
infohash := mi.InfoHash()
|
||||
info, err := mi.Info()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var peers []string
|
||||
if peeraddr != "" {
|
||||
peers = []string{peeraddr}
|
||||
} else {
|
||||
// Get the peers from the trackers in the torrent file.
|
||||
trackers := mi.Announces().Unique()
|
||||
if len(trackers) == 0 {
|
||||
fmt.Println("no trackers")
|
||||
return
|
||||
}
|
||||
|
||||
peers = getPeersFromTrackers(id, infohash, trackers)
|
||||
if len(peers) == 0 {
|
||||
fmt.Println("no peers")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// We save the downloaded file to the current directory.
|
||||
w := metainfo.NewWriter("", info, 0)
|
||||
defer w.Close()
|
||||
|
||||
// We don't request the blocks from the remote peers concurrently,
|
||||
// and it is only an example. But you can do it concurrently.
|
||||
dm := newDownloadManager(w, info)
|
||||
for peerslen := len(peers); peerslen > 0 && !dm.IsFinished(); {
|
||||
peerslen--
|
||||
peer := peers[peerslen]
|
||||
peers = peers[:peerslen]
|
||||
downloadFileFromPeer(peer, id, infohash, dm)
|
||||
}
|
||||
}
|
||||
|
||||
func downloadFileFromPeer(peer string, id, infohash metainfo.Hash, dm *downloadManager) {
|
||||
pc, err := pp.NewPeerConnByDial(peer, id, infohash, time.Second*3)
|
||||
if err != nil {
|
||||
log.Printf("fail to dial '%s'", peer)
|
||||
return
|
||||
}
|
||||
defer pc.Close()
|
||||
|
||||
dm.doing = false
|
||||
pc.Timeout = time.Second * 10
|
||||
if err = pc.Handshake(); err != nil {
|
||||
log.Printf("fail to handshake with '%s': %s", peer, err)
|
||||
return
|
||||
}
|
||||
|
||||
info := dm.writer.Info()
|
||||
bdh := downloader.NewBlockDownloadHandler(info, dm.OnBlock, dm.RequestBlock)
|
||||
if err = bdh.OnHandShake(pc); err != nil {
|
||||
log.Printf("handshake error with '%s': %s", peer, err)
|
||||
return
|
||||
}
|
||||
|
||||
var msg pp.Message
|
||||
for !dm.IsFinished() {
|
||||
switch msg, err = pc.ReadMsg(); err {
|
||||
case nil:
|
||||
switch err = pc.HandleMessage(msg, bdh); err {
|
||||
case nil, pp.ErrChoked:
|
||||
default:
|
||||
log.Printf("fail to handle the msg from '%s': %s", peer, err)
|
||||
return
|
||||
}
|
||||
case io.EOF:
|
||||
log.Printf("got EOF from '%s'", peer)
|
||||
return
|
||||
default:
|
||||
log.Printf("fail to read the msg from '%s': %s", peer, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newDownloadManager(w metainfo.Writer, info metainfo.Info) *downloadManager {
|
||||
length := info.Piece(0).Length()
|
||||
return &downloadManager{writer: w, plength: length}
|
||||
}
|
||||
|
||||
type downloadManager struct {
|
||||
writer metainfo.Writer
|
||||
pindex uint32
|
||||
poffset uint32
|
||||
plength int64
|
||||
doing bool
|
||||
}
|
||||
|
||||
func (dm *downloadManager) IsFinished() bool {
|
||||
if dm.pindex >= uint32(dm.writer.Info().CountPieces()) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (dm *downloadManager) OnBlock(index, offset uint32, b []byte) (err error) {
|
||||
if dm.pindex != index {
|
||||
return fmt.Errorf("inconsistent piece: old=%d, new=%d", dm.pindex, index)
|
||||
} else if dm.poffset != offset {
|
||||
return fmt.Errorf("inconsistent offset for piece '%d': old=%d, new=%d",
|
||||
index, dm.poffset, offset)
|
||||
}
|
||||
|
||||
dm.doing = false
|
||||
n, err := dm.writer.WriteBlock(index, offset, b)
|
||||
if err == nil {
|
||||
dm.poffset = offset + uint32(n)
|
||||
dm.plength -= int64(n)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *downloadManager) RequestBlock(pc *pp.PeerConn) (err error) {
|
||||
if dm.doing {
|
||||
return
|
||||
}
|
||||
|
||||
if dm.plength <= 0 {
|
||||
dm.pindex++
|
||||
if dm.IsFinished() {
|
||||
return
|
||||
}
|
||||
|
||||
dm.poffset = 0
|
||||
dm.plength = dm.writer.Info().Piece(int(dm.pindex)).Length()
|
||||
}
|
||||
|
||||
index := dm.pindex
|
||||
begin := dm.poffset
|
||||
length := uint32(downloader.BlockSize)
|
||||
if length > uint32(dm.plength) {
|
||||
length = uint32(dm.plength)
|
||||
}
|
||||
|
||||
log.Printf("Request Block from '%s': index=%d, offset=%d, length=%d",
|
||||
pc.RemoteAddr().String(), index, begin, length)
|
||||
if err = pc.SendRequest(index, begin, length); err == nil {
|
||||
dm.doing = true
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
@ -120,8 +120,8 @@ func TestDecode(t *testing.T) {
|
||||
|
||||
{`d1:X3:foo1:Yi10e1:h3:bare`, new(dT), dT{"foo", 10, ""}, false, false},
|
||||
{`d3:fooli0ei1ee3:barli2ei3eee`, new(map[string][]int), map[string][]int{
|
||||
"foo": {0, 1},
|
||||
"bar": {2, 3},
|
||||
"foo": []int{0, 1},
|
||||
"bar": []int{2, 3},
|
||||
}, false, false},
|
||||
{`de`, new(map[string]string), map[string]string{}, false, false},
|
||||
|
||||
|
@ -2,5 +2,5 @@
|
||||
// which has a similar API to the encoding/json package and many other
|
||||
// serialization formats.
|
||||
//
|
||||
// Notice: the package is ported from github.com/zeebo/bencode@v1.0.0
|
||||
// Notice: the package is moved and modified from github.com/zeebo/bencode@v1.0.0
|
||||
package bencode
|
||||
|
@ -112,8 +112,8 @@ func TestEncode(t *testing.T) {
|
||||
{"c": 2, "d": 3},
|
||||
}, `ld1:ai0e1:bi1eed1:ci2e1:di3eee`, false},
|
||||
{[][]byte{
|
||||
{'0', '2', '4', '6', '8'},
|
||||
{'a', 'c', 'e'},
|
||||
[]byte{'0', '2', '4', '6', '8'},
|
||||
[]byte{'a', 'c', 'e'},
|
||||
}, `l5:024683:acee`, false},
|
||||
{(*[]interface{})(nil), ``, false},
|
||||
|
||||
|
@ -17,8 +17,6 @@ package dht
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
)
|
||||
|
||||
// Blacklist is used to manage the ip blacklist.
|
||||
@ -26,24 +24,25 @@ import (
|
||||
// Notice: The implementation should clear the address existed for long time.
|
||||
type Blacklist interface {
|
||||
// In reports whether the address, ip and port, is in the blacklist.
|
||||
In(krpc.Addr) bool
|
||||
In(ip string, port int) bool
|
||||
|
||||
// If port is equal to 0, it should ignore port and only use ip when matching.
|
||||
Add(krpc.Addr)
|
||||
Add(ip string, port int)
|
||||
|
||||
// If port is equal to 0, it should delete the address by only the ip.
|
||||
Del(krpc.Addr)
|
||||
Del(ip string, port int)
|
||||
|
||||
// Close is used to notice the implementation to release the underlying resource.
|
||||
// Close is used to notice the implementation to release the underlying
|
||||
// resource.
|
||||
Close()
|
||||
}
|
||||
|
||||
type noopBlacklist struct{}
|
||||
|
||||
func (nbl noopBlacklist) In(krpc.Addr) bool { return false }
|
||||
func (nbl noopBlacklist) Add(krpc.Addr) {}
|
||||
func (nbl noopBlacklist) Del(krpc.Addr) {}
|
||||
func (nbl noopBlacklist) Close() {}
|
||||
func (nbl noopBlacklist) In(ip string, port int) bool { return false }
|
||||
func (nbl noopBlacklist) Add(ip string, port int) {}
|
||||
func (nbl noopBlacklist) Del(ip string, port int) {}
|
||||
func (nbl noopBlacklist) Close() {}
|
||||
|
||||
// NewNoopBlacklist returns a no-op Blacklist.
|
||||
func NewNoopBlacklist() Blacklist { return noopBlacklist{} }
|
||||
@ -58,14 +57,14 @@ type logBlacklist struct {
|
||||
logf func(string, ...interface{})
|
||||
}
|
||||
|
||||
func (l logBlacklist) Add(addr krpc.Addr) {
|
||||
l.logf("add the addr '%s' into the blacklist", addr.String())
|
||||
l.Blacklist.Add(addr)
|
||||
func (dbl logBlacklist) Add(ip string, port int) {
|
||||
dbl.logf("add the blacklist: ip=%s, port=%d", ip, port)
|
||||
dbl.Blacklist.Add(ip, port)
|
||||
}
|
||||
|
||||
func (l logBlacklist) Del(addr krpc.Addr) {
|
||||
l.logf("delete the addr '%s' from the blacklist", addr.String())
|
||||
l.Blacklist.Del(addr)
|
||||
func (dbl logBlacklist) Del(ip string, port int) {
|
||||
dbl.logf("delete the blacklist: ip=%s, port=%d", ip, port)
|
||||
dbl.Blacklist.Del(ip, port)
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
@ -86,7 +85,7 @@ func NewMemoryBlacklist(maxnum int, duration time.Duration) Blacklist {
|
||||
type wrappedPort struct {
|
||||
Time time.Time
|
||||
Enable bool
|
||||
Ports map[uint16]struct{}
|
||||
Ports map[int]struct{}
|
||||
}
|
||||
|
||||
type blacklist struct {
|
||||
@ -124,11 +123,11 @@ func (bl *blacklist) Close() {
|
||||
}
|
||||
|
||||
// In reports whether the address, ip and port, is in the blacklist.
|
||||
func (bl *blacklist) In(addr krpc.Addr) (yes bool) {
|
||||
func (bl *blacklist) In(ip string, port int) (yes bool) {
|
||||
bl.lock.RLock()
|
||||
if wp, ok := bl.ips[addr.IP.String()]; ok {
|
||||
if wp, ok := bl.ips[ip]; ok {
|
||||
if wp.Enable {
|
||||
_, yes = wp.Ports[addr.Port]
|
||||
_, yes = wp.Ports[port]
|
||||
} else {
|
||||
yes = true
|
||||
}
|
||||
@ -137,8 +136,7 @@ func (bl *blacklist) In(addr krpc.Addr) (yes bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (bl *blacklist) Add(addr krpc.Addr) {
|
||||
ip := addr.IP.String()
|
||||
func (bl *blacklist) Add(ip string, port int) {
|
||||
bl.lock.Lock()
|
||||
wp, ok := bl.ips[ip]
|
||||
if !ok {
|
||||
@ -151,31 +149,30 @@ func (bl *blacklist) Add(addr krpc.Addr) {
|
||||
bl.ips[ip] = wp
|
||||
}
|
||||
|
||||
if addr.Port < 1 {
|
||||
if port < 1 {
|
||||
wp.Enable = false
|
||||
wp.Ports = nil
|
||||
} else if wp.Ports == nil {
|
||||
wp.Ports = map[uint16]struct{}{addr.Port: {}}
|
||||
wp.Ports = map[int]struct{}{port: struct{}{}}
|
||||
} else {
|
||||
wp.Ports[addr.Port] = struct{}{}
|
||||
wp.Ports[port] = struct{}{}
|
||||
}
|
||||
|
||||
wp.Time = time.Now()
|
||||
bl.lock.Unlock()
|
||||
}
|
||||
|
||||
func (bl *blacklist) Del(addr krpc.Addr) {
|
||||
ip := addr.IP.String()
|
||||
func (bl *blacklist) Del(ip string, port int) {
|
||||
bl.lock.Lock()
|
||||
if wp, ok := bl.ips[ip]; ok {
|
||||
if addr.Port < 1 {
|
||||
if port < 1 {
|
||||
delete(bl.ips, ip)
|
||||
} else if wp.Enable {
|
||||
switch len(wp.Ports) {
|
||||
case 0, 1:
|
||||
delete(bl.ips, ip)
|
||||
default:
|
||||
delete(wp.Ports, addr.Port)
|
||||
delete(wp.Ports, port)
|
||||
wp.Time = time.Now()
|
||||
}
|
||||
}
|
||||
|
@ -15,11 +15,8 @@
|
||||
package dht
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
)
|
||||
|
||||
func (bl *blacklist) portsLen() (n int) {
|
||||
@ -45,12 +42,12 @@ func TestMemoryBlacklist(t *testing.T) {
|
||||
bl := NewMemoryBlacklist(3, time.Second).(*blacklist)
|
||||
defer bl.Close()
|
||||
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("1.1.1.1"), 123))
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("1.1.1.1"), 456))
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("1.1.1.1"), 789))
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("2.2.2.2"), 111))
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("3.3.3.3"), 0))
|
||||
bl.Add(krpc.NewAddr(net.ParseIP("4.4.4.4"), 222))
|
||||
bl.Add("1.1.1.1", 123)
|
||||
bl.Add("1.1.1.1", 456)
|
||||
bl.Add("1.1.1.1", 789)
|
||||
bl.Add("2.2.2.2", 111)
|
||||
bl.Add("3.3.3.3", 0)
|
||||
bl.Add("4.4.4.4", 222)
|
||||
|
||||
ips := bl.getIPs()
|
||||
if len(ips) != 3 {
|
||||
@ -69,15 +66,15 @@ func TestMemoryBlacklist(t *testing.T) {
|
||||
t.Errorf("expect port num 4, but got %d", n)
|
||||
}
|
||||
|
||||
if bl.In(krpc.NewAddr(net.ParseIP("1.1.1.1"), 111)) || !bl.In(krpc.NewAddr(net.ParseIP("1.1.1.1"), 123)) {
|
||||
if bl.In("1.1.1.1", 111) || !bl.In("1.1.1.1", 123) {
|
||||
t.Fail()
|
||||
}
|
||||
if !bl.In(krpc.NewAddr(net.ParseIP("3.3.3.3"), 111)) || bl.In(krpc.NewAddr(net.ParseIP("4.4.4.4"), 222)) {
|
||||
if !bl.In("3.3.3.3", 111) || bl.In("4.4.4.4", 222) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
bl.Del(krpc.NewAddr(net.ParseIP("3.3.3.3"), 0))
|
||||
if bl.In(krpc.NewAddr(net.ParseIP("3.3.3.3"), 111)) {
|
||||
bl.Del("3.3.3.3", 0)
|
||||
if bl.In("3.3.3.3", 111) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
@ -25,9 +25,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/krpc"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -53,7 +53,7 @@ type Result struct {
|
||||
// Addr is the address of the peer where the request is sent to.
|
||||
//
|
||||
// Notice: it may be nil for "get_peers" request.
|
||||
Addr net.Addr
|
||||
Addr *net.UDPAddr
|
||||
|
||||
// For Error
|
||||
Code int // 0 represents the success.
|
||||
@ -61,7 +61,7 @@ type Result struct {
|
||||
Timeout bool // Timeout indicates whether the response is timeout.
|
||||
|
||||
// The list of the address of the peers returned by GetPeers.
|
||||
Peers []string
|
||||
Peers []metainfo.Address
|
||||
}
|
||||
|
||||
// Config is used to configure the DHT server.
|
||||
@ -134,24 +134,18 @@ type Config struct {
|
||||
// The default is log.Printf.
|
||||
ErrorLog func(format string, args ...interface{})
|
||||
|
||||
// They is used to convert the address between net.Addr and krpc.Addr.
|
||||
//
|
||||
// For the default, it asserts net.Addr to *net.UDPAddr.
|
||||
GetNetAddr func(krpc.Addr) net.Addr
|
||||
GetKrpcAddr func(net.Addr) krpc.Addr
|
||||
|
||||
// OnSearch is called when someone searches the torrent infohash,
|
||||
// that's, the "get_peers" query.
|
||||
//
|
||||
// The default callback does noting.
|
||||
OnSearch func(infohash string, addr krpc.Addr)
|
||||
OnSearch func(infohash string, ip net.Addr, port uint16)
|
||||
|
||||
// OnTorrent is called when someone has the torrent infohash
|
||||
// or someone has just downloaded the torrent infohash,
|
||||
// that's, the "get_peers" response or "announce_peer" query.
|
||||
//
|
||||
// The default callback does noting.
|
||||
OnTorrent func(infohash string, addr string)
|
||||
OnTorrent func(infohash string, ip net.Addr, port uint16)
|
||||
|
||||
// HandleInMessage is used to intercept the incoming DHT message.
|
||||
// For example, you can debug the message as the log.
|
||||
@ -159,36 +153,23 @@ type Config struct {
|
||||
// Return true if going on handling by the default. Or return false.
|
||||
//
|
||||
// The default is nil.
|
||||
HandleInMessage func(net.Addr, *krpc.Message) (handled bool)
|
||||
HandleInMessage func(*net.UDPAddr, *krpc.Message) bool
|
||||
|
||||
// HandleOutMessage is used to intercept the outgoing DHT message.
|
||||
// For example, you can debug the message as the log.
|
||||
//
|
||||
// Return (true, nil) if not going on handling by the default.
|
||||
// Return (false, nil) if going on handling by the default.
|
||||
//
|
||||
// The default is nil.
|
||||
HandleOutMessage func(net.Addr, *krpc.Message) (wrote bool, err error)
|
||||
HandleOutMessage func(*net.UDPAddr, *krpc.Message) (wrote bool, err error)
|
||||
}
|
||||
|
||||
func (c Config) in(net.Addr, *krpc.Message) bool { return false }
|
||||
func (c Config) out(net.Addr, *krpc.Message) (bool, error) { return false, nil }
|
||||
func (c Config) in(*net.UDPAddr, *krpc.Message) bool { return true }
|
||||
func (c Config) out(*net.UDPAddr, *krpc.Message) (bool, error) { return false, nil }
|
||||
|
||||
func (c Config) onSearch(string, krpc.Addr) {}
|
||||
func (c Config) onTorrent(string, string) {}
|
||||
|
||||
func (c Config) k2nAddr(a krpc.Addr) net.Addr {
|
||||
if a.Orig != nil {
|
||||
return a.Orig
|
||||
}
|
||||
return a.UDPAddr()
|
||||
}
|
||||
func (c Config) n2kAddr(a net.Addr) krpc.Addr {
|
||||
return krpc.NewAddrFromUDPAddr(a.(*net.UDPAddr))
|
||||
}
|
||||
|
||||
func (c *Config) set(conf *Config) {
|
||||
if conf != nil {
|
||||
*c = *conf
|
||||
func (c *Config) set(conf ...Config) {
|
||||
if len(conf) > 0 {
|
||||
*c = conf[0]
|
||||
}
|
||||
|
||||
if c.K <= 0 {
|
||||
@ -216,16 +197,10 @@ func (c *Config) set(conf *Config) {
|
||||
c.RespTimeout = time.Second * 10
|
||||
}
|
||||
if c.OnSearch == nil {
|
||||
c.OnSearch = c.onSearch
|
||||
c.OnSearch = func(string, net.Addr, uint16) {}
|
||||
}
|
||||
if c.OnTorrent == nil {
|
||||
c.OnTorrent = c.onTorrent
|
||||
}
|
||||
if c.GetNetAddr == nil {
|
||||
c.GetNetAddr = c.k2nAddr
|
||||
}
|
||||
if c.GetKrpcAddr == nil {
|
||||
c.GetKrpcAddr = c.n2kAddr
|
||||
c.OnTorrent = func(string, net.Addr, uint16) {}
|
||||
}
|
||||
if c.HandleInMessage == nil {
|
||||
c.HandleInMessage = c.in
|
||||
@ -240,7 +215,6 @@ type Server struct {
|
||||
conf Config
|
||||
exit chan struct{}
|
||||
conn net.PacketConn
|
||||
lock sync.Mutex
|
||||
once sync.Once
|
||||
|
||||
ipv4 bool
|
||||
@ -256,9 +230,9 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServer returns a new DHT server.
|
||||
func NewServer(conn net.PacketConn, config *Config) *Server {
|
||||
func NewServer(conn net.PacketConn, config ...Config) *Server {
|
||||
var conf Config
|
||||
conf.set(config)
|
||||
conf.set(config...)
|
||||
|
||||
if len(conf.IPProtocols) == 0 {
|
||||
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
|
||||
@ -304,6 +278,7 @@ func NewServer(conn net.PacketConn, config *Config) *Server {
|
||||
if s.peerManager == nil {
|
||||
s.peerManager = s.tokenPeerManager
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@ -317,23 +292,15 @@ func (s *Server) Bootstrap(addrs []string) {
|
||||
if (s.ipv4 && s.routingTable4.Len() == 0) ||
|
||||
(s.ipv6 && s.routingTable6.Len() == 0) {
|
||||
for _, addr := range addrs {
|
||||
ipports, err := krpc.ParseAddrs(addr)
|
||||
as, err := metainfo.NewAddressesFromString(addr)
|
||||
if err != nil {
|
||||
s.conf.ErrorLog(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
for _, ipport := range ipports {
|
||||
if isIPv6(ipport.IP) {
|
||||
if !s.ipv6 {
|
||||
continue
|
||||
}
|
||||
} else if !s.ipv4 {
|
||||
continue
|
||||
}
|
||||
|
||||
if err = s.FindNode(ipport, s.conf.ID); err != nil {
|
||||
s.conf.ErrorLog(`fail to bootstrap '%s': %s`, ipport.String(), err)
|
||||
for _, a := range as {
|
||||
if err = s.FindNode(a.UDPAddr(), s.conf.ID); err != nil {
|
||||
s.conf.ErrorLog(`fail to bootstrap '%s': %s`, a.String(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -349,12 +316,12 @@ func (s *Server) Node6Num() int { return s.routingTable6.Len() }
|
||||
// AddNode adds the node into the routing table.
|
||||
//
|
||||
// The returned value:
|
||||
// NodeAdded: The node is added successfully.
|
||||
// NodeNotAdded: The node is not added and is discarded.
|
||||
// NodeExistAndUpdated: The node has existed, and its status has been updated.
|
||||
// NodeExistAndChanged: The node has existed, but the address is inconsistent.
|
||||
// The current node will be discarded.
|
||||
//
|
||||
// NodeAdded: The node is added successfully.
|
||||
// NodeNotAdded: The node is not added and is discarded.
|
||||
// NodeExistAndUpdated: The node has existed, and its status has been updated.
|
||||
// NodeExistAndChanged: The node has existed, but the address is inconsistent.
|
||||
// The current node will be discarded.
|
||||
func (s *Server) AddNode(node krpc.Node) int {
|
||||
// For IPv6
|
||||
if isIPv6(node.Addr.IP) {
|
||||
@ -372,13 +339,13 @@ func (s *Server) AddNode(node krpc.Node) int {
|
||||
return NodeNotAdded
|
||||
}
|
||||
|
||||
func (s *Server) addNode(kaddr krpc.Addr, id metainfo.Hash, ro bool) (r int) {
|
||||
func (s *Server) addNode(a *net.UDPAddr, id metainfo.Hash, ro bool) (r int) {
|
||||
if ro { // BEP 43
|
||||
return NodeNotAdded
|
||||
}
|
||||
|
||||
if r = s.AddNode(krpc.NewNode(id, kaddr)); r == NodeExistAndChanged {
|
||||
s.conf.Blacklist.Add(kaddr)
|
||||
if r = s.AddNode(krpc.NewNodeByUDPAddr(id, a)); r == NodeExistAndChanged {
|
||||
s.conf.Blacklist.Add(a.IP.String(), a.Port)
|
||||
}
|
||||
|
||||
return
|
||||
@ -426,15 +393,11 @@ func (s *Server) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
s.handlePacket(raddr, buf[:n])
|
||||
s.handlePacket(raddr.(*net.UDPAddr), buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) isDisabled(raddr krpc.Addr) bool {
|
||||
if len(raddr.IP) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) isDisabled(raddr *net.UDPAddr) bool {
|
||||
if isIPv6(raddr.IP) {
|
||||
if !s.ipv6 {
|
||||
return true
|
||||
@ -446,16 +409,13 @@ func (s *Server) isDisabled(raddr krpc.Addr) bool {
|
||||
}
|
||||
|
||||
// HandlePacket handles the incoming DHT message.
|
||||
func (s *Server) handlePacket(raddr net.Addr, data []byte) {
|
||||
kaddr := s.conf.GetKrpcAddr(raddr)
|
||||
kaddr.Orig = raddr
|
||||
|
||||
if s.isDisabled(kaddr) {
|
||||
func (s *Server) handlePacket(raddr *net.UDPAddr, data []byte) {
|
||||
if s.isDisabled(raddr) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check whether the raddr is in the ip blacklist. If yes, discard it.
|
||||
if s.conf.Blacklist.In(kaddr) {
|
||||
if s.conf.Blacklist.In(raddr.IP.String(), raddr.Port) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -468,50 +428,46 @@ func (s *Server) handlePacket(raddr net.Addr, data []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
// (xgf): Should we use a task pool??
|
||||
go s.handleMessage(kaddr, msg)
|
||||
// TODO: Should we use a task pool??
|
||||
go s.handleMessage(raddr, msg)
|
||||
}
|
||||
|
||||
func (s *Server) handleMessage(kaddr krpc.Addr, m krpc.Message) {
|
||||
if s.conf.HandleInMessage(kaddr.Orig, &m) {
|
||||
func (s *Server) handleMessage(raddr *net.UDPAddr, m krpc.Message) {
|
||||
if !s.conf.HandleInMessage(raddr, &m) {
|
||||
return
|
||||
}
|
||||
|
||||
switch m.Y {
|
||||
case "q":
|
||||
if !m.A.ID.IsZero() {
|
||||
r := s.addNode(kaddr, m.A.ID, m.RO)
|
||||
r := s.addNode(raddr, m.A.ID, m.RO)
|
||||
if r != NodeExistAndChanged && !s.conf.ReadOnly { // BEP 43
|
||||
s.handleQuery(kaddr, m)
|
||||
s.handleQuery(raddr, m)
|
||||
}
|
||||
}
|
||||
|
||||
case "r":
|
||||
if !m.R.ID.IsZero() {
|
||||
if s.addNode(kaddr, m.R.ID, m.RO) == NodeExistAndChanged {
|
||||
if s.addNode(raddr, m.R.ID, m.RO) == NodeExistAndChanged {
|
||||
return
|
||||
}
|
||||
|
||||
if t := s.transactionManager.PopTransaction(m.T, kaddr); t != nil {
|
||||
t.OnResponse(t, kaddr, m)
|
||||
if t := s.transactionManager.PopTransaction(m.T, raddr); t != nil {
|
||||
t.OnResponse(t, raddr, m)
|
||||
}
|
||||
}
|
||||
|
||||
case "e":
|
||||
if t := s.transactionManager.PopTransaction(m.T, kaddr); t != nil {
|
||||
if t := s.transactionManager.PopTransaction(m.T, raddr); t != nil {
|
||||
t.OnError(t, m.E.Code, m.E.Reason)
|
||||
}
|
||||
|
||||
default:
|
||||
s.conf.ErrorLog("unknown dht message type '%s'", m.Y)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
func (s *Server) handleQuery(raddr *net.UDPAddr, m krpc.Message) {
|
||||
switch m.Q {
|
||||
case queryMethodPing:
|
||||
s.reply(raddr, m.T, krpc.ResponseResult{})
|
||||
|
||||
case queryMethodFindNode: // See BEP 32
|
||||
var r krpc.ResponseResult
|
||||
n4 := m.A.ContainsWant(krpc.WantNodes)
|
||||
@ -531,7 +487,6 @@ func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
}
|
||||
}
|
||||
s.reply(raddr, m.T, r)
|
||||
|
||||
case queryMethodGetPeers: // See BEP 32
|
||||
n4 := m.A.ContainsWant(krpc.WantNodes)
|
||||
n6 := m.A.ContainsWant(krpc.WantNodes6)
|
||||
@ -575,39 +530,33 @@ func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
|
||||
r.Token = s.tokenManager.Token(raddr)
|
||||
s.reply(raddr, m.T, r)
|
||||
s.conf.OnSearch(m.A.InfoHash.HexString(), raddr)
|
||||
|
||||
s.conf.OnSearch(m.A.InfoHash.HexString(), raddr.IP, uint16(raddr.Port))
|
||||
case queryMethodAnnouncePeer:
|
||||
if s.tokenManager.Check(raddr, m.A.Token) {
|
||||
return
|
||||
}
|
||||
s.reply(raddr, m.T, krpc.ResponseResult{})
|
||||
s.conf.OnTorrent(m.A.InfoHash.HexString(), raddr.String())
|
||||
|
||||
s.conf.OnTorrent(m.A.InfoHash.HexString(), raddr.IP, m.A.GetPort(raddr.Port))
|
||||
default:
|
||||
s.sendError(raddr, m.T, "unknown query method", krpc.ErrorCodeMethodUnknown)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) send(kaddr krpc.Addr, m krpc.Message) (wrote bool, err error) {
|
||||
// // (xgf): Should we check the ip blacklist??
|
||||
// if s.conf.Blacklist.In(kaddr) {
|
||||
func (s *Server) send(raddr *net.UDPAddr, m krpc.Message) (wrote bool, err error) {
|
||||
// // TODO: Should we check the ip blacklist??
|
||||
// if s.conf.Blacklist.In(raddr.IP.String(), raddr.Port) {
|
||||
// return
|
||||
// }
|
||||
|
||||
m.RO = s.conf.ReadOnly // BEP 43
|
||||
kaddr.Orig = s.conf.GetNetAddr(kaddr)
|
||||
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
if wrote, err = s.conf.HandleOutMessage(kaddr.Orig, &m); !wrote && err == nil {
|
||||
wrote, err = s._send(kaddr, m)
|
||||
if wrote, err = s.conf.HandleOutMessage(raddr, &m); !wrote && err == nil {
|
||||
wrote, err = s._send(raddr, m)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) _send(kaddr krpc.Addr, m krpc.Message) (wrote bool, err error) {
|
||||
func (s *Server) _send(raddr *net.UDPAddr, m krpc.Message) (wrote bool, err error) {
|
||||
if m.T == "" || m.Y == "" {
|
||||
panic(`DHT message "t" or "y" must not be empty`)
|
||||
}
|
||||
@ -618,10 +567,10 @@ func (s *Server) _send(kaddr krpc.Addr, m krpc.Message) (wrote bool, err error)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
n, err := s.conn.WriteTo(buf.Bytes(), kaddr.Orig)
|
||||
n, err := s.conn.WriteTo(buf.Bytes(), raddr)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error writing %d bytes to %v: %s", buf.Len(), kaddr.Orig, err)
|
||||
s.conf.Blacklist.Add(kaddr)
|
||||
err = fmt.Errorf("error writing %d bytes to %s: %s", buf.Len(), raddr, err)
|
||||
s.conf.Blacklist.Add(raddr.IP.String(), 0)
|
||||
return
|
||||
}
|
||||
|
||||
@ -633,13 +582,13 @@ func (s *Server) _send(kaddr krpc.Addr, m krpc.Message) (wrote bool, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) sendError(raddr krpc.Addr, tid, reason string, code int) {
|
||||
func (s *Server) sendError(raddr *net.UDPAddr, tid, reason string, code int) {
|
||||
if _, err := s.send(raddr, krpc.NewErrorMsg(tid, code, reason)); err != nil {
|
||||
s.conf.ErrorLog("error replying to %s: %s", raddr.String(), err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) reply(raddr krpc.Addr, tid string, r krpc.ResponseResult) {
|
||||
func (s *Server) reply(raddr *net.UDPAddr, tid string, r krpc.ResponseResult) {
|
||||
r.ID = s.conf.ID
|
||||
if _, err := s.send(raddr, krpc.NewResponseMsg(tid, r)); err != nil {
|
||||
s.conf.ErrorLog("error replying to %s: %s", raddr.String(), err.Error())
|
||||
@ -670,33 +619,17 @@ func (s *Server) onError(t *transaction, code int, reason string) {
|
||||
}
|
||||
|
||||
func (s *Server) onTimeout(t *transaction) {
|
||||
// (xgf): Should we use a task pool??
|
||||
// TODO: Should we use a task pool??
|
||||
t.Done(Result{Timeout: true})
|
||||
|
||||
var qid string
|
||||
switch t.Query {
|
||||
case "find_node":
|
||||
qid = t.Arg.Target.String()
|
||||
case "get_peers", "announce_peer":
|
||||
qid = t.Arg.InfoHash.String()
|
||||
}
|
||||
s.conf.ErrorLog("transaction '%s' timeout: sid=%s, q=%s, qid=%s, laddr=%s, raddr=%s",
|
||||
t.ID, s.conf.ID, t.Query, qid, s.conn.LocalAddr(), t.Addr.String())
|
||||
s.conf.ErrorLog("transaction '%s' timeout: query=%s, raddr=%s",
|
||||
t.ID, t.Query, t.Addr.String())
|
||||
}
|
||||
|
||||
// Ping sends a PING query to addr, and the callback function cb will be called
|
||||
// when the response or error is returned, or it's timeout.
|
||||
func (s *Server) Ping(addr krpc.Addr, cb ...func(Result)) (err error) {
|
||||
t := newTransaction(s, addr, queryMethodPing, krpc.QueryArg{}, cb...)
|
||||
t.OnResponse = s.onPingResp
|
||||
return s.request(t)
|
||||
}
|
||||
|
||||
func (s *Server) onPingResp(t *transaction, a krpc.Addr, m krpc.Message) {
|
||||
func (s *Server) onPingResp(t *transaction, a *net.UDPAddr, m krpc.Message) {
|
||||
t.Done(Result{})
|
||||
}
|
||||
|
||||
func (s *Server) onGetPeersResp(t *transaction, a krpc.Addr, m krpc.Message) {
|
||||
func (s *Server) onGetPeersResp(t *transaction, a *net.UDPAddr, m krpc.Message) {
|
||||
// Store the response node with the token.
|
||||
if m.R.Token != "" {
|
||||
s.tokenPeerManager.Set(m.R.ID, a, m.R.Token)
|
||||
@ -706,17 +639,15 @@ func (s *Server) onGetPeersResp(t *transaction, a krpc.Addr, m krpc.Message) {
|
||||
if len(m.R.Values) > 0 {
|
||||
t.Done(Result{Peers: m.R.Values})
|
||||
for _, addr := range m.R.Values {
|
||||
s.conf.OnTorrent(t.Arg.InfoHash.HexString(), addr)
|
||||
s.conf.OnTorrent(t.Arg.InfoHash.HexString(), addr.IP, addr.Port)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Terminate the transaction.
|
||||
t.Done(Result{})
|
||||
|
||||
// Search the torrent infohash recursively.
|
||||
t.Depth--
|
||||
if t.Depth < 1 {
|
||||
t.Done(Result{})
|
||||
return
|
||||
}
|
||||
|
||||
@ -749,6 +680,7 @@ func (s *Server) onGetPeersResp(t *transaction, a krpc.Addr, m krpc.Message) {
|
||||
}
|
||||
|
||||
if found || len(nodes) == 0 {
|
||||
t.Done(Result{})
|
||||
return
|
||||
}
|
||||
|
||||
@ -757,9 +689,10 @@ func (s *Server) onGetPeersResp(t *transaction, a krpc.Addr, m krpc.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getPeers(info metainfo.Hash, addr krpc.Addr, depth int, ids metainfo.Hashes, cb ...func(Result)) {
|
||||
func (s *Server) getPeers(info metainfo.Hash, addr metainfo.Address, depth int,
|
||||
ids metainfo.Hashes, cb ...func(Result)) {
|
||||
arg := krpc.QueryArg{InfoHash: info, Wants: s.want}
|
||||
t := newTransaction(s, addr, queryMethodGetPeers, arg, cb...)
|
||||
t := newTransaction(s, addr.UDPAddr(), queryMethodGetPeers, arg, cb...)
|
||||
t.OnResponse = s.onGetPeersResp
|
||||
t.Depth = depth
|
||||
t.Visited = ids
|
||||
@ -768,6 +701,14 @@ func (s *Server) getPeers(info metainfo.Hash, addr krpc.Addr, depth int, ids met
|
||||
}
|
||||
}
|
||||
|
||||
// Ping sends a PING query to addr, and the callback function cb will be called
|
||||
// when the response or error is returned, or it's timeout.
|
||||
func (s *Server) Ping(addr *net.UDPAddr, cb ...func(Result)) (err error) {
|
||||
t := newTransaction(s, addr, queryMethodPing, krpc.QueryArg{}, cb...)
|
||||
t.OnResponse = s.onPingResp
|
||||
return s.request(t)
|
||||
}
|
||||
|
||||
// GetPeers searches the peer storing the torrent by the infohash of the torrent,
|
||||
// which will search it recursively until some peers are returned or it reaches
|
||||
// the maximun depth, that's, ServerConfig.SearchDepth.
|
||||
@ -802,6 +743,7 @@ func (s *Server) GetPeers(infohash metainfo.Hash, cb ...func(Result)) {
|
||||
for _, node := range nodes {
|
||||
s.getPeers(infohash, node.Addr, s.conf.SearchDepth, ids, cb...)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// AnnouncePeer announces the torrent infohash to the K closest nodes,
|
||||
@ -821,15 +763,16 @@ func (s *Server) AnnouncePeer(infohash metainfo.Hash, port uint16, impliedPort b
|
||||
|
||||
sentNodes := make([]krpc.Node, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
token := s.tokenPeerManager.Get(infohash, node.Addr)
|
||||
addr := node.Addr.UDPAddr()
|
||||
token := s.tokenPeerManager.Get(infohash, addr)
|
||||
if token == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
arg := krpc.QueryArg{ImpliedPort: impliedPort, InfoHash: infohash, Port: port, Token: token}
|
||||
t := newTransaction(s, node.Addr, queryMethodAnnouncePeer, arg)
|
||||
t := newTransaction(s, addr, queryMethodAnnouncePeer, arg)
|
||||
if err := s.request(t); err != nil {
|
||||
s.conf.ErrorLog("fail to send query message to '%s': %s", node.Addr.String(), err)
|
||||
s.conf.ErrorLog("fail to send query message to '%s': %s", addr.String(), err)
|
||||
} else {
|
||||
sentNodes = append(sentNodes, node)
|
||||
}
|
||||
@ -841,7 +784,7 @@ func (s *Server) AnnouncePeer(infohash metainfo.Hash, port uint16, impliedPort b
|
||||
// FindNode sends the "find_node" query to the addr to find the target node.
|
||||
//
|
||||
// Notice: In general, it's used to bootstrap the routing table.
|
||||
func (s *Server) FindNode(addr krpc.Addr, target metainfo.Hash) error {
|
||||
func (s *Server) FindNode(addr *net.UDPAddr, target metainfo.Hash) error {
|
||||
if target.IsZero() {
|
||||
panic("the target is ZERO")
|
||||
}
|
||||
@ -849,7 +792,8 @@ func (s *Server) FindNode(addr krpc.Addr, target metainfo.Hash) error {
|
||||
return s.findNode(target, addr, s.conf.SearchDepth, nil)
|
||||
}
|
||||
|
||||
func (s *Server) findNode(target metainfo.Hash, addr krpc.Addr, depth int, ids metainfo.Hashes) error {
|
||||
func (s *Server) findNode(target metainfo.Hash, addr *net.UDPAddr, depth int,
|
||||
ids metainfo.Hashes) error {
|
||||
arg := krpc.QueryArg{Target: target, Wants: s.want}
|
||||
t := newTransaction(s, addr, queryMethodFindNode, arg)
|
||||
t.OnResponse = s.onFindNodeResp
|
||||
@ -857,9 +801,7 @@ func (s *Server) findNode(target metainfo.Hash, addr krpc.Addr, depth int, ids m
|
||||
return s.request(t)
|
||||
}
|
||||
|
||||
func (s *Server) onFindNodeResp(t *transaction, _ krpc.Addr, m krpc.Message) {
|
||||
t.Done(Result{})
|
||||
|
||||
func (s *Server) onFindNodeResp(t *transaction, a *net.UDPAddr, m krpc.Message) {
|
||||
// Search the target node recursively.
|
||||
t.Depth--
|
||||
if t.Depth < 1 {
|
||||
@ -899,7 +841,7 @@ func (s *Server) onFindNodeResp(t *transaction, _ krpc.Addr, m krpc.Message) {
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
err := s.findNode(t.Arg.Target, node.Addr, t.Depth, ids)
|
||||
err := s.findNode(t.Arg.Target, node.Addr.UDPAddr(), t.Depth, ids)
|
||||
if err != nil {
|
||||
s.conf.ErrorLog(`fail to send "find_node" query to '%s': %s`,
|
||||
node.Addr.String(), err)
|
||||
@ -907,14 +849,14 @@ func (s *Server) onFindNodeResp(t *transaction, _ krpc.Addr, m krpc.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
func isIPv6(ip net.IP) bool {
|
||||
func isIPv6(ip net.Addr) bool {
|
||||
if ip.To4() == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ipIsZero(ip net.IP) bool {
|
||||
func ipIsZero(ip net.Addr) bool {
|
||||
for _, b := range ip {
|
||||
if b != 0 {
|
||||
return false
|
||||
|
@ -17,33 +17,39 @@ package dht
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
type testPeerManager struct {
|
||||
lock sync.RWMutex
|
||||
peers map[metainfo.Hash][]string
|
||||
peers map[metainfo.Hash][]metainfo.Address
|
||||
}
|
||||
|
||||
func newTestPeerManager() *testPeerManager {
|
||||
return &testPeerManager{peers: make(map[metainfo.Hash][]string)}
|
||||
return &testPeerManager{peers: make(map[metainfo.Hash][]metainfo.Address)}
|
||||
}
|
||||
|
||||
func (pm *testPeerManager) AddPeer(infohash metainfo.Hash, addr string) {
|
||||
func (pm *testPeerManager) AddPeer(infohash metainfo.Hash, addr metainfo.Address) {
|
||||
pm.lock.Lock()
|
||||
defer pm.lock.Unlock()
|
||||
|
||||
if !helper.ContainsString(pm.peers[infohash], addr) {
|
||||
var exist bool
|
||||
for _, orig := range pm.peers[infohash] {
|
||||
if orig.Equal(addr) {
|
||||
exist = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exist {
|
||||
pm.peers[infohash] = append(pm.peers[infohash], addr)
|
||||
}
|
||||
pm.lock.Unlock()
|
||||
}
|
||||
|
||||
func (pm *testPeerManager) GetPeers(infohash metainfo.Hash, maxnum int, ipv6 bool) (addrs []string) {
|
||||
func (pm *testPeerManager) GetPeers(infohash metainfo.Hash, maxnum int,
|
||||
ipv6 bool) (addrs []metainfo.Address) {
|
||||
// We only supports IPv4, so ignore the ipv6 argument.
|
||||
pm.lock.RLock()
|
||||
_addrs := pm.peers[infohash]
|
||||
@ -57,11 +63,13 @@ func (pm *testPeerManager) GetPeers(infohash metainfo.Hash, maxnum int, ipv6 boo
|
||||
return
|
||||
}
|
||||
|
||||
func onSearch(infohash string, addr krpc.Addr) {
|
||||
fmt.Printf("%s is searching %s\n", addr.String(), infohash)
|
||||
func onSearch(infohash string, ip net.Addr, port uint16) {
|
||||
addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s is searching %s\n", addr, infohash)
|
||||
}
|
||||
|
||||
func onTorrent(infohash string, addr string) {
|
||||
func onTorrent(infohash string, ip net.Addr, port uint16) {
|
||||
addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s has downloaded %s\n", addr, infohash)
|
||||
}
|
||||
|
||||
@ -69,7 +77,7 @@ func newDHTServer(id metainfo.Hash, addr string, pm PeerManager) (s *Server, err
|
||||
conn, err := net.ListenPacket("udp", addr)
|
||||
if err == nil {
|
||||
c := Config{ID: id, PeerManager: pm, OnSearch: onSearch, OnTorrent: onTorrent}
|
||||
s = NewServer(conn, &c)
|
||||
s = NewServer(conn, c)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -129,10 +137,10 @@ func ExampleServer() {
|
||||
|
||||
server1.GetPeers(infohash, func(r Result) {
|
||||
if len(r.Peers) == 0 {
|
||||
fmt.Printf("%s: no peers for %s\n", r.Addr.String(), infohash)
|
||||
fmt.Printf("no peers for %s\n", infohash)
|
||||
} else {
|
||||
for _, peer := range r.Peers {
|
||||
fmt.Printf("%s: %s\n", infohash, peer)
|
||||
fmt.Printf("%s: %s\n", infohash, peer.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -141,16 +149,16 @@ func ExampleServer() {
|
||||
time.Sleep(time.Second * 2)
|
||||
|
||||
// Add the peer to let the DHT server1 has the peer.
|
||||
pm.AddPeer(infohash, "127.0.0.1:9001")
|
||||
pm.AddPeer(infohash, metainfo.NewAddress(net.ParseIP("127.0.0.1"), 9001))
|
||||
|
||||
// Search the torrent infohash again, but from DHT server2,
|
||||
// which will search the DHT server1 recursively.
|
||||
server2.GetPeers(infohash, func(r Result) {
|
||||
if len(r.Peers) == 0 {
|
||||
fmt.Printf("%s: no peers for %s\n", r.Addr.String(), infohash)
|
||||
fmt.Printf("no peers for %s\n", infohash)
|
||||
} else {
|
||||
for _, peer := range r.Peers {
|
||||
fmt.Printf("%s: %s\n", infohash, peer)
|
||||
fmt.Printf("%s: %s\n", infohash, peer.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -164,11 +172,11 @@ func ExampleServer() {
|
||||
// Server3: 2
|
||||
// 127.0.0.1:9001 is searching 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9001 is searching 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9002: no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9003: no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9002 is searching 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9002 is searching 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9003: no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 0102030405060708090a0b0c0d0e0f1011121314: 127.0.0.1:9001
|
||||
// 127.0.0.1:9001 has downloaded 0102030405060708090a0b0c0d0e0f1011121314
|
||||
}
|
||||
|
@ -15,24 +15,23 @@
|
||||
package dht
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// PeerManager is used to manage the peers.
|
||||
type PeerManager interface {
|
||||
// If ipv6 is true, only return ipv6 addresses. Or return ipv4 addresses.
|
||||
GetPeers(infohash metainfo.Hash, maxnum int, ipv6 bool) []string
|
||||
GetPeers(infohash metainfo.Hash, maxnum int, ipv6 bool) []metainfo.Address
|
||||
}
|
||||
|
||||
var _ PeerManager = new(tokenPeerManager)
|
||||
|
||||
type peer struct {
|
||||
ID metainfo.Hash
|
||||
Addr krpc.Addr
|
||||
IP net.Addr
|
||||
Port uint16
|
||||
Token string
|
||||
Time time.Time
|
||||
}
|
||||
@ -76,7 +75,7 @@ func (tpm *tokenPeerManager) Start(interval time.Duration) {
|
||||
}
|
||||
}
|
||||
|
||||
func (tpm *tokenPeerManager) Set(id metainfo.Hash, addr krpc.Addr, token string) {
|
||||
func (tpm *tokenPeerManager) Set(id metainfo.Hash, addr *net.UDPAddr, token string) {
|
||||
addrkey := addr.String()
|
||||
tpm.lock.Lock()
|
||||
peers, ok := tpm.peers[id]
|
||||
@ -84,11 +83,17 @@ func (tpm *tokenPeerManager) Set(id metainfo.Hash, addr krpc.Addr, token string)
|
||||
peers = make(map[string]peer, 4)
|
||||
tpm.peers[id] = peers
|
||||
}
|
||||
peers[addrkey] = peer{ID: id, Addr: addr, Token: token, Time: time.Now()}
|
||||
peers[addrkey] = peer{
|
||||
ID: id,
|
||||
IP: addr.IP,
|
||||
Port: uint16(addr.Port),
|
||||
Token: token,
|
||||
Time: time.Now(),
|
||||
}
|
||||
tpm.lock.Unlock()
|
||||
}
|
||||
|
||||
func (tpm *tokenPeerManager) Get(id metainfo.Hash, addr krpc.Addr) (token string) {
|
||||
func (tpm *tokenPeerManager) Get(id metainfo.Hash, addr *net.UDPAddr) (token string) {
|
||||
addrkey := addr.String()
|
||||
tpm.lock.RLock()
|
||||
if peers, ok := tpm.peers[id]; ok {
|
||||
@ -108,8 +113,9 @@ func (tpm *tokenPeerManager) Stop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (tpm *tokenPeerManager) GetPeers(infohash metainfo.Hash, maxnum int, ipv6 bool) (addrs []string) {
|
||||
addrs = make([]string, 0, maxnum)
|
||||
func (tpm *tokenPeerManager) GetPeers(infohash metainfo.Hash, maxnum int,
|
||||
ipv6 bool) (addrs []metainfo.Address) {
|
||||
addrs = make([]metainfo.Address, 0, maxnum)
|
||||
tpm.lock.RLock()
|
||||
if peers, ok := tpm.peers[infohash]; ok {
|
||||
for _, peer := range peers {
|
||||
@ -118,13 +124,13 @@ func (tpm *tokenPeerManager) GetPeers(infohash metainfo.Hash, maxnum int, ipv6 b
|
||||
}
|
||||
|
||||
if ipv6 { // For IPv6
|
||||
if isIPv6(peer.Addr.IP) {
|
||||
if isIPv6(peer.IP) {
|
||||
maxnum--
|
||||
addrs = append(addrs, peer.Addr.String())
|
||||
addrs = append(addrs, metainfo.NewAddress(peer.IP, peer.Port))
|
||||
}
|
||||
} else if !isIPv6(peer.Addr.IP) { // For IPv4
|
||||
} else if !isIPv6(peer.IP) { // For IPv4
|
||||
maxnum--
|
||||
addrs = append(addrs, peer.Addr.String())
|
||||
addrs = append(addrs, metainfo.NewAddress(peer.IP, peer.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/krpc"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
const bktlen = 160
|
||||
@ -155,12 +155,12 @@ func (rt *routingTable) Stop() {
|
||||
// AddNode adds the node into the routing table.
|
||||
//
|
||||
// The returned value:
|
||||
// NodeAdded: The node is added successfully.
|
||||
// NodeNotAdded: The node is not added and is discarded.
|
||||
// NodeExistAndUpdated: The node has existed, and its status has been updated.
|
||||
// NodeExistAndChanged: The node has existed, but the address is inconsistent.
|
||||
// The current node will be discarded.
|
||||
//
|
||||
// NodeAdded: The node is added successfully.
|
||||
// NodeNotAdded: The node is not added and is discarded.
|
||||
// NodeExistAndUpdated: The node has existed, and its status has been updated.
|
||||
// NodeExistAndChanged: The node has existed, but the address is inconsistent.
|
||||
// The current node will be discarded.
|
||||
func (rt *routingTable) AddNode(n krpc.Node) (r int) {
|
||||
if n.ID == rt.root { // Don't add itself.
|
||||
return NodeNotAdded
|
||||
@ -264,7 +264,7 @@ func (b *bucket) AddNode(n krpc.Node, now time.Time) (status int) {
|
||||
return
|
||||
}
|
||||
|
||||
// // (xgf): Should we replace the old one??
|
||||
// // TODO: Should we replace the old one??
|
||||
// b.UpdateLastChangedTime(now)
|
||||
// copy(b.Nodes[i:], b.Nodes[i+1:])
|
||||
// b.Nodes[len(b.Nodes)-1] = newWrappedNode(b, n, now)
|
||||
@ -314,7 +314,7 @@ func (b *bucket) CheckAllNodes(now time.Time) {
|
||||
case nodeStatusGood:
|
||||
case nodeStatusDubious:
|
||||
// Try to send the PING query to the dubious node to check whether it is alive.
|
||||
if err := b.table.s.Ping(node.Node.Addr); err != nil {
|
||||
if err := b.table.s.Ping(node.Node.Addr.UDPAddr()); err != nil {
|
||||
b.table.s.conf.ErrorLog("fail to ping '%s': %s", node.Node.String(), err)
|
||||
}
|
||||
case nodeStatusBad:
|
||||
@ -332,6 +332,10 @@ func (b *bucket) CheckAllNodes(now time.Time) {
|
||||
}
|
||||
|
||||
func bucketid(ownerid, nid metainfo.Hash) int {
|
||||
if ownerid == nid {
|
||||
return 0
|
||||
}
|
||||
|
||||
var i int
|
||||
var bite byte
|
||||
var bitDiff int
|
||||
@ -367,7 +371,7 @@ func bucketid(ownerid, nid metainfo.Hash) int {
|
||||
}
|
||||
|
||||
calc:
|
||||
return i*8 + (7 - bitDiff)
|
||||
return i*8 + (8 - bitDiff)
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
@ -17,8 +17,8 @@ package dht
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/krpc"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// RoutingTableNode represents the node with last changed time in the routing table.
|
||||
|
@ -15,16 +15,16 @@
|
||||
package dht
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/bt/utils"
|
||||
)
|
||||
|
||||
// TokenManager is used to manage and validate the token.
|
||||
//
|
||||
// (xgf): Should we allocate the different token for each node??
|
||||
// TODO: Should we allocate the different token for each node??
|
||||
type tokenManager struct {
|
||||
lock sync.RWMutex
|
||||
last string
|
||||
@ -34,12 +34,12 @@ type tokenManager struct {
|
||||
}
|
||||
|
||||
func newTokenManager() *tokenManager {
|
||||
token := helper.RandomString(8)
|
||||
token := utils.RandomString(8)
|
||||
return &tokenManager{last: token, new: token, exit: make(chan struct{})}
|
||||
}
|
||||
|
||||
func (tm *tokenManager) updateToken() {
|
||||
token := helper.RandomString(8)
|
||||
token := utils.RandomString(8)
|
||||
tm.lock.Lock()
|
||||
tm.last, tm.new = tm.new, token
|
||||
tm.lock.Unlock()
|
||||
@ -83,7 +83,7 @@ func (tm *tokenManager) Stop() {
|
||||
}
|
||||
|
||||
// Token allocates a token for a node addr and returns the token.
|
||||
func (tm *tokenManager) Token(addr krpc.Addr) (token string) {
|
||||
func (tm *tokenManager) Token(addr *net.UDPAddr) (token string) {
|
||||
addrs := addr.String()
|
||||
tm.lock.RLock()
|
||||
token = tm.new
|
||||
@ -94,7 +94,7 @@ func (tm *tokenManager) Token(addr krpc.Addr) (token string) {
|
||||
|
||||
// Check checks whether the token associated with the node addr is valid,
|
||||
// that's, it's not expired.
|
||||
func (tm *tokenManager) Check(addr krpc.Addr, token string) (ok bool) {
|
||||
func (tm *tokenManager) Check(addr *net.UDPAddr, token string) (ok bool) {
|
||||
tm.lock.RLock()
|
||||
last, new := tm.last, tm.new
|
||||
tm.lock.RUnlock()
|
||||
|
@ -15,20 +15,21 @@
|
||||
package dht
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/krpc"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
type transaction struct {
|
||||
ID string
|
||||
Query string
|
||||
Arg krpc.QueryArg
|
||||
Addr krpc.Addr
|
||||
Addr *net.UDPAddr
|
||||
Time time.Time
|
||||
Depth int
|
||||
|
||||
@ -36,7 +37,7 @@ type transaction struct {
|
||||
Callback func(Result)
|
||||
OnError func(t *transaction, code int, reason string)
|
||||
OnTimeout func(t *transaction)
|
||||
OnResponse func(t *transaction, radd krpc.Addr, msg krpc.Message)
|
||||
OnResponse func(t *transaction, radd *net.UDPAddr, msg krpc.Message)
|
||||
}
|
||||
|
||||
func (t *transaction) Done(r Result) {
|
||||
@ -46,8 +47,8 @@ func (t *transaction) Done(r Result) {
|
||||
}
|
||||
}
|
||||
|
||||
func noopResponse(*transaction, krpc.Addr, krpc.Message) {}
|
||||
func newTransaction(s *Server, a krpc.Addr, q string, qa krpc.QueryArg,
|
||||
func noopResponse(*transaction, *net.UDPAddr, krpc.Message) {}
|
||||
func newTransaction(s *Server, a *net.UDPAddr, q string, qa krpc.QueryArg,
|
||||
callback ...func(Result)) *transaction {
|
||||
var cb func(Result)
|
||||
if len(callback) > 0 {
|
||||
@ -140,7 +141,7 @@ func (tm *transactionManager) DeleteTransaction(t *transaction) {
|
||||
// and the peer address.
|
||||
//
|
||||
// Return nil if there is no the transaction.
|
||||
func (tm *transactionManager) PopTransaction(tid string, addr krpc.Addr) (t *transaction) {
|
||||
func (tm *transactionManager) PopTransaction(tid string, addr *net.UDPAddr) (t *transaction) {
|
||||
key := transactionkey{id: tid, addr: addr.String()}
|
||||
tm.lock.Lock()
|
||||
if t = tm.trans[key]; t != nil {
|
||||
|
@ -14,7 +14,10 @@
|
||||
|
||||
package downloader
|
||||
|
||||
import pp "github.com/xgfone/go-bt/peerprotocol"
|
||||
import (
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
pp "github.com/xgfone/bt/peerprotocol"
|
||||
)
|
||||
|
||||
// BlockDownloadHandler is used to downloads the files in the torrent file.
|
||||
type BlockDownloadHandler struct {
|
||||
@ -22,45 +25,51 @@ type BlockDownloadHandler struct {
|
||||
pp.NoopBep3Handler
|
||||
pp.NoopBep6Handler
|
||||
|
||||
OnBlock func(index, offset uint32, b []byte) error
|
||||
ReqBlock func(c *pp.PeerConn) error
|
||||
PieceNum int
|
||||
Info metainfo.Info // Required
|
||||
OnBlock func(index, offset uint32, b []byte) error // Required
|
||||
RequestBlock func(c *pp.PeerConn) error // Required
|
||||
}
|
||||
|
||||
// NewBlockDownloadHandler returns a new BlockDownloadHandler.
|
||||
func NewBlockDownloadHandler(pieceNum int, reqBlock func(c *pp.PeerConn) error,
|
||||
onBlock func(pieceIndex, pieceOffset uint32, b []byte) error) BlockDownloadHandler {
|
||||
func NewBlockDownloadHandler(info metainfo.Info,
|
||||
onBlock func(pieceIndex, pieceOffset uint32, b []byte) error,
|
||||
requestBlock func(c *pp.PeerConn) error) BlockDownloadHandler {
|
||||
return BlockDownloadHandler{
|
||||
OnBlock: onBlock,
|
||||
ReqBlock: reqBlock,
|
||||
PieceNum: pieceNum,
|
||||
Info: info,
|
||||
OnBlock: onBlock,
|
||||
RequestBlock: requestBlock,
|
||||
}
|
||||
}
|
||||
|
||||
// OnHandShake implements the interface Handler#OnHandShake.
|
||||
//
|
||||
// Notice: it uses the field Data to store the inner data, you mustn't override
|
||||
// it.
|
||||
func (fd BlockDownloadHandler) OnHandShake(c *pp.PeerConn) (err error) {
|
||||
if err = c.SetUnchoked(); err == nil {
|
||||
err = c.SetInterested()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// BEP 3
|
||||
|
||||
func (fd BlockDownloadHandler) request(pc *pp.PeerConn) (err error) {
|
||||
if fd.ReqBlock == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if pc.PeerChoked {
|
||||
err = pp.ErrChoked
|
||||
} else {
|
||||
err = fd.ReqBlock(pc)
|
||||
err = fd.RequestBlock(pc)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Piece implements the interface Bep3Handler#Piece.
|
||||
func (fd BlockDownloadHandler) Piece(c *pp.PeerConn, i, b uint32, p []byte) error {
|
||||
if fd.OnBlock != nil {
|
||||
if err := fd.OnBlock(i, b, p); err != nil {
|
||||
return err
|
||||
}
|
||||
func (fd BlockDownloadHandler) Piece(c *pp.PeerConn, i, b uint32, p []byte) (err error) {
|
||||
if err = fd.OnBlock(i, b, p); err == nil {
|
||||
err = fd.request(c)
|
||||
}
|
||||
return fd.request(c)
|
||||
return
|
||||
}
|
||||
|
||||
// Unchoke implements the interface Bep3Handler#Unchoke.
|
||||
@ -79,9 +88,7 @@ func (fd BlockDownloadHandler) Have(pc *pp.PeerConn, index uint32) (err error) {
|
||||
|
||||
// HaveAll implements the interface Bep6Handler#HaveAll.
|
||||
func (fd BlockDownloadHandler) HaveAll(pc *pp.PeerConn) (err error) {
|
||||
if fd.PieceNum > 0 {
|
||||
pc.BitField = pp.NewBitField(fd.PieceNum, true)
|
||||
}
|
||||
pc.BitField = pp.NewBitField(fd.Info.CountPieces(), true)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -26,8 +26,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
pp "github.com/xgfone/go-bt/peerprotocol"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
pp "github.com/xgfone/bt/peerprotocol"
|
||||
)
|
||||
|
||||
// BlockSize is the size of a block of the piece.
|
||||
@ -35,9 +35,9 @@ const BlockSize = 16384 // 16KiB.
|
||||
|
||||
// Request is used to send a download request.
|
||||
type request struct {
|
||||
Host string
|
||||
Port uint16
|
||||
// PeerID metainfo.Hash
|
||||
Host string
|
||||
Port uint16
|
||||
PeerID metainfo.Hash
|
||||
InfoHash metainfo.Hash
|
||||
}
|
||||
|
||||
@ -57,11 +57,6 @@ type TorrentDownloaderConfig struct {
|
||||
// The default is a random id.
|
||||
ID metainfo.Hash
|
||||
|
||||
// The size of a block of the piece.
|
||||
//
|
||||
// Default: 16384
|
||||
BlockSize uint64
|
||||
|
||||
// WorkerNum is the number of the worker downloading the torrent concurrently.
|
||||
//
|
||||
// The default is 128.
|
||||
@ -76,14 +71,11 @@ type TorrentDownloaderConfig struct {
|
||||
ErrorLog func(format string, args ...interface{})
|
||||
}
|
||||
|
||||
func (c *TorrentDownloaderConfig) set(conf *TorrentDownloaderConfig) {
|
||||
if conf != nil {
|
||||
*c = *conf
|
||||
func (c *TorrentDownloaderConfig) set(conf ...TorrentDownloaderConfig) {
|
||||
if len(conf) > 0 {
|
||||
*c = conf[0]
|
||||
}
|
||||
|
||||
if c.BlockSize <= 0 {
|
||||
c.BlockSize = BlockSize
|
||||
}
|
||||
if c.WorkerNum <= 0 {
|
||||
c.WorkerNum = 128
|
||||
}
|
||||
@ -110,15 +102,16 @@ type TorrentDownloader struct {
|
||||
// NewTorrentDownloader returns a new TorrentDownloader.
|
||||
//
|
||||
// If id is ZERO, it is reset to a random id. workerNum is 128 by default.
|
||||
func NewTorrentDownloader(c *TorrentDownloaderConfig) *TorrentDownloader {
|
||||
func NewTorrentDownloader(c ...TorrentDownloaderConfig) *TorrentDownloader {
|
||||
var conf TorrentDownloaderConfig
|
||||
conf.set(c)
|
||||
conf.set(c...)
|
||||
|
||||
d := &TorrentDownloader{
|
||||
conf: conf,
|
||||
exit: make(chan struct{}),
|
||||
requests: make(chan request, conf.WorkerNum),
|
||||
responses: make(chan TorrentResponse, 1024),
|
||||
|
||||
ehmsg: pp.ExtendedHandshakeMsg{
|
||||
M: map[string]uint8{pp.ExtendedMessageNameMetadata: 1},
|
||||
},
|
||||
@ -137,9 +130,6 @@ func NewTorrentDownloader(c *TorrentDownloaderConfig) *TorrentDownloader {
|
||||
// Notice: the remote peer must support the "ut_metadata" extenstion.
|
||||
// Or downloading fails.
|
||||
func (d *TorrentDownloader) Request(host string, port uint16, infohash metainfo.Hash) {
|
||||
if infohash.IsZero() {
|
||||
panic("infohash is ZERO")
|
||||
}
|
||||
d.requests <- request{Host: host, Port: port, InfoHash: infohash}
|
||||
}
|
||||
|
||||
@ -178,7 +168,7 @@ func (d *TorrentDownloader) worker() {
|
||||
case <-d.exit:
|
||||
return
|
||||
case r := <-d.requests:
|
||||
if err := d.download(r.Host, r.Port, r.InfoHash); err != nil {
|
||||
if err := d.download(r.Host, r.Port, r.PeerID, r.InfoHash); err != nil {
|
||||
d.conf.ErrorLog("fail to download the torrent '%s': %s",
|
||||
r.InfoHash.HexString(), err)
|
||||
}
|
||||
@ -186,7 +176,8 @@ func (d *TorrentDownloader) worker() {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *TorrentDownloader) download(host string, port uint16, infohash metainfo.Hash) (err error) {
|
||||
func (d *TorrentDownloader) download(host string, port uint16,
|
||||
peerID, infohash metainfo.Hash) (err error) {
|
||||
addr := net.JoinHostPort(host, strconv.FormatUint(uint64(port), 10))
|
||||
conn, err := pp.NewPeerConnByDial(addr, d.conf.ID, infohash, d.conf.DialTimeout)
|
||||
if err != nil {
|
||||
@ -199,6 +190,8 @@ func (d *TorrentDownloader) download(host string, port uint16, infohash metainfo
|
||||
return
|
||||
} else if !conn.PeerExtBits.IsSupportExtended() {
|
||||
return fmt.Errorf("the remote peer '%s' does not support Extended", addr)
|
||||
} else if !peerID.IsZero() && peerID != conn.PeerID {
|
||||
return fmt.Errorf("inconsistent peer id '%s'", conn.PeerID.HexString())
|
||||
}
|
||||
|
||||
if err = conn.SendExtHandshakeMsg(d.ehmsg); err != nil {
|
||||
@ -207,7 +200,7 @@ func (d *TorrentDownloader) download(host string, port uint16, infohash metainfo
|
||||
|
||||
var pieces [][]byte
|
||||
var piecesNum int
|
||||
var metadataSize uint64
|
||||
var metadataSize int
|
||||
var utmetadataID uint8
|
||||
var msg pp.Message
|
||||
|
||||
@ -254,14 +247,13 @@ func (d *TorrentDownloader) download(host string, port uint16, infohash metainfo
|
||||
}
|
||||
|
||||
metadataSize = ehmsg.MetadataSize
|
||||
piecesNum = int(metadataSize / d.conf.BlockSize)
|
||||
if metadataSize%d.conf.BlockSize != 0 {
|
||||
piecesNum = metadataSize / BlockSize
|
||||
if metadataSize%BlockSize != 0 {
|
||||
piecesNum++
|
||||
}
|
||||
|
||||
pieces = make([][]byte, piecesNum)
|
||||
go d.requestPieces(conn, utmetadataID, piecesNum)
|
||||
|
||||
case 1:
|
||||
if pieces == nil {
|
||||
return
|
||||
@ -277,8 +269,8 @@ func (d *TorrentDownloader) download(host string, port uint16, infohash metainfo
|
||||
}
|
||||
|
||||
pieceLen := len(utmsg.Data)
|
||||
if (utmsg.Piece != piecesNum-1 && pieceLen != int(d.conf.BlockSize)) ||
|
||||
(utmsg.Piece == piecesNum-1 && pieceLen != int(metadataSize%d.conf.BlockSize)) {
|
||||
if (utmsg.Piece != piecesNum-1 && pieceLen != BlockSize) ||
|
||||
(utmsg.Piece == piecesNum-1 && pieceLen != metadataSize%BlockSize) {
|
||||
return
|
||||
}
|
||||
pieces[utmsg.Piece] = utmsg.Data
|
||||
|
@ -1,124 +0,0 @@
|
||||
// Copyright 2023 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package downloader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/go-bt/peerprotocol"
|
||||
pp "github.com/xgfone/go-bt/peerprotocol"
|
||||
)
|
||||
|
||||
type bep10Handler struct {
|
||||
peerprotocol.NoopHandler // For implementing peerprotocol.Handler.
|
||||
|
||||
infodata string
|
||||
}
|
||||
|
||||
func (h bep10Handler) OnExtHandShake(c *peerprotocol.PeerConn) error {
|
||||
if _, ok := c.ExtendedHandshakeMsg.M[peerprotocol.ExtendedMessageNameMetadata]; !ok {
|
||||
return errors.New("missing the extension 'ut_metadata'")
|
||||
}
|
||||
|
||||
return c.SendExtHandshakeMsg(peerprotocol.ExtendedHandshakeMsg{
|
||||
M: map[string]uint8{pp.ExtendedMessageNameMetadata: 2},
|
||||
MetadataSize: uint64(len(h.infodata)),
|
||||
})
|
||||
}
|
||||
|
||||
func (h bep10Handler) OnPayload(c *peerprotocol.PeerConn, extid uint8, extdata []byte) error {
|
||||
if extid != 2 {
|
||||
return fmt.Errorf("unknown extension id %d", extid)
|
||||
}
|
||||
|
||||
var reqmsg peerprotocol.UtMetadataExtendedMsg
|
||||
if err := reqmsg.DecodeFromPayload(extdata); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if reqmsg.MsgType != peerprotocol.UtMetadataExtendedMsgTypeRequest {
|
||||
return errors.New("unsupported ut_metadata extension type")
|
||||
}
|
||||
|
||||
startIndex := reqmsg.Piece * BlockSize
|
||||
endIndex := startIndex + BlockSize
|
||||
if totalSize := len(h.infodata); totalSize < endIndex {
|
||||
endIndex = totalSize
|
||||
}
|
||||
|
||||
respmsg := peerprotocol.UtMetadataExtendedMsg{
|
||||
MsgType: peerprotocol.UtMetadataExtendedMsgTypeData,
|
||||
Piece: reqmsg.Piece,
|
||||
Data: []byte(h.infodata[startIndex:endIndex]),
|
||||
}
|
||||
|
||||
data, err := respmsg.EncodeToBytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
peerextid := c.ExtendedHandshakeMsg.M[peerprotocol.ExtendedMessageNameMetadata]
|
||||
return c.SendExtMsg(peerextid, data)
|
||||
}
|
||||
|
||||
func ExampleTorrentDownloader() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
handler := bep10Handler{infodata: "1234567890"}
|
||||
infohash := metainfo.NewHashFromString(handler.infodata)
|
||||
|
||||
// Start the torrent server.
|
||||
var serverConfig peerprotocol.Config
|
||||
serverConfig.ExtBits.Set(peerprotocol.ExtensionBitExtended)
|
||||
server, err := peerprotocol.NewServerByListen("tcp", "127.0.0.1:9010", metainfo.NewRandomHash(), handler, &serverConfig)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer server.Close()
|
||||
|
||||
go server.Run() // Start the torrent server.
|
||||
time.Sleep(time.Millisecond * 100) // Wait that the torrent server finishes to start.
|
||||
|
||||
// Start the torrent downloader.
|
||||
downloaderConfig := &TorrentDownloaderConfig{WorkerNum: 3, DialTimeout: time.Second}
|
||||
downloader := NewTorrentDownloader(downloaderConfig)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case result := <-downloader.Response():
|
||||
fmt.Println(string(result.InfoBytes))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Start to download the torrent.
|
||||
downloader.Request("127.0.0.1", 9010, infohash)
|
||||
|
||||
// Wait to finish the test.
|
||||
time.Sleep(time.Second)
|
||||
cancel()
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
|
||||
// Output:
|
||||
// 1234567890
|
||||
}
|
4
go.mod
4
go.mod
@ -1,3 +1,5 @@
|
||||
module github.com/xgfone/go-bt
|
||||
module github.com/xgfone/bt
|
||||
|
||||
go 1.11
|
||||
|
||||
require github.com/eyedeekay/sam3 v0.32.32 // indirect
|
||||
|
312
krpc/addr.go
312
krpc/addr.go
@ -1,312 +0,0 @@
|
||||
// Copyright 2023 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package krpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
// Addr represents an address based on ip and port,
|
||||
// which implements "Compact IP-address/port info".
|
||||
//
|
||||
// See http://bittorrent.org/beps/bep_0005.html.
|
||||
type Addr struct {
|
||||
IP net.IP // For IPv4, its length must be 4.
|
||||
Port uint16
|
||||
|
||||
// The original network address, which is only used by the DHT server.
|
||||
Orig net.Addr
|
||||
}
|
||||
|
||||
// ParseAddrs parses the address from the string s with the format "IP:PORT".
|
||||
func ParseAddrs(s string) (addrs []Addr, err error) {
|
||||
_ip, _port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
port, err := strconv.ParseUint(_port, 10, 16)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ip := net.ParseIP(_ip)
|
||||
if ip != nil {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
return []Addr{NewAddr(ip, uint16(port))}, nil
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(_ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addrs = make([]Addr, len(ips))
|
||||
for i, ip := range ips {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
addrs[i] = NewAddr(ip, uint16(port))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewAddr returns a new Addr with ip and port.
|
||||
func NewAddr(ip net.IP, port uint16) Addr {
|
||||
return Addr{IP: ip, Port: port}
|
||||
}
|
||||
|
||||
// NewAddrFromUDPAddr converts *net.UDPAddr to a new Addr.
|
||||
func NewAddrFromUDPAddr(ua *net.UDPAddr) Addr {
|
||||
return Addr{IP: ua.IP, Port: uint16(ua.Port), Orig: ua}
|
||||
}
|
||||
|
||||
// Valid reports whether the addr is valid.
|
||||
func (a Addr) Valid() bool {
|
||||
return len(a.IP) > 0 && a.Port > 0
|
||||
}
|
||||
|
||||
// Equal reports whether a is equal to o.
|
||||
func (a Addr) Equal(o Addr) bool {
|
||||
return a.Port == o.Port && a.IP.Equal(o.IP)
|
||||
}
|
||||
|
||||
// UDPAddr converts itself to *net.UDPAddr.
|
||||
func (a Addr) UDPAddr() *net.UDPAddr {
|
||||
return &net.UDPAddr{IP: a.IP, Port: int(a.Port)}
|
||||
}
|
||||
|
||||
var _ net.Addr = Addr{}
|
||||
|
||||
// Network implements the interface net.Addr#Network.
|
||||
func (a Addr) Network() string {
|
||||
return "krpc"
|
||||
}
|
||||
|
||||
func (a Addr) String() string {
|
||||
if a.Port == 0 {
|
||||
return a.IP.String()
|
||||
}
|
||||
return net.JoinHostPort(a.IP.String(), strconv.FormatUint(uint64(a.Port), 10))
|
||||
}
|
||||
|
||||
// WriteBinary is the same as MarshalBinary, but writes the result into w
|
||||
// instead of returning.
|
||||
func (a Addr) WriteBinary(w io.Writer) (n int, err error) {
|
||||
if n, err = w.Write(a.IP); err == nil {
|
||||
if err = binary.Write(w, binary.BigEndian, a.Port); err == nil {
|
||||
n += 2
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
_ encoding.BinaryMarshaler = new(Addr)
|
||||
_ encoding.BinaryUnmarshaler = new(Addr)
|
||||
)
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler,
|
||||
func (a Addr) MarshalBinary() (data []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(18)
|
||||
if _, err = a.WriteBinary(buf); err == nil {
|
||||
data = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (a *Addr) UnmarshalBinary(data []byte) error {
|
||||
_len := len(data) - 2
|
||||
switch _len {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
default:
|
||||
return errors.New("invalid compact ip-address/port info")
|
||||
}
|
||||
|
||||
a.IP = make(net.IP, _len)
|
||||
copy(a.IP, data[:_len])
|
||||
a.Port = binary.BigEndian.Uint16(data[_len:])
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(Addr)
|
||||
_ bencode.Unmarshaler = new(Addr)
|
||||
)
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (a Addr) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = a.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (a *Addr) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = a.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv4Addrs)
|
||||
_ bencode.Unmarshaler = new(CompactIPv4Addrs)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv4Addrs)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv4Addrs)
|
||||
)
|
||||
|
||||
// CompactIPv4Addrs is a set of IPv4 Addrs.
|
||||
type CompactIPv4Addrs []Addr
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cas CompactIPv4Addrs) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(6 * len(cas))
|
||||
for _, addr := range cas {
|
||||
if addr.IP = addr.IP.To4(); len(addr.IP) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if n, err := addr.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 6 {
|
||||
panic(fmt.Errorf("CompactIPv4Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cas *CompactIPv4Addrs) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%6 != 0 {
|
||||
return fmt.Errorf("CompactIPv4Addrs: invalid addr info length '%d'", _len)
|
||||
}
|
||||
|
||||
addrs := make(CompactIPv4Addrs, 0, _len/6)
|
||||
for i := 0; i < _len; i += 6 {
|
||||
var addr Addr
|
||||
if err = addr.UnmarshalBinary(b[i : i+6]); err != nil {
|
||||
return
|
||||
}
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
*cas = addrs
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cas CompactIPv4Addrs) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cas.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cas *CompactIPv4Addrs) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cas.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv6Addrs)
|
||||
_ bencode.Unmarshaler = new(CompactIPv6Addrs)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv6Addrs)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv6Addrs)
|
||||
)
|
||||
|
||||
// CompactIPv6Addrs is a set of IPv6 Addrs.
|
||||
type CompactIPv6Addrs []Addr
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cas CompactIPv6Addrs) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(18 * len(cas))
|
||||
for _, addr := range cas {
|
||||
if addr.IP = addr.IP.To4(); len(addr.IP) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if n, err := addr.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 18 {
|
||||
panic(fmt.Errorf("CompactIPv4Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cas *CompactIPv6Addrs) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%18 != 0 {
|
||||
return fmt.Errorf("CompactIPv4Addrs: invalid addr info length '%d'", _len)
|
||||
}
|
||||
|
||||
addrs := make(CompactIPv6Addrs, 0, _len/18)
|
||||
for i := 0; i < _len; i += 18 {
|
||||
var addr Addr
|
||||
if err = addr.UnmarshalBinary(b[i : i+18]); err != nil {
|
||||
return
|
||||
}
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
*cas = addrs
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cas CompactIPv6Addrs) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cas.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cas *CompactIPv6Addrs) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cas.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
// Copyright 2023 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package krpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
func TestAddr(t *testing.T) {
|
||||
addrs := []Addr{
|
||||
{IP: net.ParseIP("172.16.1.1").To4(), Port: 123},
|
||||
{IP: net.ParseIP("192.168.1.1").To4(), Port: 456},
|
||||
}
|
||||
expect := "l6:\xac\x10\x01\x01\x00\x7b6:\xc0\xa8\x01\x01\x01\xc8e"
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := bencode.NewEncoder(buf).Encode(addrs); err != nil {
|
||||
t.Error(err)
|
||||
} else if result := buf.String(); result != expect {
|
||||
t.Errorf("expect %s, but got %x\n", expect, result)
|
||||
}
|
||||
|
||||
var raddrs []Addr
|
||||
if err := bencode.DecodeString(expect, &raddrs); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(raddrs) != len(addrs) {
|
||||
t.Errorf("expect addrs length %d, but got %d\n", len(addrs), len(raddrs))
|
||||
} else {
|
||||
for i, addr := range addrs {
|
||||
if !addr.Equal(raddrs[i]) {
|
||||
t.Errorf("%d: expect addr %v, but got %v\n", i, addr, raddrs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
237
krpc/message.go
237
krpc/message.go
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -18,8 +18,8 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// Predefine some error code.
|
||||
@ -42,10 +42,10 @@ const (
|
||||
// as specified by the KRPC protocol, which are also referred to as the KRPC
|
||||
// messages.
|
||||
//
|
||||
// The message is a dictonary that is "bencoded" (serialization & compression
|
||||
// format adopted by the BitTorrent) and sent via the UDP connection to peers.
|
||||
//
|
||||
// There are three types of messages: QUERY, RESPONSE, ERROR.
|
||||
// There are three types of messages: QUERY, RESPONSE, ERROR
|
||||
// The message is a dictonary that is then "bencoded"
|
||||
// (serialization & compression format adopted by the BitTorrent)
|
||||
// and sent via the UDP connection to peers.
|
||||
//
|
||||
// A KRPC message is a single dictionary with two keys common to every message
|
||||
// and additional keys depending on the type of message. Every message has a key
|
||||
@ -58,24 +58,12 @@ const (
|
||||
// the type of message. The value of the "y" key is one of "q" for query,
|
||||
// "r" for response, or "e" for error.
|
||||
type Message struct {
|
||||
// Transaction ID
|
||||
//
|
||||
// Required
|
||||
T string `bencode:"t"` // BEP 5
|
||||
|
||||
// Message Type: "q" for QUERY, "r" for RESPONSE, "e" for ERROR
|
||||
//
|
||||
// Required
|
||||
Y string `bencode:"y"` // BEP 5
|
||||
|
||||
// Query Method: one of "ping", "find_node", "get_peers", "announce_peer"
|
||||
//
|
||||
// Required only if "y" is equal to "q".
|
||||
Q string `bencode:"q,omitempty"` // BEP 5
|
||||
|
||||
A QueryArg `bencode:"a,omitempty"` // BEP 5: Only for the QUERY message
|
||||
R ResponseResult `bencode:"r,omitempty"` // BEP 5: Only for the RESPONSE message
|
||||
E Error `bencode:"e,omitempty"` // BEP 5: Only for the ERROR message
|
||||
T string `bencode:"t"` // required: transaction ID
|
||||
Y string `bencode:"y"` // required: type of the message: q for QUERY, r for RESPONSE, e for ERROR
|
||||
Q string `bencode:"q,omitempty"` // Query method (one of 4: "ping", "find_node", "get_peers", "announce_peer")
|
||||
A QueryArg `bencode:"a,omitempty"` // named arguments sent with a query
|
||||
R ResponseResult `bencode:"r,omitempty"` // RESPONSE type only
|
||||
E Error `bencode:"e,omitempty"` // ERROR type only
|
||||
|
||||
RO bool `bencode:"ro,omitempty"` // BEP 43: ReadOnly
|
||||
}
|
||||
@ -95,24 +83,34 @@ func NewErrorMsg(tid string, code int, reason string) Message {
|
||||
return Message{T: tid, Y: "e", E: Error{Code: code, Reason: reason}}
|
||||
}
|
||||
|
||||
// IsQuery reports whether the message is a QUERY message.
|
||||
func (m Message) IsQuery() bool { return m.Y == "q" }
|
||||
// IsQuery reports whether the message is an QUERY.
|
||||
func (m Message) IsQuery() bool {
|
||||
return m.Y == "q"
|
||||
}
|
||||
|
||||
// IsResponse reports whether the message is a RESPONSE message.
|
||||
func (m Message) IsResponse() bool { return m.Y == "r" }
|
||||
// IsResponse reports whether the message is an RESPONSE.
|
||||
func (m Message) IsResponse() bool {
|
||||
return m.Y == "r"
|
||||
}
|
||||
|
||||
// IsError reports whether the message is an ERROR message.
|
||||
func (m Message) IsError() bool { return m.Y == "e" }
|
||||
// IsError reports whether the message is an ERROR.
|
||||
func (m Message) IsError() bool {
|
||||
return m.Y == "e"
|
||||
}
|
||||
|
||||
// RID returns the value named "id" in "r".
|
||||
//
|
||||
// Return the ZERO value instead if no "id".
|
||||
func (m Message) RID() metainfo.Hash { return m.R.ID }
|
||||
func (m Message) RID() metainfo.Hash {
|
||||
return m.R.ID
|
||||
}
|
||||
|
||||
// QID returns the value named "id" in "a", that's, the query arguments.
|
||||
//
|
||||
// Return the ZERO value instead if no "id".
|
||||
func (m Message) QID() metainfo.Hash { return m.A.ID }
|
||||
func (m Message) QID() metainfo.Hash {
|
||||
return m.A.ID
|
||||
}
|
||||
|
||||
// ID returns the QID or RID.
|
||||
//
|
||||
@ -130,8 +128,8 @@ func (m Message) ID() metainfo.Hash {
|
||||
|
||||
// Error represents a response error.
|
||||
type Error struct {
|
||||
Code int // BEP 5
|
||||
Reason string // BEP 5
|
||||
Code int
|
||||
Reason string
|
||||
}
|
||||
|
||||
// NewError returns a new Error.
|
||||
@ -142,7 +140,7 @@ func NewError(code int, reason string) Error {
|
||||
func (e *Error) decode(vs []interface{}) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("panic: %v", r)
|
||||
err = fmt.Errorf("unpacking %#v: %v", vs, r)
|
||||
}
|
||||
}()
|
||||
|
||||
@ -179,7 +177,7 @@ func (e Error) MarshalBencode() (ret []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(32)
|
||||
err = bencode.NewEncoder(buf).Encode([]interface{}{e.Code, e.Reason})
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
ret = buf.Bytes()
|
||||
}
|
||||
return
|
||||
@ -272,14 +270,14 @@ type ResponseResult struct {
|
||||
// of the requested ipv4 target.
|
||||
//
|
||||
// find_node
|
||||
Nodes CompactIPv4Nodes `bencode:"nodes,omitempty"` // BEP 5
|
||||
Nodes CompactIPv4Node `bencode:"nodes,omitempty"` // BEP 5
|
||||
|
||||
// Nodes6 is a string containing the compact node information for the list
|
||||
// of the ipv6 target node, or the K(8) closest good nodes in routing table
|
||||
// of the requested ipv6 target.
|
||||
//
|
||||
// find_node
|
||||
Nodes6 CompactIPv6Nodes `bencode:"nodes6,omitempty"` // BEP 32
|
||||
Nodes6 CompactIPv6Node `bencode:"nodes6,omitempty"` // BEP 32
|
||||
|
||||
// Token is used for future "announce_peer".
|
||||
//
|
||||
@ -288,14 +286,157 @@ type ResponseResult struct {
|
||||
|
||||
// Values is a list of the torrent peers.
|
||||
//
|
||||
// For each element, in general, it is a compact IP-address/port info and
|
||||
// may be decoded to Addr, for example,
|
||||
//
|
||||
// addrs := make([]Addr, len(values))
|
||||
// for i, v := range values {
|
||||
// addrs[i].UnmarshalBinary([]byte(v))
|
||||
// }
|
||||
//
|
||||
// get_peers
|
||||
Values []string `bencode:"values,omitempty"` // BEP 5
|
||||
Values CompactAddresses `bencode:"values,omitempty"` // BEP 5
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
// CompactAddresses represents a group of the compact addresses.
|
||||
type CompactAddresses []metainfo.Address
|
||||
|
||||
// MarshalBinary implements the interface binary.BinaryMarshaler.
|
||||
func (cas CompactAddresses) MarshalBinary() ([]byte, error) {
|
||||
ss := make([]string, len(cas))
|
||||
for i, addr := range cas {
|
||||
data, err := addr.MarshalBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ss[i] = string(data)
|
||||
}
|
||||
return bencode.EncodeBytes(ss)
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
|
||||
func (cas *CompactAddresses) UnmarshalBinary(b []byte) (err error) {
|
||||
var ss []string
|
||||
if err = bencode.DecodeBytes(b, &ss); err == nil {
|
||||
addrs := make(CompactAddresses, len(ss))
|
||||
for i, s := range ss {
|
||||
var addr metainfo.Address
|
||||
if err = addr.UnmarshalBinary([]byte(s)); err != nil {
|
||||
return
|
||||
}
|
||||
addrs[i] = addr
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
// CompactIPv4Node is a set of IPv4 Nodes.
|
||||
type CompactIPv4Node []Node
|
||||
|
||||
// MarshalBinary implements the interface binary.BinaryMarshaler.
|
||||
func (cn CompactIPv4Node) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(26 * len(cn))
|
||||
for _, ni := range cn {
|
||||
if ni.Addr.IP = ni.Addr.IP.To4(); len(ni.Addr.IP) == 0 {
|
||||
continue
|
||||
}
|
||||
if n, err := ni.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 26 {
|
||||
panic(fmt.Errorf("CompactIPv4NodeInfo: the invalid NodeInfo length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
|
||||
func (cn *CompactIPv4Node) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%26 != 0 {
|
||||
return fmt.Errorf("CompactIPv4NodeInfo: invalid bytes length '%d'", _len)
|
||||
}
|
||||
|
||||
nis := make([]Node, 0, _len/26)
|
||||
for i := 0; i < _len; i += 26 {
|
||||
var ni Node
|
||||
if err = ni.UnmarshalBinary(b[i : i+26]); err != nil {
|
||||
return
|
||||
}
|
||||
nis = append(nis, ni)
|
||||
}
|
||||
|
||||
*cn = nis
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cn CompactIPv4Node) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cn.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cn *CompactIPv4Node) UnmarshalBencode(b []byte) (err error) {
|
||||
var s string
|
||||
if err = bencode.DecodeBytes(b, &s); err == nil {
|
||||
err = cn.UnmarshalBinary([]byte(s))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
// CompactIPv6Node is a set of IPv6 Nodes.
|
||||
type CompactIPv6Node []Node
|
||||
|
||||
// MarshalBinary implements the interface binary.BinaryMarshaler.
|
||||
func (cn CompactIPv6Node) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(38 * len(cn))
|
||||
for _, ni := range cn {
|
||||
ni.Addr.IP = ni.Addr.IP.To16()
|
||||
if n, err := ni.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 38 {
|
||||
panic(fmt.Errorf("CompactIPv6NodeInfo: the invalid NodeInfo length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
|
||||
func (cn *CompactIPv6Node) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%38 != 0 {
|
||||
return fmt.Errorf("CompactIPv6NodeInfo: invalid bytes length '%d'", _len)
|
||||
}
|
||||
|
||||
nis := make([]Node, 0, _len/38)
|
||||
for i := 0; i < _len; i += 38 {
|
||||
var ni Node
|
||||
if err = ni.UnmarshalBinary(b[i : i+38]); err != nil {
|
||||
return
|
||||
}
|
||||
nis = append(nis, ni)
|
||||
}
|
||||
|
||||
*cn = nis
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cn CompactIPv6Node) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cn.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cn *CompactIPv6Node) UnmarshalBencode(b []byte) (err error) {
|
||||
var s string
|
||||
if err = bencode.DecodeBytes(b, &s); err == nil {
|
||||
err = cn.UnmarshalBinary([]byte(s))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ package krpc
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
)
|
||||
|
||||
func TestMessage(t *testing.T) {
|
||||
|
167
krpc/node.go
167
krpc/node.go
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -16,23 +16,29 @@ package krpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// Node represents a node information.
|
||||
type Node struct {
|
||||
ID metainfo.Hash
|
||||
Addr Addr
|
||||
Addr metainfo.Address
|
||||
}
|
||||
|
||||
// NewNode returns a new Node.
|
||||
func NewNode(id metainfo.Hash, addr Addr) Node {
|
||||
return Node{ID: id, Addr: addr}
|
||||
func NewNode(id metainfo.Hash, ip net.Addr, port int) Node {
|
||||
return Node{ID: id, Addr: metainfo.NewAddress(ip, uint16(port))}
|
||||
}
|
||||
|
||||
// NewNodeByUDPAddr returns a new Node with the id and the UDP address.
|
||||
func NewNodeByUDPAddr(id metainfo.Hash, addr *net.UDPAddr) (n Node) {
|
||||
n.ID = id
|
||||
n.Addr.FromUDPAddr(addr)
|
||||
return
|
||||
}
|
||||
|
||||
func (n Node) String() string {
|
||||
@ -57,28 +63,17 @@ func (n Node) WriteBinary(w io.Writer) (m int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
_ encoding.BinaryMarshaler = new(Node)
|
||||
_ encoding.BinaryUnmarshaler = new(Node)
|
||||
)
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler,
|
||||
// which implements "Compact node info".
|
||||
//
|
||||
// See http://bittorrent.org/beps/bep_0005.html.
|
||||
// MarshalBinary implements the interface binary.BinaryMarshaler.
|
||||
func (n Node) MarshalBinary() (data []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(40)
|
||||
buf.Grow(48)
|
||||
if _, err = n.WriteBinary(buf); err == nil {
|
||||
data = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler,
|
||||
// which implements "Compact node info".
|
||||
//
|
||||
// See http://bittorrent.org/beps/bep_0005.html.
|
||||
// UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
|
||||
func (n *Node) UnmarshalBinary(b []byte) error {
|
||||
if len(b) < 26 {
|
||||
return io.ErrShortBuffer
|
||||
@ -87,133 +82,3 @@ func (n *Node) UnmarshalBinary(b []byte) error {
|
||||
copy(n.ID[:], b[:20])
|
||||
return n.Addr.UnmarshalBinary(b[20:])
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv4Nodes)
|
||||
_ bencode.Unmarshaler = new(CompactIPv4Nodes)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv4Nodes)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv4Nodes)
|
||||
)
|
||||
|
||||
// CompactIPv4Nodes is a set of IPv4 Nodes.
|
||||
type CompactIPv4Nodes []Node
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cns CompactIPv4Nodes) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(26 * len(cns))
|
||||
for _, ni := range cns {
|
||||
if ni.Addr.IP = ni.Addr.IP.To4(); len(ni.Addr.IP) == 0 {
|
||||
continue
|
||||
}
|
||||
if n, err := ni.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 26 {
|
||||
panic(fmt.Errorf("CompactIPv4Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cns *CompactIPv4Nodes) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%26 != 0 {
|
||||
return fmt.Errorf("CompactIPv4Nodes: invalid node info length '%d'", _len)
|
||||
}
|
||||
|
||||
nis := make([]Node, 0, _len/26)
|
||||
for i := 0; i < _len; i += 26 {
|
||||
var ni Node
|
||||
if err = ni.UnmarshalBinary(b[i : i+26]); err != nil {
|
||||
return
|
||||
}
|
||||
nis = append(nis, ni)
|
||||
}
|
||||
|
||||
*cns = nis
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cns CompactIPv4Nodes) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cns.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cns *CompactIPv4Nodes) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cns.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv6Nodes)
|
||||
_ bencode.Unmarshaler = new(CompactIPv6Nodes)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv6Nodes)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv6Nodes)
|
||||
)
|
||||
|
||||
// CompactIPv6Nodes is a set of IPv6 Nodes.
|
||||
type CompactIPv6Nodes []Node
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cns CompactIPv6Nodes) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(38 * len(cns))
|
||||
for _, ni := range cns {
|
||||
ni.Addr.IP = ni.Addr.IP.To16()
|
||||
if n, err := ni.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 38 {
|
||||
panic(fmt.Errorf("CompactIPv6Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cns *CompactIPv6Nodes) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%38 != 0 {
|
||||
return fmt.Errorf("CompactIPv6Nodes: invalid node info length '%d'", _len)
|
||||
}
|
||||
|
||||
nis := make([]Node, 0, _len/38)
|
||||
for i := 0; i < _len; i += 38 {
|
||||
var ni Node
|
||||
if err = ni.UnmarshalBinary(b[i : i+38]); err != nil {
|
||||
return
|
||||
}
|
||||
nis = append(nis, ni)
|
||||
}
|
||||
|
||||
*cns = nis
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cns CompactIPv6Nodes) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cns.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cns *CompactIPv6Nodes) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cns.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,270 +0,0 @@
|
||||
// Copyright 2023 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metainfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
// CompactAddr represents an address based on ip and port,
|
||||
// which implements "Compact IP-address/port info".
|
||||
//
|
||||
// See http://bittorrent.org/beps/bep_0005.html.
|
||||
type CompactAddr struct {
|
||||
IP net.IP // For IPv4, its length must be 4.
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// NewCompactAddr returns a new compact Addr with ip and port.
|
||||
func NewCompactAddr(ip net.IP, port uint16) CompactAddr {
|
||||
return CompactAddr{IP: ip, Port: port}
|
||||
}
|
||||
|
||||
// NewCompactAddrFromUDPAddr converts *net.UDPAddr to a new CompactAddr.
|
||||
func NewCompactAddrFromUDPAddr(ua *net.UDPAddr) CompactAddr {
|
||||
return CompactAddr{IP: ua.IP, Port: uint16(ua.Port)}
|
||||
}
|
||||
|
||||
// Valid reports whether the addr is valid.
|
||||
func (a CompactAddr) Valid() bool {
|
||||
return len(a.IP) > 0 && a.Port > 0
|
||||
}
|
||||
|
||||
// Equal reports whether a is equal to o.
|
||||
func (a CompactAddr) Equal(o CompactAddr) bool {
|
||||
return a.Port == o.Port && a.IP.Equal(o.IP)
|
||||
}
|
||||
|
||||
// UDPAddr converts itself to *net.Addr.
|
||||
func (a CompactAddr) UDPAddr() *net.UDPAddr {
|
||||
return &net.UDPAddr{IP: a.IP, Port: int(a.Port)}
|
||||
}
|
||||
|
||||
var _ net.Addr = CompactAddr{}
|
||||
|
||||
// Network implements the interface net.Addr#Network.
|
||||
func (a CompactAddr) Network() string {
|
||||
return "krpc"
|
||||
}
|
||||
|
||||
func (a CompactAddr) String() string {
|
||||
if a.Port == 0 {
|
||||
return a.IP.String()
|
||||
}
|
||||
return net.JoinHostPort(a.IP.String(), strconv.FormatUint(uint64(a.Port), 10))
|
||||
}
|
||||
|
||||
// WriteBinary is the same as MarshalBinary, but writes the result into w
|
||||
// instead of returning.
|
||||
func (a CompactAddr) WriteBinary(w io.Writer) (n int, err error) {
|
||||
if n, err = w.Write(a.IP); err == nil {
|
||||
if err = binary.Write(w, binary.BigEndian, a.Port); err == nil {
|
||||
n += 2
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
_ encoding.BinaryMarshaler = new(CompactAddr)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactAddr)
|
||||
)
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler,
|
||||
func (a CompactAddr) MarshalBinary() (data []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(18)
|
||||
if _, err = a.WriteBinary(buf); err == nil {
|
||||
data = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (a *CompactAddr) UnmarshalBinary(data []byte) error {
|
||||
_len := len(data) - 2
|
||||
switch _len {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
default:
|
||||
return errors.New("invalid compact ip-address/port info")
|
||||
}
|
||||
|
||||
a.IP = make(net.IP, _len)
|
||||
copy(a.IP, data[:_len])
|
||||
a.Port = binary.BigEndian.Uint16(data[_len:])
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactAddr)
|
||||
_ bencode.Unmarshaler = new(CompactAddr)
|
||||
)
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (a CompactAddr) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = a.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (a *CompactAddr) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = a.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv4Addrs)
|
||||
_ bencode.Unmarshaler = new(CompactIPv4Addrs)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv4Addrs)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv4Addrs)
|
||||
)
|
||||
|
||||
// CompactIPv4Addrs is a set of IPv4 Addrs.
|
||||
type CompactIPv4Addrs []CompactAddr
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cas CompactIPv4Addrs) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(6 * len(cas))
|
||||
for _, addr := range cas {
|
||||
if addr.IP = addr.IP.To4(); len(addr.IP) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if n, err := addr.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 6 {
|
||||
panic(fmt.Errorf("CompactIPv4Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cas *CompactIPv4Addrs) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%6 != 0 {
|
||||
return fmt.Errorf("CompactIPv4Addrs: invalid addr info length '%d'", _len)
|
||||
}
|
||||
|
||||
addrs := make(CompactIPv4Addrs, 0, _len/6)
|
||||
for i := 0; i < _len; i += 6 {
|
||||
var addr CompactAddr
|
||||
if err = addr.UnmarshalBinary(b[i : i+6]); err != nil {
|
||||
return
|
||||
}
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
*cas = addrs
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cas CompactIPv4Addrs) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cas.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cas *CompactIPv4Addrs) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cas.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(CompactIPv6Addrs)
|
||||
_ bencode.Unmarshaler = new(CompactIPv6Addrs)
|
||||
_ encoding.BinaryMarshaler = new(CompactIPv6Addrs)
|
||||
_ encoding.BinaryUnmarshaler = new(CompactIPv6Addrs)
|
||||
)
|
||||
|
||||
// CompactIPv6Addrs is a set of IPv6 Addrs.
|
||||
type CompactIPv6Addrs []CompactAddr
|
||||
|
||||
// MarshalBinary implements the interface encoding.BinaryMarshaler.
|
||||
func (cas CompactIPv6Addrs) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(18 * len(cas))
|
||||
for _, addr := range cas {
|
||||
addr.IP = addr.IP.To16()
|
||||
if n, err := addr.WriteBinary(buf); err != nil {
|
||||
return nil, err
|
||||
} else if n != 18 {
|
||||
panic(fmt.Errorf("CompactIPv4Nodes: the invalid node info length '%d'", n))
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
|
||||
func (cas *CompactIPv6Addrs) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b)
|
||||
if _len%18 != 0 {
|
||||
return fmt.Errorf("CompactIPv4Addrs: invalid addr info length '%d'", _len)
|
||||
}
|
||||
|
||||
addrs := make(CompactIPv6Addrs, 0, _len/18)
|
||||
for i := 0; i < _len; i += 18 {
|
||||
var addr CompactAddr
|
||||
if err = addr.UnmarshalBinary(b[i : i+18]); err != nil {
|
||||
return
|
||||
}
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
*cas = addrs
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (cas CompactIPv6Addrs) MarshalBencode() (b []byte, err error) {
|
||||
if b, err = cas.MarshalBinary(); err == nil {
|
||||
b, err = bencode.EncodeBytes(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (cas *CompactIPv6Addrs) UnmarshalBencode(b []byte) (err error) {
|
||||
var data []byte
|
||||
if err = bencode.DecodeBytes(b, &data); err == nil {
|
||||
err = cas.UnmarshalBinary(data)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
// Copyright 2023 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metainfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
func TestCompactAddr(t *testing.T) {
|
||||
addrs := []CompactAddr{
|
||||
{IP: net.ParseIP("172.16.1.1").To4(), Port: 123},
|
||||
{IP: net.ParseIP("192.168.1.1").To4(), Port: 456},
|
||||
}
|
||||
expect := "l6:\xac\x10\x01\x01\x00\x7b6:\xc0\xa8\x01\x01\x01\xc8e"
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := bencode.NewEncoder(buf).Encode(addrs); err != nil {
|
||||
t.Error(err)
|
||||
} else if result := buf.String(); result != expect {
|
||||
t.Errorf("expect %s, but got %x\n", expect, result)
|
||||
}
|
||||
|
||||
var raddrs []CompactAddr
|
||||
if err := bencode.DecodeString(expect, &raddrs); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(raddrs) != len(addrs) {
|
||||
t.Errorf("expect addrs length %d, but got %d\n", len(addrs), len(raddrs))
|
||||
} else {
|
||||
for i, addr := range addrs {
|
||||
if !addr.Equal(raddrs[i]) {
|
||||
t.Errorf("%d: expect addr %v, but got %v\n", i, addr, raddrs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metainfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
// HostAddr represents an address based on host and port.
|
||||
type HostAddr struct {
|
||||
Host string
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// NewHostAddr returns a new host Addr.
|
||||
func NewHostAddr(host string, port uint16) HostAddr {
|
||||
return HostAddr{Host: host, Port: port}
|
||||
}
|
||||
|
||||
// ParseHostAddr parses a string s to Addr.
|
||||
func ParseHostAddr(s string) (HostAddr, error) {
|
||||
host, port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return HostAddr{}, err
|
||||
}
|
||||
|
||||
_port, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return HostAddr{}, err
|
||||
}
|
||||
|
||||
return NewHostAddr(host, uint16(_port)), nil
|
||||
}
|
||||
|
||||
func (a HostAddr) String() string {
|
||||
if a.Port == 0 {
|
||||
return a.Host
|
||||
}
|
||||
return net.JoinHostPort(a.Host, strconv.FormatUint(uint64(a.Port), 10))
|
||||
}
|
||||
|
||||
// Equal reports whether a is equal to o.
|
||||
func (a HostAddr) Equal(o HostAddr) bool {
|
||||
return a.Port == o.Port && a.Host == o.Host
|
||||
}
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(HostAddr)
|
||||
_ bencode.Unmarshaler = new(HostAddr)
|
||||
)
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (a HostAddr) MarshalBencode() (b []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(64)
|
||||
err = bencode.NewEncoder(buf).Encode([]interface{}{a.Host, a.Port})
|
||||
if err == nil {
|
||||
b = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (a *HostAddr) UnmarshalBencode(b []byte) (err error) {
|
||||
var iface interface{}
|
||||
if err = bencode.NewDecoder(bytes.NewBuffer(b)).Decode(&iface); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch v := iface.(type) {
|
||||
case string:
|
||||
*a, err = ParseHostAddr(v)
|
||||
|
||||
case []interface{}:
|
||||
err = a.decode(v)
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("unsupported type: %T", iface)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *HostAddr) decode(vs []interface{}) (err error) {
|
||||
defer func() {
|
||||
switch e := recover().(type) {
|
||||
case nil:
|
||||
case error:
|
||||
err = e
|
||||
default:
|
||||
err = fmt.Errorf("%v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
a.Host = vs[0].(string)
|
||||
a.Port = uint16(vs[1].(int64))
|
||||
return
|
||||
}
|
369
metainfo/address.go
Normal file
369
metainfo/address.go
Normal file
@ -0,0 +1,369 @@
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metainfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/eyedeekay/sam3/i2pkeys"
|
||||
)
|
||||
|
||||
func to4(ip net.Addr) (net.IP, net.Addr) {
|
||||
switch v := ip.(type) {
|
||||
case *net.IPAddr:
|
||||
return v.IP.To4(), v
|
||||
case *i2pkeys.I2PAddr:
|
||||
return net.ParseIP(""), nil
|
||||
case *net.TCPAddr:
|
||||
return v.IP.To4(), v
|
||||
case *net.UDPAddr:
|
||||
return v.IP.To4(), v
|
||||
}
|
||||
return net.ParseIP(""), nil
|
||||
}
|
||||
|
||||
// ErrInvalidAddr is returned when the compact address is invalid.
|
||||
var ErrInvalidAddr = fmt.Errorf("invalid compact information of ip and port")
|
||||
|
||||
// Address represents a client/server listening on a UDP port implementing
|
||||
// the DHT protocol.
|
||||
type Address struct {
|
||||
IP net.Addr // For IPv4, its length must be 4.
|
||||
// Port uint16
|
||||
}
|
||||
|
||||
// NewAddress returns a new Address.
|
||||
func NewAddress(ip net.Addr, port uint16) Address {
|
||||
if ipv4, ad := to4(ip); len(ipv4) > 0 {
|
||||
ip = ad
|
||||
}
|
||||
return Address{IP: ip}
|
||||
}
|
||||
|
||||
// NewAddressFromString returns a new Address by the address string.
|
||||
func NewAddressFromString(s string) (addr Address, err error) {
|
||||
err = addr.FromString(s)
|
||||
return
|
||||
}
|
||||
|
||||
// NewAddressesFromString returns a list of Addresses by the address string.
|
||||
func NewAddressesFromString(s string) (addrs []Address, err error) {
|
||||
shost, sport, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid address '%s': %s", s, err)
|
||||
}
|
||||
|
||||
var port uint16
|
||||
if sport != "" {
|
||||
v, err := strconv.ParseUint(sport, 10, 16)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid address '%s': %s", s, err)
|
||||
}
|
||||
port = uint16(v)
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(shost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to lookup the domain '%s': %s", shost, err)
|
||||
}
|
||||
|
||||
addrs = make([]Address, len(ips))
|
||||
for i, ip := range ips {
|
||||
if ipv4, ad := to4(&net.IPAddr{IP:ip}); len(ipv4) != 0 {
|
||||
addrs[i] = Address{IP: ad}
|
||||
} else {
|
||||
addrs[i] = Address{IP: ad}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FromString parses and sets the ip from the string addr.
|
||||
func (a *Address) FromString(addr string) (err error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid address '%s': %s", addr, err)
|
||||
}
|
||||
|
||||
if port != "" {
|
||||
v, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid address '%s': %s", addr, err)
|
||||
}
|
||||
// a.Port = uint16(v)
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to lookup the domain '%s': %s", host, err)
|
||||
} else if len(ips) == 0 {
|
||||
return fmt.Errorf("the domain '%s' has no ips", host)
|
||||
}
|
||||
|
||||
a.IP = &net.IPAddr{IP:ips[0]}
|
||||
if ip, ad := to4(a.IP); len(ip) > 0 {
|
||||
a.IP = ad
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FromUDPAddr sets the ip from net.UDPAddr.
|
||||
func (a *Address) FromUDPAddr(ua *net.UDPAddr) {
|
||||
// a.Port = uint16(ua.Port)
|
||||
a.IP = &net.IPAddr{ua.IP}
|
||||
if ipv4 := to4(&net.IPAddr{a.IP}); len(ipv4) != 0 {
|
||||
a.IP = ipv4
|
||||
}
|
||||
}
|
||||
|
||||
// UDPAddr creates a new net.UDPAddr.
|
||||
func (a Address) UDPAddr() *net.UDPAddr {
|
||||
return &net.UDPAddr{
|
||||
IP: a.IP,
|
||||
Port: int(a.Port),
|
||||
}
|
||||
}
|
||||
|
||||
func (a Address) String() string {
|
||||
if a.Port == 0 {
|
||||
return a.IP.String()
|
||||
}
|
||||
return net.JoinHostPort(a.IP.String(), strconv.FormatUint(uint64(a.Port), 10))
|
||||
}
|
||||
|
||||
// Equal reports whether n is equal to o, which is equal to
|
||||
// n.HasIPAndPort(o.IP, o.Port)
|
||||
func (a Address) Equal(o Address) bool {
|
||||
return a.Port == o.Port && a.IP.Equal(o.IP)
|
||||
}
|
||||
|
||||
// HasIPAndPort reports whether the current node has the ip and the port.
|
||||
func (a Address) HasIPAndPort(ip net.Addr, port uint16) bool {
|
||||
return port == a.Port && a.IP.Equal(ip)
|
||||
}
|
||||
|
||||
// WriteBinary is the same as MarshalBinary, but writes the result into w
|
||||
// instead of returning.
|
||||
func (a Address) WriteBinary(w io.Writer) (m int, err error) {
|
||||
if m, err = w.Write(a.IP); err == nil {
|
||||
if err = binary.Write(w, binary.BigEndian, a.Port); err == nil {
|
||||
m += 2
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
|
||||
func (a *Address) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b) - 2
|
||||
switch _len {
|
||||
case net.Addrv4len, net.Addrv6len:
|
||||
default:
|
||||
return ErrInvalidAddr
|
||||
}
|
||||
|
||||
a.IP = make(net.Addr, _len)
|
||||
copy(a.IP, b[:_len])
|
||||
a.Port = binary.BigEndian.Uint16(b[_len:])
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBinary implements the interface binary.BinaryMarshaler.
|
||||
func (a Address) MarshalBinary() (data []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(20)
|
||||
if _, err = a.WriteBinary(buf); err == nil {
|
||||
data = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Address) decode(vs []interface{}) (err error) {
|
||||
defer func() {
|
||||
switch e := recover().(type) {
|
||||
case nil:
|
||||
case error:
|
||||
err = e
|
||||
default:
|
||||
err = fmt.Errorf("%v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
host := vs[0].(string)
|
||||
if a.IP = net.ParseIP(host); len(a.IP) == 0 {
|
||||
return ErrInvalidAddr
|
||||
} else if ip := to4(a.IP); len(ip) != 0 {
|
||||
a.IP = ip
|
||||
}
|
||||
|
||||
a.Port = uint16(vs[1].(int64))
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (a *Address) UnmarshalBencode(b []byte) (err error) {
|
||||
var iface interface{}
|
||||
if err = bencode.NewDecoder(bytes.NewBuffer(b)).Decode(&iface); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch v := iface.(type) {
|
||||
case string:
|
||||
err = a.FromString(v)
|
||||
case []interface{}:
|
||||
err = a.decode(v)
|
||||
default:
|
||||
err = fmt.Errorf("unsupported type: %T", iface)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (a Address) MarshalBencode() (b []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(32)
|
||||
err = bencode.NewEncoder(buf).Encode([]interface{}{a.IP.String(), a.Port})
|
||||
if err == nil {
|
||||
b = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
// HostAddress is the same as the Address, but the host part may be
|
||||
// either a domain or a ip.
|
||||
type HostAddress struct {
|
||||
Host string
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// NewHostAddress returns a new host addrress.
|
||||
func NewHostAddress(host string, port uint16) HostAddress {
|
||||
return HostAddress{Host: host}
|
||||
}
|
||||
|
||||
// NewHostAddressFromString returns a new host address by the string.
|
||||
func NewHostAddressFromString(s string) (addr HostAddress, err error) {
|
||||
err = addr.FromString(s)
|
||||
return
|
||||
}
|
||||
|
||||
// FromString parses and sets the host from the string addr.
|
||||
func (a *HostAddress) FromString(addr string) (err error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid address '%s': %s", addr, err)
|
||||
} else if host == "" {
|
||||
return fmt.Errorf("invalid address '%s': missing host", addr)
|
||||
}
|
||||
|
||||
if port != "" {
|
||||
v, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid address '%s': %s", addr, err)
|
||||
}
|
||||
a.Port = uint16(v)
|
||||
}
|
||||
|
||||
a.Host = host
|
||||
return
|
||||
}
|
||||
|
||||
func (a HostAddress) String() string {
|
||||
if a.Port == 0 {
|
||||
return a.Host
|
||||
}
|
||||
return net.JoinHostPort(a.Host, strconv.FormatUint(uint64(a.Port), 10))
|
||||
}
|
||||
|
||||
// Addresses parses the host address to a list of Addresses.
|
||||
func (a HostAddress) Addresses() (addrs []Address, err error) {
|
||||
if ip := net.ParseIP(a.Host); len(ip) != 0 {
|
||||
return []Address{NewAddress(ip, a.Port)}, nil
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(a.Host)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("fail to lookup the domain '%s': %s", a.Host, err)
|
||||
} else {
|
||||
addrs = make([]Address, len(ips))
|
||||
for i, ip := range ips {
|
||||
addrs[i] = NewAddress(ip, a.Port)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Equal reports whether a is equal to o.
|
||||
func (a HostAddress) Equal(o HostAddress) bool {
|
||||
return a.Port == o.Port && a.Host == o.Host
|
||||
}
|
||||
|
||||
func (a *HostAddress) decode(vs []interface{}) (err error) {
|
||||
defer func() {
|
||||
switch e := recover().(type) {
|
||||
case nil:
|
||||
case error:
|
||||
err = e
|
||||
default:
|
||||
err = fmt.Errorf("%v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
a.Host = vs[0].(string)
|
||||
a.Port = uint16(vs[1].(int64))
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (a *HostAddress) UnmarshalBencode(b []byte) (err error) {
|
||||
var iface interface{}
|
||||
if err = bencode.NewDecoder(bytes.NewBuffer(b)).Decode(&iface); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch v := iface.(type) {
|
||||
case string:
|
||||
err = a.FromString(v)
|
||||
case []interface{}:
|
||||
err = a.decode(v)
|
||||
default:
|
||||
err = fmt.Errorf("unsupported type: %T", iface)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (a HostAddress) MarshalBencode() (b []byte, err error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Grow(32)
|
||||
err = bencode.NewEncoder(buf).Encode([]interface{}{a.Host, a.Port})
|
||||
if err == nil {
|
||||
b = buf.Bytes()
|
||||
}
|
||||
return
|
||||
}
|
@ -16,33 +16,33 @@ package metainfo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
)
|
||||
|
||||
func TestAddress(t *testing.T) {
|
||||
addrs := []HostAddr{
|
||||
{Host: "1.2.3.4", Port: 123},
|
||||
{Host: "www.example.com", Port: 456},
|
||||
}
|
||||
expect := `ll7:1.2.3.4i123eel15:www.example.comi456eee`
|
||||
|
||||
if result, err := bencode.EncodeString(addrs); err != nil {
|
||||
var addr1 Address
|
||||
if err := addr1.FromString("1.2.3.4:1234"); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expect {
|
||||
t.Errorf("expect %s, but got %s\n", expect, result)
|
||||
return
|
||||
}
|
||||
|
||||
var raddrs []HostAddr
|
||||
if err := bencode.DecodeString(expect, &raddrs); err != nil {
|
||||
data, err := addr1.MarshalBencode()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if len(raddrs) != len(addrs) {
|
||||
t.Errorf("expect addrs length %d, but got %d\n", len(addrs), len(raddrs))
|
||||
} else {
|
||||
for i, addr := range addrs {
|
||||
if !addr.Equal(raddrs[i]) {
|
||||
t.Errorf("%d: expect %v, but got %v\n", i, addr, raddrs[i])
|
||||
}
|
||||
}
|
||||
return
|
||||
} else if s := string(data); s != `l7:1.2.3.4i1234ee` {
|
||||
t.Errorf(`expected 'l7:1.2.3.4i1234ee', but got '%s'`, s)
|
||||
}
|
||||
|
||||
var addr2 Address
|
||||
if err = addr2.UnmarshalBencode(data); err != nil {
|
||||
t.Error(err)
|
||||
} else if addr2.String() != `1.2.3.4:1234` {
|
||||
t.Errorf("expected '1.2.3.4:1234', but got '%s'", addr2)
|
||||
}
|
||||
|
||||
if data, err = addr2.MarshalBinary(); err != nil {
|
||||
t.Error(err)
|
||||
} else if s := string(data); s != "\x01\x02\x03\x04\x04\xd2" {
|
||||
t.Errorf(`expected '\x01\x02\x03\x04\x04\xd2', but got '%#x'`, s)
|
||||
}
|
||||
}
|
@ -14,7 +14,9 @@
|
||||
|
||||
package metainfo
|
||||
|
||||
import "path/filepath"
|
||||
import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// File represents a file in the multi-file case.
|
||||
type File struct {
|
||||
|
@ -89,10 +89,10 @@ func TestNewInfoFromFilePath(t *testing.T) {
|
||||
t.Errorf("invalid info %+v\n", info)
|
||||
}
|
||||
|
||||
info, err = NewInfoFromFilePath("../../go-bt", PieceSize256KB)
|
||||
info, err = NewInfoFromFilePath("../../bt", PieceSize256KB)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if info.Name != "go-bt" || info.Files == nil || info.Length > 0 {
|
||||
} else if info.Name != "bt" || info.Files == nil || info.Length > 0 {
|
||||
t.Errorf("invalid info %+v\n", info)
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
)
|
||||
|
||||
var zeroHash Hash
|
||||
@ -145,12 +145,10 @@ func (h *Hash) FromString(s string) (err error) {
|
||||
copy(h[:], bs)
|
||||
}
|
||||
default:
|
||||
hasher := sha1.New()
|
||||
hasher.Write([]byte(s))
|
||||
copy(h[:], hasher.Sum(nil))
|
||||
err = fmt.Errorf("hash string has bad length: %d", len(s))
|
||||
}
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// FromHexString resets the info hash from the hex string.
|
||||
|
@ -25,15 +25,16 @@ const xtPrefix = "urn:btih:"
|
||||
// Peers returns the list of the addresses of the peers.
|
||||
//
|
||||
// See BEP 9
|
||||
func (m Magnet) Peers() (peers []HostAddr, err error) {
|
||||
func (m Magnet) Peers() (peers []HostAddress, err error) {
|
||||
vs := m.Params["x.pe"]
|
||||
peers = make([]HostAddr, 0, len(vs))
|
||||
peers = make([]HostAddress, 0, len(vs))
|
||||
for _, v := range vs {
|
||||
if v != "" {
|
||||
addr, err := ParseHostAddr(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var addr HostAddress
|
||||
if err = addr.FromString(v); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
peers = append(peers, addr)
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/utils"
|
||||
)
|
||||
|
||||
// Bytes is the []byte type.
|
||||
@ -35,7 +35,7 @@ func (al AnnounceList) Unique() (announces []string) {
|
||||
announces = make([]string, 0, len(al))
|
||||
for _, tier := range al {
|
||||
for _, v := range tier {
|
||||
if v != "" && !helper.ContainsString(announces, v) {
|
||||
if v != "" && !utils.InStringSlice(announces, v) {
|
||||
announces = append(announces, v)
|
||||
}
|
||||
}
|
||||
@ -93,11 +93,11 @@ func (us *URLList) UnmarshalBencode(b []byte) (err error) {
|
||||
|
||||
// MetaInfo represents the .torrent file.
|
||||
type MetaInfo struct {
|
||||
InfoBytes Bytes `bencode:"info"` // BEP 3
|
||||
Announce string `bencode:"announce,omitempty"` // BEP 3, Single Tracker
|
||||
AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12, Multi-Tracker
|
||||
Nodes []HostAddr `bencode:"nodes,omitempty"` // BEP 5, DHT
|
||||
URLList URLList `bencode:"url-list,omitempty"` // BEP 19, WebSeed
|
||||
InfoBytes Bytes `bencode:"info"` // BEP 3
|
||||
Announce string `bencode:"announce,omitempty"` // BEP 3
|
||||
AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12
|
||||
Nodes []HostAddress `bencode:"nodes,omitempty"` // BEP 5
|
||||
URLList URLList `bencode:"url-list,omitempty"` // BEP 19
|
||||
|
||||
// Where's this specified?
|
||||
// Mentioned at https://wiki.theory.org/index.php/BitTorrentSpecification.
|
||||
|
@ -21,12 +21,9 @@ import (
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/xgfone/bt/utils"
|
||||
)
|
||||
|
||||
// BlockSize is the default size of a piece block.
|
||||
const BlockSize = 16 * 1024 // 2^14 = 16KB
|
||||
|
||||
// Predefine some sizes of the pieces.
|
||||
const (
|
||||
PieceSize256KB = 1024 * 256
|
||||
@ -72,7 +69,7 @@ func GeneratePieces(r io.Reader, pieceLength int64) (hs Hashes, err error) {
|
||||
buf := make([]byte, pieceLength)
|
||||
for {
|
||||
h := sha1.New()
|
||||
written, err := helper.CopyNBuffer(h, r, pieceLength, buf)
|
||||
written, err := utils.CopyNBuffer(h, r, pieceLength, buf)
|
||||
if written > 0 {
|
||||
hs = append(hs, NewHash(h.Sum(nil)))
|
||||
}
|
||||
@ -95,7 +92,7 @@ func writeFiles(w io.Writer, files []File, open func(File) (io.ReadCloser, error
|
||||
return fmt.Errorf("error opening %s: %s", file, err)
|
||||
}
|
||||
|
||||
n, err := helper.CopyNBuffer(w, r, file.Length, buf)
|
||||
n, err := utils.CopyNBuffer(w, r, file.Length, buf)
|
||||
r.Close()
|
||||
|
||||
if n != file.Length {
|
||||
|
@ -86,7 +86,7 @@ func (w *writer) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteBlock writes a block data.
|
||||
// WriteBlock writes a data block.
|
||||
func (w *writer) WriteBlock(pieceIndex, pieceOffset uint32, p []byte) (int, error) {
|
||||
return w.WriteAt(p, w.info.PieceOffset(pieceIndex, pieceOffset))
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
)
|
||||
|
||||
var errInvalidIP = errors.New("invalid ipv4 or ipv6")
|
||||
@ -43,15 +43,15 @@ const (
|
||||
)
|
||||
|
||||
// CompactIP is used to handle the compact ipv4 or ipv6.
|
||||
type CompactIP net.IP
|
||||
type CompactIP net.Addr
|
||||
|
||||
func (ci CompactIP) String() string {
|
||||
return net.IP(ci).String()
|
||||
return net.Addr(ci).String()
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
ip := net.IP(ci)
|
||||
ip := net.Addr(ci)
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
ip = ipv4
|
||||
}
|
||||
@ -60,13 +60,13 @@ func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (ci *CompactIP) UnmarshalBencode(b []byte) (err error) {
|
||||
var ip net.IP
|
||||
var ip net.Addr
|
||||
if err = bencode.DecodeBytes(b, &ip); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch len(ip) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
case net.Addrv4len, net.Addrv6len:
|
||||
default:
|
||||
return errInvalidIP
|
||||
}
|
||||
@ -86,16 +86,16 @@ type ExtendedHandshakeMsg struct {
|
||||
// M is the type of map[ExtendedMessageName]ExtendedMessageID.
|
||||
M map[string]uint8 `bencode:"m"` // BEP 10
|
||||
V string `bencode:"v,omitempty"` // BEP 10
|
||||
Reqq uint64 `bencode:"reqq,omitempty"` // BEP 10. The default in in libtorrent is 250.
|
||||
Reqq int `bencode:"reqq,omitempty"` // BEP 10
|
||||
|
||||
// Port is the local client port, which is redundant and no need
|
||||
// for the receiving side of the connection to send this.
|
||||
Port uint16 `bencode:"p,omitempty"` // BEP 10
|
||||
IPv6 net.IP `bencode:"ipv6,omitempty"` // BEP 10
|
||||
IPv6 net.Addr `bencode:"ipv6,omitempty"` // BEP 10
|
||||
IPv4 CompactIP `bencode:"ipv4,omitempty"` // BEP 10
|
||||
YourIP CompactIP `bencode:"yourip,omitempty"` // BEP 10
|
||||
|
||||
MetadataSize uint64 `bencode:"metadata_size,omitempty"` // BEP 9
|
||||
MetadataSize int `bencode:"metadata_size,omitempty"` // BEP 9
|
||||
}
|
||||
|
||||
// Decode decodes the extended handshake message from b.
|
||||
@ -118,7 +118,7 @@ type UtMetadataExtendedMsg struct {
|
||||
Piece int `bencode:"piece"` // BEP 9
|
||||
|
||||
// They are only used by "data" type
|
||||
TotalSize uint64 `bencode:"total_size,omitempty"` // BEP 9
|
||||
TotalSize int `bencode:"total_size,omitempty"` // BEP 9
|
||||
Data []byte `bencode:"-"`
|
||||
}
|
||||
|
||||
@ -139,9 +139,10 @@ func (um UtMetadataExtendedMsg) EncodeToPayload(buf *bytes.Buffer) (err error) {
|
||||
|
||||
// EncodeToBytes is equal to
|
||||
//
|
||||
// buf := new(bytes.Buffer)
|
||||
// err = um.EncodeToPayload(buf)
|
||||
// return buf.Bytes(), err
|
||||
// buf := new(bytes.Buffer)
|
||||
// err = um.EncodeToPayload(buf)
|
||||
// return buf.Bytes(), err
|
||||
//
|
||||
func (um UtMetadataExtendedMsg) EncodeToBytes() (b []byte, err error) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 128))
|
||||
if err = um.EncodeToPayload(buf); err == nil {
|
||||
|
@ -19,20 +19,19 @@ import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// GenerateAllowedFastSet generates some allowed fast set of the torrent file.
|
||||
//
|
||||
// Argument:
|
||||
//
|
||||
// set: generated piece set, the length of which is the number to be generated.
|
||||
// sz: the number of pieces in torrent.
|
||||
// ip: the of the remote peer of the connection.
|
||||
// infohash: infohash of torrent.
|
||||
// set: generated piece set, the length of which is the number to be generated.
|
||||
// sz: the number of pieces in torrent.
|
||||
// ip: the of the remote peer of the connection.
|
||||
// infohash: infohash of torrent.
|
||||
//
|
||||
// BEP 6
|
||||
func GenerateAllowedFastSet(set []uint32, sz uint32, ip net.IP, infohash metainfo.Hash) {
|
||||
func GenerateAllowedFastSet(set []uint32, sz uint32, ip net.Addr, infohash metainfo.Hash) {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
func TestGenerateAllowedFastSet(t *testing.T) {
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
var errInvalidProtocolHeader = fmt.Errorf("unexpected peer protocol header string")
|
||||
@ -44,12 +44,12 @@ func (eb ExtensionBits) String() string {
|
||||
}
|
||||
|
||||
// Set sets the bit to 1, that's, to set it to be on.
|
||||
func (eb *ExtensionBits) Set(bit uint) {
|
||||
func (eb ExtensionBits) Set(bit uint) {
|
||||
eb[7-bit/8] |= 1 << (bit % 8)
|
||||
}
|
||||
|
||||
// Unset sets the bit to 0, that's, to set it to be off.
|
||||
func (eb *ExtensionBits) Unset(bit uint) {
|
||||
func (eb ExtensionBits) Unset(bit uint) {
|
||||
eb[7-bit/8] &^= 1 << (bit % 8)
|
||||
}
|
||||
|
||||
|
@ -125,27 +125,18 @@ func (bf BitField) Unsets() (pieces Pieces) {
|
||||
return
|
||||
}
|
||||
|
||||
// CanSet reports whether the index can be set.
|
||||
func (bf BitField) CanSet(index uint32) bool {
|
||||
return (int(index) / 8) < len(bf)
|
||||
}
|
||||
|
||||
// Set sets the bit of the piece to 1 by its index.
|
||||
func (bf BitField) Set(index uint32) (ok bool) {
|
||||
func (bf BitField) Set(index uint32) {
|
||||
if i := int(index) / 8; i < len(bf) {
|
||||
bf[i] |= (1 << byte(7-index%8))
|
||||
ok = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Unset sets the bit of the piece to 0 by its index.
|
||||
func (bf BitField) Unset(index uint32) (ok bool) {
|
||||
func (bf BitField) Unset(index uint32) {
|
||||
if i := int(index) / 8; i < len(bf) {
|
||||
bf[i] &^= (1 << byte(7-index%8))
|
||||
ok = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// IsSet reports whether the bit of the piece is set to 1.
|
||||
@ -344,9 +335,7 @@ func (m Message) Encode(buf *bytes.Buffer) (err error) {
|
||||
if !m.Keepalive {
|
||||
if err = buf.WriteByte(byte(m.Type)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = m.marshalBinaryType(buf); err != nil {
|
||||
} else if err = m.marshalBinaryType(buf); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@ -387,7 +376,7 @@ func (m Message) marshalBinaryType(buf *bytes.Buffer) (err error) {
|
||||
}
|
||||
_, err = buf.Write(m.Piece)
|
||||
case MTypeExtended:
|
||||
if err = buf.WriteByte(byte(m.ExtendedID)); err == nil {
|
||||
if err = buf.WriteByte(byte(m.ExtendedID)); err != nil {
|
||||
_, err = buf.Write(m.ExtendedPayload)
|
||||
}
|
||||
case MTypePort:
|
||||
|
@ -14,7 +14,9 @@
|
||||
|
||||
package peerprotocol
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBitField(t *testing.T) {
|
||||
bf := NewBitFieldFromBools([]bool{
|
||||
|
@ -21,18 +21,18 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// Predefine some errors about extension support.
|
||||
var (
|
||||
ErrChoked = fmt.Errorf("choked")
|
||||
ErrNotFirstMsg = fmt.Errorf("not the first message")
|
||||
ErrNotSupportDHT = fmt.Errorf("not support DHT extension")
|
||||
ErrNotSupportFast = fmt.Errorf("not support Fast extension")
|
||||
ErrNotSupportExtended = fmt.Errorf("not support Extended extension")
|
||||
ErrSecondExtHandshake = fmt.Errorf("second extended handshake")
|
||||
ErrNoExtMessageID = fmt.Errorf("no extended message id")
|
||||
ErrNoExtHandshake = fmt.Errorf("no extended handshake")
|
||||
)
|
||||
|
||||
@ -79,38 +79,6 @@ type Bep10Handler interface {
|
||||
OnPayload(conn *PeerConn, extid uint8, payload []byte) error
|
||||
}
|
||||
|
||||
// ConnStage represents the stage of connection to the peer.
|
||||
type ConnStage int
|
||||
|
||||
// IsConnected reports whether the connection stage is connected.
|
||||
func (s ConnStage) IsConnected() bool { return s == ConnStageConnected }
|
||||
|
||||
// IsHandshook reports whether the connection stage is handshook.
|
||||
func (s ConnStage) IsHandshook() bool { return s == ConnStageHandshook }
|
||||
|
||||
// IsMessage reports whether the connection stage is message.
|
||||
func (s ConnStage) IsMessage() bool { return s == ConnStageMessage }
|
||||
|
||||
func (s ConnStage) String() string {
|
||||
switch s {
|
||||
case ConnStageConnected:
|
||||
return "connected"
|
||||
case ConnStageHandshook:
|
||||
return "handshook"
|
||||
case ConnStageMessage:
|
||||
return "message"
|
||||
default:
|
||||
return fmt.Sprintf("ConnStage(%d)", s)
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-define some connection stages.
|
||||
const (
|
||||
ConnStageConnected ConnStage = iota
|
||||
ConnStageHandshook
|
||||
ConnStageMessage
|
||||
)
|
||||
|
||||
// PeerConn is used to manage the connection to the peer.
|
||||
type PeerConn struct {
|
||||
net.Conn
|
||||
@ -122,7 +90,6 @@ type PeerConn struct {
|
||||
|
||||
PeerID metainfo.Hash // The ID of the remote peer.
|
||||
PeerExtBits ExtensionBits // The extension bits of the remote peer.
|
||||
PeerStage ConnStage // The connection stage of peer.
|
||||
|
||||
// These two states is controlled by the local client peer.
|
||||
//
|
||||
@ -155,13 +122,9 @@ type PeerConn struct {
|
||||
PeerInterested bool
|
||||
|
||||
// Timeout is used to control the timeout of reading/writing the message.
|
||||
// If WriteTimeout or ReadTimeout is ZERO, try to use Timeout instead.
|
||||
//
|
||||
//
|
||||
// The default is 0, which represents no timeout.
|
||||
WriteTimeout time.Duration
|
||||
ReadTimeout time.Duration
|
||||
Timeout time.Duration
|
||||
Timeout time.Duration
|
||||
|
||||
// MaxLength is used to limit the maximum number of the message body.
|
||||
//
|
||||
@ -184,6 +147,9 @@ type PeerConn struct {
|
||||
//
|
||||
// Optional.
|
||||
OnWriteMsg func(pc *PeerConn, m Message) error
|
||||
|
||||
notFirstMsg bool
|
||||
extHandshake bool
|
||||
}
|
||||
|
||||
// NewPeerConn returns a new PeerConn.
|
||||
@ -192,7 +158,6 @@ type PeerConn struct {
|
||||
// for the peer server, but not for the peer client.
|
||||
func NewPeerConn(conn net.Conn, id, infohash metainfo.Hash) *PeerConn {
|
||||
return &PeerConn{
|
||||
PeerStage: ConnStageConnected,
|
||||
Conn: conn,
|
||||
ID: id,
|
||||
InfoHash: infohash,
|
||||
@ -203,8 +168,8 @@ func NewPeerConn(conn net.Conn, id, infohash metainfo.Hash) *PeerConn {
|
||||
}
|
||||
|
||||
// NewPeerConnByDial returns a new PeerConn by dialing to addr with the "tcp" network.
|
||||
func NewPeerConnByDial(addr string, id, infohash metainfo.Hash, dialTimeout time.Duration) (pc *PeerConn, err error) {
|
||||
conn, err := net.DialTimeout("tcp", addr, dialTimeout)
|
||||
func NewPeerConnByDial(addr string, id, infohash metainfo.Hash, timeout time.Duration) (pc *PeerConn, err error) {
|
||||
conn, err := net.DialTimeout("tcp", addr, timeout)
|
||||
if err == nil {
|
||||
pc = NewPeerConn(conn, id, infohash)
|
||||
}
|
||||
@ -212,53 +177,17 @@ func NewPeerConnByDial(addr string, id, infohash metainfo.Hash, dialTimeout time
|
||||
}
|
||||
|
||||
func (pc *PeerConn) setReadTimeout() {
|
||||
switch {
|
||||
case pc.ReadTimeout > 0:
|
||||
pc.SetReadTimeout(pc.ReadTimeout)
|
||||
case pc.Timeout > 0:
|
||||
pc.SetReadTimeout(pc.Timeout)
|
||||
if pc.Timeout > 0 {
|
||||
pc.Conn.SetReadDeadline(time.Now().Add(pc.Timeout))
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PeerConn) setWriteTimeout() {
|
||||
switch {
|
||||
case pc.WriteTimeout > 0:
|
||||
pc.SetWriteTimeout(pc.WriteTimeout)
|
||||
case pc.Timeout > 0:
|
||||
pc.SetWriteTimeout(pc.Timeout)
|
||||
if pc.Timeout > 0 {
|
||||
pc.Conn.SetWriteDeadline(time.Now().Add(pc.Timeout))
|
||||
}
|
||||
}
|
||||
|
||||
// SetTimeout is a convenient method to set timeout of the read&write operation.
|
||||
//
|
||||
// If timeout is ZERO or negative, clear the read&write timeout.
|
||||
func (pc *PeerConn) SetTimeout(timeout time.Duration) error {
|
||||
if timeout > 0 {
|
||||
pc.Conn.SetDeadline(time.Now().Add(timeout))
|
||||
}
|
||||
return pc.Conn.SetDeadline(time.Time{})
|
||||
}
|
||||
|
||||
// SetReadTimeout is a convenient method to set timeout of the read operation.
|
||||
//
|
||||
// If timeout is ZERO or negative, clear the read timeout.
|
||||
func (pc *PeerConn) SetReadTimeout(timeout time.Duration) error {
|
||||
if timeout > 0 {
|
||||
pc.Conn.SetReadDeadline(time.Now().Add(timeout))
|
||||
}
|
||||
return pc.Conn.SetReadDeadline(time.Time{})
|
||||
}
|
||||
|
||||
// SetWriteTimeout is a convenient method to set timeout of the write operation.
|
||||
//
|
||||
// If timeout is ZERO or negative, clear the write timeout.
|
||||
func (pc *PeerConn) SetWriteTimeout(timeout time.Duration) error {
|
||||
if timeout > 0 {
|
||||
pc.Conn.SetWriteDeadline(time.Now().Add(timeout))
|
||||
}
|
||||
return pc.Conn.SetWriteDeadline(time.Time{})
|
||||
}
|
||||
|
||||
// SetChoked sets the Choked state of the local client peer.
|
||||
//
|
||||
// Notice: if the current state is not Choked, it will send a Choked message
|
||||
@ -315,29 +244,21 @@ func (pc *PeerConn) SetNotInterested() (err error) {
|
||||
//
|
||||
// BEP 3
|
||||
func (pc *PeerConn) Handshake() error {
|
||||
if !pc.PeerStage.IsConnected() {
|
||||
return fmt.Errorf("the peer connection stage '%s' is not connected", pc.PeerStage)
|
||||
}
|
||||
|
||||
m := HandshakeMsg{ExtensionBits: pc.ExtBits, PeerID: pc.ID, InfoHash: pc.InfoHash}
|
||||
pc.setReadTimeout()
|
||||
rhm, err := Handshake(pc.Conn, m)
|
||||
if err != nil {
|
||||
return err
|
||||
if err == nil {
|
||||
pc.PeerID = rhm.PeerID
|
||||
pc.PeerExtBits = rhm.ExtensionBits
|
||||
if pc.InfoHash.IsZero() {
|
||||
pc.InfoHash = rhm.InfoHash
|
||||
} else if pc.InfoHash != rhm.InfoHash {
|
||||
return fmt.Errorf("inconsistent infohash: local(%s)=%s, remote(%s)=%s",
|
||||
pc.Conn.LocalAddr().String(), pc.InfoHash.String(),
|
||||
pc.Conn.RemoteAddr().String(), rhm.InfoHash.String())
|
||||
}
|
||||
}
|
||||
|
||||
pc.PeerID = rhm.PeerID
|
||||
pc.PeerExtBits = rhm.ExtensionBits
|
||||
if pc.InfoHash.IsZero() {
|
||||
pc.InfoHash = rhm.InfoHash
|
||||
} else if pc.InfoHash != rhm.InfoHash {
|
||||
return fmt.Errorf("inconsistent infohash: local(%s)=%s, remote(%s)=%s",
|
||||
pc.Conn.LocalAddr().String(), pc.InfoHash.String(),
|
||||
pc.Conn.RemoteAddr().String(), rhm.InfoHash.String())
|
||||
}
|
||||
|
||||
pc.PeerStage = ConnStageHandshook
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadMsg reads the message.
|
||||
@ -346,22 +267,6 @@ func (pc *PeerConn) Handshake() error {
|
||||
func (pc *PeerConn) ReadMsg() (m Message, err error) {
|
||||
pc.setReadTimeout()
|
||||
err = m.Decode(pc.Conn, pc.MaxLength)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch m.Type {
|
||||
case MTypeBitField, MTypeHaveAll, MTypeHaveNone:
|
||||
if !pc.PeerStage.IsHandshook() {
|
||||
err = fmt.Errorf("%s is not first message after handshake", m.Type.String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if pc.PeerStage.IsHandshook() {
|
||||
pc.PeerStage = ConnStageMessage
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -539,19 +444,14 @@ func (pc *PeerConn) SendExtMsg(extID uint8, payload []byte) error {
|
||||
})
|
||||
}
|
||||
|
||||
// PeerHasPiece reports whether the peer has the piece.
|
||||
func (pc *PeerConn) PeerHasPiece(index uint32) bool {
|
||||
return pc.BitField.IsSet(index)
|
||||
}
|
||||
|
||||
// HandleMessage calls the method of the handler to handle the message.
|
||||
//
|
||||
// If handler has also implemented the interfaces Bep3Handler, Bep5Handler,
|
||||
// Bep6Handler or Bep10Handler, their methods will be called instead of
|
||||
// Handler.OnMessage for the corresponding type message.
|
||||
func (pc *PeerConn) HandleMessage(msg Message, handler Handler) error {
|
||||
func (pc *PeerConn) HandleMessage(msg Message, handler Handler) (err error) {
|
||||
if msg.Keepalive {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
switch msg.Type {
|
||||
@ -559,143 +459,167 @@ func (pc *PeerConn) HandleMessage(msg Message, handler Handler) error {
|
||||
case MTypeChoke:
|
||||
pc.PeerChoked = true
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Choke(pc)
|
||||
err = h.Choke(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeUnchoke:
|
||||
pc.PeerChoked = false
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Unchoke(pc)
|
||||
err = h.Unchoke(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeInterested:
|
||||
pc.PeerInterested = true
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Interested(pc)
|
||||
err = h.Interested(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeNotInterested:
|
||||
pc.PeerInterested = false
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.NotInterested(pc)
|
||||
err = h.NotInterested(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeHave:
|
||||
pc.BitField.Set(msg.Index)
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Have(pc, msg.Index)
|
||||
err = h.Have(pc, msg.Index)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeBitField:
|
||||
pc.BitField = msg.BitField
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.BitField(pc, msg.BitField)
|
||||
if pc.notFirstMsg {
|
||||
err = ErrNotFirstMsg
|
||||
} else {
|
||||
pc.BitField = msg.BitField
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
err = h.BitField(pc, msg.BitField)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
}
|
||||
|
||||
case MTypeRequest:
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Request(pc, msg.Index, msg.Begin, msg.Length)
|
||||
err = h.Request(pc, msg.Index, msg.Begin, msg.Length)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypePiece:
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Piece(pc, msg.Index, msg.Begin, msg.Piece)
|
||||
err = h.Piece(pc, msg.Index, msg.Begin, msg.Piece)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeCancel:
|
||||
if h, ok := handler.(Bep3Handler); ok {
|
||||
return h.Cancel(pc, msg.Index, msg.Begin, msg.Length)
|
||||
err = h.Cancel(pc, msg.Index, msg.Begin, msg.Length)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
// BEP 5 - DHT Protocol
|
||||
case MTypePort:
|
||||
if !pc.ExtBits.IsSupportDHT() {
|
||||
return ErrNotSupportDHT
|
||||
err = ErrNotSupportDHT
|
||||
} else if h, ok := handler.(Bep5Handler); ok {
|
||||
return h.Port(pc, msg.Port)
|
||||
err = h.Port(pc, msg.Port)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
// BEP 6 - Fast Extension
|
||||
case MTypeSuggest:
|
||||
if !pc.ExtBits.IsSupportFast() {
|
||||
return ErrNotSupportFast
|
||||
err = ErrNotSupportFast
|
||||
} else {
|
||||
pc.Suggests = pc.Suggests.Append(msg.Index)
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
err = h.Suggest(pc, msg.Index)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
}
|
||||
|
||||
pc.Suggests = pc.Suggests.Append(msg.Index)
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
return h.Suggest(pc, msg.Index)
|
||||
}
|
||||
|
||||
case MTypeHaveAll:
|
||||
if !pc.ExtBits.IsSupportFast() {
|
||||
return ErrNotSupportFast
|
||||
if pc.notFirstMsg {
|
||||
err = ErrNotFirstMsg
|
||||
} else if !pc.ExtBits.IsSupportFast() {
|
||||
err = ErrNotSupportFast
|
||||
} else if h, ok := handler.(Bep6Handler); ok {
|
||||
err = h.HaveAll(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
return h.HaveAll(pc)
|
||||
}
|
||||
|
||||
case MTypeHaveNone:
|
||||
if !pc.ExtBits.IsSupportFast() {
|
||||
return ErrNotSupportFast
|
||||
if pc.notFirstMsg {
|
||||
err = ErrNotFirstMsg
|
||||
} else if !pc.ExtBits.IsSupportFast() {
|
||||
err = ErrNotSupportFast
|
||||
} else if h, ok := handler.(Bep6Handler); ok {
|
||||
err = h.HaveNone(pc)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
return h.HaveNone(pc)
|
||||
}
|
||||
|
||||
case MTypeReject:
|
||||
if !pc.ExtBits.IsSupportFast() {
|
||||
return ErrNotSupportFast
|
||||
err = ErrNotSupportFast
|
||||
} else if h, ok := handler.(Bep6Handler); ok {
|
||||
return h.Reject(pc, msg.Index, msg.Begin, msg.Length)
|
||||
err = h.Reject(pc, msg.Index, msg.Begin, msg.Length)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
case MTypeAllowedFast:
|
||||
if !pc.ExtBits.IsSupportFast() {
|
||||
return ErrNotSupportFast
|
||||
}
|
||||
|
||||
pc.Fasts = pc.Fasts.Append(msg.Index)
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
return h.AllowedFast(pc, msg.Index)
|
||||
err = ErrNotSupportFast
|
||||
} else {
|
||||
pc.Fasts = pc.Fasts.Append(msg.Index)
|
||||
if h, ok := handler.(Bep6Handler); ok {
|
||||
err = h.AllowedFast(pc, msg.Index)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// BEP 10 - Extension Protocol
|
||||
case MTypeExtended:
|
||||
if !pc.ExtBits.IsSupportExtended() {
|
||||
return ErrNotSupportExtended
|
||||
err = ErrNotSupportExtended
|
||||
} else if h, ok := handler.(Bep10Handler); ok {
|
||||
return pc.handleExtMsg(h, msg)
|
||||
err = pc.handleExtMsg(h, msg)
|
||||
} else {
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
// Other
|
||||
default:
|
||||
// (xgf): Do something??
|
||||
err = handler.OnMessage(pc, msg)
|
||||
}
|
||||
|
||||
return handler.OnMessage(pc, msg)
|
||||
if !pc.notFirstMsg {
|
||||
pc.notFirstMsg = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (pc *PeerConn) handleExtMsg(h Bep10Handler, m Message) (err error) {
|
||||
// For Extended Message Handshake
|
||||
if m.ExtendedID == ExtendedIDHandshake {
|
||||
if len(pc.ExtendedHandshakeMsg.M) > 0 { // The extended handshake has done.
|
||||
if pc.extHandshake {
|
||||
return ErrSecondExtHandshake
|
||||
}
|
||||
|
||||
pc.extHandshake = true
|
||||
err = bencode.DecodeBytes(m.ExtendedPayload, &pc.ExtendedHandshakeMsg)
|
||||
if err != nil {
|
||||
return
|
||||
if err == nil {
|
||||
err = h.OnExtHandShake(pc)
|
||||
}
|
||||
|
||||
if len(pc.ExtendedHandshakeMsg.M) == 0 { // The extended message ids must exist.
|
||||
return ErrNoExtMessageID
|
||||
}
|
||||
|
||||
return h.OnExtHandShake(pc)
|
||||
} else if pc.extHandshake {
|
||||
err = h.OnPayload(pc, m.ExtendedID, m.ExtendedPayload)
|
||||
} else {
|
||||
err = ErrNoExtHandshake
|
||||
}
|
||||
|
||||
// For Extended Message
|
||||
if len(pc.ExtendedHandshakeMsg.M) == 0 {
|
||||
return ErrNoExtHandshake
|
||||
}
|
||||
return h.OnPayload(pc, m.ExtendedID, m.ExtendedPayload)
|
||||
return
|
||||
}
|
||||
|
@ -14,7 +14,9 @@
|
||||
|
||||
package peerprotocol
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ProtocolHeader is the BT protocal prefix.
|
||||
//
|
||||
|
@ -22,7 +22,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// Handler is used to handle the incoming peer connection.
|
||||
@ -65,9 +65,9 @@ type Config struct {
|
||||
HandleMessage func(pc *PeerConn, msg Message, handler Handler) error
|
||||
}
|
||||
|
||||
func (c *Config) set(conf *Config) {
|
||||
if conf != nil {
|
||||
*c = *conf
|
||||
func (c *Config) set(conf ...Config) {
|
||||
if len(conf) > 0 {
|
||||
*c = conf[0]
|
||||
}
|
||||
|
||||
if c.MaxLength == 0 {
|
||||
@ -93,35 +93,33 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServerByListen returns a new Server by listening on the address.
|
||||
func NewServerByListen(network, address string, id metainfo.Hash, h Handler, c *Config) (*Server, error) {
|
||||
func NewServerByListen(network, address string, id metainfo.Hash, h Handler,
|
||||
c ...Config) (*Server, error) {
|
||||
ln, err := net.Listen(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewServer(ln, id, h, c), nil
|
||||
return NewServer(ln, id, h, c...), nil
|
||||
}
|
||||
|
||||
// NewServer returns a new Server.
|
||||
func NewServer(ln net.Listener, id metainfo.Hash, h Handler, c *Config) *Server {
|
||||
func NewServer(ln net.Listener, id metainfo.Hash, h Handler, c ...Config) *Server {
|
||||
if id.IsZero() {
|
||||
panic("the peer node id must not be empty")
|
||||
}
|
||||
|
||||
var conf Config
|
||||
conf.set(c)
|
||||
conf.set(c...)
|
||||
return &Server{Listener: ln, ID: id, Handler: h, Config: conf}
|
||||
}
|
||||
|
||||
// Run starts the peer protocol server.
|
||||
func (s *Server) Run() {
|
||||
s.Config.set(nil)
|
||||
s.Config.set()
|
||||
for {
|
||||
conn, err := s.Listener.Accept()
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "closed") {
|
||||
s.Config.ErrorLog("fail to accept new connection: %s", err)
|
||||
}
|
||||
return
|
||||
s.Config.ErrorLog("fail to accept new connection: %s", err)
|
||||
}
|
||||
go s.handleConn(conn)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -16,20 +16,81 @@ package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// GetPeers gets the peers from the tracker.
|
||||
func GetPeers(ctx context.Context, tracker string, nodeID, infoHash metainfo.Hash, totalLength int64) (AnnounceResponse, error) {
|
||||
client, err := NewClient(tracker, nodeID, nil)
|
||||
if err != nil {
|
||||
return AnnounceResponse{}, err
|
||||
// GetPeersResult represents the result of getting the peers from the tracker.
|
||||
type GetPeersResult struct {
|
||||
Error error // nil stands for success. Or, for failure.
|
||||
Tracker string
|
||||
Resp AnnounceResponse
|
||||
}
|
||||
|
||||
// GetPeers gets the peers from the trackers.
|
||||
//
|
||||
// Notice: the returned chan will be closed when all the requests end.
|
||||
func GetPeers(ctx context.Context, id, infohash metainfo.Hash, trackers []string) []GetPeersResult {
|
||||
if len(trackers) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return client.Announce(ctx, AnnounceRequest{
|
||||
Left: totalLength,
|
||||
InfoHash: infoHash,
|
||||
Port: 6881,
|
||||
})
|
||||
for i, t := range trackers {
|
||||
if u, err := url.Parse(t); err == nil && u.Path == "" {
|
||||
u.Path = "/announce"
|
||||
trackers[i] = u.String()
|
||||
}
|
||||
}
|
||||
|
||||
_len := len(trackers)
|
||||
wlen := _len
|
||||
if wlen > 10 {
|
||||
wlen = 10
|
||||
}
|
||||
|
||||
reqs := make(chan string, wlen)
|
||||
go func() {
|
||||
for i := 0; i < _len; i++ {
|
||||
reqs <- trackers[i]
|
||||
}
|
||||
}()
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(_len)
|
||||
|
||||
var lock sync.Mutex
|
||||
results := make([]GetPeersResult, 0, _len)
|
||||
for i := 0; i < wlen; i++ {
|
||||
go func() {
|
||||
for tracker := range reqs {
|
||||
resp, err := getPeers(ctx, wg, tracker, id, infohash)
|
||||
lock.Lock()
|
||||
results = append(results, GetPeersResult{
|
||||
Tracker: tracker,
|
||||
Error: err,
|
||||
Resp: resp,
|
||||
})
|
||||
lock.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(reqs)
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
func getPeers(ctx context.Context, wg *sync.WaitGroup, tracker string,
|
||||
nodeID, infoHash metainfo.Hash) (resp AnnounceResponse, err error) {
|
||||
defer wg.Done()
|
||||
|
||||
client, err := NewClient(tracker, ClientConfig{ID: nodeID})
|
||||
if err == nil {
|
||||
resp, err = client.Announce(ctx, AnnounceRequest{InfoHash: infoHash})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build go1.13
|
||||
// +build go1.13
|
||||
|
||||
package httptracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewRequestWithContext returns a new Request given a method, URL, and optional body.
|
||||
func NewRequestWithContext(c context.Context, method, url string, body io.Reader) (*http.Request, error) {
|
||||
return http.NewRequestWithContext(c, method, url, body)
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !go1.13
|
||||
// +build !go1.13
|
||||
|
||||
package httptracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewRequestWithContext returns a new Request given a method, URL, and optional body.
|
||||
func NewRequestWithContext(c context.Context, method, url string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return req.WithContext(c), nil
|
||||
}
|
@ -21,15 +21,14 @@ package httptracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// AnnounceRequest is the tracker announce requests.
|
||||
@ -101,7 +100,7 @@ type AnnounceRequest struct {
|
||||
|
||||
// ToQuery converts the Request to URL Query.
|
||||
func (r AnnounceRequest) ToQuery() (vs url.Values) {
|
||||
vs = make(url.Values, 10)
|
||||
vs = make(url.Values, 9)
|
||||
vs.Set("info_hash", r.InfoHash.BytesString())
|
||||
vs.Set("peer_id", r.PeerID.BytesString())
|
||||
vs.Set("uploaded", strconv.FormatInt(r.Uploaded, 10))
|
||||
@ -268,7 +267,6 @@ func (sr ScrapeResponse) EncodeTo(w io.Writer) (err error) {
|
||||
|
||||
// Client represents a tracker client based on HTTP/HTTPS.
|
||||
type Client struct {
|
||||
Client *http.Client
|
||||
ID metainfo.Hash
|
||||
AnnounceURL string
|
||||
ScrapeURL string
|
||||
@ -278,15 +276,11 @@ type Client struct {
|
||||
//
|
||||
// scrapeURL may be empty, which will replace the "announce" in announceURL
|
||||
// with "scrape" to generate the scrapeURL.
|
||||
func NewClient(id metainfo.Hash, announceURL, scrapeURL string) *Client {
|
||||
func NewClient(announceURL, scrapeURL string) *Client {
|
||||
if scrapeURL == "" {
|
||||
scrapeURL = strings.Replace(announceURL, "announce", "scrape", -1)
|
||||
}
|
||||
|
||||
if id.IsZero() {
|
||||
id = metainfo.NewRandomHash()
|
||||
}
|
||||
|
||||
id := metainfo.NewRandomHash()
|
||||
return &Client{AnnounceURL: announceURL, ScrapeURL: scrapeURL, ID: id}
|
||||
}
|
||||
|
||||
@ -295,54 +289,41 @@ func (t *Client) Close() error { return nil }
|
||||
func (t *Client) String() string { return t.AnnounceURL }
|
||||
|
||||
func (t *Client) send(c context.Context, u string, vs url.Values, r interface{}) (err error) {
|
||||
var url string
|
||||
if strings.IndexByte(u, '?') < 0 {
|
||||
url = fmt.Sprintf("%s?%s", u, vs.Encode())
|
||||
} else {
|
||||
url = fmt.Sprintf("%s&%s", u, vs.Encode())
|
||||
sym := "?"
|
||||
if strings.IndexByte(u, '?') > 0 {
|
||||
sym = "&"
|
||||
}
|
||||
|
||||
req, err := NewRequestWithContext(c, http.MethodGet, url, nil)
|
||||
resp, err := http.Get(u + sym + vs.Encode())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var resp *http.Response
|
||||
if t.Client == nil {
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
} else {
|
||||
resp, err = t.Client.Do(req)
|
||||
}
|
||||
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
return bencode.NewDecoder(resp.Body).Decode(r)
|
||||
}
|
||||
|
||||
// Announce sends a Announce request to the tracker.
|
||||
func (t *Client) Announce(c context.Context, req AnnounceRequest) (resp AnnounceResponse, err error) {
|
||||
if req.InfoHash.IsZero() {
|
||||
panic("infohash is ZERO")
|
||||
func (t *Client) Announce(c context.Context, req AnnounceRequest) (
|
||||
resp AnnounceResponse, err error) {
|
||||
if req.PeerID.IsZero() {
|
||||
if t.ID.IsZero() {
|
||||
req.PeerID = metainfo.NewRandomHash()
|
||||
} else {
|
||||
req.PeerID = t.ID
|
||||
}
|
||||
}
|
||||
|
||||
req.PeerID = t.ID
|
||||
err = t.send(c, t.AnnounceURL, req.ToQuery(), &resp)
|
||||
return
|
||||
}
|
||||
|
||||
// Scrape sends a Scrape request to the tracker.
|
||||
func (t *Client) Scrape(c context.Context, infohashes []metainfo.Hash) (resp ScrapeResponse, err error) {
|
||||
func (t *Client) Scrape(c context.Context, infohashes []metainfo.Hash) (
|
||||
resp ScrapeResponse, err error) {
|
||||
hs := make([]string, len(infohashes))
|
||||
for i, h := range infohashes {
|
||||
hs[i] = h.BytesString()
|
||||
}
|
||||
|
||||
err = t.send(c, t.ScrapeURL, url.Values{"info_hash": hs}, &resp)
|
||||
return
|
||||
}
|
||||
|
@ -15,26 +15,43 @@
|
||||
package httptracker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
var errInvalidPeer = errors.New("invalid bt peer information format")
|
||||
var errInvalidPeer = errors.New("invalid peer information format")
|
||||
|
||||
// Peer is a tracker peer.
|
||||
type Peer struct {
|
||||
ID string `bencode:"peer id"` // BEP 3, the peer's self-selected ID.
|
||||
IP string `bencode:"ip"` // BEP 3, an IP address or dns name.
|
||||
Port uint16 `bencode:"port"` // BEP 3
|
||||
// ID is the peer's self-selected ID.
|
||||
ID string `bencode:"peer id"` // BEP 3
|
||||
|
||||
// IP is the IP address or dns name.
|
||||
IP string `bencode:"ip"` // BEP 3
|
||||
Port uint16 `bencode:"port"` // BEP 3
|
||||
}
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(Peers)
|
||||
_ bencode.Unmarshaler = new(Peers)
|
||||
)
|
||||
// Addresses returns the list of the addresses that the peer listens on.
|
||||
func (p Peer) Addresses() (addrs []metainfo.Address, err error) {
|
||||
if ip := net.ParseIP(p.IP); len(ip) != 0 {
|
||||
return []metainfo.Address{{IP: ip, Port: p.Port}}, nil
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(p.IP)
|
||||
if _len := len(ips); err == nil && len(ips) != 0 {
|
||||
addrs = make([]metainfo.Address, _len)
|
||||
for i, ip := range ips {
|
||||
addrs[i] = metainfo.Address{IP: ip, Port: p.Port}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Peers is a set of the peers.
|
||||
type Peers []Peer
|
||||
@ -48,17 +65,21 @@ func (ps *Peers) UnmarshalBencode(b []byte) (err error) {
|
||||
|
||||
switch vs := v.(type) {
|
||||
case string: // BEP 23
|
||||
var addrs metainfo.CompactIPv4Addrs
|
||||
if err = addrs.UnmarshalBinary([]byte(vs)); err != nil {
|
||||
return err
|
||||
_len := len(vs)
|
||||
if _len%6 != 0 {
|
||||
return metainfo.ErrInvalidAddr
|
||||
}
|
||||
|
||||
peers := make(Peers, len(addrs))
|
||||
for i, addr := range addrs {
|
||||
peers[i] = Peer{IP: addr.IP.String(), Port: addr.Port}
|
||||
peers := make(Peers, 0, _len/6)
|
||||
for i := 0; i < _len; i += 6 {
|
||||
var addr metainfo.Address
|
||||
if err = addr.UnmarshalBinary([]byte(vs[i : i+6])); err != nil {
|
||||
return
|
||||
}
|
||||
peers = append(peers, Peer{IP: addr.IP.String(), Port: addr.Port})
|
||||
}
|
||||
|
||||
*ps = peers
|
||||
|
||||
case []interface{}: // BEP 3
|
||||
peers := make(Peers, len(vs))
|
||||
for i, p := range vs {
|
||||
@ -85,7 +106,6 @@ func (ps *Peers) UnmarshalBencode(b []byte) (err error) {
|
||||
peers[i] = Peer{ID: pid, IP: ip, Port: uint16(port)}
|
||||
}
|
||||
*ps = peers
|
||||
|
||||
default:
|
||||
return errInvalidPeer
|
||||
}
|
||||
@ -94,32 +114,38 @@ func (ps *Peers) UnmarshalBencode(b []byte) (err error) {
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ps Peers) MarshalBencode() (b []byte, err error) {
|
||||
// BEP 23
|
||||
if b, err = ps.marshalCompactBencode(); err == nil {
|
||||
return
|
||||
for _, p := range ps {
|
||||
if p.ID == "" {
|
||||
return ps.marshalCompactBencode() // BEP 23
|
||||
}
|
||||
}
|
||||
|
||||
// BEP 3
|
||||
return bencode.EncodeBytes([]Peer(ps))
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 64*len(ps)))
|
||||
buf.WriteByte('l')
|
||||
for _, p := range ps {
|
||||
if err = bencode.NewEncoder(buf).Encode(p); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
buf.WriteByte('e')
|
||||
b = buf.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
func (ps Peers) marshalCompactBencode() (b []byte, err error) {
|
||||
addrs := make(metainfo.CompactIPv4Addrs, len(ps))
|
||||
for i, p := range ps {
|
||||
ip := net.ParseIP(p.IP).To4()
|
||||
if ip == nil {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 6*len(ps)))
|
||||
for _, peer := range ps {
|
||||
ip := net.ParseIP(peer.IP).To4()
|
||||
if len(ip) == 0 {
|
||||
return nil, errInvalidPeer
|
||||
}
|
||||
addrs[i] = metainfo.CompactAddr{IP: ip, Port: p.Port}
|
||||
buf.Write(ip[:])
|
||||
binary.Write(buf, binary.BigEndian, peer.Port)
|
||||
}
|
||||
return addrs.MarshalBencode()
|
||||
return bencode.EncodeBytes(buf.Bytes())
|
||||
}
|
||||
|
||||
var (
|
||||
_ bencode.Marshaler = new(Peers6)
|
||||
_ bencode.Unmarshaler = new(Peers6)
|
||||
)
|
||||
|
||||
// Peers6 is a set of the peers for IPv6 in the compact case.
|
||||
//
|
||||
// BEP 7
|
||||
@ -132,29 +158,35 @@ func (ps *Peers6) UnmarshalBencode(b []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
var addrs metainfo.CompactIPv6Addrs
|
||||
if err = addrs.UnmarshalBinary([]byte(s)); err != nil {
|
||||
return err
|
||||
_len := len(s)
|
||||
if _len%18 != 0 {
|
||||
return metainfo.ErrInvalidAddr
|
||||
}
|
||||
|
||||
peers := make(Peers6, len(addrs))
|
||||
for i, addr := range addrs {
|
||||
peers[i] = Peer{IP: addr.IP.String(), Port: addr.Port}
|
||||
peers := make(Peers6, 0, _len/18)
|
||||
for i := 0; i < _len; i += 18 {
|
||||
var addr metainfo.Address
|
||||
if err = addr.UnmarshalBinary([]byte(s[i : i+18])); err != nil {
|
||||
return
|
||||
}
|
||||
peers = append(peers, Peer{IP: addr.IP.String(), Port: addr.Port})
|
||||
}
|
||||
|
||||
*ps = peers
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ps Peers6) MarshalBencode() (b []byte, err error) {
|
||||
addrs := make(metainfo.CompactIPv6Addrs, len(ps))
|
||||
for i, p := range ps {
|
||||
ip := net.ParseIP(p.IP)
|
||||
if ip == nil {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 18*len(ps)))
|
||||
for _, peer := range ps {
|
||||
ip := net.ParseIP(peer.IP).To16()
|
||||
if len(ip) == 0 {
|
||||
return nil, errInvalidPeer
|
||||
}
|
||||
addrs[i] = metainfo.CompactAddr{IP: ip, Port: p.Port}
|
||||
|
||||
buf.Write(ip[:])
|
||||
binary.Write(buf, binary.BigEndian, peer.Port)
|
||||
}
|
||||
return addrs.MarshalBencode()
|
||||
return bencode.EncodeBytes(buf.Bytes())
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ import (
|
||||
|
||||
func TestPeers(t *testing.T) {
|
||||
peers := Peers{
|
||||
{IP: "1.1.1.1", Port: 80},
|
||||
{IP: "2.2.2.2", Port: 81},
|
||||
{ID: "123", IP: "1.1.1.1", Port: 80},
|
||||
{ID: "456", IP: "2.2.2.2", Port: 81},
|
||||
}
|
||||
|
||||
b, err := peers.MarshalBencode()
|
||||
@ -71,6 +71,5 @@ func TestPeers6(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(ps, peers) {
|
||||
t.Errorf("%v != %v", ps, peers)
|
||||
t.Error(string(b))
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
func TestHTTPAnnounceRequest(t *testing.T) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -23,12 +23,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/go-bt/tracker/httptracker"
|
||||
"github.com/xgfone/go-bt/tracker/udptracker"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
"github.com/xgfone/bt/tracker/httptracker"
|
||||
"github.com/xgfone/bt/tracker/udptracker"
|
||||
)
|
||||
|
||||
// Predefine some announce events.
|
||||
@ -53,9 +52,9 @@ type AnnounceRequest struct {
|
||||
Left int64 // Required, but default: 0, which should be only used for test or last.
|
||||
Event uint32 // Required, but default: 0
|
||||
|
||||
IP net.IP // Optional
|
||||
IP net.Addr // Optional
|
||||
Key int32 // Optional
|
||||
NumWant int32 // Optional
|
||||
NumWant int32 // Optional, BEP 15: -1 for default. But we use 0 as default.
|
||||
Port uint16 // Optional
|
||||
}
|
||||
|
||||
@ -77,7 +76,6 @@ func (ar AnnounceRequest) ToHTTPAnnounceRequest() httptracker.AnnounceRequest {
|
||||
Event: ar.Event,
|
||||
NumWant: ar.NumWant,
|
||||
Key: ar.Key,
|
||||
Compact: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,6 +88,7 @@ func (ar AnnounceRequest) ToUDPAnnounceRequest() udptracker.AnnounceRequest {
|
||||
Left: ar.Left,
|
||||
Uploaded: ar.Uploaded,
|
||||
Event: ar.Event,
|
||||
IP: ar.IP,
|
||||
Key: ar.Key,
|
||||
NumWant: ar.NumWant,
|
||||
Port: ar.Port,
|
||||
@ -100,10 +99,10 @@ func (ar AnnounceRequest) ToUDPAnnounceRequest() udptracker.AnnounceRequest {
|
||||
//
|
||||
// BEP 3, 15
|
||||
type AnnounceResponse struct {
|
||||
Interval uint32 // Reflush Interval
|
||||
Leechers uint32 // Incomplete
|
||||
Seeders uint32 // Complete
|
||||
Addresses []metainfo.HostAddr
|
||||
Interval uint32
|
||||
Leechers uint32
|
||||
Seeders uint32
|
||||
Addresses []metainfo.Address
|
||||
}
|
||||
|
||||
// FromHTTPAnnounceResponse sets itself from r.
|
||||
@ -111,12 +110,14 @@ func (ar *AnnounceResponse) FromHTTPAnnounceResponse(r httptracker.AnnounceRespo
|
||||
ar.Interval = r.Interval
|
||||
ar.Leechers = r.Incomplete
|
||||
ar.Seeders = r.Complete
|
||||
ar.Addresses = make([]metainfo.HostAddr, 0, len(r.Peers)+len(r.Peers6))
|
||||
for _, p := range r.Peers {
|
||||
ar.Addresses = append(ar.Addresses, metainfo.NewHostAddr(p.IP, p.Port))
|
||||
ar.Addresses = make([]metainfo.Address, 0, len(r.Peers)+len(r.Peers6))
|
||||
for _, peer := range r.Peers {
|
||||
addrs, _ := peer.Addresses()
|
||||
ar.Addresses = append(ar.Addresses, addrs...)
|
||||
}
|
||||
for _, p := range r.Peers6 {
|
||||
ar.Addresses = append(ar.Addresses, metainfo.NewHostAddr(p.IP, p.Port))
|
||||
for _, peer := range r.Peers6 {
|
||||
addrs, _ := peer.Addresses()
|
||||
ar.Addresses = append(ar.Addresses, addrs...)
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,11 +126,7 @@ func (ar *AnnounceResponse) FromUDPAnnounceResponse(r udptracker.AnnounceRespons
|
||||
ar.Interval = r.Interval
|
||||
ar.Leechers = r.Leechers
|
||||
ar.Seeders = r.Seeders
|
||||
|
||||
ar.Addresses = make([]metainfo.HostAddr, len(r.Addresses))
|
||||
for i, a := range r.Addresses {
|
||||
ar.Addresses[i] = metainfo.NewHostAddr(a.IP.String(), a.Port)
|
||||
}
|
||||
ar.Addresses = r.Addresses
|
||||
}
|
||||
|
||||
// ScrapeResponseResult is a commont Scrape response result.
|
||||
@ -173,7 +170,8 @@ func (sr ScrapeResponse) FromHTTPScrapeResponse(r httptracker.ScrapeResponse) {
|
||||
}
|
||||
|
||||
// FromUDPScrapeResponse sets itself from hs and r.
|
||||
func (sr ScrapeResponse) FromUDPScrapeResponse(hs []metainfo.Hash, r []udptracker.ScrapeResponse) {
|
||||
func (sr ScrapeResponse) FromUDPScrapeResponse(hs []metainfo.Hash,
|
||||
r []udptracker.ScrapeResponse) {
|
||||
klen := len(hs)
|
||||
if _len := len(r); _len < klen {
|
||||
klen = _len
|
||||
@ -196,37 +194,42 @@ type Client interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ClientConfig is used to configure the defalut client implementation.
|
||||
type ClientConfig struct {
|
||||
ID metainfo.Hash // The ID of the local client peer.
|
||||
}
|
||||
|
||||
// NewClient returns a new Client.
|
||||
//
|
||||
// If id is ZERO, use a random hash instead.
|
||||
// If client is nil, use http.DefaultClient instead for the http tracker.
|
||||
func NewClient(connURL string, id metainfo.Hash, client *http.Client) (c Client, err error) {
|
||||
func NewClient(connURL string, conf ...ClientConfig) (c Client, err error) {
|
||||
var id metainfo.Hash
|
||||
if len(conf) > 0 {
|
||||
id = conf[0].ID
|
||||
}
|
||||
|
||||
u, err := url.Parse(connURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tclient := &tclient{url: connURL}
|
||||
switch u.Scheme {
|
||||
case "http", "https":
|
||||
tclient.http = httptracker.NewClient(id, connURL, "")
|
||||
tclient.http.Client = client
|
||||
|
||||
case "udp", "udp4", "udp6":
|
||||
tclient.udp, err = udptracker.NewClientByDial(u.Scheme, u.Host, id)
|
||||
if err != nil {
|
||||
return
|
||||
if err == nil {
|
||||
switch u.Scheme {
|
||||
case "http", "https":
|
||||
c = &tclient{url: connURL, http: httptracker.NewClient(connURL, "")}
|
||||
if !id.IsZero() {
|
||||
c.(*tclient).http.ID = id
|
||||
}
|
||||
case "udp", "udp4", "udp6":
|
||||
var utc *udptracker.Client
|
||||
config := udptracker.ClientConfig{ID: id}
|
||||
utc, err = udptracker.NewClientByDial(u.Scheme, u.Host, config)
|
||||
if err == nil {
|
||||
var e []udptracker.Extension
|
||||
if p := u.RequestURI(); p != "" {
|
||||
e = []udptracker.Extension{udptracker.NewURLData([]byte(p))}
|
||||
}
|
||||
c = &tclient{url: connURL, exts: e, udp: utc}
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown url scheme '%s'", u.Scheme)
|
||||
}
|
||||
|
||||
if p := u.RequestURI(); p != "" {
|
||||
tclient.exts = []udptracker.Extension{udptracker.NewURLData([]byte(p))}
|
||||
}
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("unknown url scheme '%s'", u.Scheme)
|
||||
}
|
||||
|
||||
return tclient, nil
|
||||
return
|
||||
}
|
||||
|
||||
type tclient struct {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -22,14 +22,15 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/go-bt/tracker/udptracker"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
"github.com/xgfone/bt/tracker/udptracker"
|
||||
)
|
||||
|
||||
type testHandler struct{}
|
||||
|
||||
func (testHandler) OnConnect(raddr *net.UDPAddr) (err error) { return }
|
||||
func (testHandler) OnAnnounce(raddr *net.UDPAddr, req udptracker.AnnounceRequest) (r udptracker.AnnounceResponse, err error) {
|
||||
func (testHandler) OnAnnounce(raddr *net.UDPAddr, req udptracker.AnnounceRequest) (
|
||||
r udptracker.AnnounceResponse, err error) {
|
||||
if req.Port != 80 {
|
||||
err = errors.New("port is not 80")
|
||||
return
|
||||
@ -50,11 +51,12 @@ func (testHandler) OnAnnounce(raddr *net.UDPAddr, req udptracker.AnnounceRequest
|
||||
Interval: 1,
|
||||
Leechers: 2,
|
||||
Seeders: 3,
|
||||
Addresses: []metainfo.CompactAddr{{IP: net.ParseIP("127.0.0.1"), Port: 8000}},
|
||||
Addresses: []metainfo.Address{{IP: net.ParseIP("127.0.0.1"), Port: 8000}},
|
||||
}
|
||||
return
|
||||
}
|
||||
func (testHandler) OnScrap(raddr *net.UDPAddr, infohashes []metainfo.Hash) (rs []udptracker.ScrapeResponse, err error) {
|
||||
func (testHandler) OnScrap(raddr *net.UDPAddr, infohashes []metainfo.Hash) (
|
||||
rs []udptracker.ScrapeResponse, err error) {
|
||||
rs = make([]udptracker.ScrapeResponse, len(infohashes))
|
||||
for i := range infohashes {
|
||||
rs[i] = udptracker.ScrapeResponse{
|
||||
@ -72,7 +74,7 @@ func ExampleClient() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server := udptracker.NewServer(sconn, testHandler{}, 0)
|
||||
server := udptracker.NewServer(sconn, testHandler{})
|
||||
defer server.Close()
|
||||
go server.Run()
|
||||
|
||||
@ -80,14 +82,14 @@ func ExampleClient() {
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// Create a client and dial to the UDP tracker server.
|
||||
client, err := NewClient("udp://127.0.0.1:8000/path?a=1&b=2", metainfo.Hash{}, nil)
|
||||
client, err := NewClient("udp://127.0.0.1:8000/path?a=1&b=2")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Send the ANNOUNCE request to the UDP tracker server,
|
||||
// and get the ANNOUNCE response.
|
||||
req := AnnounceRequest{InfoHash: metainfo.NewRandomHash(), IP: net.ParseIP("127.0.0.1"), Port: 80}
|
||||
req := AnnounceRequest{IP: net.ParseIP("127.0.0.1"), Port: 80}
|
||||
resp, err := client.Announce(context.Background(), req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -97,7 +99,7 @@ func ExampleClient() {
|
||||
fmt.Printf("Leechers: %d\n", resp.Leechers)
|
||||
fmt.Printf("Seeders: %d\n", resp.Seeders)
|
||||
for i, addr := range resp.Addresses {
|
||||
fmt.Printf("Address[%d].IP: %s\n", i, addr.Host)
|
||||
fmt.Printf("Address[%d].IP: %s\n", i, addr.IP.String())
|
||||
fmt.Printf("Address[%d].Port: %d\n", i, addr.Port)
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -25,11 +25,9 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
const maxBufSize = 2048
|
||||
|
||||
// ProtocolID is magic constant for the udp tracker connection.
|
||||
//
|
||||
// BEP 15
|
||||
@ -57,15 +55,16 @@ type AnnounceRequest struct {
|
||||
Uploaded int64
|
||||
Event uint32
|
||||
|
||||
IP net.Addr
|
||||
Key int32
|
||||
NumWant int32 // -1 for default and use -1 instead if 0
|
||||
NumWant int32 // -1 for default
|
||||
Port uint16
|
||||
|
||||
Exts []Extension // BEP 41
|
||||
}
|
||||
|
||||
// DecodeFrom decodes the request from b.
|
||||
func (r *AnnounceRequest) DecodeFrom(b []byte) {
|
||||
func (r *AnnounceRequest) DecodeFrom(b []byte, ipv4 bool) {
|
||||
r.InfoHash = metainfo.NewHash(b[0:20])
|
||||
r.PeerID = metainfo.NewHash(b[20:40])
|
||||
r.Downloaded = int64(binary.BigEndian.Uint64(b[40:48]))
|
||||
@ -73,13 +72,21 @@ func (r *AnnounceRequest) DecodeFrom(b []byte) {
|
||||
r.Uploaded = int64(binary.BigEndian.Uint64(b[56:64]))
|
||||
r.Event = binary.BigEndian.Uint32(b[64:68])
|
||||
|
||||
// ignore b[68:72] // 4 bytes
|
||||
if ipv4 {
|
||||
r.IP = make(net.Addr, net.Addrv4len)
|
||||
copy(r.IP, b[68:72])
|
||||
b = b[72:]
|
||||
} else {
|
||||
r.IP = make(net.Addr, net.Addrv6len)
|
||||
copy(r.IP, b[68:84])
|
||||
b = b[84:]
|
||||
}
|
||||
|
||||
r.Key = int32(binary.BigEndian.Uint32(b[72:76]))
|
||||
r.NumWant = int32(binary.BigEndian.Uint32(b[76:80]))
|
||||
r.Port = binary.BigEndian.Uint16(b[80:82])
|
||||
r.Key = int32(binary.BigEndian.Uint32(b[0:4]))
|
||||
r.NumWant = int32(binary.BigEndian.Uint32(b[4:8]))
|
||||
r.Port = binary.BigEndian.Uint16(b[8:10])
|
||||
|
||||
b = b[82:]
|
||||
b = b[10:]
|
||||
for len(b) > 0 {
|
||||
var ext Extension
|
||||
parsed := ext.DecodeFrom(b)
|
||||
@ -90,25 +97,26 @@ func (r *AnnounceRequest) DecodeFrom(b []byte) {
|
||||
|
||||
// EncodeTo encodes the request to buf.
|
||||
func (r AnnounceRequest) EncodeTo(buf *bytes.Buffer) {
|
||||
if r.NumWant <= 0 {
|
||||
r.NumWant = -1
|
||||
buf.Grow(82)
|
||||
buf.Write(r.InfoHash[:])
|
||||
buf.Write(r.PeerID[:])
|
||||
|
||||
binary.Write(buf, binary.BigEndian, r.Downloaded)
|
||||
binary.Write(buf, binary.BigEndian, r.Left)
|
||||
binary.Write(buf, binary.BigEndian, r.Uploaded)
|
||||
binary.Write(buf, binary.BigEndian, r.Event)
|
||||
|
||||
if ip := r.IP.To4(); ip != nil {
|
||||
buf.Write(ip[:])
|
||||
} else {
|
||||
buf.Write(r.IP[:])
|
||||
}
|
||||
|
||||
buf.Grow(82)
|
||||
buf.Write(r.InfoHash[:]) // 20: 16 - 36
|
||||
buf.Write(r.PeerID[:]) // 20: 36 - 56
|
||||
binary.Write(buf, binary.BigEndian, r.Key)
|
||||
binary.Write(buf, binary.BigEndian, r.NumWant)
|
||||
binary.Write(buf, binary.BigEndian, r.Port)
|
||||
|
||||
binary.Write(buf, binary.BigEndian, r.Downloaded) // 8: 56 - 64
|
||||
binary.Write(buf, binary.BigEndian, r.Left) // 8: 64 - 72
|
||||
binary.Write(buf, binary.BigEndian, r.Uploaded) // 8: 72 - 80
|
||||
binary.Write(buf, binary.BigEndian, r.Event) // 4: 80 - 84
|
||||
binary.Write(buf, binary.BigEndian, uint32(0)) // 4: 84 - 88
|
||||
|
||||
binary.Write(buf, binary.BigEndian, r.Key) // 4: 88 - 92
|
||||
binary.Write(buf, binary.BigEndian, r.NumWant) // 4: 92 - 96
|
||||
binary.Write(buf, binary.BigEndian, r.Port) // 2: 96 - 98
|
||||
|
||||
for _, ext := range r.Exts { // N: 98 -
|
||||
for _, ext := range r.Exts {
|
||||
ext.EncodeTo(buf)
|
||||
}
|
||||
}
|
||||
@ -120,49 +128,45 @@ type AnnounceResponse struct {
|
||||
Interval uint32
|
||||
Leechers uint32
|
||||
Seeders uint32
|
||||
Addresses []metainfo.CompactAddr
|
||||
Addresses []metainfo.Address
|
||||
}
|
||||
|
||||
// EncodeTo encodes the response to buf.
|
||||
func (r AnnounceResponse) EncodeTo(buf *bytes.Buffer, ipv4 bool) {
|
||||
func (r AnnounceResponse) EncodeTo(buf *bytes.Buffer) {
|
||||
buf.Grow(12 + len(r.Addresses)*18)
|
||||
binary.Write(buf, binary.BigEndian, r.Interval)
|
||||
binary.Write(buf, binary.BigEndian, r.Leechers)
|
||||
binary.Write(buf, binary.BigEndian, r.Seeders)
|
||||
for i, addr := range r.Addresses {
|
||||
if ipv4 {
|
||||
addr.IP = addr.IP.To4()
|
||||
for _, addr := range r.Addresses {
|
||||
if ip := addr.IP.To4(); ip != nil {
|
||||
buf.Write(ip[:])
|
||||
} else {
|
||||
addr.IP = addr.IP.To16()
|
||||
buf.Write(addr.IP[:])
|
||||
}
|
||||
if len(addr.IP) == 0 {
|
||||
panic(fmt.Errorf("invalid ip '%s'", r.Addresses[i].IP.String()))
|
||||
}
|
||||
addr.WriteBinary(buf)
|
||||
binary.Write(buf, binary.BigEndian, addr.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeFrom decodes the response from b.
|
||||
func (r *AnnounceResponse) DecodeFrom(b []byte, ipv4 bool) {
|
||||
r.Interval = binary.BigEndian.Uint32(b[:4]) // 4: 8 - 12
|
||||
r.Leechers = binary.BigEndian.Uint32(b[4:8]) // 4: 12 - 16
|
||||
r.Seeders = binary.BigEndian.Uint32(b[8:12]) // 4: 16 - 20
|
||||
r.Interval = binary.BigEndian.Uint32(b[:4])
|
||||
r.Leechers = binary.BigEndian.Uint32(b[4:8])
|
||||
r.Seeders = binary.BigEndian.Uint32(b[8:12])
|
||||
|
||||
b = b[12:] // N*(6|18): 20 -
|
||||
iplen := net.IPv6len
|
||||
b = b[12:]
|
||||
iplen := net.Addrv6len
|
||||
if ipv4 {
|
||||
iplen = net.IPv4len
|
||||
iplen = net.Addrv4len
|
||||
}
|
||||
|
||||
_len := len(b)
|
||||
step := iplen + 2
|
||||
r.Addresses = make([]metainfo.CompactAddr, 0, _len/step)
|
||||
r.Addresses = make([]metainfo.Address, 0, _len/step)
|
||||
for i := step; i <= _len; i += step {
|
||||
var addr metainfo.CompactAddr
|
||||
if err := addr.UnmarshalBinary(b[i-step : i]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r.Addresses = append(r.Addresses, addr)
|
||||
ip := make(net.Addr, iplen)
|
||||
copy(ip, b[i-step:i-2])
|
||||
port := binary.BigEndian.Uint16(b[i-2 : i])
|
||||
r.Addresses = append(r.Addresses, metainfo.Address{IP: ip, Port: port})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -19,38 +19,49 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// NewClientByDial returns a new Client by dialing.
|
||||
func NewClientByDial(network, address string, id metainfo.Hash) (*Client, error) {
|
||||
func NewClientByDial(network, address string, c ...ClientConfig) (*Client, error) {
|
||||
conn, err := net.Dial(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewClient(conn.(*net.UDPConn), id), nil
|
||||
|
||||
return NewClient(conn.(*net.UDPConn), c...), nil
|
||||
}
|
||||
|
||||
// NewClient returns a new Client.
|
||||
func NewClient(conn *net.UDPConn, id metainfo.Hash) *Client {
|
||||
func NewClient(conn *net.UDPConn, c ...ClientConfig) *Client {
|
||||
var conf ClientConfig
|
||||
conf.set(c...)
|
||||
ipv4 := strings.Contains(conn.LocalAddr().String(), ".")
|
||||
if id.IsZero() {
|
||||
id = metainfo.NewRandomHash()
|
||||
return &Client{conn: conn, conf: conf, ipv4: ipv4}
|
||||
}
|
||||
|
||||
// ClientConfig is used to configure the Client.
|
||||
type ClientConfig struct {
|
||||
ID metainfo.Hash
|
||||
MaxBufSize int // Default: 2048
|
||||
}
|
||||
|
||||
func (c *ClientConfig) set(conf ...ClientConfig) {
|
||||
if len(conf) > 0 {
|
||||
*c = conf[0]
|
||||
}
|
||||
|
||||
return &Client{
|
||||
MaxBufSize: maxBufSize,
|
||||
|
||||
conn: conn,
|
||||
ipv4: ipv4,
|
||||
id: id,
|
||||
if c.ID.IsZero() {
|
||||
c.ID = metainfo.NewRandomHash()
|
||||
}
|
||||
if c.MaxBufSize <= 0 {
|
||||
c.MaxBufSize = 2048
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,14 +72,12 @@ func NewClient(conn *net.UDPConn, id metainfo.Hash) *Client {
|
||||
//
|
||||
// BEP 15
|
||||
type Client struct {
|
||||
MaxBufSize int // Default: 2048
|
||||
|
||||
id metainfo.Hash
|
||||
ipv4 bool
|
||||
conf ClientConfig
|
||||
conn *net.UDPConn
|
||||
last time.Time
|
||||
cid uint64
|
||||
tid uint32
|
||||
ipv4 bool
|
||||
}
|
||||
|
||||
// Close closes the UDP tracker client.
|
||||
@ -131,21 +140,21 @@ func (utc *Client) connect(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
data = data[:n]
|
||||
switch action := binary.BigEndian.Uint32(data[:4]); action {
|
||||
switch binary.BigEndian.Uint32(data[:4]) {
|
||||
case ActionConnect:
|
||||
case ActionError:
|
||||
_, reason := utc.parseError(data[4:])
|
||||
return errors.New(reason)
|
||||
default:
|
||||
return fmt.Errorf("tracker response is not connect action: %d", action)
|
||||
return errors.New("tracker response not connect action")
|
||||
}
|
||||
|
||||
if n < 16 {
|
||||
return io.ErrShortBuffer
|
||||
}
|
||||
|
||||
if _tid := binary.BigEndian.Uint32(data[4:8]); _tid != tid {
|
||||
return fmt.Errorf("expect transaction id %d, but got %d", tid, _tid)
|
||||
if binary.BigEndian.Uint32(data[4:8]) != tid {
|
||||
return errors.New("invalid transaction id")
|
||||
}
|
||||
|
||||
utc.cid = binary.BigEndian.Uint64(data[8:16])
|
||||
@ -163,7 +172,8 @@ func (utc *Client) getConnectionID(ctx context.Context) (cid uint64, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r AnnounceResponse, err error) {
|
||||
func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (
|
||||
r AnnounceResponse, err error) {
|
||||
cid, err := utc.getConnectionID(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@ -171,21 +181,16 @@ func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r Announc
|
||||
|
||||
tid := utc.getTranID()
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 110))
|
||||
binary.Write(buf, binary.BigEndian, cid) // 8: 0 - 8
|
||||
binary.Write(buf, binary.BigEndian, ActionAnnounce) // 4: 8 - 12
|
||||
binary.Write(buf, binary.BigEndian, tid) // 4: 12 - 16
|
||||
binary.Write(buf, binary.BigEndian, cid)
|
||||
binary.Write(buf, binary.BigEndian, ActionAnnounce)
|
||||
binary.Write(buf, binary.BigEndian, tid)
|
||||
req.EncodeTo(buf)
|
||||
b := buf.Bytes()
|
||||
if err = utc.send(b); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
bufSize := utc.MaxBufSize
|
||||
if bufSize <= 0 {
|
||||
bufSize = maxBufSize
|
||||
}
|
||||
|
||||
data := make([]byte, bufSize)
|
||||
data := make([]byte, utc.conf.MaxBufSize)
|
||||
n, err := utc.readResp(ctx, data)
|
||||
if err != nil {
|
||||
return
|
||||
@ -195,14 +200,14 @@ func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r Announc
|
||||
}
|
||||
|
||||
data = data[:n]
|
||||
switch action := binary.BigEndian.Uint32(data[:4]); action {
|
||||
switch binary.BigEndian.Uint32(data[:4]) {
|
||||
case ActionAnnounce:
|
||||
case ActionError:
|
||||
_, reason := utc.parseError(data[4:])
|
||||
err = errors.New(reason)
|
||||
return
|
||||
default:
|
||||
err = fmt.Errorf("tracker response is not connect action: %d", action)
|
||||
err = errors.New("tracker response not connect action")
|
||||
return
|
||||
}
|
||||
|
||||
@ -211,8 +216,8 @@ func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r Announc
|
||||
return
|
||||
}
|
||||
|
||||
if _tid := binary.BigEndian.Uint32(data[4:8]); _tid != tid {
|
||||
err = fmt.Errorf("expect transaction id %d, but got %d", tid, _tid)
|
||||
if binary.BigEndian.Uint32(data[4:8]) != tid {
|
||||
err = errors.New("invalid transaction id")
|
||||
return
|
||||
}
|
||||
|
||||
@ -223,20 +228,19 @@ func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r Announc
|
||||
// Announce sends a Announce request to the tracker.
|
||||
//
|
||||
// Notice:
|
||||
// 1. if it does not connect to the UDP tracker server, it will connect to it,
|
||||
// then send the ANNOUNCE request.
|
||||
// 2. If returning an error, you should retry it.
|
||||
// See http://www.bittorrent.org/beps/bep_0015.html#time-outs
|
||||
// 1. if it does not connect to the UDP tracker server, it will connect to it,
|
||||
// then send the ANNOUNCE request.
|
||||
// 2. If returning an error, you should retry it.
|
||||
// See http://www.bittorrent.org/beps/bep_0015.html#time-outs
|
||||
func (utc *Client) Announce(c context.Context, r AnnounceRequest) (AnnounceResponse, error) {
|
||||
if r.InfoHash.IsZero() {
|
||||
panic("infohash is ZERO")
|
||||
if r.PeerID.IsZero() {
|
||||
r.PeerID = utc.conf.ID
|
||||
}
|
||||
|
||||
r.PeerID = utc.id
|
||||
return utc.announce(c, r)
|
||||
}
|
||||
|
||||
func (utc *Client) scrape(c context.Context, ihs []metainfo.Hash) (rs []ScrapeResponse, err error) {
|
||||
func (utc *Client) scrape(c context.Context, ihs []metainfo.Hash) (
|
||||
rs []ScrapeResponse, err error) {
|
||||
cid, err := utc.getConnectionID(c)
|
||||
if err != nil {
|
||||
return
|
||||
@ -244,24 +248,17 @@ func (utc *Client) scrape(c context.Context, ihs []metainfo.Hash) (rs []ScrapeRe
|
||||
|
||||
tid := utc.getTranID()
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 16+len(ihs)*20))
|
||||
binary.Write(buf, binary.BigEndian, cid) // 8: 0 - 8
|
||||
binary.Write(buf, binary.BigEndian, ActionScrape) // 4: 8 - 12
|
||||
binary.Write(buf, binary.BigEndian, tid) // 4: 12 - 16
|
||||
|
||||
for _, h := range ihs { // 20*N: 16 -
|
||||
binary.Write(buf, binary.BigEndian, cid)
|
||||
binary.Write(buf, binary.BigEndian, ActionScrape)
|
||||
binary.Write(buf, binary.BigEndian, tid)
|
||||
for _, h := range ihs {
|
||||
buf.Write(h[:])
|
||||
}
|
||||
|
||||
if err = utc.send(buf.Bytes()); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
bufSize := utc.MaxBufSize
|
||||
if bufSize <= 0 {
|
||||
bufSize = maxBufSize
|
||||
}
|
||||
|
||||
data := make([]byte, bufSize)
|
||||
data := make([]byte, utc.conf.MaxBufSize)
|
||||
n, err := utc.readResp(c, data)
|
||||
if err != nil {
|
||||
return
|
||||
@ -271,19 +268,19 @@ func (utc *Client) scrape(c context.Context, ihs []metainfo.Hash) (rs []ScrapeRe
|
||||
}
|
||||
|
||||
data = data[:n]
|
||||
switch action := binary.BigEndian.Uint32(data[:4]); action {
|
||||
switch binary.BigEndian.Uint32(data[:4]) {
|
||||
case ActionScrape:
|
||||
case ActionError:
|
||||
_, reason := utc.parseError(data[4:])
|
||||
err = errors.New(reason)
|
||||
return
|
||||
default:
|
||||
err = fmt.Errorf("tracker response is not connect action %d", action)
|
||||
err = errors.New("tracker response not connect action")
|
||||
return
|
||||
}
|
||||
|
||||
if _tid := binary.BigEndian.Uint32(data[4:8]); _tid != tid {
|
||||
err = fmt.Errorf("expect transaction id %d, but got %d", tid, _tid)
|
||||
if binary.BigEndian.Uint32(data[4:8]) != tid {
|
||||
err = errors.New("invalid transaction id")
|
||||
return
|
||||
}
|
||||
|
||||
@ -302,10 +299,10 @@ func (utc *Client) scrape(c context.Context, ihs []metainfo.Hash) (rs []ScrapeRe
|
||||
// Scrape sends a Scrape request to the tracker.
|
||||
//
|
||||
// Notice:
|
||||
// 1. if it does not connect to the UDP tracker server, it will connect to it,
|
||||
// then send the ANNOUNCE request.
|
||||
// 2. If returning an error, you should retry it.
|
||||
// See http://www.bittorrent.org/beps/bep_0015.html#time-outs
|
||||
// 1. if it does not connect to the UDP tracker server, it will connect to it,
|
||||
// then send the ANNOUNCE request.
|
||||
// 2. If returning an error, you should retry it.
|
||||
// See http://www.bittorrent.org/beps/bep_0015.html#time-outs
|
||||
func (utc *Client) Scrape(c context.Context, hs []metainfo.Hash) ([]ScrapeResponse, error) {
|
||||
return utc.scrape(c, hs)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -24,7 +24,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
// ServerHandler is used to handle the request from the client.
|
||||
@ -45,14 +45,25 @@ type wrappedPeerAddr struct {
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
type buffer struct{ Data []byte }
|
||||
// ServerConfig is used to configure the Server.
|
||||
type ServerConfig struct {
|
||||
MaxBufSize int // Default: 2048
|
||||
ErrorLog func(format string, args ...interface{}) // Default: log.Printf
|
||||
}
|
||||
|
||||
func (c *ServerConfig) setDefault() {
|
||||
if c.MaxBufSize <= 0 {
|
||||
c.MaxBufSize = 2048
|
||||
}
|
||||
if c.ErrorLog == nil {
|
||||
c.ErrorLog = log.Printf
|
||||
}
|
||||
}
|
||||
|
||||
// Server is a tracker server based on UDP.
|
||||
type Server struct {
|
||||
// Default: log.Printf
|
||||
ErrorLog func(format string, args ...interface{})
|
||||
|
||||
conn net.PacketConn
|
||||
conf ServerConfig
|
||||
handler ServerHandler
|
||||
bufpool sync.Pool
|
||||
|
||||
@ -63,20 +74,23 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServer returns a new Server.
|
||||
func NewServer(c net.PacketConn, h ServerHandler, bufSize int) *Server {
|
||||
if bufSize <= 0 {
|
||||
bufSize = maxBufSize
|
||||
func NewServer(c net.PacketConn, h ServerHandler, sc ...ServerConfig) *Server {
|
||||
var conf ServerConfig
|
||||
if len(sc) > 0 {
|
||||
conf = sc[0]
|
||||
}
|
||||
conf.setDefault()
|
||||
|
||||
return &Server{
|
||||
s := &Server{
|
||||
conf: conf,
|
||||
conn: c,
|
||||
handler: h,
|
||||
exit: make(chan struct{}),
|
||||
conns: make(map[uint64]wrappedPeerAddr, 128),
|
||||
bufpool: sync.Pool{New: func() interface{} {
|
||||
return &buffer{Data: make([]byte, bufSize)}
|
||||
}},
|
||||
}
|
||||
s.bufpool.New = func() interface{} { return make([]byte, conf.MaxBufSize) }
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Close closes the tracker server.
|
||||
@ -112,11 +126,11 @@ func (uts *Server) cleanConnectionID(interval time.Duration) {
|
||||
func (uts *Server) Run() {
|
||||
go uts.cleanConnectionID(time.Minute * 2)
|
||||
for {
|
||||
buf := uts.bufpool.Get().(*buffer)
|
||||
n, raddr, err := uts.conn.ReadFrom(buf.Data)
|
||||
buf := uts.bufpool.Get().([]byte)
|
||||
n, raddr, err := uts.conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "closed") {
|
||||
uts.errorf("failed to read udp tracker request: %s", err)
|
||||
uts.conf.ErrorLog("failed to read udp tracker request: %s", err)
|
||||
}
|
||||
return
|
||||
} else if n < 16 {
|
||||
@ -126,26 +140,18 @@ func (uts *Server) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
func (uts *Server) errorf(format string, args ...interface{}) {
|
||||
if uts.ErrorLog == nil {
|
||||
log.Printf(format, args...)
|
||||
} else {
|
||||
uts.ErrorLog(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (uts *Server) handleRequest(raddr *net.UDPAddr, buf *buffer, n int) {
|
||||
func (uts *Server) handleRequest(raddr *net.UDPAddr, buf []byte, n int) {
|
||||
defer uts.bufpool.Put(buf)
|
||||
uts.handlePacket(raddr, buf.Data[:n])
|
||||
uts.handlePacket(raddr, buf[:n])
|
||||
}
|
||||
|
||||
func (uts *Server) send(raddr *net.UDPAddr, b []byte) {
|
||||
n, err := uts.conn.WriteTo(b, raddr)
|
||||
if err != nil {
|
||||
uts.errorf("fail to send the udp tracker response to '%s': %s",
|
||||
uts.conf.ErrorLog("fail to send the udp tracker response to '%s': %s",
|
||||
raddr.String(), err)
|
||||
} else if n < len(b) {
|
||||
uts.errorf("too short udp tracker response sent to '%s'", raddr.String())
|
||||
uts.conf.ErrorLog("too short udp tracker response sent to '%s'", raddr.String())
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,14 +190,16 @@ func (uts *Server) sendConnResp(raddr *net.UDPAddr, tid uint32, cid uint64) {
|
||||
uts.send(raddr, buf.Bytes())
|
||||
}
|
||||
|
||||
func (uts *Server) sendAnnounceResp(raddr *net.UDPAddr, tid uint32, resp AnnounceResponse) {
|
||||
func (uts *Server) sendAnnounceResp(raddr *net.UDPAddr, tid uint32,
|
||||
resp AnnounceResponse) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 8+12+len(resp.Addresses)*18))
|
||||
encodeResponseHeader(buf, ActionAnnounce, tid)
|
||||
resp.EncodeTo(buf, raddr.IP.To4() != nil)
|
||||
resp.EncodeTo(buf)
|
||||
uts.send(raddr, buf.Bytes())
|
||||
}
|
||||
|
||||
func (uts *Server) sendScrapResp(raddr *net.UDPAddr, tid uint32, rs []ScrapeResponse) {
|
||||
func (uts *Server) sendScrapResp(raddr *net.UDPAddr, tid uint32,
|
||||
rs []ScrapeResponse) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 8+len(rs)*12))
|
||||
encodeResponseHeader(buf, ActionScrape, tid)
|
||||
for _, r := range rs {
|
||||
@ -201,9 +209,9 @@ func (uts *Server) sendScrapResp(raddr *net.UDPAddr, tid uint32, rs []ScrapeResp
|
||||
}
|
||||
|
||||
func (uts *Server) handlePacket(raddr *net.UDPAddr, b []byte) {
|
||||
cid := binary.BigEndian.Uint64(b[:8]) // 8: 0 - 8
|
||||
action := binary.BigEndian.Uint32(b[8:12]) // 4: 8 - 12
|
||||
tid := binary.BigEndian.Uint32(b[12:16]) // 4: 12 - 16
|
||||
cid := binary.BigEndian.Uint64(b[:8])
|
||||
action := binary.BigEndian.Uint32(b[8:12])
|
||||
tid := binary.BigEndian.Uint32(b[12:16])
|
||||
b = b[16:]
|
||||
|
||||
// Handle the connection request.
|
||||
@ -233,13 +241,13 @@ func (uts *Server) handlePacket(raddr *net.UDPAddr, b []byte) {
|
||||
uts.sendError(raddr, tid, "invalid announce request")
|
||||
return
|
||||
}
|
||||
req.DecodeFrom(b)
|
||||
req.DecodeFrom(b, true)
|
||||
} else { // For ipv6
|
||||
if len(b) < 94 {
|
||||
uts.sendError(raddr, tid, "invalid announce request")
|
||||
return
|
||||
}
|
||||
req.DecodeFrom(b)
|
||||
req.DecodeFrom(b, false)
|
||||
}
|
||||
|
||||
resp, err := uts.handler.OnAnnounce(raddr, req)
|
||||
@ -248,7 +256,6 @@ func (uts *Server) handlePacket(raddr *net.UDPAddr, b []byte) {
|
||||
} else {
|
||||
uts.sendAnnounceResp(raddr, tid, resp)
|
||||
}
|
||||
|
||||
case ActionScrape:
|
||||
_len := len(b)
|
||||
infohashes := make([]metainfo.Hash, 0, _len/20)
|
||||
@ -267,7 +274,6 @@ func (uts *Server) handlePacket(raddr *net.UDPAddr, b []byte) {
|
||||
} else {
|
||||
uts.sendScrapResp(raddr, tid, resps)
|
||||
}
|
||||
|
||||
default:
|
||||
uts.sendError(raddr, tid, "unkwnown action")
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -22,13 +22,14 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/xgfone/bt/metainfo"
|
||||
)
|
||||
|
||||
type testHandler struct{}
|
||||
|
||||
func (testHandler) OnConnect(raddr *net.UDPAddr) (err error) { return }
|
||||
func (testHandler) OnAnnounce(raddr *net.UDPAddr, req AnnounceRequest) (r AnnounceResponse, err error) {
|
||||
func (testHandler) OnAnnounce(raddr *net.UDPAddr, req AnnounceRequest) (
|
||||
r AnnounceResponse, err error) {
|
||||
if req.Port != 80 {
|
||||
err = errors.New("port is not 80")
|
||||
return
|
||||
@ -49,11 +50,12 @@ func (testHandler) OnAnnounce(raddr *net.UDPAddr, req AnnounceRequest) (r Announ
|
||||
Interval: 1,
|
||||
Leechers: 2,
|
||||
Seeders: 3,
|
||||
Addresses: []metainfo.CompactAddr{{IP: net.ParseIP("127.0.0.1"), Port: 8001}},
|
||||
Addresses: []metainfo.Address{{IP: net.ParseIP("127.0.0.1"), Port: 8001}},
|
||||
}
|
||||
return
|
||||
}
|
||||
func (testHandler) OnScrap(raddr *net.UDPAddr, infohashes []metainfo.Hash) (rs []ScrapeResponse, err error) {
|
||||
func (testHandler) OnScrap(raddr *net.UDPAddr, infohashes []metainfo.Hash) (
|
||||
rs []ScrapeResponse, err error) {
|
||||
rs = make([]ScrapeResponse, len(infohashes))
|
||||
for i := range infohashes {
|
||||
rs[i] = ScrapeResponse{
|
||||
@ -71,7 +73,7 @@ func ExampleClient() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server := NewServer(sconn, testHandler{}, 0)
|
||||
server := NewServer(sconn, testHandler{})
|
||||
defer server.Close()
|
||||
go server.Run()
|
||||
|
||||
@ -79,7 +81,7 @@ func ExampleClient() {
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// Create a client and dial to the UDP tracker server.
|
||||
client, err := NewClientByDial("udp4", "127.0.0.1:8001", metainfo.Hash{})
|
||||
client, err := NewClientByDial("udp4", "127.0.0.1:8001")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -87,7 +89,7 @@ func ExampleClient() {
|
||||
// Send the ANNOUNCE request to the UDP tracker server,
|
||||
// and get the ANNOUNCE response.
|
||||
exts := []Extension{NewURLData([]byte("data")), NewNop()}
|
||||
req := AnnounceRequest{InfoHash: metainfo.NewRandomHash(), Port: 80, Exts: exts}
|
||||
req := AnnounceRequest{IP: net.ParseIP("127.0.0.1"), Port: 80, Exts: exts}
|
||||
resp, err := client.Announce(context.Background(), req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
50
utils/bool.go
Normal file
50
utils/bool.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Bool is used to implement a atomic bool.
|
||||
type Bool struct {
|
||||
v uint32
|
||||
}
|
||||
|
||||
// NewBool returns a new Bool with the initialized value.
|
||||
func NewBool(t bool) (b Bool) {
|
||||
if t {
|
||||
b.v = 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the bool value.
|
||||
func (b *Bool) Get() bool { return atomic.LoadUint32(&b.v) == 1 }
|
||||
|
||||
// SetTrue sets the bool value to true.
|
||||
func (b *Bool) SetTrue() { atomic.StoreUint32(&b.v, 1) }
|
||||
|
||||
// SetFalse sets the bool value to false.
|
||||
func (b *Bool) SetFalse() { atomic.StoreUint32(&b.v, 0) }
|
||||
|
||||
// Set sets the bool value to t.
|
||||
func (b *Bool) Set(t bool) {
|
||||
if t {
|
||||
b.SetTrue()
|
||||
} else {
|
||||
b.SetFalse()
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,6 +12,5 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package downloader is used to download the torrent or the real file
|
||||
// from the peer node by the peer wire protocol.
|
||||
package downloader
|
||||
// Package utils supplies some convenient functions.
|
||||
package utils
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,12 +12,18 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helper
|
||||
package utils
|
||||
|
||||
import "io"
|
||||
|
||||
// CopyNBuffer is the same as io.CopyN, but uses the given buf as the buffer.
|
||||
//
|
||||
// If buf is nil or empty, it will make a new one with 2048.
|
||||
func CopyNBuffer(dst io.Writer, src io.Reader, n int64, buf []byte) (written int64, err error) {
|
||||
if len(buf) == 0 {
|
||||
buf = make([]byte, 2048)
|
||||
}
|
||||
|
||||
written, err = io.CopyBuffer(dst, io.LimitReader(src, n), buf)
|
||||
if written == n {
|
||||
return n, nil
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,14 +12,15 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helper
|
||||
package utils
|
||||
|
||||
// ContainsString reports whether s is in ss.
|
||||
func ContainsString(ss []string, s string) bool {
|
||||
// InStringSlice reports whether s is in ss.
|
||||
func InStringSlice(ss []string, s string) bool {
|
||||
for _, v := range ss {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,16 +12,18 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helper
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContainsString(t *testing.T) {
|
||||
if !ContainsString([]string{"a", "b"}, "a") {
|
||||
func TestInStringSlice(t *testing.T) {
|
||||
if !InStringSlice([]string{"a", "b"}, "a") {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if ContainsString([]string{"a", "b"}, "z") {
|
||||
if InStringSlice([]string{"a", "b"}, "z") {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2023 xgfone
|
||||
// Copyright 2020 xgfone
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,20 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helper
|
||||
package utils
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"math/rand"
|
||||
)
|
||||
import "crypto/rand"
|
||||
|
||||
// RandomString generates a size-length string randomly.
|
||||
func RandomString(size int) string {
|
||||
bs := make([]byte, size)
|
||||
if n, _ := crand.Read(bs); n < size {
|
||||
for ; n < size; n++ {
|
||||
bs[n] = byte(rand.Intn(256))
|
||||
}
|
||||
}
|
||||
rand.Read(bs)
|
||||
return string(bs)
|
||||
}
|
Reference in New Issue
Block a user