Compare commits
11 commits
dependabot
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
226d6eace4 | ||
|
48b28d0b07 | ||
|
494d67fae4 | ||
|
a7b4e6a559 | ||
|
1b6b7f1b5b | ||
|
6baeb76ce2 | ||
|
c1fcd3d624 | ||
|
9330b3fd59 | ||
|
c8f0c1eed8 | ||
|
737053bd0e | ||
|
441b06169f |
8 changed files with 270 additions and 158 deletions
|
@ -1,80 +1,3 @@
|
||||||
# How to Contribute
|
## Contributing to LBRY
|
||||||
|
|
||||||
## Discussion
|
https://lbry.tech/contribute
|
||||||
|
|
||||||
Long-term discussion and bug reports are maintained via [GitHub Issues].
|
|
||||||
Code review is done via [GitHub Pull Requests].
|
|
||||||
Real-time discussion is done via [freenode IRC].
|
|
||||||
|
|
||||||
[GitHub Issues]: https://github.com/chihaya/chihaya/issues
|
|
||||||
[GitHub Pull Requests]: https://github.com/chihaya/chihaya/pulls
|
|
||||||
[freenode IRC]: http://webchat.freenode.net/?channels=chihaya
|
|
||||||
|
|
||||||
## Pull Request Procedure
|
|
||||||
|
|
||||||
If you're looking to contribute, search the GitHub for issues labeled "low-hanging fruit".
|
|
||||||
You can also hop into IRC and ask a developer who's online for their opinion.
|
|
||||||
|
|
||||||
Small, self-describing fixes are perfectly fine to submit without discussion.
|
|
||||||
However, please do not submit a massive Pull Request without prior communication.
|
|
||||||
Large, unannounced changes usually lead to confusion and time wasted for everyone.
|
|
||||||
If you were planning to write a large change, post an issue on GitHub first and discuss it.
|
|
||||||
|
|
||||||
Pull Requests will be treated as "review requests", and we will give feedback we expect to see corrected on style and substance before merging.
|
|
||||||
Changes contributed via Pull Request should focus on a single issue at a time.
|
|
||||||
We will not accept pull-requests that try to "sneak" unrelated changes in.
|
|
||||||
|
|
||||||
The average contribution flow is as follows:
|
|
||||||
|
|
||||||
- Determine what to work on via creating and issue or finding an issue you want to solve.
|
|
||||||
- Create a topic branch from where you want to base your work. This is usually `master`.
|
|
||||||
- Make commits of logical units.
|
|
||||||
- Make sure your commit messages are in the proper format
|
|
||||||
- Push your changes to a topic branch in your fork of the repository.
|
|
||||||
- Submit a pull request.
|
|
||||||
- Your PR will be reviewed and merged by one of the maintainers.
|
|
||||||
- You may be asked to make changes and [rebase] your commits.
|
|
||||||
|
|
||||||
[rebase]: https://git-scm.com/book/en/v2/Git-Branching-Rebasin://git-scm.com/book/en/v2/Git-Branching-Rebasing
|
|
||||||
|
|
||||||
## Style
|
|
||||||
|
|
||||||
Any new files should include the license header found at the top of every source file.
|
|
||||||
|
|
||||||
### Go
|
|
||||||
|
|
||||||
The project follows idiomatic [Go conventions] for style.
|
|
||||||
If you're just starting out writing Go, you can check out this [meta-package] that documents style idiomatic style decisions you will find in open source Go code.
|
|
||||||
All files should have `gofmt` executed on them and code should strive to have full coverage of static analysis tools like [govet] and [golint].
|
|
||||||
|
|
||||||
[Go conventions]: https://github.com/golang/go/wiki/CodeReviewComments
|
|
||||||
[meta-package]: https://github.com/jzelinskie/conventions
|
|
||||||
[govet]: https://golang.org/cmd/vet
|
|
||||||
[golint]: https://github.com/golang/lint
|
|
||||||
|
|
||||||
### Commit Messages
|
|
||||||
|
|
||||||
We follow a rough convention for commit messages that is designed to answer two questions: what changed and why.
|
|
||||||
The subject line should feature the what and the body of the commit should describe the why.
|
|
||||||
|
|
||||||
```git
|
|
||||||
scripts: add the test-cluster command
|
|
||||||
|
|
||||||
this uses tmux to setup a test cluster that you can easily kill and
|
|
||||||
start for debugging.
|
|
||||||
|
|
||||||
Fixes #38
|
|
||||||
```
|
|
||||||
|
|
||||||
The format can be described more formally as follows:
|
|
||||||
|
|
||||||
```git
|
|
||||||
<subsystem>: <what changed>
|
|
||||||
<BLANK LINE>
|
|
||||||
<why this change was made>
|
|
||||||
<BLANK LINE>
|
|
||||||
<footer>
|
|
||||||
```
|
|
||||||
|
|
||||||
The first line is the subject and should be no longer than 70 characters, the second line is always blank, and other lines should be wrapped at 80 characters.
|
|
||||||
This allows the message to be easier to read on GitHub as well as in various git tools.
|
|
||||||
|
|
18
LICENSE
18
LICENSE
|
@ -1,3 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015-2022 LBRY Inc
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||||
|
following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Chihaya is released under a BSD 2-Clause license, reproduced below.
|
Chihaya is released under a BSD 2-Clause license, reproduced below.
|
||||||
|
|
||||||
Copyright (c) 2015, The Chihaya Authors
|
Copyright (c) 2015, The Chihaya Authors
|
||||||
|
|
180
README.md
180
README.md
|
@ -1,64 +1,17 @@
|
||||||
# Chihaya
|
# LBRY Tracker
|
||||||
|
|
||||||
[![Build Status](https://github.com/chihaya/chihaya/workflows/Build%20&%20Test/badge.svg)](https://github.com/chihaya/chihaya/actions)
|
The LBRY tracker is a server that helps peers find each other. It was forked from [Chihaya](https://github.com/chihaya/chihaya), an open-source [BitTorrent tracker](https://en.wikipedia.org/wiki/BitTorrent_tracker).
|
||||||
[![Container Image](https://img.shields.io/github/v/release/chihaya/chihaya?color=%232496ED&label=container&logo=docker "Container Image")](https://quay.io/repository/jzelinskie/chihaya?tab=tags)
|
|
||||||
[![GoDoc](https://godoc.org/github.com/chihaya/chihaya?status.svg)](https://godoc.org/github.com/chihaya/chihaya)
|
|
||||||
[![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://en.wikipedia.org/wiki/BSD_licenses#2-clause_license_.28.22Simplified_BSD_License.22_or_.22FreeBSD_License.22.29)
|
|
||||||
[![IRC Channel](https://img.shields.io/badge/IRC-%23chihaya-%235555ff.svg "IRC Channel")](https://web.libera.chat/#chihaya)
|
|
||||||
|
|
||||||
Chihaya is an open source [BitTorrent tracker] written in [Go].
|
|
||||||
|
|
||||||
Differentiating features include:
|
## Installation and Usage
|
||||||
|
|
||||||
- HTTP and UDP protocols
|
### Building from HEAD
|
||||||
- IPv4 and IPv6 support
|
|
||||||
- Pre/Post middleware hooks
|
|
||||||
- [YAML] configuration
|
|
||||||
- Metrics via [Prometheus]
|
|
||||||
- High Availability via [Redis]
|
|
||||||
- Kubernetes deployment via [Helm]
|
|
||||||
|
|
||||||
**Note:** The master branch may be in an unstable or even broken state during development.
|
|
||||||
Please use [releases] instead of the master branch in order to get stable binaries.
|
|
||||||
|
|
||||||
[releases]: https://github.com/chihaya/chihaya/releases
|
|
||||||
[BitTorrent tracker]: https://en.wikipedia.org/wiki/BitTorrent_tracker
|
|
||||||
[Go]: https://golang.org
|
|
||||||
[YAML]: https://yaml.org
|
|
||||||
[Prometheus]: https://prometheus.io
|
|
||||||
[Redis]: https://redis.io
|
|
||||||
[Helm]: https://helm.sh
|
|
||||||
|
|
||||||
## Why Chihaya?
|
|
||||||
|
|
||||||
Chihaya is built for developers looking to integrate BitTorrent into a preexisting production environment.
|
|
||||||
Chihaya's pluggable architecture and middleware framework offers a simple and flexible integration point that abstracts the BitTorrent tracker protocols.
|
|
||||||
The most common use case for Chihaya is enabling peer-to-peer cloud software deployments.
|
|
||||||
|
|
||||||
## Development
|
|
||||||
|
|
||||||
### Contributing
|
|
||||||
|
|
||||||
Long-term discussion and bug reports are maintained via [GitHub Issues].
|
|
||||||
Code review is done via [GitHub Pull Requests].
|
|
||||||
Real-time discussion is done via [freenode IRC].
|
|
||||||
|
|
||||||
For more information read [CONTRIBUTING.md].
|
|
||||||
|
|
||||||
[GitHub Issues]: https://github.com/chihaya/chihaya/issues
|
|
||||||
[GitHub Pull Requests]: https://github.com/chihaya/chihaya/pulls
|
|
||||||
[freenode IRC]: http://webchat.freenode.net/?channels=chihaya
|
|
||||||
[CONTRIBUTING.md]: https://github.com/chihaya/chihaya/blob/master/CONTRIBUTING.md
|
|
||||||
|
|
||||||
### Getting Started
|
|
||||||
|
|
||||||
#### Building from HEAD
|
|
||||||
|
|
||||||
In order to compile the project, the [latest stable version of Go] and knowledge of a [working Go environment] are required.
|
In order to compile the project, the [latest stable version of Go] and knowledge of a [working Go environment] are required.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone git@github.com:chihaya/chihaya.git
|
git clone git@github.com:lbryio/tracker.git
|
||||||
cd chihaya
|
cd tracker
|
||||||
go build ./cmd/chihaya
|
go build ./cmd/chihaya
|
||||||
./chihaya --help
|
./chihaya --help
|
||||||
```
|
```
|
||||||
|
@ -66,14 +19,7 @@ go build ./cmd/chihaya
|
||||||
[latest stable version of Go]: https://golang.org/dl
|
[latest stable version of Go]: https://golang.org/dl
|
||||||
[working Go environment]: https://golang.org/doc/code.html
|
[working Go environment]: https://golang.org/doc/code.html
|
||||||
|
|
||||||
#### Docker
|
### Testing
|
||||||
|
|
||||||
Docker containers are available for [HEAD] and [stable] releases.
|
|
||||||
|
|
||||||
[HEAD]: https://quay.io/jzelinskie/chihaya-git
|
|
||||||
[stable]: https://quay.io/jzelinskie/chihaya
|
|
||||||
|
|
||||||
#### Testing
|
|
||||||
|
|
||||||
The following will run all tests and benchmarks.
|
The following will run all tests and benchmarks.
|
||||||
Removing `-bench` will just run unit tests.
|
Removing `-bench` will just run unit tests.
|
||||||
|
@ -82,21 +28,119 @@ Removing `-bench` will just run unit tests.
|
||||||
go test -bench $(go list ./...)
|
go test -bench $(go list ./...)
|
||||||
```
|
```
|
||||||
|
|
||||||
The Chihaya executable contains a command to end-to-end test a BitTorrent tracker.
|
The tracker executable contains a command to end-to-end test a BitTorrent tracker.
|
||||||
See
|
See
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
chihaya --help
|
tracker --help
|
||||||
```
|
```
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
Configuration of Chihaya is done via one YAML configuration file.
|
Configuration of the tracker is done via one YAML configuration file.
|
||||||
The `dist/` directory contains an example configuration file.
|
The `dist/` directory contains an example configuration file.
|
||||||
Files and directories under `docs/` contain detailed information about configuring middleware, storage implementations, architecture etc.
|
Files and directories under `docs/` contain detailed information about configuring middleware, storage implementations, architecture etc.
|
||||||
|
|
||||||
## Related projects
|
This is an example for an UDP server running on 9252 with metrics enabled. Remember to **change the private key** to some random string.
|
||||||
|
|
||||||
- [BitTorrent.org](https://github.com/bittorrent/bittorrent.org): a static website containing the BitTorrent spec and all BEPs
|
```
|
||||||
- [OpenTracker](http://erdgeist.org/arts/software/opentracker): a popular BitTorrent tracker written in C
|
---
|
||||||
- [Ocelot](https://github.com/WhatCD/Ocelot): a private BitTorrent tracker written in C++
|
chihaya:
|
||||||
|
announce_interval: "30m"
|
||||||
|
min_announce_interval: "15m"
|
||||||
|
metrics_addr: "0.0.0.0:6880"
|
||||||
|
udp:
|
||||||
|
addr: "0.0.0.0:9252"
|
||||||
|
max_clock_skew: "10s"
|
||||||
|
private_key: ">>>>CHANGE THIS TO SOME RANDOM THING<<<<"
|
||||||
|
enable_request_timing: false
|
||||||
|
allow_ip_spoofing: false
|
||||||
|
max_numwant: 100
|
||||||
|
default_numwant: 50
|
||||||
|
max_scrape_infohashes: 50
|
||||||
|
storage:
|
||||||
|
name: "memory"
|
||||||
|
config:
|
||||||
|
gc_interval: "3m"
|
||||||
|
peer_lifetime: "31m"
|
||||||
|
shard_count: 1024
|
||||||
|
prometheus_reporting_interval: "1s"
|
||||||
|
```
|
||||||
|
|
||||||
|
# Running from Docker
|
||||||
|
|
||||||
|
This section assumes `docker` and `docker-compose` to be installed on a Linux distro. Please check official docs on how to install [Docker Engine](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/).
|
||||||
|
|
||||||
|
## Docker Compose from lbry/tracker
|
||||||
|
In order to define a tracker service and let Docker Compose manage it, create a file named `docker-compose.yml` with:
|
||||||
|
```
|
||||||
|
version: "3"
|
||||||
|
services:
|
||||||
|
tracker:
|
||||||
|
image: lbry/tracker
|
||||||
|
command: --config /config/conf.yml
|
||||||
|
volumes:
|
||||||
|
- .:/config
|
||||||
|
network_mode: host
|
||||||
|
restart: always
|
||||||
|
```
|
||||||
|
Unfortunately the tracker does not work without `network_mode: host` due some bug with UDP on Docker. In this mode, firewall configuration needs to be done manually. If using `ufw`, try `ufw allow 9252`.
|
||||||
|
|
||||||
|
Now, move the configuration to the same directory as `docker-compose.yml`, naming it `conf.yml`. If it is not ready, check the configuration section above.
|
||||||
|
|
||||||
|
Start the tracker by running the following in the same directory as the compose file:
|
||||||
|
`docker-compose up -d`
|
||||||
|
Logs can be read with:
|
||||||
|
`docker-compose logs`
|
||||||
|
To stop:
|
||||||
|
`docker-compose down`
|
||||||
|
|
||||||
|
## Building the containter
|
||||||
|
A Dockerfile is provided within the repo. To build the container locally, run this command on the same directory the repo was cloned:
|
||||||
|
`sudo docker build -f Dockerfile . -t some_name/tracker:latest`
|
||||||
|
It will produce an image called `some_name/tracker`, which can be used in the Docker Compose section.
|
||||||
|
|
||||||
|
# Running from source as a service
|
||||||
|
|
||||||
|
For ease of maintenance, it is recommended to run the tracker as a service.
|
||||||
|
|
||||||
|
This is an example for running it under as the current user using `systemd`:
|
||||||
|
```
|
||||||
|
[Unit]
|
||||||
|
Description=Chihaya BT tracker
|
||||||
|
After=network.target
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
#User=chihaya
|
||||||
|
#Group=chihaya
|
||||||
|
WorkingDirectory=/home/user/github/tracker
|
||||||
|
ExecStart=/home/user/github/tracker/chihaya --config dist/example_config.yaml
|
||||||
|
Restart=on-failure
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
```
|
||||||
|
|
||||||
|
To try it, change `/home/user/github/tracker` to where the code was cloned and run:
|
||||||
|
```bash=
|
||||||
|
mkdir -p ~/.config/systemd/user
|
||||||
|
# PASTE FILE IN ~/.config/systemd/user/tracker.service
|
||||||
|
systemctl --user enable tracker
|
||||||
|
systemctl --user start tracker
|
||||||
|
systemctl --user status tracker
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributions to this project are welcome, encouraged, and compensated. For more details, please check [this](https://lbry.tech/contribute) link.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
LBRY's code changes are MIT licensed, and the upstream Chihaya code is licensed under a BSD 2-Clause license. For the full license, see [LICENSE](LICENSE).
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
We take security seriously. Please contact security@lbry.com regarding any security issues. [Our PGP key is here](https://lbry.com/faq/pgp-key) if you need it.
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
The primary contact for this project is [@shyba](mailto:vshyba@lbry.com).
|
||||||
|
|
|
@ -9,15 +9,9 @@ import (
|
||||||
// ErrInvalidIP indicates an invalid IP for an Announce.
|
// ErrInvalidIP indicates an invalid IP for an Announce.
|
||||||
var ErrInvalidIP = ClientError("invalid IP")
|
var ErrInvalidIP = ClientError("invalid IP")
|
||||||
|
|
||||||
// ErrInvalidPort indicates an invalid Port for an Announce.
|
|
||||||
var ErrInvalidPort = ClientError("invalid port")
|
|
||||||
|
|
||||||
// SanitizeAnnounce enforces a max and default NumWant and coerces the peer's
|
// SanitizeAnnounce enforces a max and default NumWant and coerces the peer's
|
||||||
// IP address into the proper format.
|
// IP address into the proper format.
|
||||||
func SanitizeAnnounce(r *AnnounceRequest, maxNumWant, defaultNumWant uint32) error {
|
func SanitizeAnnounce(r *AnnounceRequest, maxNumWant, defaultNumWant uint32) error {
|
||||||
if r.Port == 0 {
|
|
||||||
return ErrInvalidPort
|
|
||||||
}
|
|
||||||
|
|
||||||
if !r.NumWantProvided {
|
if !r.NumWantProvided {
|
||||||
r.NumWant = defaultNumWant
|
r.NumWant = defaultNumWant
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
|
|
||||||
// Imports to register middleware drivers.
|
// Imports to register middleware drivers.
|
||||||
_ "github.com/chihaya/chihaya/middleware/clientapproval"
|
_ "github.com/chihaya/chihaya/middleware/clientapproval"
|
||||||
|
_ "github.com/chihaya/chihaya/middleware/fixedpeer"
|
||||||
_ "github.com/chihaya/chihaya/middleware/jwt"
|
_ "github.com/chihaya/chihaya/middleware/jwt"
|
||||||
_ "github.com/chihaya/chihaya/middleware/torrentapproval"
|
_ "github.com/chihaya/chihaya/middleware/torrentapproval"
|
||||||
_ "github.com/chihaya/chihaya/middleware/varinterval"
|
_ "github.com/chihaya/chihaya/middleware/varinterval"
|
||||||
|
|
84
middleware/fixedpeer/fixedpeer.go
Normal file
84
middleware/fixedpeer/fixedpeer.go
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
// Package fixedpeers implements a Hook that
|
||||||
|
//appends a fixed peer to every Announce request
|
||||||
|
package fixedpeers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya/bittorrent"
|
||||||
|
"github.com/chihaya/chihaya/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Name is the name by which this middleware is registered with Chihaya.
|
||||||
|
const Name = "fixed peers"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
middleware.RegisterDriver(Name, driver{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ middleware.Driver = driver{}
|
||||||
|
|
||||||
|
type driver struct{}
|
||||||
|
|
||||||
|
func (d driver) NewHook(optionBytes []byte) (middleware.Hook, error) {
|
||||||
|
var cfg Config
|
||||||
|
err := yaml.Unmarshal(optionBytes, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid options for middleware %s: %w", Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewHook(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
FixedPeers []string `yaml:"fixed_peers"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type hook struct {
|
||||||
|
peers []bittorrent.Peer
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHook returns an instance of the torrent approval middleware.
|
||||||
|
func NewHook(cfg Config) (middleware.Hook, error) {
|
||||||
|
var peers []bittorrent.Peer
|
||||||
|
for _, peerString := range cfg.FixedPeers {
|
||||||
|
parts := strings.Split(peerString, ":")
|
||||||
|
port, err := strconv.Atoi(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ip := net.ParseIP(parts[0]).To4()
|
||||||
|
if ip == nil {
|
||||||
|
panic("Invalid ip4 on fixed_peers")
|
||||||
|
}
|
||||||
|
peers = append(peers,
|
||||||
|
bittorrent.Peer{
|
||||||
|
ID: bittorrent.PeerID{0},
|
||||||
|
Port: uint16(port),
|
||||||
|
IP: bittorrent.IP{IP: ip},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
h := &hook{
|
||||||
|
peers: peers,
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
|
||||||
|
for _, peer := range h.peers {
|
||||||
|
resp.IPv4Peers = append(resp.IPv4Peers, peer)
|
||||||
|
resp.Complete += 1
|
||||||
|
}
|
||||||
|
return ctx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
|
||||||
|
// Scrapes don't require any protection.
|
||||||
|
return ctx, nil
|
||||||
|
}
|
47
middleware/fixedpeer/fixedpeer_test.go
Normal file
47
middleware/fixedpeer/fixedpeer_test.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package fixedpeers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya/bittorrent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAppendFixedPeer(t *testing.T) {
|
||||||
|
conf := Config{
|
||||||
|
FixedPeers: []string{"8.8.8.8:4040", "1.1.1.1:111"},
|
||||||
|
}
|
||||||
|
h, err := NewHook(conf)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
req := &bittorrent.AnnounceRequest{}
|
||||||
|
resp := &bittorrent.AnnounceResponse{}
|
||||||
|
|
||||||
|
hashbytes, err := hex.DecodeString("3000000000000000000000000000000000000000")
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
hashinfo := bittorrent.InfoHashFromBytes(hashbytes)
|
||||||
|
|
||||||
|
req.InfoHash = hashinfo
|
||||||
|
|
||||||
|
nctx, err := h.HandleAnnounce(ctx, req, resp)
|
||||||
|
require.Equal(t, ctx, nctx)
|
||||||
|
peers := []bittorrent.Peer{
|
||||||
|
bittorrent.Peer{
|
||||||
|
ID: bittorrent.PeerID{0},
|
||||||
|
Port: 4040,
|
||||||
|
IP: bittorrent.IP{net.ParseIP("8.8.8.8"), bittorrent.IPv4},
|
||||||
|
},
|
||||||
|
bittorrent.Peer{
|
||||||
|
ID: bittorrent.PeerID{0},
|
||||||
|
Port: 111,
|
||||||
|
IP: bittorrent.IP{net.ParseIP("1.1.1.1"), bittorrent.IPv4},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
require.Equal(t, peers, resp.IPv4Peers)
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/bittorrent"
|
"github.com/chihaya/chihaya/bittorrent"
|
||||||
"github.com/chihaya/chihaya/storage"
|
"github.com/chihaya/chihaya/storage"
|
||||||
)
|
)
|
||||||
|
@ -36,6 +35,8 @@ func (h *swarmInteractionHook) HandleAnnounce(ctx context.Context, req *bittorre
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
case req.Port < 100:
|
||||||
|
return ctx, nil
|
||||||
case req.Event == bittorrent.Stopped:
|
case req.Event == bittorrent.Stopped:
|
||||||
err = h.store.DeleteSeeder(req.InfoHash, req.Peer)
|
err = h.store.DeleteSeeder(req.InfoHash, req.Peer)
|
||||||
if err != nil && !errors.Is(err, storage.ErrResourceDoesNotExist) {
|
if err != nil && !errors.Is(err, storage.ErrResourceDoesNotExist) {
|
||||||
|
@ -97,8 +98,8 @@ func (h *responseHook) HandleAnnounce(ctx context.Context, req *bittorrent.Annou
|
||||||
|
|
||||||
// Add the Scrape data to the response.
|
// Add the Scrape data to the response.
|
||||||
s := h.store.ScrapeSwarm(req.InfoHash, req.IP.AddressFamily)
|
s := h.store.ScrapeSwarm(req.InfoHash, req.IP.AddressFamily)
|
||||||
resp.Incomplete = s.Incomplete
|
resp.Incomplete += s.Incomplete
|
||||||
resp.Complete = s.Complete
|
resp.Complete += s.Complete
|
||||||
|
|
||||||
err = h.appendPeers(req, resp)
|
err = h.appendPeers(req, resp)
|
||||||
return ctx, err
|
return ctx, err
|
||||||
|
@ -124,9 +125,9 @@ func (h *responseHook) appendPeers(req *bittorrent.AnnounceRequest, resp *bittor
|
||||||
|
|
||||||
switch req.IP.AddressFamily {
|
switch req.IP.AddressFamily {
|
||||||
case bittorrent.IPv4:
|
case bittorrent.IPv4:
|
||||||
resp.IPv4Peers = peers
|
resp.IPv4Peers = append(resp.IPv4Peers, peers...)
|
||||||
case bittorrent.IPv6:
|
case bittorrent.IPv6:
|
||||||
resp.IPv6Peers = peers
|
resp.IPv6Peers = append(resp.IPv6Peers, peers...)
|
||||||
default:
|
default:
|
||||||
panic("attempted to append peer that is neither IPv4 nor IPv6")
|
panic("attempted to append peer that is neither IPv4 nor IPv6")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue