2010-05-16 18:20:00 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (pkgs) tor privoxy;
|
|
|
|
|
|
|
|
stateDir = "/var/lib/tor";
|
|
|
|
privoxyDir = stateDir+"/privoxy";
|
|
|
|
|
2010-07-12 08:03:52 +02:00
|
|
|
cfg = config.services.tor;
|
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
torUser = "tor";
|
|
|
|
|
2010-07-12 08:03:52 +02:00
|
|
|
opt = name: value: if value != "" then "${name} ${value}" else "";
|
|
|
|
optint = name: value: if value != 0 then "${name} ${toString value}" else "";
|
2012-03-17 18:26:17 +01:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
services.tor = {
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
2011-09-14 20:20:50 +02:00
|
|
|
Extra configuration. Contents will be added verbatim to the
|
2010-05-16 18:20:00 +02:00
|
|
|
configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
client = {
|
2010-05-16 18:20:00 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
enable = mkOption {
|
2010-09-18 18:36:03 +02:00
|
|
|
default = false;
|
2010-09-18 13:30:14 +02:00
|
|
|
description = ''
|
|
|
|
Whether to enable Tor daemon to route application connections.
|
|
|
|
You might want to disable this if you plan running a dedicated Tor relay.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
socksListenAddress = mkOption {
|
|
|
|
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 = ''
|
2011-09-14 20:20:50 +02:00
|
|
|
Bind to this address to listen for connections from Socks-speaking
|
2010-10-02 04:19:17 +02:00
|
|
|
applications.
|
2010-09-18 13:30:14 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-10-30 18:30:23 +01:00
|
|
|
socksListenAddressFaster = mkOption {
|
2013-01-14 06:37:13 +01:00
|
|
|
default = "127.0.0.1:9063";
|
2013-10-30 18:30:23 +01:00
|
|
|
description = ''
|
2013-01-14 06:37:13 +01:00
|
|
|
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 {
|
|
|
|
default = "";
|
|
|
|
example = "accept 192.168.0.0/16, reject *";
|
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
privoxy = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable a special instance of privoxy dedicated to Tor.
|
2011-09-14 20:20:50 +02:00
|
|
|
To have anonymity, protocols need to be scrubbed of identifying
|
2010-09-18 13:30:14 +02:00
|
|
|
information.
|
2011-09-14 20:20:50 +02:00
|
|
|
Most people using Tor want to anonymize their web traffic, so by
|
2010-09-18 13:30:14 +02:00
|
|
|
default we enable an special instance of privoxy specifically for
|
|
|
|
Tor.
|
|
|
|
However, if you are only going to use Tor only for other kinds of
|
|
|
|
traffic then you can disable this option.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
|
|
|
default = "127.0.0.1:8118";
|
|
|
|
description = ''
|
|
|
|
Address that Tor's instance of privoxy is listening to.
|
2011-09-14 20:20:50 +02:00
|
|
|
*This does not configure the standard NixOS instance of privoxy.*
|
|
|
|
This is for Tor connections only!
|
|
|
|
See services.privoxy.listenAddress to configure the standard NixOS
|
2010-09-18 13:30:14 +02:00
|
|
|
instace of privoxy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
2011-09-14 20:20:50 +02:00
|
|
|
Extra configuration for Tor's instance of privoxy. Contents will be
|
2010-09-18 13:30:14 +02:00
|
|
|
added verbatim to the configuration file.
|
2011-09-14 20:20:50 +02:00
|
|
|
*This does not configure the standard NixOS instance of privoxy.*
|
|
|
|
This is for Tor connections only!
|
|
|
|
See services.privoxy.extraConfig to configure the standard NixOS
|
2010-09-18 13:30:14 +02:00
|
|
|
instace of privoxy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
relay = {
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
enable = mkOption {
|
|
|
|
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 {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-06-04 12:02:37 +02:00
|
|
|
You need to set relay.enable to true for this option to take effect.
|
|
|
|
|
|
|
|
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 {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
An exit relay allows Tor users to access regular Internet services.
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02: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
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
You can specify which services Tor users may access via your exit relay using exitPolicy option.
|
|
|
|
'';
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
nickname = mkOption {
|
|
|
|
default = "anonymous";
|
|
|
|
description = ''
|
|
|
|
A unique handle for your TOR relay.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
bandwidthRate = mkOption {
|
|
|
|
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 {
|
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
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
port = mkOption {
|
|
|
|
default = 9001;
|
|
|
|
description = ''
|
|
|
|
What port to advertise for Tor connections.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "0.0.0.0:9090";
|
|
|
|
description = ''
|
|
|
|
Set this if you need to listen on a port other than the one advertised
|
|
|
|
in relayPort (e.g. to advertise 443 but bind to 9090). You'll need to do
|
|
|
|
ipchains or other port forwsarding yourself to make this work.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exitPolicy = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "accept *:6660-6667,reject *:*";
|
|
|
|
description = ''
|
|
|
|
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
|
|
|
|
|
|
|
|
Look at https://www.torproject.org/faq-abuse.html#TypicalAbuses
|
|
|
|
for issues you might encounter if you use the default exit policy.
|
|
|
|
|
|
|
|
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-07-12 08:03:52 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2013-10-30 18:30:23 +01:00
|
|
|
config = mkIf (cfg.client.enable || cfg.relay.enable) {
|
|
|
|
|
|
|
|
assertions = singleton
|
|
|
|
{ assertion = cfg.relay.enable -> !(cfg.relay.isBridge && cfg.relay.isExit);
|
|
|
|
message = "Can't be both an exit and a bridge relay at the same time";
|
|
|
|
};
|
2010-07-12 08:03:52 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = torUser;
|
|
|
|
uid = config.ids.uids.tor;
|
|
|
|
description = "Tor daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
|
|
|
|
2010-09-19 17:40:03 +02:00
|
|
|
jobs = {
|
|
|
|
tor = { name = "tor";
|
|
|
|
|
|
|
|
startOn = "started network-interfaces";
|
|
|
|
stopOn = "stopping network-interfaces";
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${torUser} ${stateDir}
|
|
|
|
'';
|
|
|
|
exec = "${tor}/bin/tor -f ${pkgs.writeText "torrc" cfg.config}";
|
|
|
|
}; }
|
|
|
|
// optionalAttrs (cfg.client.privoxy.enable && cfg.client.enable) {
|
|
|
|
torPrivoxy = { name = "tor-privoxy";
|
|
|
|
|
2013-02-10 19:29:17 +01:00
|
|
|
startOn = "started network-interfaces";
|
|
|
|
stopOn = "stopping network-interfaces";
|
2010-09-19 17:40:03 +02:00
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p ${privoxyDir}
|
|
|
|
chown ${torUser} ${privoxyDir}
|
|
|
|
'';
|
|
|
|
exec = "${privoxy}/sbin/privoxy --no-daemon --user ${torUser} ${pkgs.writeText "torPrivoxy.conf" cfg.client.privoxy.config}";
|
|
|
|
}; };
|
2010-05-16 18:20:00 +02:00
|
|
|
|
|
|
|
services.tor.config = ''
|
|
|
|
DataDirectory ${stateDir}
|
|
|
|
User ${torUser}
|
2010-07-12 08:03:52 +02:00
|
|
|
''
|
2010-09-18 13:30:14 +02:00
|
|
|
+ optionalString cfg.client.enable ''
|
2013-01-14 06:37:13 +01:00
|
|
|
SOCKSPort ${cfg.client.socksListenAddress} IsolateDestAddr
|
2013-10-30 18:30:23 +01:00
|
|
|
SOCKSPort ${cfg.client.socksListenAddressFaster}
|
2010-09-18 13:30:14 +02:00
|
|
|
${opt "SocksPolicy" cfg.client.socksPolicy}
|
2010-07-12 08:03:52 +02:00
|
|
|
''
|
2010-09-18 13:30:14 +02:00
|
|
|
+ optionalString cfg.relay.enable ''
|
|
|
|
ORPort ${toString cfg.relay.port}
|
|
|
|
${opt "ORListenAddress" cfg.relay.listenAddress }
|
|
|
|
${opt "Nickname" cfg.relay.nickname}
|
|
|
|
${optint "RelayBandwidthRate" cfg.relay.bandwidthRate}
|
|
|
|
${optint "RelayBandwidthBurst" cfg.relay.bandwidthBurst}
|
|
|
|
${if cfg.relay.isExit then opt "ExitPolicy" cfg.relay.exitPolicy else "ExitPolicy reject *:*"}
|
2013-06-04 12:02:37 +02:00
|
|
|
${if cfg.relay.isBridge then ''
|
|
|
|
BridgeRelay 1
|
|
|
|
ServerTransportPlugin obfs2,obfs3 exec ${pkgs.pythonPackages.obfsproxy}/bin/obfsproxy managed
|
|
|
|
'' else ""}
|
2010-05-16 18:20:00 +02:00
|
|
|
'';
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-09-18 13:30:14 +02:00
|
|
|
services.tor.client.privoxy.config = ''
|
2010-05-16 18:20:00 +02:00
|
|
|
# Generally, this file goes in /etc/privoxy/config
|
|
|
|
#
|
|
|
|
# Tor listens as a SOCKS4a proxy here:
|
2013-01-14 06:37:13 +01:00
|
|
|
forward-socks4a / ${cfg.client.socksListenAddressFaster} .
|
2010-05-16 18:20:00 +02:00
|
|
|
confdir ${privoxy}/etc
|
|
|
|
logdir ${privoxyDir}
|
|
|
|
# actionsfile standard # Internal purpose, recommended
|
|
|
|
actionsfile default.action # Main actions file
|
|
|
|
actionsfile user.action # User customizations
|
|
|
|
filterfile default.filter
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
# Don't log interesting things, only startup messages, warnings and errors
|
|
|
|
logfile logfile
|
|
|
|
#jarfile jarfile
|
|
|
|
#debug 0 # show each GET/POST/CONNECT request
|
|
|
|
debug 4096 # Startup banner and warnings
|
|
|
|
debug 8192 # Errors - *we highly recommended enabling this*
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
user-manual ${privoxy}/doc/privoxy/user-manual
|
2010-09-18 13:30:14 +02:00
|
|
|
listen-address ${cfg.client.privoxy.listenAddress}
|
2010-05-16 18:20:00 +02:00
|
|
|
toggle 1
|
|
|
|
enable-remote-toggle 0
|
|
|
|
enable-edit-actions 0
|
|
|
|
enable-remote-http-toggle 0
|
|
|
|
buffer-limit 4096
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
# Extra config goes here
|
|
|
|
'';
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2013-10-30 18:30:23 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-05-16 18:20:00 +02:00
|
|
|
}
|