]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/tor.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013-2019 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
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";
33 # Init libloc database connection.
34 my $db_handle = &Location
::Functions
::init
();
36 #workaround to suppress a warning when a variable is used only once
37 my @dummy = ( ${Header
::colouryellow
} );
40 my @bandwidth_limits = (
41 1000 * 1024, # 1 GBit/s
44 100 * 1024, # 100 MBit/s
56 my @accounting_periods = ('daily', 'weekly', 'monthly');
58 my $TOR_CONTROL_PORT = 9051;
69 open(FILE
, '/usr/local/bin/addonctrl tor status | ');
72 $string = join("", @tor);
73 $string =~ s/[a-z_]//gi;
74 $string =~ s/\[[0-1]\;[0-9]+//gi;
75 $string =~ s/[\(\)\.]//gi;
78 @pid = split(/\s/,$string);
79 if (open(FILE
, "/proc/$pid[0]/statm")){
81 @memory = split(/ /,$temp);
88 our %netsettings = ();
89 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
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);
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'}";
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';
119 $settings{'ACTION'} = '';
121 my $errormessage = '';
122 my $warnmessage = '';
124 &Header
::showhttpheaders
();
127 &Header
::getcgihash
(\
%settings);
129 # Create tor command connection.
130 our $torctrl = &TorConnect
();
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'}";
140 if (!&General
::validport
($settings{'TOR_SOCKS_PORT'})) {
141 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
144 if (!&General
::validport
($settings{'TOR_RELAY_PORT'})) {
145 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
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'}";
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'}";
159 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
160 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
163 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
164 $settings{'TOR_ALLOWED_SUBNETS'} = "";
166 s/^\s+//g; s/\s+$//g;
168 unless (&General
::validipandmask
($_)) {
169 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
171 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
175 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
176 $settings{'TOR_USE_EXIT_NODES'} = "";
178 s/^\s+//g; s/\s+$//g;
180 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
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;
188 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
189 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
192 if ($errormessage eq '') {
193 # Write configuration settings to file.
194 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
196 # Update configuration files.
197 &BuildConfiguration
();
200 # Load settings from file.
201 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
206 # Close Tor control connection.
215 $checked{'TOR_ENABLED'}{'on'} = '';
216 $checked{'TOR_ENABLED'}{'off'} = '';
217 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
219 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
220 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
221 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
223 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
224 &Header
::openbigbox
('100%', 'left', '', $errormessage);
227 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
228 print "<font class='base'>$errormessage </font>\n";
232 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
234 &Header
::openbox
('100%', 'center', $Lang::tr
{'tor'});
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>";
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>";
259 &Header
::openbox
('100%', 'center', $Lang::tr
{'tor configuration'});
264 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor common settings'}</b></td>
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'}: <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>
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>
281 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
282 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
284 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
285 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
293 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor acls'}</b></td>
296 <td colspan='2' class='base' width='55%'>
297 $Lang::tr{'tor allowed subnets'}:
299 <td colspan='2' width='45%'></td>
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>
305 <td colspan='2' width='45%'></td>
314 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor exit nodes'}</b></td>
317 <td colspan='2' class='base' width='55%'></td>
318 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
321 <td width='50%' colspan='2'>
322 <select name='TOR_EXIT_COUNTRY'>
323 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
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);
332 my $country_name = &Location
::Functions
::get_country_name
($country_code);
334 print "<option value='$country_code'";
336 if ($settings{'TOR_EXIT_COUNTRY'} eq $country_code) {
340 print ">$country_name ($country_code)</option>\n";
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>
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';
362 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
363 foreach (@bandwidth_limits) {
364 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
366 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
368 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
369 foreach (@bandwidth_limits) {
370 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
372 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
374 foreach (@accounting_periods) {
375 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
377 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
379 &Header
::openbox
('100%', 'center', $Lang::tr
{'tor relay configuration'});
384 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
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>
393 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}:</td>
395 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' maxlength='19' />
399 <td width='25%' class='base'>$Lang::tr{'tor relay address'}:</td>
401 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
403 <td width='25%' class='base'>$Lang::tr{'tor relay port'}: <img src='/blob.gif' alt='*' /></td>
405 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
409 <td width='25%'> </td>
410 <td width='30%'> </td>
411 <td width='25%' class='base'>$Lang::tr{'tor directory port'}: <img src='/blob.gif' alt='*' /></td>
413 <input type='text' name='TOR_RELAY_DIRPORT' value='$settings{'TOR_RELAY_DIRPORT'}' size='5' /> $Lang::tr{'tor 0 = disabled'}
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%;' />
428 <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
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'>
436 foreach (@bandwidth_limits) {
438 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." Mbit/s</option
>\n";
440 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kbit
/s</option
>\n";
445 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
448 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}: <img src='/blob.gif' alt='*' /></td>
450 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
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'>
459 foreach (@bandwidth_limits) {
461 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." Mbit/s</option
>\n";
463 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kbit
/s</option
>\n";
467 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
470 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
472 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
475 foreach (@accounting_periods) {
476 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
491 <td><img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'required field'}</font></td>
492 <td align='right'> </td>
501 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
507 # If we have a control connection, show the stats.
509 &Header
::openbox
('100%', 'center', $Lang::tr
{'tor stats'});
511 my @traffic = &TorTrafficStats
($torctrl);
518 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
519 my $fingerprint = &TorRelayFingerprint
($torctrl);
523 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
525 <a href='https://metrics.torproject.org/rs.html#details/$fingerprint' target='_blank'>$fingerprint</a>
532 my $address = TorGetInfo
($torctrl, "address");
536 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
537 <td width='60%'>$address</td>
544 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
546 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
553 my $accounting = &TorAccountingStats
($torctrl);
558 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
562 if ($accounting->{'hibernating'} eq "hard") {
565 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
566 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
570 } elsif ($accounting->{'hibernating'} eq "soft") {
573 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
574 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
582 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
584 $accounting->{'interval-start'} - $accounting->{'interval-end'}
588 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
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'})";
603 my @nodes = &TorORConnStatus
($torctrl);
605 my $nodes_length = scalar @nodes;
609 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
610 <td width='60%' colspan='2'>($nodes_length)</td>
614 foreach my $node (@nodes) {
618 <a href='https://metrics.torproject.org/rs.html#details/$node->{'fingerprint'}' target='_blank'>
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'});
629 # Check if a flag for the given country is available.
631 print "<a href='country.cgi#$node->{'country_code'}'><img src='$flag_icon' border='0' align='absmiddle' alt='$node->{'country_code'}'></a>";
633 print "<img src='/images/flags/blank.png' border='0' align='absmiddle'/>";
638 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
640 <td width='30%' align='right'>
641 ~$node->{'bandwidth_string'}
654 &Header
::closebigbox
();
655 &Header
::closepage
();
658 sub BuildConfiguration
() {
660 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
662 my $torrc = "${General::swroot}/tor/torrc";
664 open(FILE
, ">$torrc");
667 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
669 if ($settings{'TOR_ENABLED'} eq 'on') {
670 my $strict_nodes = 0;
672 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
674 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
676 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
678 print FILE
"SocksPolicy reject *\n" if (@subnets);
680 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
683 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
686 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
689 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
691 print FILE
"ExitNode $_\n";
695 if ($strict_nodes > 0) {
696 print FILE
"StrictNodes 1\n";
700 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
701 # Reject access to private networks.
702 print FILE
"ExitPolicyRejectPrivate 1\n";
704 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}\n";
706 if ($settings{'TOR_RELAY_DIRPORT'} ne '0') {
707 print FILE
"DirPort $settings{'TOR_RELAY_DIRPORT'}\n";
710 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
711 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
714 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
715 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
718 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
719 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
722 # Limit to bridge mode.
725 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
729 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
732 print FILE
"PublishServerDescriptor 0\n";
735 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
736 print FILE
"ExitPolicy accept *:*\n";
739 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
740 print FILE
"ExitPolicy reject *:*\n";
743 if ($is_bridge > 0) {
744 print FILE
"BridgeRelay 1\n";
745 print FILE
"Exitpolicy reject *:*\n";
748 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
749 print FILE
"RelayBandwidthRate ";
750 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
753 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
754 print FILE
"RelayBandwidthBurst ";
755 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
760 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
761 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
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";
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");
779 system("/usr/local/bin/torctrl stop &>/dev/null");
781 # Update pid and memory
786 my $socket = new IO
::Socket
::INET
(
787 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
790 $socket->autoflush(1);
793 &TorSendCommand
($socket, "AUTHENTICATE");
798 sub TorSendCommand
() {
799 my ($socket, $cmd) = @_;
801 # Replace line ending with \r\n.
808 while (my $line = <$socket>) {
810 if ($line =~ /^.\r\n$/) {
814 # Command has been successfully executed.
815 if ($line =~ /250 OK/) {
819 } elsif ($line =~ /^5\d+/) {
823 # Remove line endings.
826 push(@output, $line);
833 sub TorSendCommandOneLine
() {
834 my ($tor, $cmd) = @_;
836 my @output = &TorSendCommand
($tor, $cmd);
841 my ($tor, $cmd) = @_;
843 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
845 my ($key, $value) = split("=", $output);
853 $socket->shutdown(2);
857 sub TorTrafficStats
() {
860 my $output_read = &TorGetInfo
($tor, "traffic/read");
861 my $output_written = &TorGetInfo
($tor, "traffic/written");
863 return ($output_read, $output_written);
866 sub TorRelayFingerprint
() {
869 return &TorGetInfo
($tor, "fingerprint");
872 sub TorORConnStatus
() {
876 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
878 $_ =~ s/^250[\+-]orconn-status=//;
881 next unless ($_ =~ /^\$/);
883 my @line = split(" ", $_);
884 my @node = split(/[=~]/, $line[0]);
886 my $node = &TorNodeDescription
($tor, $node[0]);
893 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
898 sub TorNodeDescription
() {
899 my ($tor, $fingerprint) = @_;
900 $fingerprint =~ s/\$//;
903 fingerprint
=> $fingerprint,
907 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
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;
916 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
917 $node->{'country_code'} = $country_code;
920 } elsif ($_ =~ /^s (.*)$/) {
921 $node->{'flags'} = split(" ", $1);
923 foreach my $flag ($node->{'flags'}) {
924 if ($flag eq "Exit") {
925 $node->{'exit_node'}++;
930 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
931 $node->{'bandwidth'} = $1 * 8;
932 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
936 if (exists($node->{'name'})) {
941 sub TorAccountingStats
() {
945 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
946 if ($enabled ne '1') {
950 my @cmds = ("hibernating", "interval-start", "interval-end");
952 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
955 my @cmds = ("bytes", "bytes-left");
957 my $output = &TorGetInfo
($tor, "accounting/$_");
958 my @bytes = split(" ", $output);
960 $ret->{$_."_read"} = $bytes[0];
961 $ret->{$_."_written"} = $bytes[1];
970 my @units = ("B", "KB", "MB", "GB", "TB");
973 while (($units_index <= $#units) && ($bytes >= 1024)) {
978 return sprintf("%.2f %s", $bytes, $units[$units_index]);
981 sub FormatBitsPerSecond
() {
984 my @units = ("bit/s", "kbit/s", "Mbit/s", "Gbit/s", "Tbit/s");
987 while (($units_index <= $#units) && ($bits >= 1024)) {
992 return sprintf("%.2f %s", $bits, $units[$units_index]);