Stay Hungry. Stay Foolish.

前言

前些日子,兽兽大佬发了一篇文章介绍了 Debian 9 安装 rTorrent + ruTorrent + Nginx 搭建自己的 PT / BT 盒子,作为国服大刷子(上传量很大很大的人),他的文章是非常有学习价值的了,我正好也想玩玩 PT,正好某个 PT 站公开发邀请,买了几台独服开始玩玩,作为纯新手,记录一下自己学习的过程吧。本文很多都抄自兽兽大佬的文章

Private Tracker,简称 PT,即私用种子服务器。Private Tracker 可以统计用户的上传和下载量,计算用户的分享率。可以简单将它理解为 BT 的升级版,在拥有 BT 所有功能的同时,需要达到一定的分享率才能不被删除帐号。PT 下载其实也是 BT 下载的一种,但有两个明显的改进:一是私密的小范围下载,二是进行流量统计,根据上载量决定你的权限

安装 rTorrent

业内用来刷 PT/BT 的有很多种软件,比如 TransmissionDelugeuTorrent等等,这边选择了 rTorrent,至于原因,据兽兽大佬所说,rTorrent 上传量最高,适合大刷子使用,其他的特点还有占用内存小、频宽利用率高等等。

本次安装使用 Debian 10
Debian 10、Ubuntu 18.10 之前自带的 rTorrent 版本为 0.9.6,会导致误报上传,故使用最新的系统源安装 0.9.7 最新版

安装必备软件

这边的介绍非常简单,详细的设置可以参考 LEMP 安装教程

apt update && apt dist-upgrade -y && apt install sudo nano curl vim wget rtorrent apt-transport-https lsb-release ca-certificates git subversion python-pip python-dev build-essential unrar-free unzip apache2-utils mediainfo man-db nginx-extras -y

curl -sL https://deb.nodesource.com/setup_10.x | bash -
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt update && apt install nodejs yarn -y && yarn global add pm2 node-gyp

后台运行 rTorrent

为了简化步骤,我们假设在 root 用户下运行 rTorrent,并且设置下载目录在 /var/www/rtorrent/,虽然这样并不是特别的安全。

mkdir -p /var/www/rtorrent && mkdir -p /etc/rtorrent

创建配置文件:

nano /etc/rtorrent/rtorrent.rc

参考配置内容如下:

## Instance layout (base paths)
method.insert = cfg.basedir,  private|const|string, (cat,"/var/www/rtorrent/")
method.insert = cfg.download, private|const|string, (cat,(cfg.basedir),"download/")
method.insert = cfg.logs,     private|const|string, (cat,(cfg.basedir),"log/")
method.insert = cfg.logfile,  private|const|string, (cat,(cfg.logs),"rtorrent-",(system.time),".log")
method.insert = cfg.session,  private|const|string, (cat,(cfg.basedir),".session/")
method.insert = cfg.watch,    private|const|string, (cat,(cfg.basedir),"watch/")

## Create instance directories
execute.throw = sh, -c, (cat,\
    "mkdir -p \"",(cfg.download),"\" ",\
    "\"",(cfg.logs),"\" ",\
    "\"",(cfg.session),"\" ",\
    "\"",(cfg.watch),"/load\" ",\
    "\"",(cfg.watch),"/start\" ")

## Listening port for incoming peer traffic (fixed; you can also randomize it)
network.port_range.set = 55950-56000
network.port_random.set = yes

## Tracker-less torrent and UDP tracker support
## (conservative settings for 'private' trackers, change for 'public')
dht.mode.set = disable
protocol.pex.set = no
trackers.use_udp.set = yes

## Peer settings
throttle.max_downloads.global.set = 300
throttle.max_uploads.global.set = 300
throttle.max_downloads.set = 50
throttle.max_uploads.set = 50
throttle.global_down.max_rate.set_kb = 0
throttle.global_up.max_rate.set_kb = 0

throttle.min_peers.normal.set = 90
throttle.max_peers.normal.set = 100
throttle.min_peers.seed.set = -1
throttle.max_peers.seed.set = -1
trackers.numwant.set = 100

protocol.encryption.set = allow_incoming,try_outgoing,enable_retry

## Limits for file handle resources, this is optimized for
## an `ulimit` of 1024 (a common default). You MUST leave
## a ceiling of handles reserved for rTorrent's internal needs!
network.http.max_open.set = 99
network.max_open_files.set = 600
network.max_open_sockets.set = 900
network.receive_buffer.size.set = 4M
network.send_buffer.size.set = 12M

## Memory resource usage (increase if you have a large number of items loaded,
## and/or the available resources to spend)
pieces.memory.max.set = 1024M
network.xmlrpc.size_limit.set = 4M

