Giuseppe Parrello

 

How to install a BitTorrent server on a router


In this page I describe how to install a BitTorrent server on a router (Asus RT-AC56U) - we use some Entware packages, so please refer to this page on how to install Entware on a router.
We will use the "Transmission" server (daemon), one of the most used BitTorrent server in Linux environments. To install this server, we need to install several Entware's packages (transmission-web, transmission-daemon-openssl, transmission-remote-openssl) with the following instruction:

opkg install transmission-web transmission-daemon-openssl transmission-remote-openssl

Installing transmission-web (2.93-4) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/transmission-web_2.93-4_armv7-2.6.ipk
Installing transmission-daemon-openssl (2.93-4) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/transmission-daemon-openssl_2.93-4_armv7-2.6.ipk
Installing libcurl (7.58.0-1) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/libcurl_7.58.0-1_armv7-2.6.ipk
Installing libevent2 (2.0.22-1) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/libevent2_2.0.22-1_armv7-2.6.ipk
Installing libminiupnpc (2.0.20170509-1) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/libminiupnpc_2.0.20170509-1_armv7-2.6.ipk
Installing transmission-remote-openssl (2.93-4) to root...
Downloading http://bin.entware.net/armv7sf-k2.6/transmission-remote-openssl_2.93-4_armv7-2.6.ipk
Configuring libcurl.
Configuring libevent2.
Configuring libminiupnpc.
Configuring transmission-remote-openssl.
Configuring transmission-daemon-openssl.
Configuring transmission-web.

Package "transmission-web" contains the web interface resources for Transmission, the package "transmission-daemon-openssl" contains the daemon itself, while the package "transmission-remote-openssl" is the CLI (Command Line Interface) remote interface for Transmission. We give a look at the package "transmission-daemon-openssl", it contains the following files:

opkg files transmission-daemon-openssl | sort

/opt/bin/transmission-daemon
/opt/etc/init.d/S88transmission
/opt/etc/sysctl.d/20-transmission.conf
/opt/etc/transmission/settings.json

File "/opt/bin/transmission-daemon" is the daemon itself, file "/opt/etc/init.d/S88transmission" contains the script to start and stop the Transmission daemon, file "/opt/etc/sysctl.d/20-transmission.conf" is reserved for Transmission, while the file "/opt/etc/transmission/settings.json" contains the Transmission's configuration file.
We need to setup the Transmission's configuration file (see next section on this page), then we can start the Transmission's daemon using the script file with parameter "start", this is a list of parameters we can pass to the Transmission's script:

/opt/etc/init.d/S88transmission start
Starting transmission-daemon... done.

/opt/etc/init.d/S88transmission check
Checking transmission-daemon... alive.

/opt/etc/init.d/S88transmission stop
Checking transmission-daemon... alive.
Shutting down transmission-daemon... done.

/opt/etc/init.d/S88transmission check
Checking transmission-daemon... dead.

The parameter "start" is used to run the daemon, parameter "check" is used to check if daemon is running or not, while parameter "stop" is used to stop the daemon.
After starting the Transmission daemon, you can access to the web user interface. For further details, please read the section at the end of this page.
Note : due to the fact that Transmission daemon needs a large amount of system memory, I suggest to enable a swap file on router, as described in web page "How to enable a swap file on a router".


How to setup the Transmission's configuration file

In this section, we briefly explain the file "/opt/etc/transmission/settings.json" containing the Transmission's configuration file. This file contains all the settings used by the Transmission daemon, so it is important to understand how to change it before using the Transmission daemon. Please take note that this configuration file cannot be changed when Transmission daemon is running, so Transmission daemon must be stopped before changing this configuration file.
Firstly I provide you with a sample default content of the configuration file:

