1{ config, lib, pkgs, ... }:
2with lib;
3let
4
5 cfg = config.programs.proxychains;
6
7 configFile = ''
8 ${cfg.chain.type}_chain
9 ${optionalString (cfg.chain.type == "random")
10 "chain_len = ${builtins.toString cfg.chain.length}"}
11 ${optionalString cfg.proxyDNS "proxy_dns"}
12 ${optionalString cfg.quietMode "quiet_mode"}
13 remote_dns_subnet ${builtins.toString cfg.remoteDNSSubnet}
14 tcp_read_time_out ${builtins.toString cfg.tcpReadTimeOut}
15 tcp_connect_time_out ${builtins.toString cfg.tcpConnectTimeOut}
16 localnet ${cfg.localnet}
17 [ProxyList]
18 ${builtins.concatStringsSep "\n"
19 (lib.mapAttrsToList (k: v: "${v.type} ${v.host} ${builtins.toString v.port}")
20 (lib.filterAttrs (k: v: v.enable) cfg.proxies))}
21 '';
22
23 proxyOptions = {
24 options = {
25 enable = mkEnableOption "this proxy";
26
27 type = mkOption {
28 type = types.enum [ "http" "socks4" "socks5" ];
29 description = "Proxy type.";
30 };
31
32 host = mkOption {
33 type = types.str;
34 description = "Proxy host or IP address.";
35 };
36
37 port = mkOption {
38 type = types.port;
39 description = "Proxy port";
40 };
41 };
42 };
43
44in {
45
46 ###### interface
47
48 options = {
49
50 programs.proxychains = {
51
52 enable = mkEnableOption "installing proxychains configuration";
53
54 chain = {
55 type = mkOption {
56 type = types.enum [ "dynamic" "strict" "random" ];
57 default = "strict";
58 description = ''
59 <literal>dynamic</literal> - Each connection will be done via chained proxies
60 all proxies chained in the order as they appear in the list
61 at least one proxy must be online to play in chain
62 (dead proxies are skipped)
63 otherwise <literal>EINTR</literal> is returned to the app.
64
65 <literal>strict</literal> - Each connection will be done via chained proxies
66 all proxies chained in the order as they appear in the list
67 all proxies must be online to play in chain
68 otherwise <literal>EINTR</literal> is returned to the app.
69
70 <literal>random</literal> - Each connection will be done via random proxy
71 (or proxy chain, see <option>programs.proxychains.chain.length</option>) from the list.
72 '';
73 };
74 length = mkOption {
75 type = types.nullOr types.int;
76 default = null;
77 description = ''
78 Chain length for random chain.
79 '';
80 };
81 };
82
83 proxyDNS = mkOption {
84 type = types.bool;
85 default = true;
86 description = "Proxy DNS requests - no leak for DNS data.";
87 };
88
89 quietMode = mkEnableOption "Quiet mode (no output from the library).";
90
91 remoteDNSSubnet = mkOption {
92 type = types.enum [ 10 127 224 ];
93 default = 224;
94 description = ''
95 Set the class A subnet number to use for the internal remote DNS mapping, uses the reserved 224.x.x.x range by default.
96 '';
97 };
98
99 tcpReadTimeOut = mkOption {
100 type = types.int;
101 default = 15000;
102 description = "Connection read time-out in milliseconds.";
103 };
104
105 tcpConnectTimeOut = mkOption {
106 type = types.int;
107 default = 8000;
108 description = "Connection time-out in milliseconds.";
109 };
110
111 localnet = mkOption {
112 type = types.str;
113 default = "127.0.0.0/255.0.0.0";
114 description = "By default enable localnet for loopback address ranges.";
115 };
116
117 proxies = mkOption {
118 type = types.attrsOf (types.submodule proxyOptions);
119 description = ''
120 Proxies to be used by proxychains.
121 '';
122
123 example = literalExpression ''
124 { myproxy =
125 { type = "socks4";
126 host = "127.0.0.1";
127 port = 1337;
128 };
129 }
130 '';
131 };
132
133 };
134
135 };
136
137 ###### implementation
138
139 meta.maintainers = with maintainers; [ sorki ];
140
141 config = mkIf cfg.enable {
142
143 assertions = singleton {
144 assertion = cfg.chain.type != "random" && cfg.chain.length == null;
145 message = ''
146 Option `programs.proxychains.chain.length`
147 only makes sense with `programs.proxychains.chain.type` = "random".
148 '';
149 };
150
151 programs.proxychains.proxies = mkIf config.services.tor.client.enable
152 {
153 torproxy = mkDefault {
154 enable = true;
155 type = "socks4";
156 host = "127.0.0.1";
157 port = 9050;
158 };
159 };
160
161 environment.etc."proxychains.conf".text = configFile;
162 environment.systemPackages = [ pkgs.proxychains ];
163 };
164
165}