]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - html/cgi-bin/tor.cgi
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into next
[people/teissler/ipfire-2.x.git] / html / cgi-bin / tor.cgi
CommitLineData
13b5ce6e
MT
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2013 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
22use strict;
23use Locale::Country;
24
25# enable only the following on debugging purpose
26use warnings;
27use CGI::Carp 'fatalsToBrowser';
28
29require '/var/ipfire/general-functions.pl';
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
32
33#workaround to suppress a warning when a variable is used only once
34my @dummy = ( ${Header::colouryellow} );
35undef (@dummy);
36
37my @bandwidth_limits = (
38 1000 * 1024, # 1G
39 500 * 1024,
40 200 * 1024,
41 100 * 1024, # 100M
42 64 * 1024,
43 50 * 1024,
44 25 * 1024,
45 20 * 1024,
46 16 * 1024,
47 10 * 1024,
48 8 * 1024,
49 4 * 1024,
50 2 * 1024,
51 1024, # 1M
52 512,
53 256,
6eb9c49d 54 160
13b5ce6e
MT
55);
56my @accounting_periods = ('daily', 'weekly', 'monthly');
57
58my $TOR_CONTROL_PORT = 9051;
59
3387469b
JPT
60my $string=();
61my $memory=();
62my @memory=();
63my @pid=();
64my @tor=();
65sub 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}
86daemonstats();
87
13b5ce6e
MT
88our %netsettings = ();
89&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
90
3387469b
JPT
91our %color = ();
92our %mainsettings = ();
93&General::readhash("${General::swroot}/main/settings", \%mainsettings);
94&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
95
13b5ce6e
MT
96our %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'}";
103if (&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'} = 'exit';
bd8b0330 109$settings{'TOR_RELAY_ADDRESS'} = '';
13b5ce6e 110$settings{'TOR_RELAY_PORT'} = 9001;
4245fe34 111$settings{'TOR_RELAY_DIRPORT'} = 0;
b0449403
MT
112$settings{'TOR_RELAY_NICKNAME'} = '';
113$settings{'TOR_RELAY_CONTACT_INFO'} = '';
13b5ce6e
MT
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
121my $errormessage = '';
122my $warnmessage = '';
123
124&Header::showhttpheaders();
125
13b5ce6e
MT
126# Get GUI values.
127&Header::getcgihash(\%settings);
128
129# Create tor command connection.
130our $torctrl = &TorConnect();
131
132# Toggle enable/disable field.
133if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
a03547fe
MT
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 }
b0449403
MT
138 }
139
818f47d0
MT
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 }
4245fe34
JPT
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 }
818f47d0 152
bd8b0330
MT
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
56bf9f21
MT
159 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
160 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
161 }
162
13b5ce6e
MT
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
3308f8d0
MT
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
13b5ce6e
MT
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 }
b0449403
MT
199} else {
200 # Load settings from file.
201 &General::readhash("${General::swroot}/tor/settings", \%settings);
13b5ce6e
MT
202}
203
204&showMainBox();
205
206# Close Tor control connection.
207&TorClose($torctrl);
208
209# Functions
210
211sub 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
3387469b
JPT
234 &Header::openbox('100%', 'center', $Lang::tr{'tor'});
235
236
237if ( ($memory != 0) && (@pid[0] ne "///") ){
f76b104c
AM
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>";
3387469b
JPT
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 {
f76b104c
AM
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>";
3387469b
JPT
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'});
13b5ce6e
MT
260
261 print <<END;
3387469b 262 <table width='95%'>
13b5ce6e 263 <tr>
3387469b 264 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor common settings'}</b></td>
13b5ce6e
MT
265 </tr>
266 <tr>
267 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
005db206
MT
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'}:</td>
270 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
13b5ce6e
MT
271 </tr>
272 <tr>
273 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
005db206 274 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
13b5ce6e 275 <td width='25%' class='base'></td>
005db206 276 <td width='20%'></td>
13b5ce6e
MT
277 </tr>
278 </table>
279END
280
a03547fe
MT
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>
a03547fe
MT
289 <br>
290
3387469b 291 <table width='95%'>
a03547fe 292 <tr>
3387469b 293 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor acls'}</b></td>
a03547fe
MT
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>
a03547fe
MT
310 <br>
311
3387469b 312 <table width='95%'>
a03547fe 313 <tr>
3387469b 314 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor exit nodes'}</b></td>
a03547fe
MT
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>
13b5ce6e
MT
324END
325
a03547fe
MT
326 my @country_names = Locale::Country::all_country_names();
327 foreach my $country_name (sort @country_names) {
328 my $country_code = Locale::Country::country2code($country_name);
329 $country_code = uc($country_code);
26cce22d
MT
330 print "<option value='$country_code'";
331
332 if ($settings{'TOR_EXIT_COUNTRY'} eq $country_code) {
333 print " selected";
334 }
335
336 print ">$country_name ($country_code)</option>\n";
a03547fe 337 }
13b5ce6e 338
a03547fe
MT
339 print <<END;
340 </select>
341 </td>
342 <td width='50%' colspan='2'>
343 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
344 </td>
345 </tr>
346 </table>
13b5ce6e 347END
13b5ce6e 348
005db206
MT
349 &Header::closebox();
350
a03547fe 351 # Tor relay box
a03547fe
MT
352 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
353 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
354 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
355 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
356 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
13b5ce6e 357
a03547fe
MT
358 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
359 foreach (@bandwidth_limits) {
360 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
361 }
362 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
13b5ce6e 363
a03547fe
MT
364 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
365 foreach (@bandwidth_limits) {
366 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
367 }
368 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
13b5ce6e 369
a03547fe
MT
370 foreach (@accounting_periods) {
371 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
372 }
373 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
374
3387469b 375 &Header::openbox('100%', 'center', $Lang::tr{'tor relay configuration'});
a03547fe
MT
376
377 print <<END;
3387469b 378 <table width='95%'>
a03547fe
MT
379 <tr>
380 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
381 <td width='30%'>
382 <select name='TOR_RELAY_MODE'>
383 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
384 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
385 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
386 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
387 </select>
388 </td>
919a5020 389 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
a03547fe 390 <td width='20%'>
b31af085 391 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' maxlength='19' />
a03547fe
MT
392 </td>
393 </tr>
394 <tr>
395 <td width='25%' class='base'>$Lang::tr{'tor relay address'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
396 <td width='30%'>
397 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
398 </td>
919a5020 399 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
a03547fe 400 <td width='20%'>
919a5020 401 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
a03547fe 402 </td>
a03547fe 403 </tr>
4245fe34
JPT
404 <tr>
405 <td width='25%'>&nbsp;</td>
406 <td width='30%'>&nbsp;</td>
407 <td width='25%' class='base'>$Lang::tr{'tor directory port'}:</td>
408 <td width='20%'>
409 <input type='text' name='TOR_RELAY_DIRPORT' value='$settings{'TOR_RELAY_DIRPORT'}' size='5' />&nbsp;$Lang::tr{'tor 0 = disabled'}
410 </td>
411 </tr>
a03547fe
MT
412 <tr>
413 <td width='25%' class='base'>$Lang::tr{'tor contact info'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
414 <td width='75%' colspan='3'>
919a5020 415 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' style='width: 98%;' />
a03547fe
MT
416 </td>
417 </tr>
418 </table>
419
3387469b 420 <br>
a03547fe 421
3387469b 422 <table width='95%'>
a03547fe 423 <tr>
3387469b 424 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
a03547fe
MT
425 </tr>
426 <tr>
427 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
428 <td width='30%' class='base'>
429 <select name='TOR_RELAY_BANDWIDTH_RATE'>
13b5ce6e
MT
430END
431
a03547fe
MT
432 foreach (@bandwidth_limits) {
433 if ($_ >= 1024) {
434 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option>\n";
435 } else {
436 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit/s</option>\n";
13b5ce6e 437 }
a03547fe 438 }
13b5ce6e 439
a03547fe
MT
440 print <<END;
441 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
442 </select>
443 </td>
444 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
445 <td width='20%'>
446 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
447 </td>
448 </tr>
449 <tr>
450 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
451 <td width='20%' class='base'>
452 <select name='TOR_RELAY_BANDWIDTH_BURST'>
13b5ce6e
MT
453END
454
a03547fe
MT
455 foreach (@bandwidth_limits) {
456 if ($_ >= 1024) {
457 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option>\n";
458 } else {
459 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit/s</option>\n";
13b5ce6e 460 }
a03547fe
MT
461 }
462 print <<END;
463 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
464 </select>
465 </td>
466 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
467 <td width='20%'>
468 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
13b5ce6e
MT
469END
470
a03547fe
MT
471 foreach (@accounting_periods) {
472 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
473 }
13b5ce6e 474
a03547fe
MT
475 print <<END;
476 </select>
477 </td>
478 </tr>
479 </table>
13b5ce6e
MT
480END
481
a03547fe 482 &Header::closebox();
13b5ce6e
MT
483
484 print <<END;
3387469b 485 <table width='95%'>
13b5ce6e
MT
486 <tr>
487 <td>
488 <img src='/blob.gif' align='top' alt='*' />&nbsp;<font class='base'>$Lang::tr{'this field may be blank'}</font>
489 </td>
490 <td align='right'>&nbsp;</td>
491 </tr>
492 </table>
493
494 <hr>
495
3387469b 496 <table width='95%'>
13b5ce6e
MT
497 <tr>
498 <td>&nbsp;</td>
499 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
500 <td>&nbsp;</td>
501 </tr>
502 </table>
503END
504
505 # If we have a control connection, show the stats.
506 if ($torctrl) {
3387469b 507 &Header::openbox('100%', 'center', $Lang::tr{'tor stats'});
13b5ce6e
MT
508
509 my @traffic = &TorTrafficStats($torctrl);
510
511 if (@traffic) {
512 print <<END;
3387469b 513 <table width='95%'>
13b5ce6e
MT
514END
515
516 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
517 my $fingerprint = &TorRelayFingerprint($torctrl);
518 if ($fingerprint) {
519 print <<END;
520 <tr>
521 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
522 <td width='60%'>
523 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
524 </td>
525 </tr>
526END
527 }
528 }
529
530 my $address = TorGetInfo($torctrl, "address");
531 if ($address) {
532 print <<END;
533 <tr>
534 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
535 <td width='60%'>$address</td>
536 </tr>
537END
538 }
539
540 print <<END;
541 <tr>
542 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
543END
544 print "<td width='60%'>" . &FormatBytes($traffic[0]) ."/". &FormatBytes($traffic[1]) . "</td>";
545 print <<END;
546 </tr>
547 </table>
548END
549 }
550
551 my $accounting = &TorAccountingStats($torctrl);
552 if ($accounting) {
553 print <<END;
3387469b 554 <table width='95%'>
13b5ce6e
MT
555 <tr>
556 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
557 </tr>
558END
559
560 if ($accounting->{'hibernating'} eq "hard") {
561 print <<END;
562 <tr>
563 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
564 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
565 </td>
566 </tr>
567END
568 } elsif ($accounting->{'hibernating'} eq "soft") {
569 print <<END;
570 <tr>
571 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
572 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
573 </td>
574 </tr>
575END
576 }
577
578 print <<END;
579 <tr>
580 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
581 <td width='60%'>
582 $accounting->{'interval-start'} - $accounting->{'interval-end'}
583 </td>
584 </tr>
585 <tr>
586 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
587 <td width='60%'>
588END
589
590 print &FormatBytes($accounting->{'bytes_read'}) . "/" . &FormatBytes($accounting->{'bytes_written'});
591 print " (" . &FormatBytes($accounting->{'bytes-left_read'}) . "/" . &FormatBytes($accounting->{'bytes-left_written'});
592 print " $Lang::tr{'tor accounting bytes left'})";
593
594 print <<END;
595 </td>
596 </tr>
597 </table>
598END
599 }
600
601 my @nodes = &TorORConnStatus($torctrl);
602 if (@nodes) {
f16bcc3e 603 my $nodes_length = scalar @nodes;
13b5ce6e 604 print <<END;
3387469b 605 <table width='95%'>
13b5ce6e 606 <tr>
f16bcc3e
MT
607 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
608 <td width='60%' colspan='2'>($nodes_length)</td>
13b5ce6e
MT
609 </tr>
610END
611
612 foreach my $node (@nodes) {
613 print <<END;
614 <tr>
615 <td width='40%'>
616 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
617 $node->{'name'}
618 </a>
619 </td>
620 <td width='30%'>
621END
622
623 if (exists($node->{'country_code'})) {
ae666bf1 624 if (!$node->{'country_code'} or $node->{'country_code'} eq '??') {
3387469b
JPT
625 print "<img src='/images/flags/blank.png' border='0' align='absmiddle'/>";
626 } else {
13b5ce6e 627 print "<a href='country.cgi#$node->{'country_code'}'><img src='/images/flags/$node->{'country_code'}.png' border='0' align='absmiddle' alt='$node->{'country_code'}'></a>";
3387469b 628 }
13b5ce6e
MT
629 }
630
631 print <<END;
632 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
633 </td>
634 <td width='30%' align='right'>
635 ~$node->{'bandwidth_string'}
636 </td>
637 </tr>
638END
639 }
640 print "</table>";
641 }
642
643 &Header::closebox();
644 }
645
646 print "</form>\n";
647
648 &Header::closebigbox();
649 &Header::closepage();
650}
651
652sub BuildConfiguration() {
653 my %settings = ();
654 &General::readhash("${General::swroot}/tor/settings", \%settings);
655
656 my $torrc = "${General::swroot}/tor/torrc";
657
658 open(FILE, ">$torrc");
659
660 # Global settings.
661 print FILE "ControlPort $TOR_CONTROL_PORT\n";
662
663 if ($settings{'TOR_ENABLED'} eq 'on') {
664 my $strict_nodes = 0;
665
666 print FILE "SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
667
668 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
669 foreach (@subnets) {
670 print FILE "SocksPolicy accept $_\n" if (&General::validipandmask($_));
671 }
672 print FILE "SocksPolicy reject *\n" if (@subnets);
673
674 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
675 $strict_nodes = 1;
676
677 print FILE "ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
678 }
679
680 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
681 $strict_nodes = 1;
682
683 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
684 foreach (@nodes) {
685 print FILE "ExitNode $_\n";
686 }
687 }
688
689 if ($strict_nodes > 0) {
690 print FILE "StrictNodes 1\n";
691 }
692 }
693
694 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
695 # Reject access to private networks.
696 print FILE "ExitPolicyRejectPrivate 1\n";
697
919a5020 698 print FILE "ORPort $settings{'TOR_RELAY_PORT'}\n";
13b5ce6e 699
4245fe34
JPT
700 if ($settings{'TOR_RELAY_DIRPORT'} ne '0') {
701 print FILE "DirPort $settings{'TOR_RELAY_DIRPORT'}\n";
702 }
703
13b5ce6e
MT
704 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
705 print FILE "Address $settings{'TOR_RELAY_ADDRESS'}\n";
706 }
707
708 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
709 print FILE "Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
710 }
711
712 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
713 print FILE "ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
714 }
715
716 # Limit to bridge mode.
717 my $is_bridge = 0;
718
719 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
720 $is_bridge++;
721
722 # Private bridge.
723 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
724 $is_bridge++;
725
726 print FILE "PublishServerDescriptor 0\n";
727
728 # Exit node.
729 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
730 print FILE "ExitPolicy accept *:*\n";
731
732 # Relay only.
733 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
734 print FILE "ExitPolicy reject *:*\n";
735 }
736
737 if ($is_bridge > 0) {
738 print FILE "BridgeRelay 1\n";
739 print FILE "Exitpolicy reject *:*\n";
740 }
741
742 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
743 print FILE "RelayBandwidthRate ";
744 print FILE $settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
745 print FILE " KB\n";
746
747 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
748 print FILE "RelayBandwidthBurst ";
749 print FILE $settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
750 print FILE " KB\n";
751 }
752 }
753
754 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
755 print FILE "AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
756
757 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
758 print FILE "AccountingStart day 00:00\n";
759 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
760 print FILE "AccountingStart week 1 00:00\n";
761 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
762 print FILE "AccountingStart month 1 00:00\n";
763 }
764 }
765 }
766
767 close(FILE);
768
769 # Restart the service.
770 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
005db206 771 system("/usr/local/bin/torctrl restart &>/dev/null");
13b5ce6e 772 } else {
005db206 773 system("/usr/local/bin/torctrl stop &>/dev/null");
13b5ce6e 774 }
3387469b
JPT
775 # Update pid and memory
776 daemonstats();
13b5ce6e
MT
777}
778
779sub TorConnect() {
780 my $socket = new IO::Socket::INET(
781 Proto => 'tcp', PeerAddr => '127.0.0.1', PeerPort => $TOR_CONTROL_PORT,
782 ) or return;
783
784 $socket->autoflush(1);
785
786 # Authenticate.
787 &TorSendCommand($socket, "AUTHENTICATE");
788
789 return $socket;
790}
791
792sub TorSendCommand() {
793 my ($socket, $cmd) = @_;
794
795 # Replace line ending with \r\n.
796 chomp $cmd;
797 $cmd .= "\r\n";
798
799 $socket->send($cmd);
800
801 my @output = ();
802 while (my $line = <$socket>) {
803 # Skip empty lines.
804 if ($line =~ /^.\r\n$/) {
805 next;
806 }
807
808 # Command has been successfully executed.
809 if ($line =~ /250 OK/) {
810 last;
811
812 # Error.
813 } elsif ($line =~ /^5\d+/) {
814 last;
815
816 } else {
817 # Remove line endings.
818 $line =~ s/\r\n$//;
819
820 push(@output, $line);
821 }
822 }
823
824 return @output;
825}
826
827sub TorSendCommandOneLine() {
828 my ($tor, $cmd) = @_;
829
830 my @output = &TorSendCommand($tor, $cmd);
831 return $output[0];
832}
833
834sub TorGetInfo() {
835 my ($tor, $cmd) = @_;
836
837 my $output = &TorSendCommandOneLine($tor, "GETINFO ".$cmd);
838
839 my ($key, $value) = split("=", $output);
840 return $value;
841}
842
843sub TorClose() {
844 my $socket = shift;
845
846 if ($socket) {
847 $socket->shutdown(2);
848 }
849}
850
851sub TorTrafficStats() {
852 my $tor = shift;
853
854 my $output_read = &TorGetInfo($tor, "traffic/read");
855 my $output_written = &TorGetInfo($tor, "traffic/written");
856
857 return ($output_read, $output_written);
858}
859
860sub TorRelayFingerprint() {
861 my $tor = shift;
862
863 return &TorGetInfo($tor, "fingerprint");
864}
865
866sub TorORConnStatus() {
867 my $tor = shift;
868 my @nodes = ();
869
870 my @output = &TorSendCommand($tor, "GETINFO orconn-status");
871 foreach (@output) {
872 $_ =~ s/^250[\+-]orconn-status=//;
873 next if ($_ eq "");
874 last if ($_ eq ".");
875 next unless ($_ =~ /^\$/);
876
877 my @line = split(" ", $_);
878 my @node = split(/[=~]/, $line[0]);
879
880 my $node = &TorNodeDescription($tor, $node[0]);
881 if ($node) {
882 push(@nodes, $node);
883 }
884 }
885
886 # Sort by names.
887 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
888
889 return @nodes;
890}
891
892sub TorNodeDescription() {
893 my ($tor, $fingerprint) = @_;
894 $fingerprint =~ s/\$//;
895
896 my $node = {
897 fingerprint => $fingerprint,
898 exit_node => 0,
899 };
900
901 my @output = &TorSendCommand($tor, "GETINFO ns/id/$node->{'fingerprint'}");
902
903 foreach (@output) {
904 # Router
905 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
906 $node->{'name'} = $1;
907 $node->{'address'} = $3;
908 $node->{'port'} = $4;
909
910 my $country_code = &TorGetInfo($tor, "ip-to-country/$node->{'address'}");
911 $node->{'country_code'} = $country_code;
912
913 # Flags
914 } elsif ($_ =~ /^s (.*)$/) {
915 $node->{'flags'} = split(" ", $1);
916
917 foreach my $flag ($node->{'flags'}) {
918 if ($flag eq "Exit") {
919 $node->{'exit_node'}++;
920 }
921 }
922
923 # Bandwidth
924 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
925 $node->{'bandwidth'} = $1 * 8;
926 $node->{'bandwidth_string'} = &FormatBitsPerSecond($node->{'bandwidth'});
927 }
928 }
929
930 if (exists($node->{'name'})) {
931 return $node;
932 }
933}
934
935sub TorAccountingStats() {
936 my $tor = shift;
937 my $ret = {};
938
939 my $enabled = &TorGetInfo($tor, "accounting/enabled");
940 if ($enabled ne '1') {
941 return;
942 }
943
944 my @cmds = ("hibernating", "interval-start", "interval-end");
945 foreach (@cmds) {
946 $ret->{$_} = &TorGetInfo($tor, "accounting/$_");
947 }
948
949 my @cmds = ("bytes", "bytes-left");
950 foreach (@cmds) {
951 my $output = &TorGetInfo($tor, "accounting/$_");
952 my @bytes = split(" ", $output);
953
954 $ret->{$_."_read"} = $bytes[0];
955 $ret->{$_."_written"} = $bytes[1];
956 }
957
958 return $ret;
959}
960
961sub FormatBytes() {
962 my $bytes = shift;
963
964 my @units = ("B", "KB", "MB", "GB", "TB");
965 my $units_index = 0;
966
967 while (($units_index <= $#units) && ($bytes >= 1024)) {
968 $units_index++;
969 $bytes /= 1024;
970 }
971
972 return sprintf("%.2f %s", $bytes, $units[$units_index]);
973}
974
975sub FormatBitsPerSecond() {
976 my $bits = shift;
977
978 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
979 my $units_index = 0;
980
981 while (($units_index <= $#units) && ($bits >= 1024)) {
982 $units_index++;
983 $bits /= 1024;
984 }
985
986 return sprintf("%.2f %s", $bits, $units[$units_index]);
987}