{
    "alt-speed-down": 50,
    "alt-speed-enabled": false,
    "alt-speed-time-begin": 540,
    "alt-speed-time-day": 127,
    "alt-speed-time-enabled": false,
    "alt-speed-time-end": 1020,
    "alt-speed-up": 50,
    "bind-address-ipv4": "0.0.0.0",
    "bind-address-ipv6": "::",
    "blocklist-enabled": false,
    "blocklist-url": "http://list.iblocklist.com/?list=bt_level1",
    "cache-size-mb": 2,
    "dht-enabled": true,
    "download-dir": "/opt/downloads/torrent",
    "download-queue-enabled": true,
    "download-queue-size": 5,
    "encryption": 0,
    "idle-seeding-limit": 30,
    "idle-seeding-limit-enabled": false,
    "incomplete-dir": "/opt/downloads/torrent/incomplete",
    "incomplete-dir-enabled": false,
    "lpd-enabled": true,
    "message-level": 1,
    "peer-congestion-algorithm": "",
    "peer-id-ttl-hours": 6,
    "peer-limit-global": 40,
    "peer-limit-per-torrent": 8,
    "peer-port": 51413,
    "peer-port-random-high": 65535,
    "peer-port-random-low": 49152,
    "peer-port-random-on-start": false,
    "peer-socket-tos": "lowcost",
    "pex-enabled": true,
    "port-forwarding-enabled": false,
    "preallocation": 1,
    "prefetch-enabled": false,
    "queue-stalled-enabled": true,
    "queue-stalled-minutes": 30,
    "ratio-limit": 2,
    "ratio-limit-enabled": false,
    "rename-partial-files": true,
    "rpc-authentication-required": false,
    "rpc-bind-address": "0.0.0.0",
    "rpc-enabled": true,
    "rpc-host-whitelist": "",
    "rpc-host-whitelist-enabled": true,
    "rpc-password": "{b0b6e62c1e81950c884b369a771d4977ceb191c5dEkYqx3E",
    "rpc-port": 9091,
    "rpc-url": "/transmission/",
    "rpc-username": "root",
    "rpc-whitelist": "127.0.0.1",
    "rpc-whitelist-enabled": false,
    "scrape-paused-torrents-enabled": true,
    "script-torrent-added-enabled": false,
    "script-torrent-added-filename": "",
    "script-torrent-done-enabled": false,
    "script-torrent-done-filename": "",
    "seed-queue-enabled": false,
    "seed-queue-size": 10,
    "speed-limit-down": 100,
    "speed-limit-down-enabled": false,
    "speed-limit-up": 100,
    "speed-limit-up-enabled": false,
    "start-added-torrents": true,
    "trash-original-torrent-files": true,
    "umask": 18,
    "upload-slots-per-torrent": 14,
    "utp-enabled": true,
    "watch-dir": "/opt/etc/transmission/watchdir",
    "watch-dir-enabled": true
}

The following table briefly explains the value of each parameter inside configuration file - more details may be found at this web address :

String Value type Description
alt-speed-down Number Max global download speed (KBps)
alt-speed-enabled Boolean True means use the alt speeds
alt-speed-time-begin Number When to turn on alt speeds (units: minutes after midnight)
alt-speed-time-day Number What day(s) to turn on alt speeds
alt-speed-time-enabled Boolean True means the scheduled on/off times are used
alt-speed-time-end Number When to turn off alt speeds (units: same)
alt-speed-up Number Max global upload speed (KBps)
bind-address-ipv4 String Where to listen for peer connections
bind-address-ipv6 String Where to listen for peer connections
blocklist-enabled Boolean True means enabled
blocklist-url String Location of the blocklist to use for "blocklist-update"
blocklist-size Number Number of rules in the blocklist
cache-size-mb Number Maximum size of the disk cache (MB)
dht-enabled Boolean True means allow DHT in public torrents
download-dir String Default path to download torrents
download-queue-enabled Boolean If true, limit how many torrents can be downloaded at once
download-queue-size Number Max number of torrents to download at once (see download-queue-enabled)
encryption String 0 = Prefer unencrypted connections, 1 = Prefer encrypted connections, 2 = Require encrypted connections
idle-seeding-limit Number Torrents we're seeding will be stopped if they're idle for this long
idle-seeding-limit-enabled Boolean True if the seeding inactivity limit is honored by default
incomplete-dir String Path for incomplete torrents, when enabled
incomplete-dir-enabled Boolean True means keep torrents in incomplete-dir until done
lazy-bitfield-enabled Boolean May help get around some ISP filtering
lpd-enabled Boolean True means allow Local Peer Discovery in public torrents
message-level Number Set verbosity of transmission messages (0 = None, 1 = Error, 2 = Info, 3 = Debug)
peer-congestion-algorithm String Normally is blank (use "lp" to use TCP-LP)
peer-id-ttl-hours Number Recycle the peer id used for public torrents after N hours of use
peer-limit-global Number Maximum global number of peers
peer-limit-per-torrent Number Maximum global number of peers
peer-port Number Peer port number
peer-port-random-high Number Max peer port number
peer-port-random-low Number Min peer port number
peer-port-random-on-start Boolean True means pick a random peer port on launch
peer-socket-tos String Set the Type-Of-Service (TOS) parameter for outgoing TCP packets. Possible values are "default", "lowcost", "throughput", "lowdelay" and "reliability". The value "lowcost" is recommended if you're using a smart router
pex-enabled Boolean True means allow PEX (Peer exchange Peer Exchange) in public torrents
port-forwarding-enabled Boolean If true, it enables UPnP or NAT-PMP
preallocation Number 0 = Off, 1 = Fast, 2 = Full (slower but reduces disk fragmentation) - default is 1
prefetch-enabled Boolean When enabled, Transmission will hint to the OS which piece data it's about to read from disk in order to satisfy requests from peers
queue-stalled-enabled Boolean Whether or not to consider idle torrents as stalled
queue-stalled-minutes Number Torrents that are idle for N minuets aren't counted toward seed-queue-size or download-queue-size
ratio-limit Number Torrent ratio limit
ratio-limit-enabled Boolean True means set a ratio limit
rename-partial-files Boolean True means append ".part" to incomplete files
rpc-authentication-required Boolean If true, userid/password are required to access to web gui
rpc-bind-address String Where to listen for RPC connections
rpc-enabled Boolean If true, enables the web gui
rpc-host-whitelist String Comma-delimited list of domain names. Wildcards allowed using '*'. Example: "*.foo.org,example.com"
rpc-host-whitelist-enabled Boolean If true, enable option "rpc-host-whitelist"
rpc-password String Password required to access to web gui  (if "rpc-authentication-required" is true). Clear text will be automatically encrypted by the daemon
rpc-port Number Port used to access to web gui
rpc-url String Additional path of web gui address
rpc-username String User name required to access to web gui (if "rpc-authentication-required" is true)
rpc-whitelist String Comma-delimited list of IP addresses. Wildcards allowed using '*'. Example: "127.0.0.*,192.168.*.*"
rpc-whitelist-enabled Boolean If true, it enables the "rpc-whitelist" option
scrape-paused-torrents-enabled Boolean If true, it enables the scraping of paused torrents
script-torrent-added-enabled Boolean If true, it runs a script at torrent adding
script-torrent-added-filename String Filename of the script to run whenever a torrent is added
script-torrent-done-enabled Boolean Whether or not to call the "done" script
script-torrent-done-filename String Filename of the script to run whenever a torrent is done
seed-queue-enabled Boolean If true, limit how many torrents can be uploaded at once
seed-queue-size Number Max number of torrents to uploaded at once (see seed-queue-enabled)
seedRatioLimit Double The default seed ratio for torrents to use
seedRatioLimited Boolean True if seedRatioLimit is honored by default
speed-limit-down Number Max global download speed (KBps)
speed-limit-down-enabled Boolean True means enabled
speed-limit-up Number Max global upload speed (KBps)
speed-limit-up-enabled Boolean True means enabled
start-added-torrents Boolean True means added torrents will be started right away
trash-original-torrent-files Boolean True means the .torrent file of added torrents will be deleted
umask Number Sets transmission's file mode creation mask (base 10). Users who want their saved torrents to be world-writable may want to set this value to 0
upload-slots-per-torrent Number Max upload slots per torrent
utp-enabled Boolean True means allow UTP
watch-dir String Watch directory
watch-dir-enabled Boolean If true, watches a directory for torrent files (.torrent files) and add them to transmission

 

How to use the Transmission's Web User Interface

