-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodule.nix
More file actions
295 lines (272 loc) · 9.68 KB
/
module.nix
File metadata and controls
295 lines (272 loc) · 9.68 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
flake: { config, pkgs, lib, ... }:
with lib;
let
inherit (flake.packages.${pkgs.stdenv.hostPlatform.system}) p2p-crawler;
cfg = config.services.p2p-crawler;
in
{
options = {
services.p2p-crawler = {
enable = mkEnableOption "p2p-crawler";
tor = {
enable = mkEnableOption "TOR" // { default = true; };
proxy-host = mkOption {
type = types.str;
default = "127.0.0.1";
example = "10.0.0.1";
description = mdDoc "SOCKS5 proxy host for TOR.";
};
proxy-port = mkOption {
type = types.port;
default = 9050;
example = 19050;
description = mdDoc "SOCKS5 proxy port for TOR.";
};
};
i2p = {
enable = mkEnableOption "I2P" // { default = true; };
sam-host = mkOption {
type = types.str;
default = "127.0.0.1";
example = "10.0.0.1";
description = mdDoc "SAM host for I2P.";
};
sam-port = mkOption {
type = types.port;
default = 7656;
example = 17656;
description = mdDoc "SAM port for I2P.";
};
};
schedule = mkOption {
type = types.str;
default = "*-*-* 00,12:00:00 UTC";
example = "daily";
description = mdDoc "Systemd OnCalendar interval for running the crawler.";
};
timestamp = mkOption {
type = types.nullOr types.str;
default = null;
example = "2023-10-26T10:55:00Z";
description = mdDoc "Manually set crawler started timestamp.";
};
node-share = mkOption {
type = types.float;
default = 1.0;
example = 0.05;
description = mdDoc "Share of reachable nodes to ask for peers via getaddr.";
};
workers = mkOption {
type = types.int;
default = 64;
example = 30;
description = mdDoc "Number of crawler coroutines.";
};
delay-start = mkOption {
type = types.int;
default = 10;
example = 5;
description = mdDoc "Seconds to wait before starting the crawler.";
};
reachable-node-history = mkEnableOption "retaining and retrying reachable nodes from previous runs" // { default = true; };
reachable-node-history-max-retries = mkOption {
type = types.int;
default = 3;
example = 10;
description = mdDoc "Maximum number of runs to retain reachable nodes if they're not reachable.";
};
getaddr-attempts = mkOption {
type = types.int;
default = 2;
example = 3;
description = mdDoc "Number of attempts for getaddr requests.";
};
log-level = mkOption {
type = types.str;
default = "INFO";
example = "DEBUG";
description = mdDoc "Log verbosity for console.";
};
result-path = mkOption {
type = types.path;
default = "/home/p2p-crawler/";
example = "/scratch/results/p2p-crawler";
description = mdDoc "Directory for results.";
};
store-debug-log = mkEnableOption "storing the debug log" // { default = true; };
record-addr-data = mkEnableOption "collecting and storing addr data on a per-node basis";
gcs = {
enable = mkEnableOption "storing to GCS";
bucket = mkOption {
type = types.nullOr types.str;
default = null;
example = "bitcoin_p2p_crawler";
description = mdDoc "GCS bucket.";
};
location = mkOption {
type = types.nullOr types.str;
default = null;
example = "foo/bar";
description = mdDoc "Location in GCS bucket.";
};
credentials = mkOption {
type = types.nullOr types.path;
default = null;
example = "secrets/key.json";
description = mdDoc "Path to GCS credentials file.";
};
};
timeout = {
ip = {
connect = mkOption {
type = types.int;
default = 3;
example = 10;
description = mdDoc "Timeout for establishing connections via IPv4 and IPv6.";
};
message = mkOption {
type = types.int;
default = 30;
example = 10;
description = mdDoc "Timeout for replies from peer via IPv4 and IPv6.";
};
getaddr = mkOption {
type = types.int;
default = 70;
example = 10;
description = mdDoc "Max. duration for receiving addr messages via IPv4 and IPv6.";
};
};
tor = {
connect = mkOption {
type = types.int;
default = 120;
example = 10;
description = mdDoc "Timeout for establishing connections via TOR.";
};
message = mkOption {
type = types.int;
default = 40;
example = 10;
description = mdDoc "Timeout for replies from peer via TOR.";
};
getaddr = mkOption {
type = types.int;
default = 90;
example = 10;
description = mdDoc "Max. duration for receiving addr messages via TOR.";
};
};
i2p = {
connect = mkOption {
type = types.int;
default = 30;
example = 10;
description = mdDoc "Timeout for establishing connections via I2P.";
};
message = mkOption {
type = types.int;
default = 80;
example = 10;
description = mdDoc "Timeout for replies from peer via I2P.";
};
getaddr = mkOption {
type = types.int;
default = 170;
example = 10;
description = mdDoc "Max. duration for receiving addr messages via I2P.";
};
};
cjdns = {
connect = mkOption {
type = types.int;
default = 10;
example = 10;
description = mdDoc "Timeout for establishing connections via CJDNS.";
};
message = mkOption {
type = types.int;
default = 30;
example = 10;
description = mdDoc "Timeout for replies from peer via CJDNS.";
};
getaddr = mkOption {
type = types.int;
default = 70;
example = 10;
description = mdDoc "Max. duration for receiving addr messages via CJDNS.";
};
};
};
};
};
config = mkIf cfg.enable {
services.tor = mkIf (cfg.tor != false) {
enable = true;
client.enable = true;
};
services.i2pd = mkIf (cfg.i2p != false) {
enable = true;
proto.sam.enable = true;
};
users = {
users.p2p-crawler = {
isSystemUser = true;
group = "p2p-crawler";
home = "/home/p2p-crawler";
createHome = true;
homeMode = "755";
};
groups.p2p-crawler = { };
};
systemd.timers.p2p-crawler = {
wantedBy = [ "timers.target" ];
timerConfig =
{
OnCalendar = cfg.schedule;
Unit = [ "p2p-crawler.service" ];
};
};
systemd.services.p2p-crawler = {
description = "p2p-crawler";
# service should only run when scheduled, not when system is booted so no
# wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
serviceConfig = {
ExecStart = ''${p2p-crawler}/bin/p2p-crawler \
--node-share ${toString cfg.node-share} \
--delay-start ${toString cfg.delay-start} \
--num-workers ${toString cfg.workers} \
--getaddr-attempts ${toString cfg.getaddr-attempts} \
${if cfg.reachable-node-history then "--reachable-node-history" else "--no-reachable-node-history"} \
--reachable-node-history-max-retries ${toString cfg.reachable-node-history-max-retries} \
--log-level ${cfg.log-level} \
--result-path ${cfg.result-path} \
${if cfg.store-debug-log then "--store-debug-log" else "--no-store-debug-log"} \
${if cfg.record-addr-data then "--record-addr-data" else "--no-record-addr-data"} \
${if cfg.gcs.enable
then "--store-to-gcs ${optionalString (cfg.gcs.bucket != null) "--gcs-bucket ${cfg.gcs.bucket}"} ${optionalString (cfg.gcs.location != null) "--gcs-location ${cfg.gcs.location}"} ${optionalString (cfg.gcs.credentials != null) "--gcs-credentials ${cfg.gcs.credentials}"}"
else "--no-store-to-gcs"} \
${optionalString (cfg.timestamp != null) "--timestamp=${cfg.timestamp}" } \
${optionalString (cfg.tor.enable != false) "--tor-proxy-host=${cfg.tor.proxy-host} --tor-proxy-port=${toString cfg.tor.proxy-port}" } \
${optionalString (cfg.i2p.enable != false) "--i2p-sam-host=${cfg.i2p.sam-host} --i2p-sam-port=${toString cfg.i2p.sam-port}" } \
--ip-connect-timeout ${toString cfg.timeout.ip.connect} \
--ip-message-timeout ${toString cfg.timeout.ip.message} \
--ip-getaddr-timeout ${toString cfg.timeout.ip.getaddr} \
--tor-connect-timeout ${toString cfg.timeout.tor.connect} \
--tor-message-timeout ${toString cfg.timeout.tor.message} \
--tor-getaddr-timeout ${toString cfg.timeout.tor.getaddr} \
--i2p-connect-timeout ${toString cfg.timeout.i2p.connect} \
--i2p-message-timeout ${toString cfg.timeout.i2p.message} \
--i2p-getaddr-timeout ${toString cfg.timeout.i2p.getaddr}
'';
MemoryDenyWriteExecute = true;
ReadWriteDirectories = "/home/p2p-crawler/";
DynamicUser = true;
User = "p2p-crawler";
Group = "p2p-crawler";
};
};
};
}