]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/tor.cgi
6b4d9cc1b25b370905d5331f30c231dc70acd7c2
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 = (
57 my @accounting_periods = ('daily', 'weekly', 'monthly');
59 my $TOR_CONTROL_PORT = 9051;
61 our %netsettings = ();
62 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
66 $settings{'TOR_ENABLED'} = 'off';
67 $settings{'TOR_SOCKS_PORT'} = 9050;
68 $settings{'TOR_EXIT_COUNTRY'} = '';
69 $settings{'TOR_USE_EXIT_NODES'} = '';
70 $settings{'TOR_ALLOWED_SUBNETS'} = "$netsettings{'GREEN_NETADDRESS'}\/$netsettings{'GREEN_NETMASK'}";
71 if (&Header
::blue_used
()) {
72 $settings{'TOR_ALLOWED_SUBNETS'} .= ",$netsettings{'BLUE_NETADDRESS'}\/$netsettings{'BLUE_NETMASK'}";
75 $settings{'TOR_RELAY_ENABLED'} = 'off';
76 $settings{'TOR_RELAY_MODE'} = 'exit';
77 $settings{'TOR_RELAY_PORT'} = 9001;
78 $settings{'TOR_RELAY_NOADVERTISE'} = 'off';
79 $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0;
80 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
81 $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0;
82 $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily';
84 $settings{'ACTION'} = '';
86 my $errormessage = '';
89 &Header
::showhttpheaders
();
91 # Load settings from file.
92 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
95 &Header
::getcgihash
(\
%settings);
97 # Create tor command connection.
98 our $torctrl = &TorConnect
();
100 # Toggle enable/disable field.
101 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
102 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
103 $settings{'TOR_ALLOWED_SUBNETS'} = "";
105 s/^\s+//g; s/\s+$//g;
107 unless (&General
::validipandmask
($_)) {
108 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
110 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
114 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
115 $settings{'TOR_USE_EXIT_NODES'} = "";
117 s/^\s+//g; s/\s+$//g;
119 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
123 if ($errormessage eq '') {
124 # Write configuration settings to file.
125 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
127 # Update configuration files.
128 &BuildConfiguration
();
132 $settings{'ACTION'} = '';
137 # Close Tor control connection.
146 $checked{'TOR_ENABLED'}{'on'} = '';
147 $checked{'TOR_ENABLED'}{'off'} = '';
148 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
150 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
151 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
152 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
154 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
155 &Header
::openbigbox
('100%', 'left', '', $errormessage);
158 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
159 print "<font class='base'>$errormessage </font>\n";
163 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
165 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
170 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
173 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
174 <td width='20%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
175 <td width='25%' class='base'></td>
176 <td width='30%'></td>
179 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
180 <td width='20%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
181 <td width='25%' class='base'></td>
182 <td width='30%'></td>
189 if ($settings{'TOR_ENABLED'} eq 'on') {
190 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
191 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
193 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
194 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
196 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
201 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
202 <td width='30%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
203 <td width='50%' class='base' colspan='2'></td>
211 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
214 <td colspan='2' class='base' width='55%'>
215 $Lang::tr{'tor allowed subnets'}:
217 <td colspan='2' width='45%'></td>
220 <td colspan='2' class='base' width='55%'>
221 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
223 <td colspan='2' width='45%'></td>
231 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
234 <td colspan='2' class='base' width='55%'></td>
235 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
238 <td width='50%' colspan='2'>
239 <select name='TOR_EXIT_COUNTRY'>
240 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
243 my @country_names = Locale
::Country
::all_country_names
();
244 foreach my $country_name (sort @country_names) {
245 my $country_code = Locale
::Country
::country2code
($country_name);
246 $country_code = uc($country_code);
247 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
254 <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);
522 <td colspan='3' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
526 foreach my $node (@nodes) {
530 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
537 if (exists($node->{'country_code'})) {
538 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>";
542 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
544 <td width='30%' align='right'>
545 ~$node->{'bandwidth_string'}
558 &Header
::closebigbox
();
559 &Header
::closepage
();
562 sub BuildConfiguration
() {
564 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
566 my $torrc = "${General::swroot}/tor/torrc";
568 open(FILE
, ">$torrc");
571 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
573 if ($settings{'TOR_ENABLED'} eq 'on') {
574 my $strict_nodes = 0;
576 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
578 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
580 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
582 print FILE
"SocksPolicy reject *\n" if (@subnets);
584 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
587 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
590 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
593 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
595 print FILE
"ExitNode $_\n";
599 if ($strict_nodes > 0) {
600 print FILE
"StrictNodes 1\n";
604 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
605 # Reject access to private networks.
606 print FILE
"ExitPolicyRejectPrivate 1\n";
608 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
609 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
610 print FILE
" NoAdvertise";
614 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
615 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
618 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
619 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
622 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
623 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
626 # Limit to bridge mode.
629 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
633 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
636 print FILE
"PublishServerDescriptor 0\n";
639 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
640 print FILE
"ExitPolicy accept *:*\n";
643 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
644 print FILE
"ExitPolicy reject *:*\n";
647 if ($is_bridge > 0) {
648 print FILE
"BridgeRelay 1\n";
649 print FILE
"Exitpolicy reject *:*\n";
652 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
653 print FILE
"RelayBandwidthRate ";
654 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
657 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
658 print FILE
"RelayBandwidthBurst ";
659 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
664 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
665 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
667 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
668 print FILE
"AccountingStart day 00:00\n";
669 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
670 print FILE
"AccountingStart week 1 00:00\n";
671 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
672 print FILE
"AccountingStart month 1 00:00\n";
679 # Restart the service.
680 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
681 system("/usr/local/bin/torctrl restart");
683 system("/usr/local/bin/torctrl stop");
688 my $socket = new IO
::Socket
::INET
(
689 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
692 $socket->autoflush(1);
695 &TorSendCommand
($socket, "AUTHENTICATE");
700 sub TorSendCommand
() {
701 my ($socket, $cmd) = @_;
703 # Replace line ending with \r\n.
710 while (my $line = <$socket>) {
712 if ($line =~ /^.\r\n$/) {
716 # Command has been successfully executed.
717 if ($line =~ /250 OK/) {
721 } elsif ($line =~ /^5\d+/) {
725 # Remove line endings.
728 push(@output, $line);
735 sub TorSendCommandOneLine
() {
736 my ($tor, $cmd) = @_;
738 my @output = &TorSendCommand
($tor, $cmd);
743 my ($tor, $cmd) = @_;
745 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
747 my ($key, $value) = split("=", $output);
755 $socket->shutdown(2);
759 sub TorTrafficStats
() {
762 my $output_read = &TorGetInfo
($tor, "traffic/read");
763 my $output_written = &TorGetInfo
($tor, "traffic/written");
765 return ($output_read, $output_written);
768 sub TorRelayFingerprint
() {
771 return &TorGetInfo
($tor, "fingerprint");
774 sub TorORConnStatus
() {
778 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
780 $_ =~ s/^250[\+-]orconn-status=//;
783 next unless ($_ =~ /^\$/);
785 my @line = split(" ", $_);
786 my @node = split(/[=~]/, $line[0]);
788 my $node = &TorNodeDescription
($tor, $node[0]);
795 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
800 sub TorNodeDescription
() {
801 my ($tor, $fingerprint) = @_;
802 $fingerprint =~ s/\$//;
805 fingerprint
=> $fingerprint,
809 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
813 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
814 $node->{'name'} = $1;
815 $node->{'address'} = $3;
816 $node->{'port'} = $4;
818 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
819 $node->{'country_code'} = $country_code;
822 } elsif ($_ =~ /^s (.*)$/) {
823 $node->{'flags'} = split(" ", $1);
825 foreach my $flag ($node->{'flags'}) {
826 if ($flag eq "Exit") {
827 $node->{'exit_node'}++;
832 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
833 $node->{'bandwidth'} = $1 * 8;
834 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
838 if (exists($node->{'name'})) {
843 sub TorAccountingStats
() {
847 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
848 if ($enabled ne '1') {
852 my @cmds = ("hibernating", "interval-start", "interval-end");
854 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
857 my @cmds = ("bytes", "bytes-left");
859 my $output = &TorGetInfo
($tor, "accounting/$_");
860 my @bytes = split(" ", $output);
862 $ret->{$_."_read"} = $bytes[0];
863 $ret->{$_."_written"} = $bytes[1];
872 my @units = ("B", "KB", "MB", "GB", "TB");
875 while (($units_index <= $#units) && ($bytes >= 1024)) {
880 return sprintf("%.2f %s", $bytes, $units[$units_index]);
883 sub FormatBitsPerSecond
() {
886 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
889 while (($units_index <= $#units) && ($bits >= 1024)) {
894 return sprintf("%.2f %s", $bits, $units[$units_index]);