Multiplayer torrent rocking on transmission

I want to talk about how to implement a torrent daemon with user separation. As a torrent client, transmission-daemon will be used.
A torrent daemon is a program (service) that acts as a torrent client (rocking chair) and is usually used on all sorts of HTPC, NAS, routers, etc. to download / distribute torrents not on the main PC / laptop, but on a device that is always on / online.

Introduction, for those who are not familiar with what transmission-daemon is


One of the common torrent daemons is transmission-daemon. For him, there are many GUI clients for different platforms that work via http and can connect to a rocking chair remotely, which is very convenient. Using such a client for the user will not differ from ordinary torrent clients, and if the client is closed, the download / distribution does not stop, but continues on the remote server machine. Also transmission-daemon has its own “standard” web-interface.

Problem:


And everything would be fine, but transmission-daemon, although it provides for authorization, is only for one user. Therefore, if you want someone other than you to use your torrent rocking chair, you will have to share one account - you can both view and manage each other's downloads, which is not very convenient.

Solution method


The most obvious simple and elegant, in my opinion, solution is to launch transnmission simultaneously from different users. In this case, Ubuntu Server 12.10 is used as the OS, so you can create a separate Upstart User Job for each user. For other distributions, you can simply create init.d scripts instead.

Decision


Prepare the system:

install transmission-daemon: Let's enable the
sudo apt-get install transmission-daemon
execution of user daemons or User Jobs, for this you need to replace the file "/etc/dbus-1/system.d/Upstart.conf" , after making a backup of the old one.
sudo mv /etc/dbus-1/system.d/Upstart.conf /etc/dbus-1/system.d/Upstart.conf.save
new file /etc/dbus-1/system.d/Upstart.conf :

Next, turn off the standard demon, which starts from the root at system startup:

sudo sh -c 'echo "manual" > /etc/init/transmission-daemon.override'
sudo service transmission-daemon stop


Configuring a specific user:

Create Upstart User Job

Log in under the next user who will use transmission-daemon and execute the script:
mkdir $HOME/.init
cat < $HOME/.init/transmissiond-$USER.conf
start on $USER-logged-in
stop on runlevel [!2345]
kill timeout 30
respawn
setuid $USER
env HOME=$HOME
pre-start script
  # stop job from continuing if no config file found for daemon
  [ ! -f \$HOME/.transmissiond/transmission-daemon ] && { stop; exit 0; }
  # source the config file
  . \$HOME/.transmissiond/transmission-daemon
  # stop job from continuing if admin has not enabled service in
  # config file.
  [ "\$ENABLE_DAEMON" != 0 ] || { stop; exit 0; }
end script
script
  . \$HOME/.transmissiond/transmission-daemon
  exec /usr/bin/transmission-daemon -f \$OPTIONS
end script
End-of-list

The script will create a file ~/.init/transmissiond-$USER.confwith the necessary settings for the user daemon.
The field that you should pay attention to is " start ". In this case, Job will start only if the user has logged into the system, which is not necessary at all. However, if encryption of the home directory is installed, it is necessary, otherwise there will be nowhere to save the downloaded. If the download directory is accessible even before the user login, then "start on $USER-logged-in"you can replace it with "start on runlevel [2345]". In this case, however, Job does not need to be made custom, but can be placed in / etc / init /

Configure env user-demona:
cat < $HOME/.transmissiond/transmission-daemon
ENABLE_DAEMON=1
CONFIG_DIR="$HOME/.transmissiond"
OPTIONS="--config-dir \$CONFIG_DIR"
End-of-text


Create a torrent client settings file

