From Kromonos, 1 Year ago, written in Bash.
Embed
  1. #!/bin/bash
  2.  
  3. function write_systemD_init() {
  4.     cat > /etc/systemd/system/rtorrent.service << EOF
  5. [Unit]
  6. Description=rTorrent
  7. After=network.target
  8.  
  9. [Service]
  10. User=rtorrent
  11. Type=forking
  12. KillMode=none
  13. ExecStart=/usr/bin/screen -d -m -fa -S rtorrent /usr/local/bin/rtorrent
  14. ExecStop=/usr/bin/killall -w -s 2 /usr/local/bin/rtorrent
  15. WorkingDirectory=%h
  16.  
  17. [Install]
  18. WantedBy=default.target
  19. EOF
  20.  
  21.     cat > /etc/systemd/system/flood.service << EOF
  22. [Unit]
  23. Description=Flood rTorrent WebUI
  24. After=network.target
  25.  
  26. [Service]
  27. WorkingDirectory=/home/rtorrent/flood
  28. ExecStart=/usr/bin/npm start --production /home/rtorrent/flood
  29. User=flood
  30.  
  31. [Install]
  32. WantedBy=multi-user.target
  33. EOF
  34.  
  35.     systemctl daemon-reload
  36.     systemctl enable rtorrent.service
  37.     systemctl enable flood.service
  38.     systemctl start flood.service
  39.     systemctl start rtorrent.service    
  40. }
  41.  
  42. function write_systemV_init () {
  43.  
  44.     cat > /etc/init.d/rtorrent.sh << EOF
  45. #!/bin/bash
  46. ### BEGIN INIT INFO
  47. # Provides:          rtorrent
  48. # Required-Start:    $local_fs $remote_fs $network $syslog
  49. # Required-Stop:     $local_fs $remote_fs $network $syslog
  50. # Default-Start:     2 3 4 5
  51. # Default-Stop:      0 1 6
  52. # Short-Description: Start/stop rtorrent daemon
  53. ### END INIT INFO
  54.  
  55. # ------------------------------------------------------------------------------
  56. # /etc/init.d/rtorrent
  57. #
  58. # This script is an init script to run rtorrent in the background, using a
  59. # screen. The script was designed and tested for Debian systems, but may work on
  60. # other systems. On Debian, enable it by moving the script to
  61. # "/etc/init.d/rtorrent" and issuing the command
  62. # "update-rc.d rtorrent defaults 99"
  63. #    ____                _ _
  64. #   / ___|  ___  ___  __| | |__   _____  __
  65. #   \___ \ / _ \/ _ \/ _` | '_ \ / _ \ \/ /
  66. #    ___) |  __/  __/ (_| | |_) | (_) >  <
  67. #   |____/ \___|\___|\__,_|_.__/ \___/_/\_\
  68. #
  69. # @see http://methvin.net/scripts/rtorrent
  70. # @see http://tldp.org/LDP/abs/html/
  71. # ------------------------------------------------------------------------------
  72.  
  73. ## Username to run rtorrent under, make sure you have a .rtorrent.rc in the
  74. ## home directory of this user!
  75. USER="rtorrent"
  76.  
  77. ## Absolute path to the rtorrent binary.
  78. RTORRENT="/usr/bin/rtorrent"
  79.  
  80. ## Absolute path to the screen binary.
  81. SCREEN="/usr/bin/screen"
  82.  
  83. ## Name of the screen session, you can then "screen -r rtorrent" to get it back
  84. ## to the forground and work with it on your shell.
  85. SCREEN_NAME="rtorrent"
  86.  
  87. ## Absolute path to rtorrent's PID file.
  88. PIDFILE="/var/run/rtorrent.pid"
  89.  
  90. ## Absolute path to rtorrent's XMLRPC socket.
  91. SOCKET="/var/run/rtorrent/rpc.socket"
  92.  
  93. ## Check if the socket exists and if it exists delete it.
  94. delete_socket() {
  95.     if [[ -e $SOCKET ]]; then
  96.         rm -f $SOCKET
  97.     fi
  98. }
  99.  
  100. case "$1" in
  101.     ## Start rtorrent in the background.
  102.     start)
  103.         echo "Starting rtorrent."
  104.         delete_socket
  105.         start-stop-daemon --start --background --oknodo \
  106.             --pidfile "$PIDFILE" --make-pidfile \
  107.             --chuid $USER \
  108.             --exec $SCREEN -- -DmUS $SCREEN_NAME $RTORRENT
  109.         if [[ $? -ne 0 ]]; then
  110.             echo "Error: rtorrent failed to start."
  111.             exit 1
  112.         fi
  113.         echo "rtorrent started successfully."
  114.         ;;
  115.  
  116.     ## Stop rtorrent.
  117.     stop)
  118.         echo "Stopping rtorrent."
  119.         start-stop-daemon --stop --oknodo --pidfile "$PIDFILE"
  120.         if [[ $? -ne 0 ]]; then
  121.             echo "Error: failed to stop rtorrent process."
  122.             exit 1
  123.         fi
  124.         delete_socket
  125.         echo "rtorrent stopped successfully."
  126.         ;;
  127.  
  128.     ## Restart rtorrent.
  129.     restart)
  130.         "$0" stop
  131.         sleep 1
  132.         "$0" start || exit 1
  133.         ;;
  134.  
  135.     ## Print usage information if the user gives an invalid option.
  136.     *)
  137.         echo "Usage: $0 [start|stop|restart]"
  138.         exit 1
  139.         ;;
  140.  
  141. esac
  142. EOF
  143.    
  144. }
  145.  
  146. if [ "${1}" -eq "" ]; then
  147.     SERVERNAME="localhost"
  148. else
  149.     SERVERNAME="${1}"
  150. fi
  151.  
  152. apt install -y build-essential subversion autoconf g++ gcc curl comerr-dev pkg-config cfv libtool libssl-dev libncurses5-dev ncurses-term libsigc++-2.0-dev libcppunit-dev libcurl3 libcurl4-openssl-dev git ffmpeg mediainfo screen
  153.  
  154. cd /tmp
  155. svn checkout http://svn.code.sf.net/p/xmlrpc-c/code/stable xmlrpc-c
  156. cd xmlrpc-c/
  157. ./configure
  158. make -j $(nproc)
  159. make install
  160.  
  161. cd /tmp
  162. git clone https://github.com/rakshasa/libtorrent.git
  163. cd libtorrent
  164. git checkout `git tag | tail -1`
  165. ./autogen.sh
  166. ./configure
  167. make -j $(nproc)
  168. make install
  169.  
  170. cd /tmp
  171. git clone https://github.com/rakshasa/rtorrent.git
  172. cd rtorrent
  173. git checkout `git tag | tail -1`
  174. ./autogen.sh
  175. ./configure --with-xmlrpc-c
  176. make -j $(nproc)
  177. make install
  178.  
  179. ldconfig
  180.  
  181. #nginx mainline repository
  182. cat >> /etc/apt/sources.list << EOF
  183. deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
  184. deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx
  185. EOF
  186.  
  187. wget http://nginx.org/keys/nginx_signing.key
  188. apt-key add nginx_signing.key
  189. apt update
  190. apt install nginx -y
  191.  
  192. mkdir -p /etc/nginx/conf.d/
  193.  
  194. cat > /etc/nginx/conf.d/seedbox.conf << EOF
  195. server {
  196.     listen 80;
  197.     server_name seedbox.example.com;
  198.     return         301 https://$server_name$request_uri;
  199. }
  200.  
  201. server {
  202.     listen 443 ssl http2;
  203.     server_name $SERVERNAME
  204.     ssl             on;
  205.     ssl_certificate         /etc/letsencrypt/live/seedbox.example.com/fullchain.pem;
  206.     ssl_certificate_key     /etc/letsencrypt/live/seedbox.example.com/privkey.pem;
  207.     ssl_trusted_certificate     /etc/letsencrypt/live/seedbox.example.com/chain.pem;
  208.  
  209.     add_header Strict-Transport-Security "max-age=30";
  210.     ssl_protocols TLSv1.2;
  211.     ssl_ciphers EECDH+CHACHA20:EECDH+AESGCM:EECDH+AES;
  212.     ssl_ecdh_curve secp384r1;
  213.     ssl_session_cache shared:SSL:10m;
  214.     ssl_session_tickets off; # Requires nginx >= 1.5.9
  215.     ssl_stapling on; # Requires nginx >= 1.3.7
  216.     ssl_stapling_verify on; # Requires nginx => 1.3.7
  217.     ssl_prefer_server_ciphers on;
  218.     ssl_session_timeout  5m;
  219.     add_header X-Frame-Options DENY;
  220.     add_header X-Content-Type-Options nosniff;
  221.  
  222.  
  223.     location / {
  224.         client_max_body_size 1024m;
  225.         proxy_set_header X-Forwarded-Host $host;
  226.         proxy_set_header X-Forwarded-Server $host;
  227.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  228.         proxy_buffering off;
  229.         proxy_request_buffering off;
  230.         proxy_http_version 1.1;
  231.         proxy_pass http://localhost:3000;
  232.     }
  233. }
  234. EOF
  235.  
  236. adduser --disabled-password rtorrent
  237. mkdir -p /home/rtorrent/{downloads,.session}
  238. chmod 775 -R /home/rtorrent
  239. chown rtorrent:rtorrent -R /home/rtorrent
  240. cat > /home/rtorrent/.rtorrent.rc << EOF
  241. # Download location for rTorrent, for me it is /media/data/downloads for you it can be /home/rtorrent/downloads or torrents or whatever
  242. directory = /home/rtorrent/downloads
  243.  
  244. # Session location for rTorrent
  245. session = /home/rtorrent/.session
  246.  
  247. # rTorrent port settings
  248. port_range = 45000-65000
  249. port_random = no
  250.  
  251. # Check the hash after download
  252. check_hash = no
  253.  
  254. # DHT settings (mostly for public trackers)
  255. dht = off
  256. peer_exchange = no
  257.  
  258. # Authorize UDP trackers
  259. use_udp_trackers = yes
  260.  
  261. # Enable encryption
  262. encryption = allow_incoming, try_outgoing, enable_retry
  263.  
  264. # SCGI port for Flood, ruTorrent etc.
  265. scgi_port = 127.0.0.1:5000
  266.  
  267. #This fixes weird Flood bug where uploading some .torrent files gives 500 error
  268. network.xmlrpc.size_limit.set = 2M
  269. EOF
  270.  
  271. chown rtorrent:rtorrent /home/rtorrent/.rtorrent.rc
  272.  
  273. curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
  274. apt install -y nodejs
  275.  
  276. cd /home/rtorrent
  277. git clone https://github.com/jfurrow/flood.git
  278. cd flood
  279. cp config.template.js config.js
  280. npm install --production
  281. adduser --disabled-password flood
  282. chown -R flood:flood /home/rtorrent/flood/
  283.  
  284.