]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/tor.cgi
tor.cgi: Use own location functions.
[ipfire-2.x.git] / html / cgi-bin / tor.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013-2019 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/location-functions.pl";
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 # Init libloc database connection.
34 my $db_handle = &Location::Functions::init();
35
36 #workaround to suppress a warning when a variable is used only once
37 my @dummy = ( ${Header::colouryellow} );
38 undef (@dummy);
39
40 my @bandwidth_limits = (
41 1000 * 1024, # 1 GBit/s
42 500 * 1024,
43 200 * 1024,
44 100 * 1024, # 100 MBit/s
45 64 * 1024,
46 50 * 1024,
47 25 * 1024,
48 20 * 1024,
49 16 * 1024,
50 10 * 1024,
51 8 * 1024,
52 4 * 1024,
53 2 * 1024,
54 1024 # 1 MBit/s
55 );
56 my @accounting_periods = ('daily', 'weekly', 'monthly');
57
58 my $TOR_CONTROL_PORT = 9051;
59
60 my $string=();
61 my $memory=();
62 my @memory=();
63 my @pid=();
64 my @tor=();
65 sub daemonstats
66 {
67 $memory = 0;
68 # for pid and memory
69 open(FILE, '/usr/local/bin/addonctrl tor status | ');
70 @tor = <FILE>;
71 close(FILE);
72 $string = join("", @tor);
73 $string =~ s/[a-z_]//gi;
74 $string =~ s/\[[0-1]\;[0-9]+//gi;
75 $string =~ s/[\(\)\.]//gi;
76 $string =~ s/ //gi;
77 $string =~ s/\e//gi;
78 @pid = split(/\s/,$string);
79 if (open(FILE, "/proc/$pid[0]/statm")){
80 my $temp = <FILE>;
81 @memory = split(/ /,$temp);
82 close(FILE);
83 }
84 $memory+=$memory[0];
85 }
86 daemonstats();
87
88 our %netsettings = ();
89 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
90
91 our %color = ();
92 our %mainsettings = ();
93 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
94 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
95
96 our %settings = ();
97
98 $settings{'TOR_ENABLED'} = 'off';
99 $settings{'TOR_SOCKS_PORT'} = 9050;
100 $settings{'TOR_EXIT_COUNTRY'} = '';
101 $settings{'TOR_USE_EXIT_NODES'} = '';
102 $settings{'TOR_ALLOWED_SUBNETS'} = "$netsettings{'GREEN_NETADDRESS'}\/$netsettings{'GREEN_NETMASK'}";
103 if (&Header::blue_used()) {
104 $settings{'TOR_ALLOWED_SUBNETS'} .= ",$netsettings{'BLUE_NETADDRESS'}\/$netsettings{'BLUE_NETMASK'}";
105 }
106
107 $settings{'TOR_RELAY_ENABLED'} = 'off';
108 $settings{'TOR_RELAY_MODE'} = 'relay';
109 $settings{'TOR_RELAY_ADDRESS'} = '';
110 $settings{'TOR_RELAY_PORT'} = 9001;
111 $settings{'TOR_RELAY_DIRPORT'} = 0;
112 $settings{'TOR_RELAY_NICKNAME'} = '';
113 $settings{'TOR_RELAY_CONTACT_INFO'} = '';
114 $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0;
115 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
116 $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0;
117 $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily';
118
119 $settings{'ACTION'} = '';
120
121 my $errormessage = '';
122 my $warnmessage = '';
123
124 &Header::showhttpheaders();
125
126 # Get GUI values.
127 &Header::getcgihash(\%settings);
128
129 # Create tor command connection.
130 our $torctrl = &TorConnect();
131
132 # Toggle enable/disable field.
133 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
134 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
135 if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) {
136 $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}";
137 }
138 }
139
140 if (!&General::validport($settings{'TOR_SOCKS_PORT'})) {
141 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
142 }
143
144 if (!&General::validport($settings{'TOR_RELAY_PORT'})) {
145 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
146 }
147 if ($settings{'TOR_RELAY_DIRPORT'} ne '0') {
148 if (!&General::validport($settings{'TOR_RELAY_DIRPORT'})) {
149 $errormessage = "$Lang::tr{'tor errmsg invalid directory port'}: $settings{'TOR_RELAY_DIRPORT'}";
150 }
151 }
152
153 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
154 if ((!&General::validfqdn($settings{'TOR_RELAY_ADDRESS'})) && (!&General::validip($settings{'TOR_RELAY_ADDRESS'}))) {
155 $errormessage = "$Lang::tr{'tor errmsg invalid relay address'}: $settings{'TOR_RELAY_ADDRESS'}";
156 }
157 }
158
159 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
160 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
161 }
162
163 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
164 $settings{'TOR_ALLOWED_SUBNETS'} = "";
165 foreach (@temp) {
166 s/^\s+//g; s/\s+$//g;
167 if ($_) {
168 unless (&General::validipandmask($_)) {
169 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
170 }
171 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
172 }
173 }
174
175 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
176 $settings{'TOR_USE_EXIT_NODES'} = "";
177 foreach (@temp) {
178 s/^\s+//g; s/\s+$//g;
179 if ($_) {
180 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
181 }
182 }
183
184 # Burst bandwidth must be less or equal to bandwidth rate.
185 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
186 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
187
188 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
189 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
190 }
191
192 if ($errormessage eq '') {
193 # Write configuration settings to file.
194 &General::writehash("${General::swroot}/tor/settings", \%settings);
195
196 # Update configuration files.
197 &BuildConfiguration();
198 }
199 } else {
200 # Load settings from file.
201 &General::readhash("${General::swroot}/tor/settings", \%settings);
202 }
203
204 &showMainBox();
205
206 # Close Tor control connection.
207 &TorClose($torctrl);
208
209 # Functions
210
211 sub showMainBox() {
212 my %checked = ();
213 my %selected = ();
214
215 $checked{'TOR_ENABLED'}{'on'} = '';
216 $checked{'TOR_ENABLED'}{'off'} = '';
217 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
218
219 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
220 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
221 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
222
223 &Header::openpage($Lang::tr{'tor configuration'}, 1, '');
224 &Header::openbigbox('100%', 'left', '', $errormessage);
225
226 if ($errormessage) {
227 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
228 print "<font class='base'>$errormessage&nbsp;</font>\n";
229 &Header::closebox();
230 }
231
232 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
233
234 &Header::openbox('100%', 'center', $Lang::tr{'tor'});
235
236
237 if ( ($memory != 0) && (@pid[0] ne "///") ){
238 print "<table width='95%' cellspacing='0' class='tbl'>";
239 print "<tr><th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'tor service'}</strong></th></tr>";
240 print "<tr><td class='base'>$Lang::tr{'tor daemon'}</td>";
241 print "<td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td></tr>";
242 print "<tr><td class='base'></td>";
243 print "<td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>";
244 print "<td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td></tr>";
245 print "<tr><td class='base'></td>";
246 print "<td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>";
247 print "<td bgcolor='$color{'color22'}' align='center'>$memory KB</td></tr>";
248 print "</table>";
249 } else {
250 print "<table width='95%' cellspacing='0' class='tbl'>";
251 print "<tr><th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'tor service'}</strong></th></tr>";
252 print "<tr><td class='base'>$Lang::tr{'tor daemon'}</td>";
253 print "<td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td></tr>";
254 print "</table>";
255 }
256
257 &Header::closebox();
258
259 &Header::openbox('100%', 'center', $Lang::tr{'tor configuration'});
260
261 print <<END;
262 <table width='95%'>
263 <tr>
264 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor common settings'}</b></td>
265 </tr>
266 <tr>
267 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
268 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
269 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
270 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
271 </tr>
272 <tr>
273 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
274 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
275 <td width='25%' class='base'></td>
276 <td width='20%'></td>
277 </tr>
278 </table>
279 END
280
281 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
282 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
283
284 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
285 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
286
287 print <<END;
288 <br>
289 <br>
290
291 <table width='95%'>
292 <tr>
293 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor acls'}</b></td>
294 </tr>
295 <tr>
296 <td colspan='2' class='base' width='55%'>
297 $Lang::tr{'tor allowed subnets'}:
298 </td>
299 <td colspan='2' width='45%'></td>
300 </tr>
301 <tr>
302 <td colspan='2' class='base' width='55%'>
303 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
304 </td>
305 <td colspan='2' width='45%'></td>
306 </tr>
307 </table>
308
309 <br>
310 <br>
311
312 <table width='95%'>
313 <tr>
314 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor exit nodes'}</b></td>
315 </tr>
316 <tr>
317 <td colspan='2' class='base' width='55%'></td>
318 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
319 </tr>
320 <tr>
321 <td width='50%' colspan='2'>
322 <select name='TOR_EXIT_COUNTRY'>
323 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
324 END
325
326 my @country_codes = &Location::database_countries();
327 foreach my $country_code (@country_codes) {
328 # Convert country code into upper case format.
329 $country_code = uc($country_code);
330
331 # Get country name.
332 my $country_name = &Location::Functions::get_country_name($country_code);
333
334 print "<option value='$country_code'";
335
336 if ($settings{'TOR_EXIT_COUNTRY'} eq $country_code) {
337 print " selected";
338 }
339
340 print ">$country_name ($country_code)</option>\n";
341 }
342
343 print <<END;
344 </select>
345 </td>
346 <td width='50%' colspan='2'>
347 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
348 </td>
349 </tr>
350 </table>
351 END
352
353 &Header::closebox();
354
355 # Tor relay box
356 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
357 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
358 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
359 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
360 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
361
362 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
363 foreach (@bandwidth_limits) {
364 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
365 }
366 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
367
368 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
369 foreach (@bandwidth_limits) {
370 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
371 }
372 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
373
374 foreach (@accounting_periods) {
375 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
376 }
377 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
378
379 &Header::openbox('100%', 'center', $Lang::tr{'tor relay configuration'});
380
381 print <<END;
382 <table width='95%'>
383 <tr>
384 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
385 <td width='30%'>
386 <select name='TOR_RELAY_MODE'>
387 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
388 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
389 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
390 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
391 </select>
392 </td>
393 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}:</td>
394 <td width='20%'>
395 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' maxlength='19' />
396 </td>
397 </tr>
398 <tr>
399 <td width='25%' class='base'>$Lang::tr{'tor relay address'}:</td>
400 <td width='30%'>
401 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
402 </td>
403 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
404 <td width='20%'>
405 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
406 </td>
407 </tr>
408 <tr>
409 <td width='25%'>&nbsp;</td>
410 <td width='30%'>&nbsp;</td>
411 <td width='25%' class='base'>$Lang::tr{'tor directory port'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
412 <td width='20%'>
413 <input type='text' name='TOR_RELAY_DIRPORT' value='$settings{'TOR_RELAY_DIRPORT'}' size='5' />&nbsp;$Lang::tr{'tor 0 = disabled'}
414 </td>
415 </tr>
416 <tr>
417 <td width='25%' class='base'>$Lang::tr{'tor contact info'}:</td>
418 <td width='75%' colspan='3'>
419 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' style='width: 98%;' />
420 </td>
421 </tr>
422 </table>
423
424 <br>
425
426 <table width='95%'>
427 <tr>
428 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
429 </tr>
430 <tr>
431 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
432 <td width='30%' class='base'>
433 <select name='TOR_RELAY_BANDWIDTH_RATE'>
434 END
435
436 foreach (@bandwidth_limits) {
437 if ($_ >= 1024) {
438 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." Mbit/s</option>\n";
439 } else {
440 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kbit/s</option>\n";
441 }
442 }
443
444 print <<END;
445 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
446 </select>
447 </td>
448 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
449 <td width='20%'>
450 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
451 </td>
452 </tr>
453 <tr>
454 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
455 <td width='20%' class='base'>
456 <select name='TOR_RELAY_BANDWIDTH_BURST'>
457 END
458
459 foreach (@bandwidth_limits) {
460 if ($_ >= 1024) {
461 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." Mbit/s</option>\n";
462 } else {
463 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kbit/s</option>\n";
464 }
465 }
466 print <<END;
467 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
468 </select>
469 </td>
470 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
471 <td width='20%'>
472 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
473 END
474
475 foreach (@accounting_periods) {
476 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
477 }
478
479 print <<END;
480 </select>
481 </td>
482 </tr>
483 </table>
484 END
485
486 &Header::closebox();
487
488 print <<END;
489 <table width='95%'>
490 <tr>
491 <td><img src='/blob.gif' align='top' alt='*' />&nbsp;<font class='base'>$Lang::tr{'required field'}</font></td>
492 <td align='right'>&nbsp;</td>
493 </tr>
494 </table>
495
496 <hr>
497
498 <table width='95%'>
499 <tr>
500 <td>&nbsp;</td>
501 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
502 <td>&nbsp;</td>
503 </tr>
504 </table>
505 END
506
507 # If we have a control connection, show the stats.
508 if ($torctrl) {
509 &Header::openbox('100%', 'center', $Lang::tr{'tor stats'});
510
511 my @traffic = &TorTrafficStats($torctrl);
512
513 if (@traffic) {
514 print <<END;
515 <table width='95%'>
516 END
517
518 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
519 my $fingerprint = &TorRelayFingerprint($torctrl);
520 if ($fingerprint) {
521 print <<END;
522 <tr>
523 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
524 <td width='60%'>
525 <a href='https://metrics.torproject.org/rs.html#details/$fingerprint' target='_blank'>$fingerprint</a>
526 </td>
527 </tr>
528 END
529 }
530 }
531
532 my $address = TorGetInfo($torctrl, "address");
533 if ($address) {
534 print <<END;
535 <tr>
536 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
537 <td width='60%'>$address</td>
538 </tr>
539 END
540 }
541
542 print <<END;
543 <tr>
544 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
545 END
546 print "<td width='60%'>" . &FormatBytes($traffic[0]) ."/". &FormatBytes($traffic[1]) . "</td>";
547 print <<END;
548 </tr>
549 </table>
550 END
551 }
552
553 my $accounting = &TorAccountingStats($torctrl);
554 if ($accounting) {
555 print <<END;
556 <table width='95%'>
557 <tr>
558 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
559 </tr>
560 END
561
562 if ($accounting->{'hibernating'} eq "hard") {
563 print <<END;
564 <tr>
565 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
566 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
567 </td>
568 </tr>
569 END
570 } elsif ($accounting->{'hibernating'} eq "soft") {
571 print <<END;
572 <tr>
573 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
574 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
575 </td>
576 </tr>
577 END
578 }
579
580 print <<END;
581 <tr>
582 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
583 <td width='60%'>
584 $accounting->{'interval-start'} - $accounting->{'interval-end'}
585 </td>
586 </tr>
587 <tr>
588 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
589 <td width='60%'>
590 END
591
592 print &FormatBytes($accounting->{'bytes_read'}) . "/" . &FormatBytes($accounting->{'bytes_written'});
593 print " (" . &FormatBytes($accounting->{'bytes-left_read'}) . "/" . &FormatBytes($accounting->{'bytes-left_written'});
594 print " $Lang::tr{'tor accounting bytes left'})";
595
596 print <<END;
597 </td>
598 </tr>
599 </table>
600 END
601 }
602
603 my @nodes = &TorORConnStatus($torctrl);
604 if (@nodes) {
605 my $nodes_length = scalar @nodes;
606 print <<END;
607 <table width='95%'>
608 <tr>
609 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
610 <td width='60%' colspan='2'>($nodes_length)</td>
611 </tr>
612 END
613
614 foreach my $node (@nodes) {
615 print <<END;
616 <tr>
617 <td width='40%'>
618 <a href='https://metrics.torproject.org/rs.html#details/$node->{'fingerprint'}' target='_blank'>
619 $node->{'name'}
620 </a>
621 </td>
622 <td width='30%'>
623 END
624
625 if (exists($node->{'country_code'})) {
626 # Get the flag icon of the country.
627 my $flag_icon = &Location::Functions::get_flag_icon($node->{'country_code'});
628
629 # Check if a flag for the given country is available.
630 if ($flag_icon) {
631 print "<a href='country.cgi#$node->{'country_code'}'><img src='$flag_icon' border='0' align='absmiddle' alt='$node->{'country_code'}'></a>";
632 } else {
633 print "<img src='/images/flags/blank.png' border='0' align='absmiddle'/>";
634 }
635 }
636
637 print <<END;
638 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
639 </td>
640 <td width='30%' align='right'>
641 ~$node->{'bandwidth_string'}
642 </td>
643 </tr>
644 END
645 }
646 print "</table>";
647 }
648
649 &Header::closebox();
650 }
651
652 print "</form>\n";
653
654 &Header::closebigbox();
655 &Header::closepage();
656 }
657
658 sub BuildConfiguration() {
659 my %settings = ();
660 &General::readhash("${General::swroot}/tor/settings", \%settings);
661
662 my $torrc = "${General::swroot}/tor/torrc";
663
664 open(FILE, ">$torrc");
665
666 # Global settings.
667 print FILE "ControlPort $TOR_CONTROL_PORT\n";
668
669 if ($settings{'TOR_ENABLED'} eq 'on') {
670 my $strict_nodes = 0;
671
672 print FILE "SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
673
674 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
675 foreach (@subnets) {
676 print FILE "SocksPolicy accept $_\n" if (&General::validipandmask($_));
677 }
678 print FILE "SocksPolicy reject *\n" if (@subnets);
679
680 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
681 $strict_nodes = 1;
682
683 print FILE "ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
684 }
685
686 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
687 $strict_nodes = 1;
688
689 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
690 foreach (@nodes) {
691 print FILE "ExitNode $_\n";
692 }
693 }
694
695 if ($strict_nodes > 0) {
696 print FILE "StrictNodes 1\n";
697 }
698 }
699
700 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
701 # Reject access to private networks.
702 print FILE "ExitPolicyRejectPrivate 1\n";
703
704 print FILE "ORPort $settings{'TOR_RELAY_PORT'}\n";
705
706 if ($settings{'TOR_RELAY_DIRPORT'} ne '0') {
707 print FILE "DirPort $settings{'TOR_RELAY_DIRPORT'}\n";
708 }
709
710 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
711 print FILE "Address $settings{'TOR_RELAY_ADDRESS'}\n";
712 }
713
714 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
715 print FILE "Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
716 }
717
718 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
719 print FILE "ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
720 }
721
722 # Limit to bridge mode.
723 my $is_bridge = 0;
724
725 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
726 $is_bridge++;
727
728 # Private bridge.
729 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
730 $is_bridge++;
731
732 print FILE "PublishServerDescriptor 0\n";
733
734 # Exit node.
735 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
736 print FILE "ExitPolicy accept *:*\n";
737
738 # Relay only.
739 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
740 print FILE "ExitPolicy reject *:*\n";
741 }
742
743 if ($is_bridge > 0) {
744 print FILE "BridgeRelay 1\n";
745 print FILE "Exitpolicy reject *:*\n";
746 }
747
748 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
749 print FILE "RelayBandwidthRate ";
750 print FILE $settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
751 print FILE " KB\n";
752
753 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
754 print FILE "RelayBandwidthBurst ";
755 print FILE $settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
756 print FILE " KB\n";
757 }
758 }
759
760 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
761 print FILE "AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
762
763 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
764 print FILE "AccountingStart day 00:00\n";
765 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
766 print FILE "AccountingStart week 1 00:00\n";
767 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
768 print FILE "AccountingStart month 1 00:00\n";
769 }
770 }
771 }
772
773 close(FILE);
774
775 # Restart the service.
776 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
777 system("/usr/local/bin/torctrl restart &>/dev/null");
778 } else {
779 system("/usr/local/bin/torctrl stop &>/dev/null");
780 }
781 # Update pid and memory
782 daemonstats();
783 }
784
785 sub TorConnect() {
786 my $socket = new IO::Socket::INET(
787 Proto => 'tcp', PeerAddr => '127.0.0.1', PeerPort => $TOR_CONTROL_PORT,
788 ) or return;
789
790 $socket->autoflush(1);
791
792 # Authenticate.
793 &TorSendCommand($socket, "AUTHENTICATE");
794
795 return $socket;
796 }
797
798 sub TorSendCommand() {
799 my ($socket, $cmd) = @_;
800
801 # Replace line ending with \r\n.
802 chomp $cmd;
803 $cmd .= "\r\n";
804
805 $socket->send($cmd);
806
807 my @output = ();
808 while (my $line = <$socket>) {
809 # Skip empty lines.
810 if ($line =~ /^.\r\n$/) {
811 next;
812 }
813
814 # Command has been successfully executed.
815 if ($line =~ /250 OK/) {
816 last;
817
818 # Error.
819 } elsif ($line =~ /^5\d+/) {
820 last;
821
822 } else {
823 # Remove line endings.
824 $line =~ s/\r\n$//;
825
826 push(@output, $line);
827 }
828 }
829
830 return @output;
831 }
832
833 sub TorSendCommandOneLine() {
834 my ($tor, $cmd) = @_;
835
836 my @output = &TorSendCommand($tor, $cmd);
837 return $output[0];
838 }
839
840 sub TorGetInfo() {
841 my ($tor, $cmd) = @_;
842
843 my $output = &TorSendCommandOneLine($tor, "GETINFO ".$cmd);
844
845 my ($key, $value) = split("=", $output);
846 return $value;
847 }
848
849 sub TorClose() {
850 my $socket = shift;
851
852 if ($socket) {
853 $socket->shutdown(2);
854 }
855 }
856
857 sub TorTrafficStats() {
858 my $tor = shift;
859
860 my $output_read = &TorGetInfo($tor, "traffic/read");
861 my $output_written = &TorGetInfo($tor, "traffic/written");
862
863 return ($output_read, $output_written);
864 }
865
866 sub TorRelayFingerprint() {
867 my $tor = shift;
868
869 return &TorGetInfo($tor, "fingerprint");
870 }
871
872 sub TorORConnStatus() {
873 my $tor = shift;
874 my @nodes = ();
875
876 my @output = &TorSendCommand($tor, "GETINFO orconn-status");
877 foreach (@output) {
878 $_ =~ s/^250[\+-]orconn-status=//;
879 next if ($_ eq "");
880 last if ($_ eq ".");
881 next unless ($_ =~ /^\$/);
882
883 my @line = split(" ", $_);
884 my @node = split(/[=~]/, $line[0]);
885
886 my $node = &TorNodeDescription($tor, $node[0]);
887 if ($node) {
888 push(@nodes, $node);
889 }
890 }
891
892 # Sort by names.
893 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
894
895 return @nodes;
896 }
897
898 sub TorNodeDescription() {
899 my ($tor, $fingerprint) = @_;
900 $fingerprint =~ s/\$//;
901
902 my $node = {
903 fingerprint => $fingerprint,
904 exit_node => 0,
905 };
906
907 my @output = &TorSendCommand($tor, "GETINFO ns/id/$node->{'fingerprint'}");
908
909 foreach (@output) {
910 # Router
911 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
912 $node->{'name'} = $1;
913 $node->{'address'} = $3;
914 $node->{'port'} = $4;
915
916 my $country_code = &TorGetInfo($tor, "ip-to-country/$node->{'address'}");
917 $node->{'country_code'} = $country_code;
918
919 # Flags
920 } elsif ($_ =~ /^s (.*)$/) {
921 $node->{'flags'} = split(" ", $1);
922
923 foreach my $flag ($node->{'flags'}) {
924 if ($flag eq "Exit") {
925 $node->{'exit_node'}++;
926 }
927 }
928
929 # Bandwidth
930 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
931 $node->{'bandwidth'} = $1 * 8;
932 $node->{'bandwidth_string'} = &FormatBitsPerSecond($node->{'bandwidth'});
933 }
934 }
935
936 if (exists($node->{'name'})) {
937 return $node;
938 }
939 }
940
941 sub TorAccountingStats() {
942 my $tor = shift;
943 my $ret = {};
944
945 my $enabled = &TorGetInfo($tor, "accounting/enabled");
946 if ($enabled ne '1') {
947 return;
948 }
949
950 my @cmds = ("hibernating", "interval-start", "interval-end");
951 foreach (@cmds) {
952 $ret->{$_} = &TorGetInfo($tor, "accounting/$_");
953 }
954
955 my @cmds = ("bytes", "bytes-left");
956 foreach (@cmds) {
957 my $output = &TorGetInfo($tor, "accounting/$_");
958 my @bytes = split(" ", $output);
959
960 $ret->{$_."_read"} = $bytes[0];
961 $ret->{$_."_written"} = $bytes[1];
962 }
963
964 return $ret;
965 }
966
967 sub FormatBytes() {
968 my $bytes = shift;
969
970 my @units = ("B", "KB", "MB", "GB", "TB");
971 my $units_index = 0;
972
973 while (($units_index <= $#units) && ($bytes >= 1024)) {
974 $units_index++;
975 $bytes /= 1024;
976 }
977
978 return sprintf("%.2f %s", $bytes, $units[$units_index]);
979 }
980
981 sub FormatBitsPerSecond() {
982 my $bits = shift;
983
984 my @units = ("bit/s", "kbit/s", "Mbit/s", "Gbit/s", "Tbit/s");
985 my $units_index = 0;
986
987 while (($units_index <= $#units) && ($bits >= 1024)) {
988 $units_index++;
989 $bits /= 1024;
990 }
991
992 return sprintf("%.2f %s", $bits, $units[$units_index]);
993 }