📜 ⬆️ ⬇️

Multi torrent rocking on the transmission

I want to talk about how to implement a torrent daemon divided by users. A transmission-daemon will be used as a torrent client.
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 a transmission-daemon is


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

Problem:


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

Solution


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 the transmission-daemon:
sudo apt-get install transmission-daemon
Let us execute user daemons or User Jobs, for this you need to replace the file "/etc/dbus-1/system.d/Upstart.conf" , after making backup of the old one.
sudo mv /etc/dbus-1/system.d/Upstart.conf /etc/dbus-1/system.d/Upstart.conf.save
new /etc/dbus-1/system.d/Upstart.conf file:
 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> <busconfig> <!-- Only the root user can own the Upstart name --> <policy user="root"> <allow own="com.ubuntu.Upstart" /> </policy> <!-- Allow any user to invoke all of the methods on Upstart, its jobs or their instances, and to get and set properties - since Upstart isolates commands by user. --> <policy context="default"> <allow send_destination="com.ubuntu.Upstart" send_interface="org.freedesktop.DBus.Introspectable" /> <allow send_destination="com.ubuntu.Upstart" send_interface="org.freedesktop.DBus.Properties" /> <allow send_destination="com.ubuntu.Upstart" send_interface="com.ubuntu.Upstart0_6" /> <allow send_destination="com.ubuntu.Upstart" send_interface="com.ubuntu.Upstart0_6.Job" /> <allow send_destination="com.ubuntu.Upstart" send_interface="com.ubuntu.Upstart0_6.Instance" /> </policy> </busconfig> 

Next is to turn off the standard demon, which starts from the root when the system starts:

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


Configuring a specific user:

create an upstart user job

Login as another user who will use the transmission-daemon and execute the script:
 mkdir $HOME/.init cat <<End-of-list > $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.conf with the necessary settings for the user daemon.
The field 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 at all necessary. However, if encryption of the home directory is set, it is necessary, otherwise there will be no place to save the downloaded one. If the download directory is available before the user login, then "start on $USER-logged-in" can be replaced by "start on runlevel [2345]" . In this case, however, Job does not need to be made custom, but can be placed in / etc / init /

Configure the env user-demona:
 cat <<End-of-text > $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 launch, the transmission itself will hide the password with a hash - it does not store it in the clear.
 uid=`id -u` pass="*****" #    cat <<End-of-list > $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 management interface. The port number (web interface) of a particular user is calculated using the formula rpc-port = 9091 + $uid - 1000 Thus, a user with uid 1000 can access the web interface via server:9091 server:9091 , and the user with uid 1010 - by server:9101 server:9101 . peer-port is calculated similarly.
All under-downloaded torrents will be in ~ / downloads / incomplete, and ready torrents will be in ~ / downloads.

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

and, probably, it would be desirable that if the user made a logout - the demon continued to spin:
mv "$HOME/.ecryptfs/auto-umount" "$HOME/.ecryptfs/_auto-umount"

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

Conclusion


Each user, with whom the manipulations from the “Setting up a specific user” have been done, can use his own transmission-daemon, without interfering with others and without mixing up the downloaded ones. If you combine this with ftp / samba / ..., then users will be able to 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 that’s all - you can use.

Source: https://habr.com/ru/post/168767/


All Articles