Compare commits
23 Commits
master
...
i2p-suppor
Author | SHA1 | Date | |
---|---|---|---|
1840e97bef | |||
272ef550b7 | |||
16a5d27f0c | |||
5b8308625c | |||
4e6a12c27e | |||
f33c354e82 | |||
50d4aa85a4 | |||
1487cbf781 | |||
ed906bf55c | |||
b21d3cd500 | |||
925a365e5c | |||
079c851e09 | |||
cda34d75ad | |||
3236a67c66 | |||
a8a6984ced | |||
5c1d55731f | |||
5dac4ba515 | |||
6b9883fae7 | |||
03d2419ec3 | |||
424c19af04 | |||
3c40f43f64 | |||
3c2817b87d | |||
ff4c19cc0a |
10
.github/workflows/go.yml
vendored
10
.github/workflows/go.yml
vendored
@ -4,7 +4,7 @@ env:
|
||||
GO111MODULE: on
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-18.04
|
||||
name: Go ${{ matrix.go }}
|
||||
strategy:
|
||||
matrix:
|
||||
@ -17,12 +17,10 @@ jobs:
|
||||
- '1.16'
|
||||
- '1.17'
|
||||
- '1.18'
|
||||
- '1.19'
|
||||
- '1.20'
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v2
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v3
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
- run: go test -race -cover ./...
|
||||
- run: go test -race ./...
|
||||
|
13
.gitignore
vendored
13
.gitignore
vendored
@ -33,16 +33,11 @@ test_*
|
||||
# log
|
||||
*.log
|
||||
|
||||
vendor/
|
||||
|
||||
# VS Code
|
||||
.vscode/
|
||||
debug
|
||||
debug_test
|
||||
|
||||
# Mac
|
||||
.DS_Store
|
||||
|
||||
# hidden files
|
||||
# VS Code
|
||||
.vscode/
|
||||
|
||||
# Unix hidden files
|
||||
.*
|
||||
_*
|
||||
|
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://github.com/xgfone/bt/actions/workflows/go.yml) [](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
|
||||
}
|
||||
```
|
||||
|
@ -102,10 +102,12 @@ func NewDecoder(r io.Reader) *Decoder {
|
||||
// Decode reads the bencoded value from its input and stores it in the value pointed to by val.
|
||||
// Decode allocates maps/slices as necessary with the following additional rules:
|
||||
// To decode a bencoded value into a nil interface value, the type stored in the interface value is one of:
|
||||
// int64 for bencoded integers
|
||||
// string for bencoded strings
|
||||
// []interface{} for bencoded lists
|
||||
// map[string]interface{} for bencoded dicts
|
||||
//
|
||||
// int64 for bencoded integers
|
||||
// string for bencoded strings
|
||||
// []interface{} for bencoded lists
|
||||
// map[string]interface{} for bencoded dicts
|
||||
//
|
||||
// To unmarshal bencode into a value implementing the Unmarshaler interface,
|
||||
// Unmarshal calls that value's UnmarshalBencode method.
|
||||
// Otherwise, if the value implements encoding.TextUnmarshaler
|
||||
@ -116,7 +118,7 @@ func (d *Decoder) Decode(val interface{}) error {
|
||||
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
||||
return errors.New("Unwritable type passed into decode")
|
||||
}
|
||||
|
||||
//log.Printf("Decoding %s", rv)
|
||||
return d.decodeInto(rv)
|
||||
}
|
||||
|
||||
@ -337,7 +339,6 @@ func (d *Decoder) decodeList(v reflect.Value) error {
|
||||
return fmt.Errorf("Cant store a []interface{} into %s", v.Type())
|
||||
}
|
||||
}
|
||||
|
||||
// read out the l that prefixes the list
|
||||
ch, err := d.readByte()
|
||||
if err != nil {
|
||||
@ -529,7 +530,6 @@ func (d *Decoder) decodeDict(v reflect.Value) error {
|
||||
if err := d.decodeInto(subv); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if isMap {
|
||||
v.SetMapIndex(reflect.ValueOf(key), subv)
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ func encodeValue(w io.Writer, val reflect.Value) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("Can't encode type: %s", v.Type())
|
||||
return fmt.Errorf("can't encode type: %s", v.Type())
|
||||
}
|
||||
|
||||
// indirectEncodeValue walks down v allocating pointers as needed,
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !go1.13
|
||||
// +build !go1.13
|
||||
|
||||
package bencode
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build go1.13
|
||||
// +build go1.13
|
||||
|
||||
package bencode
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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: {}}
|
||||
} 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()
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -25,9 +25,10 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-bt/krpc"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -61,7 +62,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 +135,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)
|
||||
|
||||
// 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)
|
||||
|
||||
// HandleInMessage is used to intercept the incoming DHT message.
|
||||
// For example, you can debug the message as the log.
|
||||
@ -159,36 +154,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.Addr, *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)
|
||||
}
|
||||
|
||||
func (c Config) in(net.Addr, *krpc.Message) bool { return false }
|
||||
func (c Config) in(net.Addr, *krpc.Message) bool { return true }
|
||||
func (c Config) out(net.Addr, *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 +198,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) {}
|
||||
}
|
||||
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) {}
|
||||
}
|
||||
if c.HandleInMessage == nil {
|
||||
c.HandleInMessage = c.in
|
||||
@ -240,7 +216,6 @@ type Server struct {
|
||||
conf Config
|
||||
exit chan struct{}
|
||||
conn net.PacketConn
|
||||
lock sync.Mutex
|
||||
once sync.Once
|
||||
|
||||
ipv4 bool
|
||||
@ -256,15 +231,15 @@ 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())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else if ip := net.ParseIP(host); ipIsZero(ip) {
|
||||
} else if ip := net.ParseIP(host); utils.IpIsZero(ip) {
|
||||
conf.IPProtocols = []IPProtocolStack{IPv4Protocol, IPv6Protocol}
|
||||
} else if ip.To4() != nil {
|
||||
conf.IPProtocols = []IPProtocolStack{IPv4Protocol}
|
||||
@ -304,6 +279,7 @@ func NewServer(conn net.PacketConn, config *Config) *Server {
|
||||
if s.peerManager == nil {
|
||||
s.peerManager = s.tokenPeerManager
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@ -317,23 +293,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.Addr(), s.conf.ID); err != nil {
|
||||
s.conf.ErrorLog(`fail to bootstrap '%s': %s`, a.String(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -357,7 +325,7 @@ func (s *Server) Node6Num() int { return s.routingTable6.Len() }
|
||||
// The current node will be discarded.
|
||||
func (s *Server) AddNode(node krpc.Node) int {
|
||||
// For IPv6
|
||||
if isIPv6(node.Addr.IP) {
|
||||
if node.Addr.IsIPv6() {
|
||||
if s.ipv6 {
|
||||
return s.routingTable6.AddNode(node)
|
||||
}
|
||||
@ -372,13 +340,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.Addr, 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(utils.IPAddr(a), utils.Port(a))
|
||||
}
|
||||
|
||||
return
|
||||
@ -426,16 +394,12 @@ func (s *Server) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
s.handlePacket(raddr, buf[:n])
|
||||
s.handlePacket(raddr.(net.Addr), buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) isDisabled(raddr krpc.Addr) bool {
|
||||
if len(raddr.IP) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if isIPv6(raddr.IP) {
|
||||
func (s *Server) isDisabled(raddr net.Addr) bool {
|
||||
if utils.IsIPv6Addr(raddr) {
|
||||
if !s.ipv6 {
|
||||
return true
|
||||
}
|
||||
@ -447,15 +411,12 @@ 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) {
|
||||
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(utils.IPAddr(raddr), utils.Port(raddr)) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -468,46 +429,43 @@ 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.Addr, 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.Addr, m krpc.Message) {
|
||||
switch m.Q {
|
||||
case queryMethodPing:
|
||||
s.reply(raddr, m.T, krpc.ResponseResult{})
|
||||
@ -517,7 +475,7 @@ func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
n4 := m.A.ContainsWant(krpc.WantNodes)
|
||||
n6 := m.A.ContainsWant(krpc.WantNodes6)
|
||||
if !n4 && !n6 {
|
||||
if isIPv6(raddr.IP) {
|
||||
if utils.IsIPv6Addr(raddr) {
|
||||
r.Nodes6 = s.routingTable6.Closest(m.A.InfoHash, s.conf.K)
|
||||
} else {
|
||||
r.Nodes = s.routingTable4.Closest(m.A.InfoHash, s.conf.K)
|
||||
@ -539,7 +497,7 @@ func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
// Get the ipv4/ipv6 peers storing the torrent infohash.
|
||||
var r krpc.ResponseResult
|
||||
if !n4 && !n6 {
|
||||
r.Values = s.peerManager.GetPeers(m.A.InfoHash, s.conf.K, isIPv6(raddr.IP))
|
||||
r.Values = s.peerManager.GetPeers(m.A.InfoHash, s.conf.K, utils.IsIPv6Addr(raddr))
|
||||
} else {
|
||||
if n4 {
|
||||
r.Values = s.peerManager.GetPeers(m.A.InfoHash, s.conf.K, false)
|
||||
@ -558,7 +516,7 @@ func (s *Server) handleQuery(raddr krpc.Addr, m krpc.Message) {
|
||||
// No Peers, and return the closest other nodes.
|
||||
if len(r.Values) == 0 {
|
||||
if !n4 && !n6 {
|
||||
if isIPv6(raddr.IP) {
|
||||
if utils.IsIPv6Addr(raddr) {
|
||||
r.Nodes6 = s.routingTable6.Closest(m.A.InfoHash, s.conf.K)
|
||||
} else {
|
||||
r.Nodes = s.routingTable4.Closest(m.A.InfoHash, s.conf.K)
|
||||
@ -576,38 +534,32 @@ 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)
|
||||
|
||||
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)
|
||||
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.Addr, m krpc.Message) (wrote bool, err error) {
|
||||
// // TODO: Should we check the ip blacklist??
|
||||
// if s.conf.Blacklist.In(utils.IPaddr(raddr), utils.Port(raddr)) {
|
||||
// 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.Addr, m krpc.Message) (wrote bool, err error) {
|
||||
if m.T == "" || m.Y == "" {
|
||||
panic(`DHT message "t" or "y" must not be empty`)
|
||||
}
|
||||
@ -618,10 +570,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(utils.IPAddr(raddr), 0)
|
||||
return
|
||||
}
|
||||
|
||||
@ -633,13 +585,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.Addr, 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.Addr, 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,7 +622,7 @@ 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
|
||||
@ -684,19 +636,11 @@ func (s *Server) onTimeout(t *transaction) {
|
||||
t.ID, s.conf.ID, t.Query, qid, s.conn.LocalAddr(), 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.Addr, 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.Addr, 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,7 +650,7 @@ 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)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -757,9 +701,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.Addr(), queryMethodGetPeers, arg, cb...)
|
||||
t.OnResponse = s.onGetPeersResp
|
||||
t.Depth = depth
|
||||
t.Visited = ids
|
||||
@ -768,6 +713,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.Addr, 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.
|
||||
@ -821,15 +774,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.Addr()
|
||||
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 +795,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.Addr, target metainfo.Hash) error {
|
||||
if target.IsZero() {
|
||||
panic("the target is ZERO")
|
||||
}
|
||||
@ -849,7 +803,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.Addr, 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 +812,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.Addr, m krpc.Message) {
|
||||
// Search the target node recursively.
|
||||
t.Depth--
|
||||
if t.Depth < 1 {
|
||||
@ -899,26 +852,10 @@ 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.Addr(), t.Depth, ids)
|
||||
if err != nil {
|
||||
s.conf.ErrorLog(`fail to send "find_node" query to '%s': %s`,
|
||||
node.Addr.String(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isIPv6(ip net.IP) bool {
|
||||
if ip.To4() == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ipIsZero(ip net.IP) bool {
|
||||
for _, b := range ip {
|
||||
if b != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -20,30 +20,35 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-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,19 +62,21 @@ 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) {
|
||||
//addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s is searching %s\n", ip.String(), infohash)
|
||||
}
|
||||
|
||||
func onTorrent(infohash string, addr string) {
|
||||
fmt.Printf("%s has downloaded %s\n", addr, infohash)
|
||||
func onTorrent(infohash string, ip net.Addr) {
|
||||
//addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s has downloaded %s\n", ip.String(), infohash)
|
||||
}
|
||||
|
||||
func newDHTServer(id metainfo.Hash, addr string, pm PeerManager) (s *Server, err error) {
|
||||
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
|
||||
}
|
||||
@ -132,7 +139,7 @@ func ExampleServer() {
|
||||
fmt.Printf("%s: no peers for %s\n", r.Addr.String(), infohash)
|
||||
} else {
|
||||
for _, peer := range r.Peers {
|
||||
fmt.Printf("%s: %s\n", infohash, peer)
|
||||
fmt.Printf("%s: %s\n", infohash, peer.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -141,7 +148,7 @@ 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.
|
||||
@ -150,7 +157,7 @@ func ExampleServer() {
|
||||
fmt.Printf("%s: no peers for %s\n", r.Addr.String(), infohash)
|
||||
} else {
|
||||
for _, peer := range r.Peers {
|
||||
fmt.Printf("%s: %s\n", infohash, peer)
|
||||
fmt.Printf("%s: %s\n", infohash, peer.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -170,5 +177,5 @@ func ExampleServer() {
|
||||
// 127.0.0.1:9002 is searching 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1:9003: no peers for 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 0102030405060708090a0b0c0d0e0f1011121314: 127.0.0.1:9001
|
||||
// 127.0.0.1:9001 has downloaded 0102030405060708090a0b0c0d0e0f1011121314
|
||||
// 127.0.0.1 has downloaded 0102030405060708090a0b0c0d0e0f1011121314
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -15,24 +15,25 @@
|
||||
package dht
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/utils"
|
||||
)
|
||||
|
||||
// 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
|
||||
ID metainfo.Hash
|
||||
// IP net.IP
|
||||
IP net.Addr
|
||||
Port uint16
|
||||
Token string
|
||||
Time time.Time
|
||||
}
|
||||
@ -76,7 +77,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.Addr, token string) {
|
||||
addrkey := addr.String()
|
||||
tpm.lock.Lock()
|
||||
peers, ok := tpm.peers[id]
|
||||
@ -84,11 +85,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,
|
||||
Port: uint16(utils.Port(addr)),
|
||||
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.Addr) (token string) {
|
||||
addrkey := addr.String()
|
||||
tpm.lock.RLock()
|
||||
if peers, ok := tpm.peers[id]; ok {
|
||||
@ -108,24 +115,24 @@ 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 {
|
||||
if maxnum < 1 {
|
||||
break
|
||||
}
|
||||
|
||||
if ipv6 { // For IPv6
|
||||
if isIPv6(peer.Addr.IP) {
|
||||
maxnum--
|
||||
addrs = append(addrs, peer.Addr.String())
|
||||
}
|
||||
} else if !isIPv6(peer.Addr.IP) { // For IPv4
|
||||
maxnum--
|
||||
addrs = append(addrs, peer.Addr.String())
|
||||
}
|
||||
//if ipv6 { // For IPv6
|
||||
//if isIPv6(peer.IP) {
|
||||
maxnum--
|
||||
addrs = append(addrs, metainfo.NewAddress(peer.IP, peer.Port))
|
||||
//}
|
||||
//} else if !isIPv6(peer.IP) { // For IPv4
|
||||
// maxnum--
|
||||
// addrs = append(addrs, metainfo.NewAddress(peer.IP, peer.Port))
|
||||
//}
|
||||
}
|
||||
}
|
||||
tpm.lock.RUnlock()
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -19,8 +19,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/krpc"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
const bktlen = 160
|
||||
@ -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.Addr()); err != nil {
|
||||
b.table.s.conf.ErrorLog("fail to ping '%s': %s", node.Node.String(), err)
|
||||
}
|
||||
case nodeStatusBad:
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -17,8 +17,8 @@ package dht
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/krpc"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/krpc"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// RoutingTableNode represents the node with last changed time in the routing table.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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/eyedeekay/go-i2p-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.Addr) (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.Addr, token string) (ok bool) {
|
||||
tm.lock.RLock()
|
||||
last, new := tm.last, tm.new
|
||||
tm.lock.RUnlock()
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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/eyedeekay/go-i2p-bt/krpc"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
type transaction struct {
|
||||
ID string
|
||||
Query string
|
||||
Arg krpc.QueryArg
|
||||
Addr krpc.Addr
|
||||
Addr net.Addr
|
||||
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.Addr, 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.Addr, krpc.Message) {}
|
||||
func newTransaction(s *Server, a net.Addr, 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.Addr) (t *transaction) {
|
||||
key := transactionkey{id: tid, addr: addr.String()}
|
||||
tm.lock.Lock()
|
||||
if t = tm.trans[key]; t != nil {
|
||||
|
92
diff.patch
Normal file
92
diff.patch
Normal file
@ -0,0 +1,92 @@
|
||||
diff --git a/peerprotocol/extension.go b/peerprotocol/extension.go
|
||||
index 7ab53ff..02eb9e3 100644
|
||||
--- a/peerprotocol/extension.go
|
||||
+++ b/peerprotocol/extension.go
|
||||
@@ -17,6 +17,7 @@ package peerprotocol
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
+ "log"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/bt/bencode"
|
||||
@@ -43,39 +44,58 @@ const (
|
||||
)
|
||||
|
||||
// CompactIP is used to handle the compact ipv4 or ipv6.
|
||||
+//type CompactIPBytes []byte //net.IP
|
||||
+
|
||||
type CompactIP net.IP
|
||||
|
||||
+func NewCompactIP(b []byte) (CompactIP, error) {
|
||||
+ if len(b) == net.IPv4len {
|
||||
+ return CompactIP(b), nil
|
||||
+ }
|
||||
+ if len(b) == net.IPv6len {
|
||||
+ return CompactIP(b), nil
|
||||
+ }
|
||||
+ return nil, errInvalidIP
|
||||
+}
|
||||
+
|
||||
func (ci CompactIP) String() string {
|
||||
- return net.IP(ci).String()
|
||||
+ return string(ci) //net.IP(ci).String()
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
-func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
- ip := net.IP(ci)
|
||||
- if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
- ip = ipv4
|
||||
+func (ci *CompactIP) MarshalBencode() ([]byte, error) {
|
||||
+ ip := *ci
|
||||
+ log.Println("Marshal IP,", ip, ",", len(ip), ",", ip.String(), ",", net.IPv4len)
|
||||
+ log.Println("Marshal IP,", ci, ",", ",", ci.String(), ",", net.IPv4len)
|
||||
+ if len(ip) == net.IPv4len {
|
||||
+ ip = ip
|
||||
}
|
||||
return bencode.EncodeBytes(ip[:])
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (ci *CompactIP) UnmarshalBencode(b []byte) (err error) {
|
||||
- var ip net.IP
|
||||
+ var ip []byte
|
||||
if err = bencode.DecodeBytes(b, &ip); err != nil {
|
||||
return
|
||||
}
|
||||
+ log.Println("Unmarshal IP,", ip, ",", len(ip), ",", string(ip), ",", net.IPv4len)
|
||||
|
||||
switch len(ip) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
default:
|
||||
- return errInvalidIP
|
||||
+ if len(ip) < net.IPv6len {
|
||||
+ return errInvalidIP
|
||||
+ }
|
||||
}
|
||||
|
||||
- if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
- ip = ipv4
|
||||
+ if len(ip) == net.IPv4len {
|
||||
+ ip = []byte(ip)
|
||||
}
|
||||
+ //ip = ip
|
||||
|
||||
*ci = CompactIP(ip)
|
||||
+ log.Println("Unmarshal IP,", ci, ",", ",", ci.String(), ",", net.IPv4len)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,8 +110,9 @@ type ExtendedHandshakeMsg struct {
|
||||
|
||||
// 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
|
||||
+ Port uint16 `bencode:"p,omitempty"` // BEP 10
|
||||
+ IPv6 net.IP `bencode:"ipv6,omitempty"` // BEP 10
|
||||
+ //IPv6 []byte `bencode:"ipv6,omitempty"` // BEP 10
|
||||
IPv4 CompactIP `bencode:"ipv4,omitempty"` // BEP 10
|
||||
YourIP CompactIP `bencode:"yourip,omitempty"` // BEP 10
|
||||
|
127
diff2.patch
Normal file
127
diff2.patch
Normal file
@ -0,0 +1,127 @@
|
||||
diff --git a/peerprotocol/extension.go b/peerprotocol/extension.go
|
||||
index 7ab53ff..19b7799 100644
|
||||
--- a/peerprotocol/extension.go
|
||||
+++ b/peerprotocol/extension.go
|
||||
@@ -16,9 +16,12 @@ package peerprotocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
+ "crypto/sha256"
|
||||
"errors"
|
||||
+ "log"
|
||||
"net"
|
||||
|
||||
+ "github.com/eyedeekay/sam3/i2pkeys"
|
||||
"github.com/xgfone/bt/bencode"
|
||||
)
|
||||
|
||||
@@ -42,40 +45,83 @@ const (
|
||||
UtMetadataExtendedMsgTypeReject = 2 // BEP 9
|
||||
)
|
||||
|
||||
+const (
|
||||
+ I2Plen = 32
|
||||
+)
|
||||
+
|
||||
// CompactIP is used to handle the compact ipv4 or ipv6.
|
||||
-type CompactIP net.IP
|
||||
+type CompactIP []byte
|
||||
+
|
||||
+func NewCompactIP(addr []byte) CompactIP {
|
||||
+ switch len(addr) {
|
||||
+ case net.IPv4len:
|
||||
+ return CompactIP(addr)
|
||||
+ case net.IPv6len:
|
||||
+ return CompactIP(addr)
|
||||
+ case I2Plen:
|
||||
+ b := sha256.Sum256(addr)
|
||||
+ return CompactIP(b[:])
|
||||
+ default:
|
||||
+ return nil
|
||||
+ }
|
||||
+}
|
||||
|
||||
func (ci CompactIP) String() string {
|
||||
- return net.IP(ci).String()
|
||||
+ switch len(ci) {
|
||||
+ case net.IPv4len:
|
||||
+ return net.IP(ci).String()
|
||||
+ case net.IPv6len:
|
||||
+ if ip := net.IP(ci).To4(); len(ip) != 0 {
|
||||
+ return ip.String()
|
||||
+ }
|
||||
+ return "[" + net.IP(ci).String() + "]"
|
||||
+ case I2Plen:
|
||||
+ return i2pkeys.I2PAddr(ci).String()
|
||||
+ default:
|
||||
+ return ""
|
||||
+ }
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
- ip := net.IP(ci)
|
||||
- if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
- ip = ipv4
|
||||
+ switch len(ci) {
|
||||
+ case net.IPv4len:
|
||||
+ ip := net.IP(ci)
|
||||
+ if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
+ ip = ipv4
|
||||
+ }
|
||||
+ return bencode.EncodeBytes(ip[:])
|
||||
+ case net.IPv6len:
|
||||
+ ip := net.IP(ci)
|
||||
+ if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
+ ip = ipv4
|
||||
+ }
|
||||
+ return bencode.EncodeBytes(ip[:])
|
||||
+ case I2Plen:
|
||||
+ ip := i2pkeys.I2PAddr(ci)
|
||||
+ return bencode.EncodeBytes(ip[:])
|
||||
+ default:
|
||||
+ return nil, errInvalidIP
|
||||
}
|
||||
- return bencode.EncodeBytes(ip[:])
|
||||
+
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (ci *CompactIP) UnmarshalBencode(b []byte) (err error) {
|
||||
+ //var ip []byte //net.IP
|
||||
var ip net.IP
|
||||
if err = bencode.DecodeBytes(b, &ip); err != nil {
|
||||
return
|
||||
}
|
||||
+ log.Println("LOG", ip)
|
||||
|
||||
switch len(ip) {
|
||||
- case net.IPv4len, net.IPv6len:
|
||||
+ case net.IPv4len, net.IPv6len, I2Plen:
|
||||
default:
|
||||
return errInvalidIP
|
||||
}
|
||||
|
||||
- if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
- ip = ipv4
|
||||
- }
|
||||
-
|
||||
- *ci = CompactIP(ip)
|
||||
+ *ci = NewCompactIP(ip)
|
||||
return
|
||||
}
|
||||
|
||||
diff --git a/peerprotocol/extension_test.go b/peerprotocol/extension_test.go
|
||||
index ffcbdfd..d47cd7e 100644
|
||||
--- a/peerprotocol/extension_test.go
|
||||
+++ b/peerprotocol/extension_test.go
|
||||
@@ -29,7 +29,8 @@ func TestCompactIP(t *testing.T) {
|
||||
var ip CompactIP
|
||||
if err = ip.UnmarshalBencode(b); err != nil {
|
||||
t.Error(err)
|
||||
- } else if ip.String() != "1.2.3.4" {
|
||||
+ } else if ip.String() != "123.2.3.4" {
|
||||
+ t.Log("IP", ip.String(), "IP")
|
||||
t.Error(ip)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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/eyedeekay/go-i2p-bt/metainfo"
|
||||
pp "github.com/eyedeekay/go-i2p-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
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -12,9 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package downloader
|
||||
package blockdownload
|
||||
|
||||
import pp "github.com/xgfone/go-bt/peerprotocol"
|
||||
import (
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
pp "github.com/eyedeekay/go-i2p-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
|
||||
}
|
||||
|
9
go.mod
9
go.mod
@ -1,3 +1,8 @@
|
||||
module github.com/xgfone/go-bt
|
||||
module github.com/eyedeekay/go-i2p-bt
|
||||
|
||||
go 1.11
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/eyedeekay/i2pkeys v0.33.0
|
||||
github.com/eyedeekay/sam3 v0.33.5
|
||||
)
|
||||
|
62
go.sum
Normal file
62
go.sum
Normal file
@ -0,0 +1,62 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eyedeekay/goSam v0.32.31-0.20210122211817-f97683379f23/go.mod h1:UgJnih/LpotwKriwVPOEa6yPDM2NDdVrKfLtS5DOLPE=
|
||||
github.com/eyedeekay/i2pkeys v0.0.0-20220310052025-204d4ae6dcae/go.mod h1:W9KCm9lqZ+Ozwl3dwcgnpPXAML97+I8Jiht7o5A8YBM=
|
||||
github.com/eyedeekay/i2pkeys v0.33.0 h1:5SzUyWxNjV6AvYv/WaI8J4dSgAfv7/WEps6pDLe2YSs=
|
||||
github.com/eyedeekay/i2pkeys v0.33.0/go.mod h1:W9KCm9lqZ+Ozwl3dwcgnpPXAML97+I8Jiht7o5A8YBM=
|
||||
github.com/eyedeekay/sam3 v0.32.32/go.mod h1:qRA9KIIVxbrHlkj+ZB+OoxFGFgdKeGp1vSgPw26eOVU=
|
||||
github.com/eyedeekay/sam3 v0.33.5 h1:mY2MmEG4W35AOpG/G7DOdAhFZWRwFxlm+NmIoub1Xnw=
|
||||
github.com/eyedeekay/sam3 v0.33.5/go.mod h1:sPtlI4cRm7wD0UywOzLPvvdY1G++vBSK3n+jiIGqWlU=
|
||||
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
|
||||
github.com/getlantern/errors v1.0.1/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A=
|
||||
github.com/getlantern/go-socks5 v0.0.0-20171114193258-79d4dd3e2db5/go.mod h1:kGHRXch95rnGLHjER/GhhFiHvfnqNz7KqWD9kGfATHY=
|
||||
github.com/getlantern/golog v0.0.0-20201105130739-9586b8bde3a9/go.mod h1:ZyIjgH/1wTCl+B+7yH1DqrWp6MPJqESmwmEQ89ZfhvA=
|
||||
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7/go.mod h1:dD3CgOrwlzca8ed61CsZouQS5h5jIzkK9ZWrTcf0s+o=
|
||||
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55/go.mod h1:6mmzY2kW1TOOrVy+r41Za2MxXM+hhqTtY3oBKd2AgFA=
|
||||
github.com/getlantern/netx v0.0.0-20190110220209-9912de6f94fd/go.mod h1:wKdY0ikOgzrWSeB9UyBVKPRhjXQ+vTb+BPeJuypUuNE=
|
||||
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA=
|
||||
github.com/getlantern/ops v0.0.0-20200403153110-8476b16edcd6/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/renameio v1.0.0/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/riobard/go-x25519 v0.0.0-20190716001027-10cc4d8d0b33/go.mod h1:BjmVxzAnkLeoEbqHEerI4eSw6ua+RaIB0S4jMV21RAs=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20201125231158-b5590deeca9b/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
honnef.co/go/tools v0.0.1-2020.1.6/go.mod h1:pyyisuGw24ruLjrr1ddx39WE0y9OooInRzEYLhQB2YY=
|
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
171
krpc/message.go
171
krpc/message.go
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// Predefine some error code.
|
||||
@ -272,14 +272,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 +288,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.To4(); len(ni.Addr.IP.String()) == 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.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
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -17,7 +17,7 @@ package krpc
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-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, 2023 idk
|
||||
//
|
||||
// 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/eyedeekay/go-i2p-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.IP, 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.Addr) (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
|
||||
}
|
@ -1,48 +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 (
|
||||
"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 {
|
||||
t.Error(err)
|
||||
} else if result != expect {
|
||||
t.Errorf("expect %s, but got %s\n", expect, result)
|
||||
}
|
||||
|
||||
var raddrs []HostAddr
|
||||
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 %v, but got %v\n", i, addr, raddrs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
549
metainfo/address.go
Normal file
549
metainfo/address.go
Normal file
@ -0,0 +1,549 @@
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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"
|
||||
"strings"
|
||||
|
||||
"github.com/eyedeekay/i2pkeys"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-bt/utils"
|
||||
)
|
||||
|
||||
// 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
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// NewAddress returns a new Address.
|
||||
func NewAddress(ip interface{}, port uint16) Address {
|
||||
switch v := ip.(type) {
|
||||
case net.IP:
|
||||
return Address{IP: &net.IPAddr{
|
||||
IP: v,
|
||||
}, Port: port}
|
||||
case *net.IPAddr:
|
||||
return Address{IP: v, Port: port}
|
||||
case i2pkeys.I2PAddr:
|
||||
return Address{IP: v, Port: port}
|
||||
default:
|
||||
return Address{IP: nil, Port: 0}
|
||||
}
|
||||
}
|
||||
|
||||
// NewAddressFromString returns a new Address by the address string.
|
||||
func NewAddressFromString(s string) (addr Address, err error) {
|
||||
err = addr.FromString(s)
|
||||
return
|
||||
}
|
||||
|
||||
func Lookup(shost string, port int) ([]Address, error) {
|
||||
if strings.HasSuffix(shost, ".i2p") {
|
||||
iaddr, err := i2pkeys.Lookup(shost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
returnAddresses := []Address{
|
||||
{IP: iaddr, Port: 6881},
|
||||
}
|
||||
return returnAddresses, nil
|
||||
}
|
||||
ips, err := net.LookupIP(shost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to lookup the domain '%s': %s", shost, err)
|
||||
}
|
||||
returnAddrs := make([]Address, len(ips))
|
||||
for i, ip := range ips {
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
returnAddrs[i] = Address{
|
||||
IP: &net.IPAddr{
|
||||
IP: ipv4,
|
||||
},
|
||||
Port: uint16(port),
|
||||
}
|
||||
} else {
|
||||
returnAddrs[i] = Address{
|
||||
IP: &net.IPAddr{
|
||||
IP: ip,
|
||||
},
|
||||
Port: uint16(port),
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnAddrs, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if sport == "" && strings.HasSuffix(shost, ".i2p") {
|
||||
//Skip the missing port error if the address is an i2p address,
|
||||
//since the port is optional and we can assign 6881 automatically.
|
||||
sport = "6881"
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
||||
addrs, err = Lookup(shost, int(port))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to lookup the domain '%s': %s", shost, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func LookupNetAddr(shost string) ([]net.Addr, error) {
|
||||
if strings.HasSuffix(shost, ".i2p") {
|
||||
iaddr, err := i2pkeys.Lookup(shost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
returnAddresses := []net.Addr{
|
||||
iaddr,
|
||||
}
|
||||
return returnAddresses, nil
|
||||
}
|
||||
ips, err := net.LookupIP(shost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fail to lookup the domain '%s': %s", shost, err)
|
||||
}
|
||||
returnAddrs := make([]net.Addr, len(ips))
|
||||
for i, ip := range ips {
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
returnAddrs[i] = &net.IPAddr{
|
||||
IP: ipv4,
|
||||
}
|
||||
} else {
|
||||
returnAddrs[i] = &net.IPAddr{
|
||||
IP: ip,
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnAddrs, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if port == "" && strings.HasSuffix(host, ".i2p") {
|
||||
//Skip the missing port error if the address is an i2p address,
|
||||
//since the port is optional and we can assign 6881 automatically.
|
||||
port = "6881"
|
||||
} else {
|
||||
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 := LookupNetAddr(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 = ips[0]
|
||||
return
|
||||
}
|
||||
|
||||
// FromUDPAddr sets the ip from net.UDPAddr.
|
||||
func (a *Address) FromUDPAddr(ua net.Addr) {
|
||||
a.Port = uint16(utils.Port(ua))
|
||||
a.IP = ua
|
||||
}
|
||||
|
||||
// Addr creates a new net.Addr.
|
||||
func (a Address) Addr() net.Addr {
|
||||
switch a.IP.(type) {
|
||||
case *net.IPAddr:
|
||||
return &net.UDPAddr{
|
||||
IP: a.IP.(*net.IPAddr).IP,
|
||||
Port: int(a.Port),
|
||||
}
|
||||
case *net.UDPAddr:
|
||||
return &net.UDPAddr{
|
||||
IP: a.IP.(*net.UDPAddr).IP,
|
||||
Port: int(a.Port),
|
||||
}
|
||||
case *i2pkeys.I2PAddr:
|
||||
retvalue := a.IP.(*i2pkeys.I2PAddr)
|
||||
return retvalue
|
||||
case i2pkeys.I2PAddr:
|
||||
retvalue := a.IP.(i2pkeys.I2PAddr)
|
||||
return &retvalue
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a Address) IsIPv6() bool {
|
||||
switch a.IP.(type) {
|
||||
case *net.IPAddr:
|
||||
return a.IP.(*net.IPAddr).IP.To4() == nil
|
||||
case *net.UDPAddr:
|
||||
return a.IP.(*net.UDPAddr).IP.To4() == nil
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (a Address) To4() *net.IPAddr {
|
||||
switch a.IP.(type) {
|
||||
case *net.IPAddr:
|
||||
return &net.IPAddr{
|
||||
IP: a.IP.(*net.IPAddr).IP.To4(),
|
||||
}
|
||||
case *net.UDPAddr:
|
||||
return &net.IPAddr{
|
||||
IP: a.IP.(*net.UDPAddr).IP.To4(),
|
||||
}
|
||||
default:
|
||||
return &net.IPAddr{
|
||||
IP: net.IP{127, 0, 0, 1},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a Address) To16() *net.IPAddr {
|
||||
switch a.IP.(type) {
|
||||
case *net.IPAddr:
|
||||
return &net.IPAddr{
|
||||
IP: a.IP.(*net.IPAddr).IP.To16(),
|
||||
}
|
||||
case *net.UDPAddr:
|
||||
return &net.IPAddr{
|
||||
IP: a.IP.(*net.UDPAddr).IP.To16(),
|
||||
}
|
||||
default:
|
||||
return &net.IPAddr{
|
||||
IP: net.IPv6loopback,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.String() == o.IP.String()
|
||||
}
|
||||
|
||||
// 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.String() == ip.String()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
var ip []byte
|
||||
switch a.IP.(type) {
|
||||
case *net.IPAddr:
|
||||
ip = []byte(a.IP.(*net.IPAddr).IP)
|
||||
case *net.UDPAddr:
|
||||
ip = []byte(a.IP.(*net.UDPAddr).IP)
|
||||
case i2pkeys.I2PAddr:
|
||||
var err error
|
||||
ip, err = a.IP.(i2pkeys.I2PAddr).ToBytes()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if m, err = w.Write(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
|
||||
i2p := false
|
||||
if _len <= net.IPv6len {
|
||||
switch _len {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
default:
|
||||
return ErrInvalidAddr
|
||||
}
|
||||
} else {
|
||||
_len = len(b)
|
||||
i2p = true
|
||||
}
|
||||
|
||||
IP := make([]byte, _len)
|
||||
copy(IP, b[:_len])
|
||||
if i2p {
|
||||
a.IP, err = i2pkeys.NewI2PAddrFromBytes(IP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Port = uint16(6881)
|
||||
} else {
|
||||
a.IP = &net.IPAddr{
|
||||
IP: net.IP(IP),
|
||||
}
|
||||
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 strings.HasSuffix(host, ".i2p") {
|
||||
a.IP, err = i2pkeys.NewI2PAddrFromBytes([]byte(host))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.Port = uint16(6881)
|
||||
return
|
||||
} else {
|
||||
a.IP = &net.IPAddr{
|
||||
IP: net.ParseIP(host),
|
||||
}
|
||||
if len(a.IP.(*net.IPAddr).IP) == 0 {
|
||||
return ErrInvalidAddr
|
||||
} else if ip := a.IP.(*net.IPAddr).IP.To4(); len(ip) != 0 {
|
||||
a.IP = &net.IPAddr{
|
||||
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, Port: port}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func ConvertAddress(addr net.Addr) (a Address, err error) {
|
||||
switch v := addr.(type) {
|
||||
case *net.TCPAddr:
|
||||
a = NewAddress(v, uint16(v.Port))
|
||||
case *net.UDPAddr:
|
||||
a = NewAddress(v, uint16(v.Port))
|
||||
case *i2pkeys.I2PAddr:
|
||||
a = NewAddress(v, 6881)
|
||||
case i2pkeys.I2PAddr:
|
||||
a = NewAddress(v, 6881)
|
||||
default:
|
||||
err = fmt.Errorf("unsupported address type: %T", addr)
|
||||
}
|
||||
return
|
||||
}
|
48
metainfo/address_test.go
Normal file
48
metainfo/address_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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 (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAddress(t *testing.T) {
|
||||
var addr1 Address
|
||||
if err := addr1.FromString("1.2.3.4:1234"); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := addr1.MarshalBencode()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
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)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -14,7 +14,9 @@
|
||||
|
||||
package metainfo
|
||||
|
||||
import "path/filepath"
|
||||
import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// File represents a file in the multi-file case.
|
||||
type File struct {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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 (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-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.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -20,8 +20,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-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.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -21,12 +21,9 @@ import (
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/xgfone/go-bt/internal/helper"
|
||||
"github.com/eyedeekay/go-i2p-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 {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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))
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -19,7 +19,8 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/eyedeekay/i2pkeys"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
)
|
||||
|
||||
var errInvalidIP = errors.New("invalid ipv4 or ipv6")
|
||||
@ -43,40 +44,74 @@ const (
|
||||
)
|
||||
|
||||
// CompactIP is used to handle the compact ipv4 or ipv6.
|
||||
type CompactIP net.IP
|
||||
type CompactIP []byte
|
||||
|
||||
func (ci CompactIP) String() string {
|
||||
return net.IP(ci).String()
|
||||
//return net.IP(ci).String()
|
||||
switch len(ci) {
|
||||
case net.IPv4len:
|
||||
return net.IP(ci).String()
|
||||
case net.IPv6len:
|
||||
return net.IP(ci).String()
|
||||
case 32:
|
||||
i2p, _ := i2pkeys.DestHashFromBytes(ci)
|
||||
return i2p.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
ip := net.IP(ci)
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
ip = ipv4
|
||||
if len(ci) == net.IPv4len {
|
||||
ip := []byte(ci)
|
||||
ic, err := bencode.EncodeBytes(ip[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ic, nil
|
||||
}
|
||||
return bencode.EncodeBytes(ip[:])
|
||||
if len(ci) == net.IPv6len {
|
||||
ip := []byte(ci)
|
||||
ic, err := bencode.EncodeBytes(ip[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ic, nil
|
||||
}
|
||||
if len(ci) == 32 {
|
||||
i2p, err := i2pkeys.DestHashFromBytes(ci[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bencode.EncodeBytes(i2p[:])
|
||||
}
|
||||
return nil, errInvalidIP
|
||||
}
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (ci *CompactIP) UnmarshalBencode(b []byte) (err error) {
|
||||
var ip net.IP
|
||||
if err = bencode.DecodeBytes(b, &ip); err != nil {
|
||||
return
|
||||
if len(b) >= net.IPv4len && len(b) < net.IPv6len {
|
||||
ip := net.IP(b[len(b)-net.IPv4len:])
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
ip = ipv4
|
||||
}
|
||||
*ci = CompactIP(ip[:])
|
||||
return nil
|
||||
}
|
||||
|
||||
switch len(ip) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
default:
|
||||
return errInvalidIP
|
||||
if len(b) >= net.IPv6len && len(b) < 32 {
|
||||
ip := net.IP(b[len(b)-net.IPv6len:])
|
||||
if ipv6 := ip.To16(); len(ipv6) != 0 {
|
||||
ip = ipv6
|
||||
}
|
||||
*ci = CompactIP(ip[:])
|
||||
return nil
|
||||
}
|
||||
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
ip = ipv4
|
||||
if len(b) >= 32 {
|
||||
i2p, _ := i2pkeys.DestHashFromBytes(b[len(b)-32:])
|
||||
*ci = i2p[:]
|
||||
return nil
|
||||
}
|
||||
|
||||
*ci = CompactIP(ip)
|
||||
return
|
||||
return errInvalidIP
|
||||
}
|
||||
|
||||
// ExtendedHandshakeMsg represent the extended handshake message.
|
||||
@ -86,16 +121,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 []byte `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 +153,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:"-"`
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -16,7 +16,10 @@ package peerprotocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/eyedeekay/sam3"
|
||||
)
|
||||
|
||||
func TestCompactIP(t *testing.T) {
|
||||
@ -26,11 +29,57 @@ func TestCompactIP(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("IPv4 Test", ipv4.String(), len(ipv4))
|
||||
|
||||
var ip CompactIP
|
||||
if err = ip.UnmarshalBencode(b); err != nil {
|
||||
t.Error(err, ip)
|
||||
} else if ip.String() != "1.2.3.4" {
|
||||
t.Error(ip.String(), ",", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompactIP6(t *testing.T) {
|
||||
ipv6 := CompactIP([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
|
||||
b, err := ipv6.MarshalBencode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("IPv6 Test", ipv6.String(), len(ipv6))
|
||||
|
||||
var ip CompactIP
|
||||
if err = ip.UnmarshalBencode(b); err != nil {
|
||||
t.Error(err)
|
||||
} else if ip.String() != "1.2.3.4" {
|
||||
t.Error(ip)
|
||||
} else if ip.String() != "102:304:506:708:90a:b0c:d0e:f10" {
|
||||
t.Error(ip.String(), ",", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompactI2P(t *testing.T) {
|
||||
sam, err := sam3.NewSAM("127.0.0.1:7656")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sam.Close()
|
||||
i2pkeys, err := sam.NewKeys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dh := i2pkeys.Address.DestHash()
|
||||
i2p := CompactIP(dh[:])
|
||||
b, err := i2p.MarshalBencode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("I2P Test", i2p.String(), len(b))
|
||||
|
||||
var ip CompactIP
|
||||
if err = ip.UnmarshalBencode(b); err != nil {
|
||||
t.Error(err)
|
||||
} else if ip.String() != dh.String() {
|
||||
t.Error(ip, dh)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -19,7 +19,7 @@ import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// GenerateAllowedFastSet generates some allowed fast set of the torrent file.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -18,7 +18,7 @@ import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
func TestGenerateAllowedFastSet(t *testing.T) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -19,7 +19,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-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)
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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:
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -14,7 +14,9 @@
|
||||
|
||||
package peerprotocol
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBitField(t *testing.T) {
|
||||
bf := NewBitFieldFromBools([]bool{
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -21,21 +21,24 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-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")
|
||||
)
|
||||
|
||||
var DialTimeout = net.DialTimeout
|
||||
var Dial = net.Dial
|
||||
|
||||
// Bep3Handler is used to handle the BEP 3 type message if Handler has also
|
||||
// implemented the interface.
|
||||
type Bep3Handler interface {
|
||||
@ -79,38 +82,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 +93,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 +125,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 +150,9 @@ type PeerConn struct {
|
||||
//
|
||||
// Optional.
|
||||
OnWriteMsg func(pc *PeerConn, m Message) error
|
||||
|
||||
notFirstMsg bool
|
||||
extHandshake bool
|
||||
}
|
||||
|
||||
// NewPeerConn returns a new PeerConn.
|
||||
@ -192,7 +161,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 +171,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 := DialTimeout("tcp", addr, timeout)
|
||||
if err == nil {
|
||||
pc = NewPeerConn(conn, id, infohash)
|
||||
}
|
||||
@ -212,53 +180,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 +247,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 +270,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 +447,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 +462,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
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -14,7 +14,9 @@
|
||||
|
||||
package peerprotocol
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ProtocolHeader is the BT protocal prefix.
|
||||
//
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -22,7 +22,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// Handler is used to handle the incoming peer connection.
|
||||
@ -65,9 +65,11 @@ type Config struct {
|
||||
HandleMessage func(pc *PeerConn, msg Message, handler Handler) error
|
||||
}
|
||||
|
||||
func (c *Config) set(conf *Config) {
|
||||
if conf != nil {
|
||||
*c = *conf
|
||||
var Listen = net.Listen
|
||||
|
||||
func (c *Config) set(conf ...Config) {
|
||||
if len(conf) > 0 {
|
||||
*c = conf[0]
|
||||
}
|
||||
|
||||
if c.MaxLength == 0 {
|
||||
@ -93,35 +95,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) {
|
||||
ln, err := net.Listen(network, address)
|
||||
func NewServerByListen(network, address string, id metainfo.Hash, h Handler,
|
||||
c ...Config) (*Server, error) {
|
||||
ln, err := 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, 2023 idk
|
||||
//
|
||||
// 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,84 @@ package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-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
|
||||
// Force a peer address to be used
|
||||
var PeerAddress net.Addr
|
||||
|
||||
// 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, IP: PeerAddress})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -21,6 +21,9 @@ package httptracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -28,8 +31,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// AnnounceRequest is the tracker announce requests.
|
||||
@ -101,7 +104,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))
|
||||
@ -278,15 +281,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}
|
||||
}
|
||||
|
||||
@ -294,6 +293,28 @@ func NewClient(id metainfo.Hash, announceURL, scrapeURL string) *Client {
|
||||
func (t *Client) Close() error { return nil }
|
||||
func (t *Client) String() string { return t.AnnounceURL }
|
||||
|
||||
var (
|
||||
i2pB64enc *base64.Encoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~")
|
||||
i2pB32enc *base32.Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
||||
)
|
||||
|
||||
func Base32(b64addr string) string {
|
||||
if len(b64addr) > 300 {
|
||||
b64addr = strings.TrimSuffix(b64addr, ".i2p")
|
||||
b64, err := i2pB64enc.DecodeString(b64addr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
//hash.Write([]byte(b64))
|
||||
var s []byte
|
||||
for _, e := range sha256.Sum256(b64) {
|
||||
s = append(s, e)
|
||||
}
|
||||
return strings.ToLower(strings.Replace(i2pB32enc.EncodeToString(s), "=", "", -1)) + ".b32.i2p"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *Client) send(c context.Context, u string, vs url.Values, r interface{}) (err error) {
|
||||
var url string
|
||||
if strings.IndexByte(u, '?') < 0 {
|
||||
@ -314,7 +335,11 @@ func (t *Client) send(c context.Context, u string, vs url.Values, r interface{})
|
||||
resp, err = t.Client.Do(req)
|
||||
}
|
||||
|
||||
if resp != nil {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.Body != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
@ -326,13 +351,17 @@ func (t *Client) send(c context.Context, u string, vs url.Values, r interface{})
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -15,26 +15,54 @@
|
||||
package httptracker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/xgfone/go-bt/bencode"
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/i2pkeys"
|
||||
"github.com/eyedeekay/go-i2p-bt/bencode"
|
||||
"github.com/eyedeekay/go-i2p-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 strings.HasSuffix(p.IP, ".i2p") {
|
||||
var netAddr net.Addr
|
||||
if netAddr, err = i2pkeys.NewI2PAddrFromString(strings.TrimSuffix(p.IP, ".i2p")); err != nil {
|
||||
return
|
||||
} else {
|
||||
return []metainfo.Address{{IP: netAddr, Port: p.Port}}, nil
|
||||
}
|
||||
|
||||
}
|
||||
if ip := net.ParseIP(p.IP); len(ip) != 0 {
|
||||
return []metainfo.Address{{IP: &net.IPAddr{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: &net.IPAddr{IP: ip}, Port: p.Port}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Peers is a set of the peers.
|
||||
type Peers []Peer
|
||||
@ -48,20 +76,27 @@ 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
|
||||
addrBytes := []byte(vs[i : i+6])
|
||||
if err = addr.UnmarshalBinary(addrBytes); 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 {
|
||||
|
||||
m, ok := p.(map[string]interface{})
|
||||
if !ok {
|
||||
return errInvalidPeer
|
||||
@ -85,7 +120,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 +128,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 +172,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())
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -18,7 +18,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
func TestHTTPAnnounceRequest(t *testing.T) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -26,9 +26,9 @@ import (
|
||||
"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/eyedeekay/go-i2p-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/tracker/httptracker"
|
||||
"github.com/eyedeekay/go-i2p-bt/tracker/udptracker"
|
||||
)
|
||||
|
||||
// Predefine some announce events.
|
||||
@ -53,20 +53,24 @@ 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
|
||||
Key int32 // Optional
|
||||
NumWant int32 // Optional
|
||||
Port uint16 // Optional
|
||||
IP net.Addr // Optional
|
||||
Key int32 // Optional
|
||||
NumWant int32 // Optional, BEP 15: -1 for default. But we use 0 as default.
|
||||
Port uint16 // Optional
|
||||
}
|
||||
|
||||
// ToHTTPAnnounceRequest creates a new httptracker.AnnounceRequest from itself.
|
||||
func (ar AnnounceRequest) ToHTTPAnnounceRequest() httptracker.AnnounceRequest {
|
||||
var ip string
|
||||
if len(ar.IP) != 0 {
|
||||
func (ar *AnnounceRequest) ToHTTPAnnounceRequest() *httptracker.AnnounceRequest {
|
||||
ip := "127.0.0.1"
|
||||
if PeerAddress != nil {
|
||||
ip = PeerAddress.String()
|
||||
} else if ar.IP != nil {
|
||||
ip = ar.IP.String()
|
||||
}
|
||||
|
||||
return httptracker.AnnounceRequest{
|
||||
if ar.Port == 0 {
|
||||
ar.Port = 6881
|
||||
}
|
||||
return &httptracker.AnnounceRequest{
|
||||
InfoHash: ar.InfoHash,
|
||||
PeerID: ar.PeerID,
|
||||
Uploaded: ar.Uploaded,
|
||||
@ -77,19 +81,22 @@ func (ar AnnounceRequest) ToHTTPAnnounceRequest() httptracker.AnnounceRequest {
|
||||
Event: ar.Event,
|
||||
NumWant: ar.NumWant,
|
||||
Key: ar.Key,
|
||||
Compact: true,
|
||||
}
|
||||
}
|
||||
|
||||
// ToUDPAnnounceRequest creates a new udptracker.AnnounceRequest from itself.
|
||||
func (ar AnnounceRequest) ToUDPAnnounceRequest() udptracker.AnnounceRequest {
|
||||
return udptracker.AnnounceRequest{
|
||||
func (ar *AnnounceRequest) ToUDPAnnounceRequest() *udptracker.AnnounceRequest {
|
||||
if PeerAddress != nil {
|
||||
ar.IP = PeerAddress
|
||||
}
|
||||
return &udptracker.AnnounceRequest{
|
||||
InfoHash: ar.InfoHash,
|
||||
PeerID: ar.PeerID,
|
||||
Downloaded: ar.Downloaded,
|
||||
Left: ar.Left,
|
||||
Uploaded: ar.Uploaded,
|
||||
Event: ar.Event,
|
||||
IP: ar.IP,
|
||||
Key: ar.Key,
|
||||
NumWant: ar.NumWant,
|
||||
Port: ar.Port,
|
||||
@ -100,10 +107,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 +118,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 +134,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 +178,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 +202,48 @@ type Client interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ClientConfig is used to configure the defalut client implementation.
|
||||
type ClientConfig struct {
|
||||
// The ID of the local client peer.
|
||||
ID metainfo.Hash
|
||||
|
||||
// The http client used only the tracker client is based on HTTP.
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
// 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 config ClientConfig
|
||||
if len(conf) > 0 {
|
||||
config = conf[0]
|
||||
}
|
||||
|
||||
u, err := url.Parse(connURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
switch u.Scheme {
|
||||
case "http", "https":
|
||||
tracker := httptracker.NewClient(connURL, "")
|
||||
if !config.ID.IsZero() {
|
||||
tracker.ID = config.ID
|
||||
}
|
||||
c = &tclient{url: connURL, http: tracker}
|
||||
|
||||
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
|
||||
case "udp", "udp4", "udp6":
|
||||
var utc *udptracker.Client
|
||||
config := udptracker.ClientConfig{ID: config.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, 2023 idk
|
||||
//
|
||||
// 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/eyedeekay/go-i2p-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-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) OnConnect(raddr net.Addr) (err error) { return }
|
||||
func (testHandler) OnAnnounce(raddr net.Addr, 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.IPAddr{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.Addr, 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.IPAddr{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, 2023 idk
|
||||
//
|
||||
// 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,10 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/i2pkeys"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
const maxBufSize = 2048
|
||||
|
||||
// ProtocolID is magic constant for the udp tracker connection.
|
||||
//
|
||||
// BEP 15
|
||||
@ -57,15 +56,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, w int) {
|
||||
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 +73,28 @@ 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 w == 1 {
|
||||
ip := make(net.IP, net.IPv4len)
|
||||
copy(ip, b[68:72])
|
||||
r.IP = &net.IPAddr{IP: ip}
|
||||
b = b[72:]
|
||||
} else if w == 0 {
|
||||
ip := make(net.IP, net.IPv6len)
|
||||
copy(ip, b[68:84])
|
||||
r.IP = &net.IPAddr{IP: ip}
|
||||
b = b[84:]
|
||||
} else {
|
||||
ip := make(net.IP, 32)
|
||||
copy(ip, b[68:100])
|
||||
r.IP, _ = i2pkeys.DestHashFromBytes(ip)
|
||||
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 +105,31 @@ 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 len(r.IP.String()) < 31 {
|
||||
IP := net.ParseIP(r.IP.String())
|
||||
if ip := IP.To4(); ip != nil {
|
||||
buf.Write(ip[:])
|
||||
} else {
|
||||
buf.Write(IP[:])
|
||||
}
|
||||
} else {
|
||||
buf.Write([]byte(r.IP.String()))
|
||||
}
|
||||
|
||||
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,35 +141,32 @@ 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()
|
||||
} else {
|
||||
addr.IP = addr.IP.To16()
|
||||
for _, addr := range r.Addresses {
|
||||
if ip := addr.To4(); ip != nil {
|
||||
buf.Write(ip.IP[:])
|
||||
} else if ip := addr.To16(); ip != nil {
|
||||
buf.Write(ip.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 -
|
||||
b = b[12:]
|
||||
iplen := net.IPv6len
|
||||
if ipv4 {
|
||||
iplen = net.IPv4len
|
||||
@ -156,13 +174,12 @@ func (r *AnnounceResponse) DecodeFrom(b []byte, ipv4 bool) {
|
||||
|
||||
_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.IP, iplen)
|
||||
copy(ip, b[i-step:i-2])
|
||||
port := binary.BigEndian.Uint16(b[i-2 : i])
|
||||
r.Addresses = append(r.Addresses, metainfo.Address{IP: &net.IPAddr{IP: ip}, Port: port})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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,51 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
var Dial = net.Dial
|
||||
|
||||
// NewClientByDial returns a new Client by dialing.
|
||||
func NewClientByDial(network, address string, id metainfo.Hash) (*Client, error) {
|
||||
conn, err := net.Dial(network, address)
|
||||
func NewClientByDial(network, address string, c ...ClientConfig) (*Client, error) {
|
||||
conn, err := Dial(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewClient(conn.(*net.UDPConn), id), nil
|
||||
|
||||
return NewClient(conn, c...), nil
|
||||
}
|
||||
|
||||
// NewClient returns a new Client.
|
||||
func NewClient(conn *net.UDPConn, id metainfo.Hash) *Client {
|
||||
func NewClient(conn net.Conn, 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 +74,12 @@ func NewClient(conn *net.UDPConn, id metainfo.Hash) *Client {
|
||||
//
|
||||
// BEP 15
|
||||
type Client struct {
|
||||
MaxBufSize int // Default: 2048
|
||||
|
||||
id metainfo.Hash
|
||||
conn *net.UDPConn
|
||||
ipv4 bool
|
||||
conf ClientConfig
|
||||
conn net.Conn
|
||||
last time.Time
|
||||
cid uint64
|
||||
tid uint32
|
||||
ipv4 bool
|
||||
}
|
||||
|
||||
// Close closes the UDP tracker client.
|
||||
@ -106,6 +117,7 @@ func (utc *Client) parseError(b []byte) (tid uint32, reason string) {
|
||||
|
||||
func (utc *Client) send(b []byte) (err error) {
|
||||
n, err := utc.conn.Write(b)
|
||||
//n, err := utc.conn.WriteTo()
|
||||
if err == nil && n < len(b) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
@ -131,21 +143,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 +175,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 +184,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 +203,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 +219,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
|
||||
}
|
||||
|
||||
@ -227,16 +235,15 @@ func (utc *Client) announce(ctx context.Context, req AnnounceRequest) (r Announc
|
||||
// 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")
|
||||
func (utc *Client) Announce(c context.Context, r *AnnounceRequest) (AnnounceResponse, error) {
|
||||
if r.PeerID.IsZero() {
|
||||
r.PeerID = utc.conf.ID
|
||||
}
|
||||
|
||||
r.PeerID = utc.id
|
||||
return utc.announce(c, r)
|
||||
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 +251,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 +271,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
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020~2023 xgfone
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -24,15 +24,15 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xgfone/go-bt/metainfo"
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
)
|
||||
|
||||
// ServerHandler is used to handle the request from the client.
|
||||
type ServerHandler interface {
|
||||
// OnConnect is used to check whether to make the connection or not.
|
||||
OnConnect(raddr *net.UDPAddr) (err error)
|
||||
OnAnnounce(raddr *net.UDPAddr, req AnnounceRequest) (AnnounceResponse, error)
|
||||
OnScrap(raddr *net.UDPAddr, infohashes []metainfo.Hash) ([]ScrapeResponse, error)
|
||||
OnConnect(raddr net.Addr) (err error)
|
||||
OnAnnounce(raddr net.Addr, req AnnounceRequest) (AnnounceResponse, error)
|
||||
OnScrap(raddr net.Addr, infohashes []metainfo.Hash) ([]ScrapeResponse, error)
|
||||
}
|
||||
|
||||
func encodeResponseHeader(buf *bytes.Buffer, action, tid uint32) {
|
||||
@ -41,18 +41,29 @@ func encodeResponseHeader(buf *bytes.Buffer, action, tid uint32) {
|
||||
}
|
||||
|
||||
type wrappedPeerAddr struct {
|
||||
Addr *net.UDPAddr
|
||||
Addr net.Addr
|
||||
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,40 +126,32 @@ 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 {
|
||||
continue
|
||||
}
|
||||
go uts.handleRequest(raddr.(*net.UDPAddr), buf, n)
|
||||
go uts.handleRequest(raddr.(net.Addr), buf, n)
|
||||
}
|
||||
}
|
||||
|
||||
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.Addr, 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) {
|
||||
func (uts *Server) send(raddr net.Addr, 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())
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,45 +159,47 @@ func (uts *Server) getConnectionID() uint64 {
|
||||
return atomic.AddUint64(&uts.cid, 1)
|
||||
}
|
||||
|
||||
func (uts *Server) addConnection(cid uint64, raddr *net.UDPAddr) {
|
||||
func (uts *Server) addConnection(cid uint64, raddr net.Addr) {
|
||||
now := time.Now()
|
||||
uts.lock.Lock()
|
||||
uts.conns[cid] = wrappedPeerAddr{Addr: raddr, Time: now}
|
||||
uts.lock.Unlock()
|
||||
}
|
||||
|
||||
func (uts *Server) checkConnection(cid uint64, raddr *net.UDPAddr) (ok bool) {
|
||||
func (uts *Server) checkConnection(cid uint64, raddr net.Addr) (ok bool) {
|
||||
uts.lock.RLock()
|
||||
if w, _ok := uts.conns[cid]; _ok && w.Addr.Port == raddr.Port &&
|
||||
bytes.Equal(w.Addr.IP, raddr.IP) {
|
||||
if w, _ok := uts.conns[cid]; _ok &&
|
||||
w.Addr.String() == raddr.String() {
|
||||
ok = true
|
||||
}
|
||||
uts.lock.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (uts *Server) sendError(raddr *net.UDPAddr, tid uint32, reason string) {
|
||||
func (uts *Server) sendError(raddr net.Addr, tid uint32, reason string) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 8+len(reason)))
|
||||
encodeResponseHeader(buf, ActionError, tid)
|
||||
buf.WriteString(reason)
|
||||
uts.send(raddr, buf.Bytes())
|
||||
}
|
||||
|
||||
func (uts *Server) sendConnResp(raddr *net.UDPAddr, tid uint32, cid uint64) {
|
||||
func (uts *Server) sendConnResp(raddr net.Addr, tid uint32, cid uint64) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 16))
|
||||
encodeResponseHeader(buf, ActionConnect, tid)
|
||||
binary.Write(buf, binary.BigEndian, cid)
|
||||
uts.send(raddr, buf.Bytes())
|
||||
}
|
||||
|
||||
func (uts *Server) sendAnnounceResp(raddr *net.UDPAddr, tid uint32, resp AnnounceResponse) {
|
||||
func (uts *Server) sendAnnounceResp(raddr net.Addr, 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.Addr, tid uint32,
|
||||
rs []ScrapeResponse) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 8+len(rs)*12))
|
||||
encodeResponseHeader(buf, ActionScrape, tid)
|
||||
for _, r := range rs {
|
||||
@ -200,10 +208,10 @@ func (uts *Server) sendScrapResp(raddr *net.UDPAddr, tid uint32, rs []ScrapeResp
|
||||
uts.send(raddr, buf.Bytes())
|
||||
}
|
||||
|
||||
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
|
||||
func (uts *Server) handlePacket(raddr net.Addr, b []byte) {
|
||||
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.
|
||||
@ -228,18 +236,24 @@ func (uts *Server) handlePacket(raddr *net.UDPAddr, b []byte) {
|
||||
switch action {
|
||||
case ActionAnnounce:
|
||||
var req AnnounceRequest
|
||||
if raddr.IP.To4() != nil { // For ipv4
|
||||
if len(raddr.String()) < 16 { // For ipv4
|
||||
if len(b) < 82 {
|
||||
uts.sendError(raddr, tid, "invalid announce request")
|
||||
return
|
||||
}
|
||||
req.DecodeFrom(b)
|
||||
} else { // For ipv6
|
||||
req.DecodeFrom(b, 1)
|
||||
} else if len(raddr.String()) >= 16 && len(raddr.String()) < 32 { // for ipv6
|
||||
if len(b) < 94 {
|
||||
uts.sendError(raddr, tid, "invalid announce request")
|
||||
return
|
||||
}
|
||||
req.DecodeFrom(b)
|
||||
req.DecodeFrom(b, 0)
|
||||
} else { // for cryptographic protocols
|
||||
if len(b) < 110 {
|
||||
uts.sendError(raddr, tid, "invalid announce request")
|
||||
return
|
||||
}
|
||||
req.DecodeFrom(b, 0)
|
||||
}
|
||||
|
||||
resp, err := uts.handler.OnAnnounce(raddr, req)
|
||||
@ -248,7 +262,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 +280,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, 2023 idk
|
||||
//
|
||||
// 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/eyedeekay/go-i2p-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) OnConnect(raddr net.Addr) (err error) { return }
|
||||
func (testHandler) OnAnnounce(raddr net.Addr, 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.IPAddr{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.Addr, 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.IPAddr{IP: net.ParseIP("127.0.0.1")}, Port: 80, Exts: exts}
|
||||
resp, err := client.Announce(context.Background(), req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
1
uploader/torrent.go
Normal file
1
uploader/torrent.go
Normal file
@ -0,0 +1 @@
|
||||
package uploader
|
97
uploadhandler/block_upload_handler.go
Normal file
97
uploadhandler/block_upload_handler.go
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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 blockdownload
|
||||
|
||||
import (
|
||||
"github.com/eyedeekay/go-i2p-bt/metainfo"
|
||||
pp "github.com/eyedeekay/go-i2p-bt/peerprotocol"
|
||||
)
|
||||
|
||||
// BlockUploadHandler is used to downloads the files in the torrent file.
|
||||
type BlockUploadHandler struct {
|
||||
pp.NoopHandler
|
||||
pp.NoopBep3Handler
|
||||
pp.NoopBep6Handler
|
||||
|
||||
Info metainfo.Info // Required
|
||||
OnBlock func(index, offset uint32, b []byte) error // Required
|
||||
RespondBlock func(c *pp.PeerConn) error // Required
|
||||
}
|
||||
|
||||
// NewBlockUploadHandler returns a new BlockUploadHandler.
|
||||
func NewBlockUploadHandler(info metainfo.Info, onBlock func(pieceIndex, pieceOffset uint32, b []byte) error, respondBlock func(c *pp.PeerConn) error) BlockUploadHandler {
|
||||
return BlockUploadHandler{
|
||||
Info: info,
|
||||
OnBlock: onBlock,
|
||||
RespondBlock: respondBlock,
|
||||
}
|
||||
}
|
||||
|
||||
// OnHandShake implements the interface Handler#OnHandShake.
|
||||
//
|
||||
// Notice: it uses the field Data to store the inner data, you mustn't override
|
||||
// it.
|
||||
func (fd BlockUploadHandler) OnHandShake(c *pp.PeerConn) (err error) {
|
||||
if err = c.SetUnchoked(); err == nil {
|
||||
err = c.SetInterested()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// BEP 3
|
||||
|
||||
func (fd BlockUploadHandler) respond(pc *pp.PeerConn) (err error) {
|
||||
if pc.PeerChoked {
|
||||
err = pp.ErrChoked
|
||||
} else {
|
||||
err = fd.RespondBlock(pc)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Piece implements the interface Bep3Handler#Piece.
|
||||
func (fd BlockUploadHandler) Piece(c *pp.PeerConn, i, b uint32, p []byte) (err error) {
|
||||
if err = fd.OnBlock(i, b, p); err == nil {
|
||||
err = fd.respond(c)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Unchoke implements the interface Bep3Handler#Unchoke.
|
||||
func (fd BlockUploadHandler) Unchoke(pc *pp.PeerConn) (err error) {
|
||||
return fd.respond(pc)
|
||||
}
|
||||
|
||||
// Have implements the interface Bep3Handler#Have.
|
||||
func (fd BlockUploadHandler) Have(pc *pp.PeerConn, index uint32) (err error) {
|
||||
pc.BitField.Set(index)
|
||||
return
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// BEP 6
|
||||
|
||||
// HaveAll implements the interface Bep6Handler#HaveAll.
|
||||
func (fd BlockUploadHandler) HaveAll(pc *pp.PeerConn) (err error) {
|
||||
pc.BitField = pp.NewBitField(fd.Info.CountPieces(), true)
|
||||
return
|
||||
}
|
||||
|
||||
// Reject implements the interface Bep6Handler#Reject.
|
||||
func (fd BlockUploadHandler) Reject(pc *pp.PeerConn, index, begin, length uint32) (err error) {
|
||||
pc.BitField.Unset(index)
|
||||
return fd.respond(pc)
|
||||
}
|
105
utils/addr.go
Normal file
105
utils/addr.go
Normal file
@ -0,0 +1,105 @@
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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 (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/eyedeekay/i2pkeys"
|
||||
)
|
||||
|
||||
func NetIPAddr(a net.Addr) net.IP {
|
||||
switch a := a.(type) {
|
||||
case *net.TCPAddr:
|
||||
return a.IP
|
||||
case *net.UDPAddr:
|
||||
return a.IP
|
||||
case *i2pkeys.I2PAddr:
|
||||
return net.ParseIP("127.0.0.1")
|
||||
case i2pkeys.I2PAddr:
|
||||
return net.ParseIP("127.0.0.1")
|
||||
default:
|
||||
ip, _, err := net.SplitHostPort(a.String())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return net.ParseIP(ip)
|
||||
}
|
||||
}
|
||||
|
||||
func IPAddr(a net.Addr) string {
|
||||
switch a := a.(type) {
|
||||
case *net.TCPAddr:
|
||||
return a.IP.String()
|
||||
case *net.UDPAddr:
|
||||
return a.IP.String()
|
||||
case *i2pkeys.I2PAddr:
|
||||
return a.DestHash().Hash()
|
||||
case i2pkeys.I2PAddr:
|
||||
return a.DestHash().Hash()
|
||||
default:
|
||||
ip, _, err := net.SplitHostPort(a.String())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
func Port(a net.Addr) int {
|
||||
switch a := a.(type) {
|
||||
case *net.TCPAddr:
|
||||
return a.Port
|
||||
case *net.UDPAddr:
|
||||
return a.Port
|
||||
case *i2pkeys.I2PAddr:
|
||||
return 6881
|
||||
case i2pkeys.I2PAddr:
|
||||
return 6881
|
||||
default:
|
||||
_, port, err := net.SplitHostPort(a.String())
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
iport, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return iport
|
||||
}
|
||||
}
|
||||
|
||||
func IsIPv6Addr(addr net.Addr) bool {
|
||||
switch xaddr := addr.(type) {
|
||||
case *net.UDPAddr:
|
||||
return xaddr.IP.To4() == nil
|
||||
case *net.TCPAddr:
|
||||
return xaddr.IP.To4() == nil
|
||||
case *net.IPAddr:
|
||||
return xaddr.IP.To4() == nil
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func IpIsZero(ip net.IP) bool {
|
||||
for _, b := range ip {
|
||||
if b != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
50
utils/bool.go
Normal file
50
utils/bool.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright 2020 xgfone, 2023 idk
|
||||
//
|
||||
// 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, 2023 idk
|
||||
//
|
||||
// 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, 2023 idk
|
||||
//
|
||||
// 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, 2023 idk
|
||||
//
|
||||
// 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, 2023 idk
|
||||
//
|
||||
// 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, 2023 idk
|
||||
//
|
||||
// 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