Transmission daemon can be accessed via the integrated Web User Interface. The web address is "http://[router address]:[rpc port]", where [router address] is the web address of the router, while [rpc port] is the port specified in configuration file under parameter "rpc-port".
Web Interface may also change some Transmission's settings in real-time without stopping and restarting the Transmission daemon - take note that only few settings may be changed, other settings must be changed via configuration file.


How to remotely access the Transmission's daemon

Transmission daemon can be accessed also via a command line utility called "transmission-remote" included in one of the package used to install Transmission. I kindly discourage to use this command line, there are so many programs compatible with Transmission for all available platforms, suitable to access Transmission remotely and that use a user-friendly graphical interface to do this job. These programs also allow to change some Transmission's settings in real-time without stopping and then restarting the Transmission's daemon. Take note of the "rpc address" and "rpc port" included in Transmission's settings, as all these programs require these two values to remotely connect to Transmission daemon.
Anyway I provide the output of command line utility "transmission-remote" to have an overview of its parameters and options:

transmission-remote --help

transmission-remote 2.93 (3c5870d4f5)
A fast and easy BitTorrent client
https://transmissionbt.com/

Usage: transmission-remote [host] [options]
       transmission-remote [port] [options]
       transmission-remote [host:port] [options]
       transmission-remote [http(s?)://host:port/transmission/] [options]

See the man page for detailed explanations and many examples.

Options:
-h    --help                         Display this help page and exit
-a    --add                          Add torrent files by filename or URL
-as   --alt-speed                    Use the alternate Limits
-AS   --no-alt-speed                 Don't use the alternate Limits
-asd  --alt-speed-downlimit <speed>  max alternate download speed (in kB/s)
-asu  --alt-speed-uplimit   <speed>  max alternate upload speed (in kB/s)
-asc  --alt-speed-scheduler          Use the scheduled on/off times
-ASC  --no-alt-speed-scheduler       Don't use the scheduled on/off times
      --alt-speed-time-begin <time>  Time to start using the alt speed limits (in hhmm)
      --alt-speed-time-end   <time>  Time to stop using the alt speed limits (in hhmm)
      --alt-speed-days       <days>  Numbers for any/all days of the week - eg. "1-7"
      --blocklist-update             Blocklist update
-c    --incomplete-dir       <dir>   Where to store new torrents until they're complete
-C    --no-incomplete-dir            Don't store incomplete torrents in a 
                                     different location
-b    --debug                        Print debugging information
-d    --downlimit           <speed>  Set the max download speed in kB/s for the current 
                                     torrent(s) or globally
-D    --no-downlimit                 Disable max download speed for the current 
                                     torrent(s) or globally
-e    --cache               <size>   Set the maximum size of the session's memory 
                                     cache (in MiB)
-er   --encryption-required          Encrypt all peer connections
-ep   --encryption-preferred         Prefer encrypted peer connections
-et   --encryption-tolerated         Prefer unencrypted peer connections
      --exit                         Tell the transmission session to shut down
-f    --files                        List the current torrent(s)' files
-g    --get                 <files>  Mark files for download
-G    --no-get              <files>  Mark files for not downloading
-i    --info                         Show the current torrent(s)' details
-if   --info-files                   List the current torrent(s)' files
-ip   --info-peers                   List the current torrent(s)' peers
-ic   --info-pieces                  List the current torrent(s)' pieces
-it   --info-trackers                List the current torrent(s)'trackers
-si   --session-info                 Show the session's details
-st   --session-stats                Show the session's statistics
-l    --list                         List all torrents
      --move                <path>   Move current torrent's data to a new folder
      --find                <path>   Tell Transmission where to find a torrent's data
-m    --portmap                      Enable portmapping via NAT-PMP or UPnP
-M    --no-portmap                   Disable portmapping
-n    --auth              <user:pw>  Set username and password
-ne   --authenv                      Set authentication info from the TR_AUTH 
                                     environment variable (user:pw)
-N    --netrc               <file>   Set authentication info from a .netrc file
      --ssl                          Use SSL when talking to daemon
-o    --dht                          Enable distributed hash tables (DHT)
-O    --no-dht                       Disable distributed hash tables (DHT)
-p    --port                <port>   Port for incoming peers (Default: 51413)
-pt   --port-test                    Port testing
-P    --random-port                  Random port for incoming peers
-ph   --priority-high       <files>  Try to download these file(s) first
-pn   --priority-normal     <files>  Try to download these file(s) normally
-pl   --priority-low        <files>  Try to download these file(s) last
-Bh   --bandwidth-high               Give this torrent first chance at
                                     available bandwidth
-Bn   --bandwidth-normal             Give this torrent bandwidth left over
                                     by high priority torrents
-Bl   --bandwidth-low                Give this torrent bandwidth left over
                                     by high and normal priority torrents
      --reannounce                   Reannounce the current torrent(s)
-r    --remove                       Remove the current torrent(s)
-pr   --peers               <max>    Set the maximum number of peers for
                                     the current torrent(s) or globally
-rad  --remove-and-delete            Remove the current torrent(s) and
                                     delete local data
      --torrent-done-script <file>   Specify a script to run when a
                                     torrent finishes
      --no-torrent-done-script       Don't run a script when torrents finish
-sr   --seedratio ratio              Let the current torrent(s) seed until
                                     a specific ratio
-srd  --seedratio-default            Let the current torrent(s) use the
                                     global seedratio settings
-SR   --no-seedratio                 Let the current torrent(s) seed
                                     regardless of ratio
-gsr  --global-seedratio     ratio   All torrents, unless overridden by a
                                     per-torrent setting, should seed
                                     until a specific ratio
-GSR  --no-global-seedratio          All torrents, unless overridden by a
                                     per-torrent setting, should seed
                                     regardless of ratio
-td   --tracker-add     <tracker>    Add a tracker to a torrent
-tr   --tracker-remove  <trackerId>  Remove a tracker from a torrent
-s    --start                        Start the current torrent(s)
-S    --stop                         Stop the current torrent(s)
-t    --torrent         <torrent>    Set the current torrent(s)
      --start-paused                 Start added torrents paused
      --no-start-paused              Start added torrents unpaused
      --trash-torrent                Delete torrents after adding
      --no-trash-torrent             Do not delete torrents after adding
-hl   --honor-session                Make the current torrent(s) honor the
                                     session limits
-HL   --no-honor-session             Make the current torrent(s) not honor
                                     the session limits
-u    --uplimit          <speed>     Set the max upload speed in kB/s for
                                     the current torrent(s) or globally
-U    --no-uplimit                   Disable max upload speed for the
                                     current torrent(s) or globally
      --utp                          Enable uTP for peer connections
      --no-utp                       Disable uTP for peer connections
-v    --verify                       Verify the current torrent(s)
-V    --version                      Show version number and exit
-w    --download-dir     <path>      When used in conjunction with --add,
                                     set the new torrent's download folder.
                                     Otherwise, set the default download folder
-x    --pex                          Enable peer exchange (PEX)
-X    --no-pex                       Disable peer exchange (PEX)
-y    --lpd                          Enable local peer discovery (LPD)
-Y    --no-lpd                       Disable local peer discovery (LPD)
-pi   --peer-info                    List the current torrent(s)' peers

 

How to send an email on add/complete Torrent file

Transmission may send an email through a script whenever a Torrent file is added or completed using the parameters of the configuration file whose names start with "script-torrent" - remember to change the Transmission's settings in order to send an email using a script whenever a Torrent file is added and/or completed.
To send an email, we use the "msmtp" Entware's package, please refer to page "How to send an email via router" for more details about this package - we will use the same SMTP server settings to send an email with Transmission.
Inside the body of the email message, we can use the Transmission's internal variables.
You can find more details about Transmission scripts on this webpage.
This is a simple script that sends an email whenever Transmission finished to download a Torrent file:

#!/bin/sh

msmtp -v -t << EOF
From: [myemail]@hotmail.com
Subject: Router - torrent completed
To: to_address@domain.xxx

Transmission finished downloading :

Torrent Name : "$TR_TORRENT_NAME" .
Local Time : "$TR_TIME_LOCALTIME" .
Torrent Dir : "$TR_TORRENT_DIR" .
Torrent Hash : "$TR_TORRENT_HASH" .
Torrent ID : "$TR_TORRENT_ID" .
App Version : "$TR_APP_VERSION" .

EOF

exit 0