Do not forget to replace pass="*****"with your password. After the first start, transmission itself will hide the password with a hash - it does not store it in the clear.
uid=`id -u`
pass="*****" #здесь забиваем пароль пользователя
cat < $HOME/.transmissiond/settings.json
{
    "alt-speed-down": 100, 
    "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": 100, 
    "bind-address-ipv4": "0.0.0.0", 
    "bind-address-ipv6": "::", 
    "blocklist-enabled": false, 
    "blocklist-url": "http://www.example.com/blocklist", 
    "cache-size-mb": 32, 
    "dht-enabled": true, 
    "download-limit": 100, 
    "download-limit-enabled": 0, 
    "download-queue-enabled": true, 
    "download-queue-size": 5, 
    "encryption": 1, 
    "idle-seeding-limit": 30, 
    "idle-seeding-limit-enabled": false, 
    "incomplete-dir-enabled": true, 
    "lpd-enabled": false, 
    "max-peers-global": 200, 
    "message-level": 2, 
    "peer-congestion-algorithm": "", 
    "peer-limit-global": 240, 
    "peer-limit-per-torrent": 60, 
    "peer-port-random-high": 65535, 
    "peer-port-random-low": 49152, 
    "peer-port-random-on-start": false, 
    "peer-socket-tos": "default", 
    "pex-enabled": true, 
    "port-forwarding-enabled": true, 
    "preallocation": 1, 
    "prefetch-enabled": 1, 
    "queue-stalled-enabled": true, 
    "queue-stalled-minutes": 30, 
    "ratio-limit": 2, 
    "ratio-limit-enabled": false, 
    "rename-partial-files": true, 
    "rpc-authentication-required": true, 
    "rpc-bind-address": "0.0.0.0", 
    "rpc-enabled": true, 
    "rpc-url": "/transmission/", 
    "rpc-whitelist": "127.0.0.1", 
    "rpc-whitelist-enabled": false, 
    "scrape-paused-torrents-enabled": true, 
    "script-torrent-done-enabled": false, 
    "script-torrent-done-filename": "", 
    "seed-queue-enabled": false, 
    "seed-queue-size": 10, 
    "speed-limit-down": 256, 
    "speed-limit-down-enabled": false, 
    "speed-limit-up": 256, 
    "speed-limit-up-enabled": false, 
    "start-added-torrents": true, 
    "trash-original-torrent-files": false, 
    "umask": 18, 
    "upload-limit": 100, 
    "upload-limit-enabled": 0, 
    "upload-slots-per-torrent": 14, 
    "utp-enabled": true,
    "download-dir": "$HOME/downloads", 
    "incomplete-dir": "$HOME/downloads/incomplete", 
    "peer-port": $((51413 + $uid - 1000)), 
    "rpc-username": "$USER", 
    "rpc-password": "$pass", 
    "rpc-port": $((9091 + $uid - 1000)) 
}
End-of-list

Each torrent daemon will hang on its own separate port, both for incoming torrent connections and for the control interface. The port number (web interface) of a particular user is calculated using the formula. rpc-port = 9091 + $uid - 1000Thus, a user with uid 1000 will be able to access the web interface using server:9091, and a user with uid 1010 will be able to access server:9101. peer-port is calculated in the same way.
All incomplete torrents will be in ~ / downloads / incomplete, and finished torrents will be in ~ / downloads.

In case encryption of the user's home directory is used, it is necessary that the daemon starts only when the user is logged in, so we add the corresponding event at the end of .bashrc so that Upstart knows that it is time to start transmission:
    echo "# start user specific daemons" >> "$HOME/.bashrc"
    echo "initctl emit $USER-logged-in" >> "$HOME/.bashrc"

and also, probably, it would be desirable if the user logged in - the daemon continued to spin:
mv "$HOME/.ecryptfs/auto-umount" "$HOME/.ecryptfs/_auto-umount"

Optionally, you can also proxy via nginx / apache so that different users can access the interface through different domains, for example.

Conclusion


Each user who has been manipulated from the “Configuring a specific user” can use his own transmission-daemon without disturbing others and without mixing the downloaded. If you combine this with ftp / samba / ..., then users can pick up the downloaded files.
Manipulations can be thrust into the script and then it will be enough for each new user to execute one command and everything can be used.

Also popular now: