]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/tor.cgi
0c5477fd60fa7040046b6d0e19df20627ecf37fd
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 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 ###############################################################################
25 # enable only the following on debugging purpose
27 use CGI
::Carp
'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header
::colouryellow
} );
37 my @bandwidth_limits = (
56 my @accounting_periods = ('daily', 'weekly', 'monthly');
58 my $TOR_CONTROL_PORT = 9051;
60 our %netsettings = ();
61 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
65 $settings{'TOR_ENABLED'} = 'off';
66 $settings{'TOR_SOCKS_PORT'} = 9050;
67 $settings{'TOR_EXIT_COUNTRY'} = '';
68 $settings{'TOR_USE_EXIT_NODES'} = '';
69 $settings{'TOR_ALLOWED_SUBNETS'} = "$netsettings{'GREEN_NETADDRESS'}\/$netsettings{'GREEN_NETMASK'}";
70 if (&Header
::blue_used
()) {
71 $settings{'TOR_ALLOWED_SUBNETS'} .= ",$netsettings{'BLUE_NETADDRESS'}\/$netsettings{'BLUE_NETMASK'}";
74 $settings{'TOR_RELAY_ENABLED'} = 'off';
75 $settings{'TOR_RELAY_MODE'} = 'exit';
76 $settings{'TOR_RELAY_PORT'} = 9001;
77 $settings{'TOR_RELAY_NOADVERTISE'} = 'off';
78 $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0;
79 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
80 $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0;
81 $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily';
83 $settings{'ACTION'} = '';
85 my $errormessage = '';
88 &Header
::showhttpheaders
();
91 &Header
::getcgihash
(\
%settings);
93 # Create tor command connection.
94 our $torctrl = &TorConnect
();
96 # Toggle enable/disable field.
97 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
98 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
99 $settings{'TOR_ALLOWED_SUBNETS'} = "";
101 s/^\s+//g; s/\s+$//g;
103 unless (&General
::validipandmask
($_)) {
104 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
106 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
110 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
111 $settings{'TOR_USE_EXIT_NODES'} = "";
113 s/^\s+//g; s/\s+$//g;
115 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
119 # Burst bandwidth must be less or equal to bandwidth rate.
120 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
121 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
123 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
124 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
127 if ($errormessage eq '') {
128 # Write configuration settings to file.
129 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
131 # Update configuration files.
132 &BuildConfiguration
();
136 $settings{'ACTION'} = '';
139 # Load settings from file.
140 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
144 # Close Tor control connection.
153 $checked{'TOR_ENABLED'}{'on'} = '';
154 $checked{'TOR_ENABLED'}{'off'} = '';
155 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
157 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
158 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
159 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
161 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
162 &Header
::openbigbox
('100%', 'left', '', $errormessage);
165 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
166 print "<font class='base'>$errormessage </font>\n";
170 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
172 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
177 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
180 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
181 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
182 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
183 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
186 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
187 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
188 <td width='25%' class='base'></td>
189 <td width='20%'></td>
194 if ($settings{'TOR_ENABLED'} eq 'on') {
195 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
196 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
198 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
199 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
208 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
211 <td colspan='2' class='base' width='55%'>
212 $Lang::tr{'tor allowed subnets'}:
214 <td colspan='2' width='45%'></td>
217 <td colspan='2' class='base' width='55%'>
218 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
220 <td colspan='2' width='45%'></td>
230 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
233 <td colspan='2' class='base' width='55%'></td>
234 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
237 <td width='50%' colspan='2'>
238 <select name='TOR_EXIT_COUNTRY'>
239 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
242 my @country_names = Locale
::Country
::all_country_names
();
243 foreach my $country_name (sort @country_names) {
244 my $country_code = Locale
::Country
::country2code
($country_name);
245 $country_code = uc($country_code);
246 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
252 <td width='50%' colspan='2'>
253 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
263 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
264 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
265 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
266 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
268 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
269 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
270 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
271 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
272 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
274 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
275 foreach (@bandwidth_limits) {
276 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
278 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
280 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
281 foreach (@bandwidth_limits) {
282 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
284 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
286 foreach (@accounting_periods) {
287 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
289 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
291 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
296 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
298 <select name='TOR_RELAY_MODE'>
299 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
300 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
301 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
302 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
305 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
307 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
311 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
313 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
315 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
317 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
321 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
323 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
325 <td colspan='2'></td>
328 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
329 <td width='75%' colspan='3'>
330 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
339 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
342 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
343 <td width='30%' class='base'>
344 <select name='TOR_RELAY_BANDWIDTH_RATE'>
347 foreach (@bandwidth_limits) {
349 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
351 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
356 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
359 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
361 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
365 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
366 <td width='20%' class='base'>
367 <select name='TOR_RELAY_BANDWIDTH_BURST'>
370 foreach (@bandwidth_limits) {
372 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
374 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
378 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
381 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
383 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
386 foreach (@accounting_periods) {
387 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
404 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
406 <td align='right'> </td>
415 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
421 # If we have a control connection, show the stats.
423 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
425 my @traffic = &TorTrafficStats
($torctrl);
432 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
433 my $fingerprint = &TorRelayFingerprint
($torctrl);
437 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
439 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
446 my $address = TorGetInfo
($torctrl, "address");
450 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
451 <td width='60%'>$address</td>
458 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
460 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
467 my $accounting = &TorAccountingStats
($torctrl);
472 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
476 if ($accounting->{'hibernating'} eq "hard") {
479 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
480 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
484 } elsif ($accounting->{'hibernating'} eq "soft") {
487 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
488 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
496 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
498 $accounting->{'interval-start'} - $accounting->{'interval-end'}
502 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
506 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
507 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
508 print " $Lang::tr{'tor accounting bytes left'})";
517 my @nodes = &TorORConnStatus
($torctrl);
519 my $nodes_length = scalar @nodes;
523 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
524 <td width='60%' colspan='2'>($nodes_length)</td>
528 foreach my $node (@nodes) {
532 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
539 if (exists($node->{'country_code'})) {
540 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>";
544 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
546 <td width='30%' align='right'>
547 ~$node->{'bandwidth_string'}
560 &Header
::closebigbox
();
561 &Header
::closepage
();
564 sub BuildConfiguration
() {
566 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
568 my $torrc = "${General::swroot}/tor/torrc";
570 open(FILE
, ">$torrc");
573 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
575 if ($settings{'TOR_ENABLED'} eq 'on') {
576 my $strict_nodes = 0;
578 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
580 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
582 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
584 print FILE
"SocksPolicy reject *\n" if (@subnets);
586 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
589 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
592 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
595 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
597 print FILE
"ExitNode $_\n";
601 if ($strict_nodes > 0) {
602 print FILE
"StrictNodes 1\n";
606 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
607 # Reject access to private networks.
608 print FILE
"ExitPolicyRejectPrivate 1\n";
610 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
611 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
612 print FILE
" NoAdvertise";
616 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
617 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
620 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
621 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
624 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
625 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
628 # Limit to bridge mode.
631 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
635 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
638 print FILE
"PublishServerDescriptor 0\n";
641 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
642 print FILE
"ExitPolicy accept *:*\n";
645 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
646 print FILE
"ExitPolicy reject *:*\n";
649 if ($is_bridge > 0) {
650 print FILE
"BridgeRelay 1\n";
651 print FILE
"Exitpolicy reject *:*\n";
654 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
655 print FILE
"RelayBandwidthRate ";
656 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
659 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
660 print FILE
"RelayBandwidthBurst ";
661 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
666 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
667 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
669 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
670 print FILE
"AccountingStart day 00:00\n";
671 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
672 print FILE
"AccountingStart week 1 00:00\n";
673 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
674 print FILE
"AccountingStart month 1 00:00\n";
681 # Restart the service.
682 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
683 system("/usr/local/bin/torctrl restart &>/dev/null");
685 system("/usr/local/bin/torctrl stop &>/dev/null");
690 my $socket = new IO
::Socket
::INET
(
691 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
694 $socket->autoflush(1);
697 &TorSendCommand
($socket, "AUTHENTICATE");
702 sub TorSendCommand
() {
703 my ($socket, $cmd) = @_;
705 # Replace line ending with \r\n.
712 while (my $line = <$socket>) {
714 if ($line =~ /^.\r\n$/) {
718 # Command has been successfully executed.
719 if ($line =~ /250 OK/) {
723 } elsif ($line =~ /^5\d+/) {
727 # Remove line endings.
730 push(@output, $line);
737 sub TorSendCommandOneLine
() {
738 my ($tor, $cmd) = @_;
740 my @output = &TorSendCommand
($tor, $cmd);
745 my ($tor, $cmd) = @_;
747 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
749 my ($key, $value) = split("=", $output);
757 $socket->shutdown(2);
761 sub TorTrafficStats
() {
764 my $output_read = &TorGetInfo
($tor, "traffic/read");
765 my $output_written = &TorGetInfo
($tor, "traffic/written");
767 return ($output_read, $output_written);
770 sub TorRelayFingerprint
() {
773 return &TorGetInfo
($tor, "fingerprint");
776 sub TorORConnStatus
() {
780 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
782 $_ =~ s/^250[\+-]orconn-status=//;
785 next unless ($_ =~ /^\$/);
787 my @line = split(" ", $_);
788 my @node = split(/[=~]/, $line[0]);
790 my $node = &TorNodeDescription
($tor, $node[0]);
797 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
802 sub TorNodeDescription
() {
803 my ($tor, $fingerprint) = @_;
804 $fingerprint =~ s/\$//;
807 fingerprint
=> $fingerprint,
811 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
815 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
816 $node->{'name'} = $1;
817 $node->{'address'} = $3;
818 $node->{'port'} = $4;
820 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
821 $node->{'country_code'} = $country_code;
824 } elsif ($_ =~ /^s (.*)$/) {
825 $node->{'flags'} = split(" ", $1);
827 foreach my $flag ($node->{'flags'}) {
828 if ($flag eq "Exit") {
829 $node->{'exit_node'}++;
834 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
835 $node->{'bandwidth'} = $1 * 8;
836 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
840 if (exists($node->{'name'})) {
845 sub TorAccountingStats
() {
849 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
850 if ($enabled ne '1') {
854 my @cmds = ("hibernating", "interval-start", "interval-end");
856 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
859 my @cmds = ("bytes", "bytes-left");
861 my $output = &TorGetInfo
($tor, "accounting/$_");
862 my @bytes = split(" ", $output);
864 $ret->{$_."_read"} = $bytes[0];
865 $ret->{$_."_written"} = $bytes[1];
874 my @units = ("B", "KB", "MB", "GB", "TB");
877 while (($units_index <= $#units) && ($bytes >= 1024)) {
882 return sprintf("%.2f %s", $bytes, $units[$units_index]);
885 sub FormatBitsPerSecond
() {
888 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
891 while (($units_index <= $#units) && ($bits >= 1024)) {
896 return sprintf("%.2f %s", $bits, $units[$units_index]);