This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
prifi.sh
executable file
·462 lines (358 loc) · 16 KB
/
prifi.sh
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env bash
# ************************************
# PriFi all-in-one startup script
# ************************************
# author : Ludovic Barman
# email : ludovic.barman@gmail.com
# belongs to : the PriFi project
# <github.com/dedis/prifi>
# ************************************
# variables that you might change often
dbg_lvl=3 # 1=less verbose, 3=more verbose. goes up to 5, but then prints the SDA's message (network framework)
try_use_real_identities="false" # if "true", will try to use "self-generated" public/private key as a replacement for the dummy keys
# we generated for you. It asks you if it does not find real keys. If false, will always use the dummy keys.
colors="true" # if "false", the output of PriFi (and this script) will be in black-n-white
socksServer1Port=8080 # the port for the SOCKS-Server-1 (part of the PriFi client)
socksServer2Port=8090 # the port to attempt connect to (from the PriFi relay) for the SOCKS-Server-2
# notes : see <https://github.com/dedis/prifi/blob/master/README_architecture.md>
all_localhost_n_clients=3 # number of clients to start in the "all-localhost" script
# default file names :
prifi_file="prifi.toml" # default name for the prifi config file (contains prifi-specific settings)
identity_file="identity.toml" # default name for the identity file (contains public + private key)
group_file="group.toml" # default name for the group file (contains public keys + address of other nodes)
# location of the buildable (go build) prifi file :
bin_file="$GOPATH/src/github.com/dedis/prifi/sda/app/prifi.go"
# we have two "identities" directory. The second one is empty unless you generate your own keys with "gen-id"
configdir="config"
defaultIdentitiesDir="identities_default" # in $configdir
realIdentitiesDir="identities_real" # in $configdir
sleeptime_between_spawns=1 # time in second between entities launch in all-localhost part
source "helpers.lib.sh"
# ------------------------
# HELPER FUNCTIONS
# ------------------------
print_usage() {
echo
echo -e "PriFi, a tracking-resistant protocol for local-area anonymity"
echo
echo -e "Usage: run-prifi.sh ${highlightOn}role/operation [params]${highlightOff}"
echo -e " ${highlightOn}role${highlightOff}: client, relay, trustee (interactive); relay-d, trustee-d (same, but in deamon)"
echo -e " ${highlightOn}operation${highlightOff}: install, all-localhost, gen-id, kill-d"
echo -e " ${highlightOn}params${highlightOff} for role ${highlightOn}relay${highlightOff}, ${highlightOn}relay-d${highlightOff}: [socks_server_port] (optional, numeric)"
echo -e " ${highlightOn}params${highlightOff} for role ${highlightOn}trustee${highlightOff}, ${highlightOn}trustee-d${highlightOff}: id (required, numeric)"
echo -e " ${highlightOn}params${highlightOff} for role ${highlightOn}client${highlightOff}: id (required, numeric), [prifi_socks_server_port] (optional, numeric)"
echo -e " ${highlightOn}params${highlightOff} for operation ${highlightOn}install${highlightOff}: none"
echo -e " ${highlightOn}params${highlightOff} for operation ${highlightOn}all-localhost${highlightOff}: none"
echo -e " ${highlightOn}params${highlightOff} for operation ${highlightOn}gen-id${highlightOff}: none"
echo -e " ${highlightOn}params${highlightOff} for operation ${highlightOn}kill-d${highlightOff}: none"
echo
echo -e "Man-page:"
echo -e " ${highlightOn}install${highlightOff}: get the dependencies, and tests the setup"
echo -e " ${highlightOn}relay${highlightOff}: starts a PriFi relay"
echo -e " ${highlightOn}relay-d${highlightOff}: starts a PriFi relay in background"
echo -e " ${highlightOn}trustee${highlightOff}: starts a PriFi trustee, using the config file trustee${highlightOn}id${highlightOff}"
echo -e " ${highlightOn}trustee-d${highlightOff}: starts a PriFi trustee, using the config file trustee${highlightOn}id${highlightOff} in background"
echo -e " ${highlightOn}client${highlightOff}: starts a PriFi client, using the config file client${highlightOn}id${highlightOff}"
echo -e " ${highlightOn}all-localhost${highlightOff}: starts a Prifi relay, a trustee, three clients all on localhost"
echo -e " ${highlightOn}gen-id${highlightOff}: interactive creation of identity.toml"
echo -e " ${highlightOn}kill-d${highlightOff}: kills all prifi-deamons (relay-d and trustee-d's)"
echo -e " Lost ? read https://github.com/dedis/prifi/README.md"
}
# ------------------------
# MAIN SWITCH
# ------------------------
# $1 is operation : "install", "relay", "client", "trustee", "sockstest", "all-localhost", "clean", "gen-id"
case $1 in
install|Install|INSTALL)
echo -n "Testing for GO... "
test_go
echo -e "$okMsg"
echo -n "Getting all go packages... "
cd sda/app; go get -u -v ./...
cd ../..
cd socks; go get -u -v ./...
cd ..
cd prifi-lib; go get -u -v ./...
cd ..
cd prifi-mobile; go get -u -v ./...
cd ..
# this package somehow fails to install itself
go get -u -v github.com/montanaflynn/stats
echo -e "$okMsg"
;;
relay|Relay|RELAY)
#test for proper setup
test_go
# the 2nd argument can replace the port number
if [ "$#" -eq 2 ]; then
test_digit "$2" 2
socksServer2Port="$2"
fi
#specialize the config file (we use the dummy folder, and maybe we replace with the real folder after)
prifi_file2="$configdir/$prifi_file"
identity_file2="$configdir/$defaultIdentitiesDir/relay/$identity_file"
group_file2="$configdir/$defaultIdentitiesDir/relay/$group_file"
#we we want to, try to replace with the real folder
if [ "$try_use_real_identities" = "true" ]; then
if [ -f "$configdir/$realIdentitiesDir/relay/$identity_file" ] && [ -f "$configdir/$defaultIdentitiesDir/relay/$group_file" ]; then
echo -e "$okMsg Found real identities (in $configdir/$realIdentitiesDir/relay/), using those."
identity_file2="$configdir/$realIdentitiesDir/relay/$identity_file"
group_file2="$configdir/$realIdentitiesDir/relay/$group_file"
else
echo -e "$warningMsg Trying to use real identities, but does not exists for relay (in $configdir/$realIdentitiesDir/relay/). Falling back to pre-generated ones."
fi
else
echo -e "$warningMsg using pre-created identities. Set \"try_use_real_identities\" to True in real deployements."
fi
# test that all files exists
test_files
#run PriFi in relay mode
DEBUG_COLOR="$colors" go run "$bin_file" --cothority_config "$identity_file2" --group "$group_file2" -d "$dbg_lvl" --prifi_config "$prifi_file2" --port "$socksServer1Port" --port_client "$socksServer2Port" relay
;;
trustee|Trustee|TRUSTEE)
trusteeId="$2"
#test for proper setup
test_go
if [ "$#" -lt 2 ]; then
echo -e "$errorMsg parameter 2 need to be the trustee id."
exit 1
fi
test_digit "$trusteeId" 2
#specialize the config file (we use the dummy folder, and maybe we replace with the real folder after)
prifi_file2="$configdir/$prifi_file"
identity_file2="$configdir/$defaultIdentitiesDir/trustee$trusteeId/$identity_file"
group_file2="$configdir/$defaultIdentitiesDir/trustee$trusteeId/$group_file"
#we we want to, try to replace with the real folder
if [ "$try_use_real_identities" = "true" ]; then
if [ -f "$configdir/$realIdentitiesDir/trustee$trusteeId/$identity_file" ] && [ -f "$configdir/$defaultIdentitiesDir/trustee$trusteeId/$group_file" ]; then
echo -e "$okMsg Found real identities (in $configdir/$realIdentitiesDir/trustee$trusteeId/), using those."
identity_file2="$configdir/$realIdentitiesDir/trustee$trusteeId/$identity_file"
group_file2="$configdir/$realIdentitiesDir/trustee$trusteeId/$group_file"
else
echo -e "$warningMsg Trying to use real identities, but does not exists for trustee $trusteeId (in $configdir/$realIdentitiesDir/trustee$trusteeId/). Falling back to pre-generated ones."
fi
else
echo -e "$warningMsg using pre-created identities. Set \"try_use_real_identities\" to True in real deployements."
fi
# test that all files exists
test_files
#run PriFi in relay mode
DEBUG_COLOR="$colors" go run "$bin_file" --cothority_config "$identity_file2" --group "$group_file2" -d "$dbg_lvl" --prifi_config "$prifi_file2" --port "$socksServer1Port" --port_client "$socksServer2Port" trustee
;;
client|Client|CLIENT)
clientId="$2"
#test for proper setup
test_go
if [ "$#" -lt 2 ]; then
echo -e "$errorMsg parameter 2 need to be the client id."
exit 1
fi
test_digit "$clientId" 2
# the 3rd argument can replace the port number
if [ "$#" -eq 3 ]; then
test_digit "$3" 3
socksServer1Port="$3"
fi
#specialize the config file (we use the dummy folder, and maybe we replace with the real folder after)
prifi_file2="$configdir/$prifi_file"
identity_file2="$configdir/$defaultIdentitiesDir/client$clientId/$identity_file"
group_file2="$configdir/$defaultIdentitiesDir/client$clientId/$group_file"
#we we want to, try to replace with the real folder
if [ "$try_use_real_identities" = "true" ]; then
if [ -f "$configdir/$realIdentitiesDir/client$clientId/$identity_file" ] && [ -f "$configdir/$realIdentitiesDir/client$clientId/$group_file" ]; then
echo -e "$okMsg Found real identities (in $configdir/$realIdentitiesDir/client$clientId/), using those."
identity_file2="$configdir/$realIdentitiesDir/client$clientId/$identity_file"
group_file2="$configdir/$realIdentitiesDir/client$clientId/$group_file"
else
echo -e "$warningMsg Trying to use real identities, but does not exists for client $clientId (in $configdir/$realIdentitiesDir/client$clientId/). Falling back to pre-generated ones."
fi
else
echo -e "$warningMsg using pre-created identities. Set \"try_use_real_identities\" to True in real deployements."
fi
# test that all files exists
test_files
#run PriFi in relay mode
DEBUG_COLOR="$colors" go run "$bin_file" --cothority_config "$identity_file2" --group "$group_file2" -d "$dbg_lvl" --prifi_config "$prifi_file2" --port "$socksServer1Port" --port_client "$socksServer2Port" client
;;
localhost|Localhost|LOCALHOST|all-localhost|All-Localhost|ALL-LOCALHOST)
thisScript="$0"
if [ "$try_use_real_identities" = "true" ]; then
echo -en "$warningMsg, try_use_real_identities set to true, but this is incompatible to all-localhost mode. Switching to false ..."
sed -i -e 's/try_use_real_identities=\"true\"/try_use_real_identities=\"false\"/g' "$thisScript"
echo -e "$okMsg"
fi
#test for proper setup
test_go
#test if a socks proxy is already running (needed for relay), or start ours
socks=$(netstat -tunpl 2>/dev/null | grep "$socksServer2Port" | wc -l)
if [ "$socks" -ne 1 ]; then
echo -n "Socks proxy not running, starting it... "
cd socks && ./run-socks-proxy.sh "$socksServer2Port" > ../socks.log 2>&1 &
SOCKSPID=$!
echo -e "$okMsg"
fi
echo -n "Starting relay... "
"$thisScript" relay > relay.log 2>&1 &
RELAYPID=$!
THISPGID=$(ps -o pgid= "$RELAYPID" | sed -e "s/^ //")
echo -e "$okMsg"
sleep "$sleeptime_between_spawns"
echo -n "Starting trustee 0... "
"$thisScript" trustee 0 > trustee0.log 2>&1 &
echo -e "$okMsg"
sleep "$sleeptime_between_spawns"
for i in `seq 0 $(($all_localhost_n_clients - 1))`
do
port=$(($socksServer1Port + $i))
log="client$i.log"
echo -n "Starting client $i... (SOCKS on :$port) "
"$thisScript" client "$i" "$port" > "$log" 2>&1 &
echo -e "$okMsg"
sleep "$sleeptime_between_spawns"
done
read -p "PriFi deployed. Press [enter] to kill all..." key
echo "Gonna run kill -TERM -- -\"$THISPGID\""
kill -TERM -- -"$THISPGID"
;;
gen-id|Gen-Id|GEN-ID)
echo -e "Going to generate private/public keys (named ${highlightOn}identity.toml${highlightOff})..."
read -p "Do you want to generate it for [r]elay, [c]lient, or [t]trustee ? " key
path=""
case "$key" in
r|R)
path="relay"
;;
t|T)
read -p "Do you want to generate it for trustee [0] or [1] (or more - enter digit) ? " key2
test_digit "$key2" 1
pathSource="trustee0"
path="trustee$key2"
;;
c|C)
read -p "Do you want to generate it for client [0],[1] or [2] (or more - enter digit) ? " key2
test_digit "$key2" 1
pathSource="client0"
path="client$key2"
;;
*)
echo -e "$errorMsg did not understand."
exit 1
;;
esac
pathReal="$configdir/$realIdentitiesDir/$path/"
pathDefault="$configdir/$defaultIdentitiesDir/$pathSource/"
echo -e "Gonna generate ${highlightOn}identity.toml${highlightOff} in ${highlightOn}$pathReal${highlightOff}"
#generate identity.toml
DEBUG_COLOR="$colors" go run "$bin_file" --default_path "$pathReal" gen-id
if [ ! -f "${pathReal}group.toml" ]; then
#now group.toml
echo -n "Done ! now copying group.toml from identities_default/ to identity_real/..."
cp "${pathDefault}/group.toml" "${pathReal}group.toml"
echo -e "$okMsg"
echo -e "Please edit ${highlightOn}$pathReal/group.toml${highlightOff} to the correct values."
else
echo -e "Group file ${highlightOn}$pathReal/group.toml${highlightOff} already exists, not overwriting! you might want to check that the contents are correct."
fi
;;
relay-d)
#test for proper setup
test_go
#if running, kill the previous one
if [ -f .GPID_RELAY ]; then
gpid=$(cat .GPID_RELAY)
echo -e "PriFi relay already running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_RELAY
fi
thisScript="$0"
echo -n "Starting relay... "
"$thisScript" relay > relay.log 2>&1 &
RELAYPID=$!
RELAYPGID=$(ps -o pgid= "$RELAYPID")
echo -e "$okMsg"
echo -e "PriFi relay deployed, PGID $RELAYPGID. Kill with \"kill -TERM -- -$RELAYPGID\""
rm -f .GPID_RELAY
echo $RELAYPGID > .GPID_RELAY
;;
trustee-d)
#test for proper setup
test_go
#if running, kill the previous one
if [ -f .GPID_TRUSTEE ]; then
gpid=$(cat .GPID_TRUSTEE)
echo -e "PriFi trustee already running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_TRUSTEE
fi
thisScript="$0"
trusteeId="$2"
if [ "$#" -lt 2 ]; then
echo -e "$errorMsg parameter 2 need to be the trustee id."
exit 1
fi
test_digit "$trusteeId" 2
echo -n "Starting trustee $trusteeId... "
"$thisScript" trustee "$trusteeId" > trustee${trusteeId}.log 2>&1 &
TRUSTEEPID=$!
TRUSTEEGPID=$(ps -o pgid= "$TRUSTEEPID")
echo -e "$okMsg"
echo -e "PriFi trustee deployed, PGID $TRUSTEEGPID. Kill with \"kill -TERM -- -$TRUSTEEGPID\""
rm -f .GPID_TRUSTEE
echo $TRUSTEEGPID > .GPID_TRUSTEE
;;
socks-d)
#if running, kill the previous one
if [ -f .GPID_SOCKSEXIT ]; then
gpid=$(cat .GPID_SOCKSEXIT)
echo -e "PriFi socks exit already running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_SOCKSEXIT
fi
echo -n "Starting SOCKS Server... "
cd socks && ./run-socks-proxy.sh "$socksServer2Port" > ../socks.log 2>&1 &
SOCKSPID=$!
SOCKSPGID=$(ps -o pgid= "$SOCKSPID")
echo -e "$okMsg"
echo -e "PriFi socks exit deployed, PGID $SOCKSPGID. Kill with \"kill -TERM -- -$SOCKSPGID\""
rm -f .GPID_SOCKSEXIT
echo $SOCKSPGID > .GPID_SOCKSEXIT
;;
kill-d)
if [ -f .GPID_RELAY ]; then
gpid=$(cat .GPID_RELAY)
echo -e "PriFi relay running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_RELAY
else
echo -e "PriFi relay not running, ignoring"
fi
if [ -f .GPID_TRUSTEE ]; then
gpid=$(cat .GPID_TRUSTEE)
echo -e "PriFi trustee running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_TRUSTEE
else
echo -e "PriFi trustee not running, ignoring"
fi
if [ -f .GPID_SOCKSEXIT ]; then
gpid=$(cat .GPID_SOCKSEXIT)
echo -e "PriFi socks exit running, killing old instance with kill -TERM -- -$gpid"
kill -TERM -- -"$gpid"
rm -f .GPID_SOCKSEXIT
else
echo -e "PriFi socks-exit not running, ignoring"
fi
;;
web-server)
cd web
go run index.go &
;;
clean|Clean|CLEAN)
echo -n "Cleaning local log files... "
rm *.log 1>/dev/null 2>&1
echo -e "$okMsg"
;;
*)
print_usage
;;
esac