2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-05-16 18:20:00 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2010-05-16 18:20:00 +02:00
|
|
|
|
|
|
|
let
|
2010-07-12 08:03:52 +02:00
|
|
|
cfg = config.services.tor;
|
2014-12-06 10:37:31 +01:00
|
|
|
torDirectory = "/var/lib/tor";
|
|
|
|
|
|
|
|
opt = name: value: optionalString (value != null) "${name} ${value}";
|
|
|
|
optint = name: value: optionalString (value != 0) "${name} ${toString value}";
|
|
|
|
|
|
|
|
torRc = ''
|
|
|
|
User tor
|
|
|
|
DataDirectory ${torDirectory}
|
|
|
|
|
|
|
|
${optint "ControlPort" cfg.controlPort}
|
|
|
|
''
|
|
|
|
# Client connection config
|
|
|
|
+ optionalString cfg.client.enable ''
|
2014-12-18 06:54:33 +01:00
|
|
|
SOCKSPort ${cfg.client.socksListenAddress} IsolateDestAddr
|
|
|
|
SOCKSPort ${cfg.client.socksListenAddressFaster}
|
2014-12-06 10:37:31 +01:00
|
|
|
${opt "SocksPolicy" cfg.client.socksPolicy}
|
|
|
|
''
|
|
|
|
# Relay config
|
|
|
|
+ optionalString cfg.relay.enable ''
|
|
|
|
ORPort ${cfg.relay.portSpec}
|
|
|
|
${opt "Nickname" cfg.relay.nickname}
|
|
|
|
${opt "ContactInfo" cfg.relay.contactInfo}
|
|
|
|
|
|
|
|
${optint "RelayBandwidthRate" cfg.relay.bandwidthRate}
|
|
|
|
${optint "RelayBandwidthBurst" cfg.relay.bandwidthBurst}
|
|
|
|
${opt "AccountingMax" cfg.relay.accountingMax}
|
|
|
|
${opt "AccountingStart" cfg.relay.accountingStart}
|
|
|
|
|
|
|
|
${if cfg.relay.isExit then
|
|
|
|
opt "ExitPolicy" cfg.relay.exitPolicy
|
|
|
|
else
|
|
|
|
"ExitPolicy reject *:*"}
|
|
|
|
|
|
|
|
${optionalString cfg.relay.isBridge ''
|
|
|
|
BridgeRelay 1
|
|
|
|
ServerTransportPlugin obfs2,obfs3 exec ${pkgs.pythonPackages.obfsproxy}/bin/obfsproxy managed
|
|
|
|
''}
|
|
|
|
''
|
|
|
|
+ cfg.extraConfig;
|
|
|
|
|
|
|
|
torRcFile = pkgs.writeText "torrc" torRc;
|
2010-05-16 18:20:00 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.tor = {
|
2014-12-06 10:37:31 +01:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Enable the Tor daemon. By default, the daemon is run without
|
|
|
|
relay, exit, bridge or client connectivity.
|
|
|
|
'';
|
|
|
|
};
|
2010-05-16 18:20:00 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
extraConfig = mkOption {
|
2014-12-11 15:23:48 +01:00
|
|
|
type = types.lines;
|
2010-05-16 18:20:00 +02:00
|
|
|
default = "";
|
|
|
|
description = ''
|
2011-09-14 20:20:50 +02:00
|
|
|
Extra configuration. Contents will be added verbatim to the
|
2014-12-06 10:37:31 +01:00
|
|
|
configuration file at the end.
|
2010-05-16 18:20:00 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
controlPort = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 0;
|
|
|
|
example = 9051;
|
|
|
|
description = ''
|
|
|
|
If set, Tor will accept connections on the specified port
|
|
|
|
and allow them to control the tor process.
|
|
|
|
'';
|
|
|
|
};
|
2010-05-16 18:20:00 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
client = {
|
2010-09-18 13:30:14 +02:00
|
|
|
enable = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.bool;
|
2010-09-18 18:36:03 +02:00
|
|
|
default = false;
|
2010-09-18 13:30:14 +02:00
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
Whether to enable Tor daemon to route application
|
|
|
|
connections. You might want to disable this if you plan
|
|
|
|
running a dedicated Tor relay.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
socksListenAddress = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.str;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = "127.0.0.1:9050";
|
2010-10-01 05:41:43 +02:00
|
|
|
example = "192.168.0.1:9100";
|
2010-09-18 13:30:14 +02:00
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
Bind to this address to listen for connections from
|
2014-12-18 06:54:33 +01:00
|
|
|
Socks-speaking applications. Provides strong circuit
|
|
|
|
isolation, separate circuit per IP address.
|
2013-01-14 06:37:13 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-12-18 06:54:33 +01:00
|
|
|
socksListenAddressFaster = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:9063";
|
|
|
|
example = "192.168.0.1:9101";
|
|
|
|
description = ''
|
|
|
|
Bind to this address to listen for connections from
|
|
|
|
Socks-speaking applications. Same as socksListenAddress
|
|
|
|
but uses weaker circuit isolation to provide performance
|
|
|
|
suitable for a web browser.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
socksPolicy = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2010-09-18 13:30:14 +02:00
|
|
|
example = "accept 192.168.0.0/16, reject *";
|
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
Entry policies to allow/deny SOCKS requests based on IP
|
|
|
|
address. First entry that matches wins. If no SocksPolicy
|
|
|
|
is set, we accept all (and only) requests from
|
|
|
|
SocksListenAddress.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
2014-12-18 07:19:57 +01:00
|
|
|
|
|
|
|
privoxy.enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable and configure the system Privoxy to use Tor's
|
|
|
|
faster port, suitable for HTTP.
|
|
|
|
|
|
|
|
To have anonymity, protocols need to be scrubbed of identifying
|
|
|
|
information, and this can be accomplished for HTTP by Privoxy.
|
|
|
|
|
|
|
|
Privoxy can also be useful for KDE torification. A good setup would be:
|
|
|
|
setting SOCKS proxy to the default Tor port, providing maximum
|
|
|
|
circuit isolation where possible; and setting HTTP proxy to Privoxy
|
|
|
|
to route HTTP traffic over faster, but less isolated port.
|
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
};
|
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
relay = {
|
2010-09-18 13:30:14 +02:00
|
|
|
enable = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.bool;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable relaying TOR traffic for others.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
See https://www.torproject.org/docs/tor-doc-relay for details.
|
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
isBridge = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.bool;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = false;
|
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
Bridge relays (or "bridges") are Tor relays that aren't
|
|
|
|
listed in the main directory. Since there is no complete
|
|
|
|
public list of them, even if an ISP is filtering
|
|
|
|
connections to all the known Tor relays, they probably
|
2010-09-18 13:30:14 +02:00
|
|
|
won't be able to block all the bridges.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
A bridge relay can't be an exit relay.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
You need to set relay.enable to true for this option to
|
|
|
|
take effect.
|
2013-06-04 12:02:37 +02:00
|
|
|
|
|
|
|
The bridge is set up with an obfuscated transport proxy.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
See https://www.torproject.org/bridges.html.en for more info.
|
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
isExit = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.bool;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = false;
|
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
An exit relay allows Tor users to access regular Internet
|
|
|
|
services.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
Unlike running a non-exit relay, running an exit relay may
|
|
|
|
expose you to abuse complaints. See
|
|
|
|
https://www.torproject.org/faq.html.en#ExitPolicies for
|
|
|
|
more info.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
You can specify which services Tor users may access via
|
|
|
|
your exit relay using exitPolicy option.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
nickname = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.str;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = "anonymous";
|
|
|
|
description = ''
|
|
|
|
A unique handle for your TOR relay.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
contactInfo = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "admin@relay.com";
|
|
|
|
description = ''
|
|
|
|
Contact information for the relay owner (e.g. a mail
|
|
|
|
address and GPG key ID).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
accountingMax = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "450 GBytes";
|
|
|
|
description = ''
|
|
|
|
Specify maximum bandwidth allowed during an accounting
|
|
|
|
period. This allows you to limit overall tor bandwidth
|
|
|
|
over some time period. See the
|
|
|
|
<literal>AccountingMax</literal> option by looking at the
|
|
|
|
tor manual (<literal>man tor</literal>) for more.
|
|
|
|
|
|
|
|
Note this limit applies individually to upload and
|
|
|
|
download; if you specify <literal>"500 GBytes"</literal>
|
|
|
|
here, then you may transfer up to 1 TBytes of overall
|
|
|
|
bandwidth (500 GB upload, 500 GB download).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
accountingStart = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "month 1 1:00";
|
|
|
|
description = ''
|
|
|
|
Specify length of an accounting period. This allows you to
|
|
|
|
limit overall tor bandwidth over some time period. See the
|
|
|
|
<literal>AccountingStart</literal> option by looking at
|
|
|
|
the tor manual (<literal>man tor</literal>) for more.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
bandwidthRate = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.int;
|
2010-09-18 13:30:14 +02:00
|
|
|
default = 0;
|
|
|
|
example = 100;
|
|
|
|
description = ''
|
|
|
|
Specify this to limit the bandwidth usage of relayed (server)
|
|
|
|
traffic. Your own traffic is still unthrottled. Units: bytes/second.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
bandwidthBurst = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.int;
|
2010-09-18 14:43:48 +02:00
|
|
|
default = cfg.relay.bandwidthRate;
|
2010-09-18 13:30:14 +02:00
|
|
|
example = 200;
|
|
|
|
description = ''
|
|
|
|
Specify this to allow bursts of the bandwidth usage of relayed (server)
|
|
|
|
traffic. The average usage will still be as specified in relayBandwidthRate.
|
|
|
|
Your own traffic is still unthrottled. Units: bytes/second.
|
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
portSpec = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "143";
|
2010-09-18 13:30:14 +02:00
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
What port to advertise for Tor connections. This corresponds
|
|
|
|
to the <literal>ORPort</literal> section in the Tor manual; see
|
|
|
|
<literal>man tor</literal> for more details.
|
|
|
|
|
|
|
|
At a minimum, you should just specify the port for the
|
|
|
|
relay to listen on; a common one like 143, 22, 80, or 443
|
|
|
|
to help Tor users who may have very restrictive port-based
|
|
|
|
firewalls.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exitPolicy = mkOption {
|
2014-12-06 10:37:31 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2010-09-18 13:30:14 +02:00
|
|
|
example = "accept *:6660-6667,reject *:*";
|
|
|
|
description = ''
|
2014-12-06 10:37:31 +01:00
|
|
|
A comma-separated list of exit policies. They're
|
|
|
|
considered first to last, and the first match wins. If you
|
|
|
|
want to _replace_ the default exit policy, end this with
|
|
|
|
either a reject *:* or an accept *:*. Otherwise, you're
|
|
|
|
_augmenting_ (prepending to) the default exit
|
|
|
|
policy. Leave commented to just use the default, which is
|
|
|
|
available in the man page or at
|
|
|
|
https://www.torproject.org/documentation.html
|
2010-09-18 13:30:14 +02:00
|
|
|
|
|
|
|
Look at https://www.torproject.org/faq-abuse.html#TypicalAbuses
|
|
|
|
for issues you might encounter if you use the default exit policy.
|
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
If certain IPs and ports are blocked externally, e.g. by
|
|
|
|
your firewall, you should update your exit policy to
|
|
|
|
reflect this -- otherwise Tor users will be told that
|
|
|
|
those destinations are down.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
};
|
2010-05-16 18:20:00 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
config = mkIf cfg.enable {
|
2013-10-30 18:30:23 +01:00
|
|
|
assertions = singleton
|
2014-12-06 10:37:31 +01:00
|
|
|
{ message = "Can't be both an exit and a bridge relay at the same time";
|
|
|
|
assertion =
|
|
|
|
cfg.relay.enable -> !(cfg.relay.isBridge && cfg.relay.isExit);
|
2013-10-30 18:30:23 +01:00
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
users.extraGroups.tor.gid = config.ids.gids.tor;
|
|
|
|
users.extraUsers.tor =
|
|
|
|
{ description = "Tor Daemon User";
|
|
|
|
createHome = true;
|
|
|
|
home = torDirectory;
|
|
|
|
group = "tor";
|
|
|
|
uid = config.ids.uids.tor;
|
2010-05-16 18:20:00 +02:00
|
|
|
};
|
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
systemd.services.tor =
|
|
|
|
{ description = "Tor Daemon";
|
|
|
|
path = [ pkgs.tor ];
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
restartTriggers = [ torRcFile ];
|
|
|
|
|
|
|
|
# Translated from the upstream contrib/dist/tor.service.in
|
|
|
|
serviceConfig =
|
|
|
|
{ Type = "simple";
|
|
|
|
ExecStartPre = "${pkgs.tor}/bin/tor -f ${torRcFile} --verify-config";
|
|
|
|
ExecStart = "${pkgs.tor}/bin/tor -f ${torRcFile} --RunAsDaemon 0";
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
TimeoutSec = 30;
|
|
|
|
Restart = "on-failure";
|
|
|
|
LimitNOFILE = 32768;
|
|
|
|
|
|
|
|
# Hardening
|
|
|
|
# Note: DevicePolicy is set to 'closed', although the
|
|
|
|
# minimal permissions are really:
|
|
|
|
# DeviceAllow /dev/null rw
|
|
|
|
# DeviceAllow /dev/urandom r
|
|
|
|
# .. but we can't specify DeviceAllow multiple times. 'closed'
|
|
|
|
# is close enough.
|
|
|
|
PrivateTmp = "yes";
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
InaccessibleDirectories = "/home";
|
|
|
|
ReadOnlyDirectories = "/";
|
|
|
|
ReadWriteDirectories = torDirectory;
|
|
|
|
NoNewPrivileges = "yes";
|
|
|
|
};
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-12-06 10:37:31 +01:00
|
|
|
environment.systemPackages = [ pkgs.tor ];
|
2014-12-18 07:19:57 +01:00
|
|
|
|
|
|
|
services.privoxy = mkIf (cfg.client.enable && cfg.client.privoxy.enable) {
|
|
|
|
enable = true;
|
|
|
|
extraConfig = ''
|
|
|
|
forward-socks4a / ${cfg.client.socksListenAddressFaster} .
|
|
|
|
toggle 1
|
|
|
|
enable-remote-toggle 0
|
|
|
|
enable-edit-actions 0
|
|
|
|
enable-remote-http-toggle 0
|
|
|
|
'';
|
|
|
|
};
|
2013-10-30 18:30:23 +01:00
|
|
|
};
|
2010-05-16 18:20:00 +02:00
|
|
|
}
|