1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.rutorrent;
rutorrent = pkgs.callPackage ./rutorrent.pkg.nix {};
rtorrentPluginDependencies = with pkgs; {
_task = [ procps ];
unpack = [ unzip unrar ];
rss = [ curl ];
mediainfo = [ mediainfo ];
spectrogram = [ sox ];
screenshots = [ ffmpeg ];
};
phpPluginDependencies = with pkgs; {
_cloudflare = [ python3 ];
};
getPluginDependencies = dependencies: concatMap (p: attrByPath [ p ] [] dependencies);
in {
options = {
services.rutorrent = {
enable = mkEnableOption "ruTorrent";
hostName = mkOption {
type = types.str;
description = "FQDN for the ruTorrent instance.";
};
dataDir = mkOption {
type = types.str;
default = "/var/lib/rutorrent";
description = "Storage path of ruTorrent.";
};
user = mkOption {
type = types.str;
default = "rutorrent";
description = ''
User which runs the ruTorrent service.
'';
};
group = mkOption {
type = types.str;
default = "rutorrent";
description = ''
Group which runs the ruTorrent service.
'';
};
rpcSocket = mkOption {
type = types.str;
default = config.services.rtorrent.rpcSocket;
defaultText = "config.services.rtorrent.rpcSocket";
description = ''
Path to rtorrent rpc socket.
'';
};
plugins = mkOption {
type = with types; listOf (either str package);
default = [ "httprpc" ];
example = literalExample ''[ "httprpc" "data" "diskspace" "edit" "erasedata" "theme" "trafic" ]'';
description = ''
List of plugins to enable. See the list of <link xlink:href="https://github.com/Novik/ruTorrent/wiki/Plugins#currently-there-are-the-following-plugins">available plugins</link>. Note: the <literal>unpack</literal> plugin needs the nonfree <literal>unrar</literal> package.
You need to either enable one of the <literal>rpc</literal> or <literal>httprpc</literal> plugin or enable the <xref linkend="opt-services.rutorrent.nginx.exposeInsecureRPC2mount"/> option.
'';
};
poolSettings = mkOption {
type = with types; attrsOf (oneOf [ str int bool ]);
default = {
"pm" = "dynamic";
"pm.max_children" = 32;
"pm.start_servers" = 2;
"pm.min_spare_servers" = 2;
"pm.max_spare_servers" = 4;
"pm.max_requests" = 500;
};
description = ''
Options for ruTorrent's PHP pool. See the documentation on <literal>php-fpm.conf</literal> for details on configuration directives.
'';
};
nginx = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable nginx virtual host management.
Further nginx configuration can be done by adapting <literal>services.nginx.virtualHosts.<name></literal>.
See <xref linkend="opt-services.nginx.virtualHosts"/> for further information.
'';
};
exposeInsecureRPC2mount = mkOption {
type = types.bool;
default = false;
description = ''
If you do not enable one of the <literal>rpc</literal> or <literal>httprpc</literal> plugins you need to expose an RPC mount through scgi using this option.
Warning: This allow to run arbitrary commands, as the rtorrent user, so make sure to use authentification. The simplest way would be to use the <literal>services.nginx.virtualHosts.<name>.basicAuth</literal> option.
'';
};
};
};
};
config = mkIf cfg.enable (mkMerge [
{ assertions = let
usedRpcPlugins = intersectLists cfg.plugins [ "httprpc" "rpc" ];
in [
{ assertion = (length usedRpcPlugins < 2);
message = "Please specify only one of httprpc or rpc plugins";
}
{ assertion = !(length usedRpcPlugins > 0 && cfg.nginx.exposeInsecureRPC2mount);
message = "Please do not use exposeInsecureRPC2mount if you use one of httprpc or rpc plugins";
}
];
warnings = let
nginxVhostCfg = config.services.nginx.virtualHosts."${cfg.hostName}";
in []
++ (optional (cfg.nginx.exposeInsecureRPC2mount && (nginxVhostCfg.basicAuth == {} || nginxVhostCfg.basicAuthFile == null )) ''
You are using exposeInsecureRPC2mount without using basic auth on the virtual host. The exposed rpc mount allow for remote command execution.
Please make sure it is not accessible from the outside.
'');
systemd = {
services = {
rtorrent.path = getPluginDependencies rtorrentPluginDependencies cfg.plugins;
rutorrent-setup = let
rutorrentConfig = pkgs.writeText "rutorrent-config.php" ''
<?php
// configuration parameters
// for snoopy client
@define('HTTP_USER_AGENT', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36', true);
@define('HTTP_TIME_OUT', 30, true); // in seconds
@define('HTTP_USE_GZIP', true, true);
$httpIP = null; // IP string. Or null for any.
$httpProxy = array
(
'use' => false,
'proto' => 'http', // 'http' or 'https'
'host' => 'PROXY_HOST_HERE',
'port' => 3128
);
@define('RPC_TIME_OUT', 5, true); // in seconds
@define('LOG_RPC_CALLS', false, true);
@define('LOG_RPC_FAULTS', true, true);
// for php
@define('PHP_USE_GZIP', false, true);
@define('PHP_GZIP_LEVEL', 2, true);
$schedule_rand = 10; // rand for schedulers start, +0..X seconds
$do_diagnostic = true;
$log_file = '${cfg.dataDir}/logs/errors.log'; // path to log file (comment or leave blank to disable logging)
$saveUploadedTorrents = true; // Save uploaded torrents to profile/torrents directory or not
$overwriteUploadedTorrents = false; // Overwrite existing uploaded torrents in profile/torrents directory or make unique name
$topDirectory = '/'; // Upper available directory. Absolute path with trail slash.
$forbidUserSettings = false;
$scgi_port = 0;
$scgi_host = "unix://${cfg.rpcSocket}";
$XMLRPCMountPoint = "/RPC2"; // DO NOT DELETE THIS LINE!!! DO NOT COMMENT THIS LINE!!!
$pathToExternals = array(
"php" => "${pkgs.php}/bin/php", // Something like /usr/bin/php. If empty, will be found in PATH.
"curl" => "${pkgs.curl}/bin/curl", // Something like /usr/bin/curl. If empty, will be found in PATH.
"gzip" => "${pkgs.gzip}/bin/gzip", // Something like /usr/bin/gzip. If empty, will be found in PATH.
"id" => "${pkgs.coreutils}/bin/id", // Something like /usr/bin/id. If empty, will be found in PATH.
"stat" => "${pkgs.coreutils}/bin/stat", // Something like /usr/bin/stat. If empty, will be found in PATH.
"pgrep" => "${pkgs.procps}/bin/pgrep", // TODO why can't we use phpEnv.PATH
);
$localhosts = array( // list of local interfaces
"127.0.0.1",
"localhost",
);
$profilePath = '${cfg.dataDir}/share'; // Path to user profiles
$profileMask = 0770; // Mask for files and directory creation in user profiles.
// Both Webserver and rtorrent users must have read-write access to it.
// For example, if Webserver and rtorrent users are in the same group then the value may be 0770.
$tempDirectory = null; // Temp directory. Absolute path with trail slash. If null, then autodetect will be used.
$canUseXSendFile = false; // If true then use X-Sendfile feature if it exist
$locale = "UTF8";
'';
in {
wantedBy = [ "multi-user.target" ];
before = [ "phpfpm-rutorrent.service" ];
script = ''
ln -sf ${rutorrent}/{css,images,js,lang,index.html} ${cfg.dataDir}/
mkdir -p ${cfg.dataDir}/{conf,logs,plugins} ${cfg.dataDir}/share/{settings,torrents,users}
ln -sf ${rutorrent}/conf/{access.ini,plugins.ini} ${cfg.dataDir}/conf/
ln -sf ${rutorrentConfig} ${cfg.dataDir}/conf/config.php
cp -r ${rutorrent}/php ${cfg.dataDir}/
${optionalString (cfg.plugins != [])
''cp -r ${concatMapStringsSep " " (p: "${rutorrent}/plugins/${p}") cfg.plugins} ${cfg.dataDir}/plugins/''}
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}/{conf,share,logs,plugins}
chmod -R 755 ${cfg.dataDir}/{conf,share,logs,plugins}
'';
serviceConfig.Type = "oneshot";
};
};
tmpfiles.rules = [ "d '${cfg.dataDir}' 0775 ${cfg.user} ${cfg.group} -" ];
};
users.groups."${cfg.group}" = {};
users.users = {
"${cfg.user}" = {
home = cfg.dataDir;
group = cfg.group;
extraGroups = [ config.services.rtorrent.group ];
description = "ruTorrent Daemon user";
isSystemUser = true;
};
"${config.services.rtorrent.user}" = {
extraGroups = [ cfg.group ];
};
};
}
(mkIf cfg.nginx.enable (mkMerge [
{ services = {
phpfpm.pools.rutorrent = let
envPath = lib.makeBinPath (getPluginDependencies phpPluginDependencies cfg.plugins);
pool = {
user = cfg.user;
group = config.services.rtorrent.group;
settings = mapAttrs (name: mkDefault) {
"listen.owner" = config.services.nginx.user;
"listen.group" = config.services.nginx.group;
} // cfg.poolSettings;
};
in if (envPath == "") then pool else pool // { phpEnv.PATH = envPath; };
nginx = {
enable = true;
virtualHosts = {
${cfg.hostName} = {
root = cfg.dataDir;
locations = {
"~ [^/]\.php(/|$)" = {
extraConfig = ''
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:${config.services.phpfpm.pools.rutorrent.socket};
fastcgi_index index.php;
include ${pkgs.nginx}/conf/fastcgi.conf;
'';
};
};
};
};
};
};
}
(mkIf cfg.nginx.exposeInsecureRPC2mount {
services.nginx.virtualHosts."${cfg.hostName}".locations."/RPC2" = {
extraConfig = ''
include ${pkgs.nginx}/conf/scgi_params;
scgi_pass unix:${cfg.rpcSocket};
'';
};
})
]))
]);
}
|