## Basic operational settings (no need to change these)
session.path.set = (cat, (cfg.session))
directory.default.set = (cat, (cfg.download))
log.execute = (cat, (cfg.logs), "execute.log")
log.xmlrpc = (cat, (cfg.logs), "xmlrpc.log")
execute.nothrow = sh, -c, (cat, "echo >",\
    (session.path), "rtorrent.pid", " ",(system.pid))

## Other operational settings (check & adapt)
encoding.add = utf8
system.umask.set = 0027
system.cwd.set = (directory.default)
network.http.dns_cache_timeout.set = 25
schedule2 = monitor_diskspace, 15, 60, ((close_low_diskspace, 1000M))
pieces.preload.type.set = 2
pieces.hash.on_completion.set = no
view.sort_current = seeding, greater=d.ratio=
keys.layout.set = qwerty
#network.http.capath.set = "/etc/ssl/certs"
network.http.ssl_verify_peer.set = 0
network.http.ssl_verify_host.set = 0

## Some additional values and commands
method.insert = system.startup_time, value|const, (system.time)
method.insert = d.data_path, simple,\
    "if=(d.is_multi_file),\
        (cat, (d.directory), /),\
        (cat, (d.directory), /, (d.name))"
method.insert = d.session_file, simple, "cat=(session.path), (d.hash), .torrent"

## Watch directories (add more as you like, but use unique schedule names)
## Add torrent
schedule2 = watch_load, 11, 10, ((load.verbose, (cat, (cfg.watch), "load/*.torrent")))
## Add & download straight away
schedule2 = watch_start, 10, 10, ((load.start_verbose, (cat, (cfg.watch), "start/*.torrent")))

## Run the rTorrent process as a daemon in the background
## (and control via XMLRPC sockets)
system.daemon.set = true
network.scgi.open_local = (cat,(session.path),rpc.socket)
execute.nothrow = chmod,770,(cat,(session.path),rpc.socket)

## Logging:
##   Levels = critical error warn notice info debug
##   Groups = connection_* dht_* peer_* rpc_* storage_* thread_* tracker_* torrent_*
print = (cat, "Logging to ", (cfg.logfile))
log.open_file = "log", (cfg.logfile)
log.add_output = "info", "log"
#log.add_output = "tracker_debug", "log"

编辑完成后按 ctrl + x 保存退出,再按 y 确认即可:

使用 Systemd 后台运行 rTorrent

nano /etc/systemd/system/rtorrent.service

文件内容为:

[Unit]
Description=systemd integration for rtorrent - starts/stops rtorrent instances on startup/shutdown
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
KillMode=process
ExecStart=/usr/bin/rtorrent -n -o import=/etc/rtorrent/rtorrent.rc
WorkingDirectory=/var/www/rtorrent

[Install]
WantedBy=multi-user.target

管理方式为:

systemctl start rtorrent
systemctl [restart|stop|enable|status] rtorrent

使用 Flood 管理 PT 下载

Flood 是一个基于 nodejs 的现代化管理 rtorrent 的 GUI 界面,整体设计较为美观,使用也比较方便。

安装配置 Flood

cd /var/www && git clone https://github.com/jfurrow/flood.git && cd flood && cp config.template.js config.js
yarn && yarn build && NODE_ENV=production pm2 start server/bin/start.js --name flood

然后 Flood 就会运行在 127.0.0.1:3000 端口,这个端口可以在 config.js 中修改。

Nginx 整合

如果修改 config.js 中端口监听 0.0.0.0 那么就直接可以公网访问,当然更好的办法是使用 Nginx 转发一下,参考配置如下:

location / {
	proxy_connect_timeout 10;
	proxy_send_timeout 60;
	proxy_read_timeout 60;
	proxy_buffer_size 64k;
	proxy_buffers 16 32k;
	proxy_busy_buffers_size 128k;
	proxy_temp_file_write_size 64k;
	proxy_pass_header Set-Cookie;
	proxy_redirect off;
	proxy_set_header Referer $http_referer;
	proxy_set_header Host $host;
	proxy_http_version 1.1;
	proxy_hide_header X-Powered-By;
	proxy_set_header Cookie $http_cookie;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-Host $host;
	proxy_set_header X-Forwarded-Server $host;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_no_cache $http_pragma $http_authorization;
	proxy_cache_bypass $http_pragma $http_authorization;
	proxy_pass http://127.0.0.1:3000;
	client_max_body_size 10m;
	client_body_buffer_size 1m;
}

目前 flood 可能会出现上传种子出现失败,选择通过链接添加种子即可。