@@ -1,85 +1,100 @@
11{ config, lib, pkgs, ... }:
2+
23with lib;
3−let
44
5− cfg = config.services.tor;
5+let
6+ cfg = config.services.tor.torsocks;
7+ optionalNullStr = b: v: optionalString (b != null) v;
68
7− makeConfig = server: ''
8− server = ${toString(head (splitString ":" server))}
9− server_port = ${toString(tail (splitString ":" server))}
9+ configFile = ''
10+ TorAddress ${toString (head (splitString ":" cfg.server))}
11+ TorPort ${toString (tail (splitString ":" cfg.server))}
1012
11− local = 127.0.0.0/255.128.0.0
12− local = 127.128.0.0/255.192.0.0
13− local = 169.254.0.0/255.255.0.0
14− local = 172.16.0.0/255.240.0.0
15− local = 192.168.0.0/255.255.0.0
13+ OnionAddrRange ${cfg.onionAddrRange}
1614
17− ${cfg.torsocks.config}
18− '';
19− makeTorsocks = name: server: pkgs.writeTextFile {
20− name = name;
21− text = ''
22− #!${pkgs.stdenv.shell}
23− TORSOCKS_CONF_FILE=${pkgs.writeText "torsocks.conf" (makeConfig server)} LD_PRELOAD="${pkgs.torsocks}/lib/torsocks/libtorsocks.so $LD_PRELOAD" "$@"
24− '';
25− executable = true;
26− destination = "/bin/${name}";
27− };
15+ ${optionalNullStr cfg.socks5Username
16+ "SOCKS5Username ${cfg.socks5Username}"}
17+ ${optionalNullStr cfg.socks5Password
18+ "SOCKS5Password ${cfg.socks5Password}"}
2819
29− torsocks = makeTorsocks "torsocks" cfg.torsocks.server;
30− torsocksFaster = makeTorsocks "torsocks-faster" cfg.torsocks.serverFaster;
20+ AllowInbound ${if cfg.allowInbound then "1" else "0"}
21+ '';
3122in
32−
3323{
34−
35− ###### interface
36−
3724 options = {
38−
3925 services.tor.torsocks = {
40−
4126 enable = mkOption {
42− default = cfg.client.enable;
27+ type = types.bool;
28+ default = false;
4329 description = ''
44− Whether to build torsocks scipt to relay application traffic via TOR.
30+ Whether to build <literal>/etc/tor/torsocks.conf</literal>
31+ containing the specified global torsocks configuration.
4532 '';
4633 };
4734
4835 server = mkOption {
49− default = cfg.client.socksListenAddress;
50− example = "192.168.0.20:9050";
36+ type = types.str;
37+ default = "127.0.0.1:9050";
38+ example = "192.168.0.20:1234";
5139 description = ''
52− IP address of TOR client to use.
40+ IP/Port of the Tor SOCKS server. Currently, hostnames are
41+ NOT supported by torsocks.
5342 '';
5443 };
5544
56− serverFaster = mkOption {
57− default = cfg.client.socksListenAddressFaster;
58− example = "192.168.0.20:9063";
45+ onionAddrRange = mkOption {
46+ type = types.str;
47+ default = "127.42.42.0/24";
5948 description = ''
60− IP address of TOR client to use for applications like web browsers which
61− need less circuit isolation to achive satisfactory performance.
49+ Tor hidden sites do not have real IP addresses. This
50+ specifies what range of IP addresses will be handed to the
51+ application as "cookies" for .onion names. Of course, you
52+ should pick a block of addresses which you aren't going to
53+ ever need to actually connect to. This is similar to the
54+ MapAddress feature of the main tor daemon.
6255 '';
6356 };
6457
65− config = mkOption {
66− default = "";
58+ socks5Username = mkOption {
59+ type = types.nullOr types.str;
60+ default = null;
61+ example = "bob";
6762 description = ''
68− Extra configuration. Contents will be added verbatim to torsocks
69− configuration file.
63+ SOCKS5 username. The <literal>TORSOCKS_USERNAME</literal>
64+ environment variable overrides this option if it is set.
7065 '';
7166 };
7267
73− };
74−
75− };
68+ socks5Password = mkOption {
69+ type = types.nullOr types.str;
70+ default = null;
71+ example = "sekret";
72+ description = ''
73+ SOCKS5 password. The <literal>TORSOCKS_PASSWORD</literal>
74+ environment variable overrides this option if it is set.
75+ '';
76+ };
7677
77− ###### implementation
78+ allowInbound = mkOption {
79+ type = types.bool;
80+ default = false;
81+ description = ''
82+ Set Torsocks to accept inbound connections. If set to
83+ <literal>true</literal>, listen() and accept() will be
84+ allowed to be used with non localhost address.
85+ '';
86+ };
7887
79− config = mkIf cfg.torsocks.enable {
88+ };
89+ };
8090
81− environment.systemPackages = [ torsocks torsocksFaster ]; # expose it to the users
91+ config = mkIf cfg.enable {
92+ environment.systemPackages = [ pkgs.torsocks ];
8293
94+ environment.etc =
95+ [ { source = pkgs.writeText "torsocks.conf" configFile;
96+ target = "tor/torsocks.conf";
97+ }
98+ ];
8399 };
84−
85100}