]>
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 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
();
92 &Header
::getcgihash
(\
%settings);
94 # Create tor command connection.
95 our $torctrl = &TorConnect
();
97 # Toggle enable/disable field.
98 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
99 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
100 $settings{'TOR_ALLOWED_SUBNETS'} = "";
102 s/^\s+//g; s/\s+$//g;
104 unless (&General
::validipandmask
($_)) {
105 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
107 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
111 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
112 $settings{'TOR_USE_EXIT_NODES'} = "";
114 s/^\s+//g; s/\s+$//g;
116 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
120 if ($errormessage eq '') {
121 # Write configuration settings to file.
122 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
124 # Update configuration files.
125 &BuildConfiguration
();
129 $settings{'ACTION'} = '';
132 # Load settings from file.
133 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
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='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
175 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
176 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
179 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
180 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
181 <td width='25%' class='base'></td>
182 <td width='20%'></td>
187 if ($settings{'TOR_ENABLED'} eq 'on') {
188 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
189 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
191 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
192 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
201 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
204 <td colspan='2' class='base' width='55%'>
205 $Lang::tr{'tor allowed subnets'}:
207 <td colspan='2' width='45%'></td>
210 <td colspan='2' class='base' width='55%'>
211 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
213 <td colspan='2' width='45%'></td>
223 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
226 <td colspan='2' class='base' width='55%'></td>
227 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
230 <td width='50%' colspan='2'>
231 <select name='TOR_EXIT_COUNTRY'>
232 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
235 my @country_names = Locale
::Country
::all_country_names
();
236 foreach my $country_name (sort @country_names) {
237 my $country_code = Locale
::Country
::country2code
($country_name);
238 $country_code = uc($country_code);
239 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
245 <td width='50%' colspan='2'>
246 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
256 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
257 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
258 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
259 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
261 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
262 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
263 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
264 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
265 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
267 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
268 foreach (@bandwidth_limits) {
269 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
271 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
273 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
274 foreach (@bandwidth_limits) {
275 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
277 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
279 foreach (@accounting_periods) {
280 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
282 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
284 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
289 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
291 <select name='TOR_RELAY_MODE'>
292 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
293 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
294 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
295 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
298 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
300 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
304 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
306 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
308 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
310 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
314 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
316 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
318 <td colspan='2'></td>
321 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
322 <td width='75%' colspan='3'>
323 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
332 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
335 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
336 <td width='30%' class='base'>
337 <select name='TOR_RELAY_BANDWIDTH_RATE'>
340 foreach (@bandwidth_limits) {
342 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
344 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
349 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
352 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
354 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
358 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
359 <td width='20%' class='base'>
360 <select name='TOR_RELAY_BANDWIDTH_BURST'>
363 foreach (@bandwidth_limits) {
365 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
367 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
371 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
374 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
376 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
379 foreach (@accounting_periods) {
380 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
397 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
399 <td align='right'> </td>
408 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
414 # If we have a control connection, show the stats.
416 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
418 my @traffic = &TorTrafficStats
($torctrl);
425 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
426 my $fingerprint = &TorRelayFingerprint
($torctrl);
430 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
432 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
439 my $address = TorGetInfo
($torctrl, "address");
443 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
444 <td width='60%'>$address</td>
451 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
453 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
460 my $accounting = &TorAccountingStats
($torctrl);
465 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
469 if ($accounting->{'hibernating'} eq "hard") {
472 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
473 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
477 } elsif ($accounting->{'hibernating'} eq "soft") {
480 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
481 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
489 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
491 $accounting->{'interval-start'} - $accounting->{'interval-end'}
495 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
499 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
500 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
501 print " $Lang::tr{'tor accounting bytes left'})";
510 my @nodes = &TorORConnStatus
($torctrl);
512 my $nodes_length = scalar @nodes;
516 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
517 <td width='60%' colspan='2'>($nodes_length)</td>
521 foreach my $node (@nodes) {
525 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
532 if (exists($node->{'country_code'})) {
533 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>";
537 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
539 <td width='30%' align='right'>
540 ~$node->{'bandwidth_string'}
553 &Header
::closebigbox
();
554 &Header
::closepage
();
557 sub BuildConfiguration
() {
559 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
561 my $torrc = "${General::swroot}/tor/torrc";
563 open(FILE
, ">$torrc");
566 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
568 if ($settings{'TOR_ENABLED'} eq 'on') {
569 my $strict_nodes = 0;
571 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
573 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
575 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
577 print FILE
"SocksPolicy reject *\n" if (@subnets);
579 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
582 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
585 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
588 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
590 print FILE
"ExitNode $_\n";
594 if ($strict_nodes > 0) {
595 print FILE
"StrictNodes 1\n";
599 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
600 # Reject access to private networks.
601 print FILE
"ExitPolicyRejectPrivate 1\n";
603 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
604 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
605 print FILE
" NoAdvertise";
609 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
610 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
613 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
614 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
617 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
618 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
621 # Limit to bridge mode.
624 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
628 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
631 print FILE
"PublishServerDescriptor 0\n";
634 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
635 print FILE
"ExitPolicy accept *:*\n";
638 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
639 print FILE
"ExitPolicy reject *:*\n";
642 if ($is_bridge > 0) {
643 print FILE
"BridgeRelay 1\n";
644 print FILE
"Exitpolicy reject *:*\n";
647 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
648 print FILE
"RelayBandwidthRate ";
649 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
652 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
653 print FILE
"RelayBandwidthBurst ";
654 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
659 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
660 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
662 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
663 print FILE
"AccountingStart day 00:00\n";
664 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
665 print FILE
"AccountingStart week 1 00:00\n";
666 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
667 print FILE
"AccountingStart month 1 00:00\n";
674 # Restart the service.
675 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
676 system("/usr/local/bin/torctrl restart &>/dev/null");
678 system("/usr/local/bin/torctrl stop &>/dev/null");
683 my $socket = new IO
::Socket
::INET
(
684 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
687 $socket->autoflush(1);
690 &TorSendCommand
($socket, "AUTHENTICATE");
695 sub TorSendCommand
() {
696 my ($socket, $cmd) = @_;
698 # Replace line ending with \r\n.
705 while (my $line = <$socket>) {
707 if ($line =~ /^.\r\n$/) {
711 # Command has been successfully executed.
712 if ($line =~ /250 OK/) {
716 } elsif ($line =~ /^5\d+/) {
720 # Remove line endings.
723 push(@output, $line);
730 sub TorSendCommandOneLine
() {
731 my ($tor, $cmd) = @_;
733 my @output = &TorSendCommand
($tor, $cmd);
738 my ($tor, $cmd) = @_;
740 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
742 my ($key, $value) = split("=", $output);
750 $socket->shutdown(2);
754 sub TorTrafficStats
() {
757 my $output_read = &TorGetInfo
($tor, "traffic/read");
758 my $output_written = &TorGetInfo
($tor, "traffic/written");
760 return ($output_read, $output_written);
763 sub TorRelayFingerprint
() {
766 return &TorGetInfo
($tor, "fingerprint");
769 sub TorORConnStatus
() {
773 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
775 $_ =~ s/^250[\+-]orconn-status=//;
778 next unless ($_ =~ /^\$/);
780 my @line = split(" ", $_);
781 my @node = split(/[=~]/, $line[0]);
783 my $node = &TorNodeDescription
($tor, $node[0]);
790 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
795 sub TorNodeDescription
() {
796 my ($tor, $fingerprint) = @_;
797 $fingerprint =~ s/\$//;
800 fingerprint
=> $fingerprint,
804 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
808 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
809 $node->{'name'} = $1;
810 $node->{'address'} = $3;
811 $node->{'port'} = $4;
813 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
814 $node->{'country_code'} = $country_code;
817 } elsif ($_ =~ /^s (.*)$/) {
818 $node->{'flags'} = split(" ", $1);
820 foreach my $flag ($node->{'flags'}) {
821 if ($flag eq "Exit") {
822 $node->{'exit_node'}++;
827 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
828 $node->{'bandwidth'} = $1 * 8;
829 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
833 if (exists($node->{'name'})) {
838 sub TorAccountingStats
() {
842 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
843 if ($enabled ne '1') {
847 my @cmds = ("hibernating", "interval-start", "interval-end");
849 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
852 my @cmds = ("bytes", "bytes-left");
854 my $output = &TorGetInfo
($tor, "accounting/$_");
855 my @bytes = split(" ", $output);
857 $ret->{$_."_read"} = $bytes[0];
858 $ret->{$_."_written"} = $bytes[1];
867 my @units = ("B", "KB", "MB", "GB", "TB");
870 while (($units_index <= $#units) && ($bytes >= 1024)) {
875 return sprintf("%.2f %s", $bytes, $units[$units_index]);
878 sub FormatBitsPerSecond
() {
881 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
884 while (($units_index <= $#units) && ($bits >= 1024)) {
889 return sprintf("%.2f %s", $bits, $units[$units_index]);