]>
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 = (
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 if ($errormessage eq '') {
120 # Write configuration settings to file.
121 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
123 # Update configuration files.
124 &BuildConfiguration
();
128 $settings{'ACTION'} = '';
131 # Load settings from file.
132 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
136 # Close Tor control connection.
145 $checked{'TOR_ENABLED'}{'on'} = '';
146 $checked{'TOR_ENABLED'}{'off'} = '';
147 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
149 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
150 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
151 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
153 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
154 &Header
::openbigbox
('100%', 'left', '', $errormessage);
157 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
158 print "<font class='base'>$errormessage </font>\n";
162 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
164 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
169 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
172 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
173 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
174 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
175 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
178 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
179 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
180 <td width='25%' class='base'></td>
181 <td width='20%'></td>
186 if ($settings{'TOR_ENABLED'} eq 'on') {
187 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
188 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
190 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
191 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
200 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
203 <td colspan='2' class='base' width='55%'>
204 $Lang::tr{'tor allowed subnets'}:
206 <td colspan='2' width='45%'></td>
209 <td colspan='2' class='base' width='55%'>
210 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
212 <td colspan='2' width='45%'></td>
222 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
225 <td colspan='2' class='base' width='55%'></td>
226 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
229 <td width='50%' colspan='2'>
230 <select name='TOR_EXIT_COUNTRY'>
231 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
234 my @country_names = Locale
::Country
::all_country_names
();
235 foreach my $country_name (sort @country_names) {
236 my $country_code = Locale
::Country
::country2code
($country_name);
237 $country_code = uc($country_code);
238 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
244 <td width='50%' colspan='2'>
245 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
255 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
256 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
257 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
258 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
260 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
261 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
262 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
263 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
264 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
266 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
267 foreach (@bandwidth_limits) {
268 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
270 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
272 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
273 foreach (@bandwidth_limits) {
274 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
276 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
278 foreach (@accounting_periods) {
279 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
281 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
283 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
288 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
290 <select name='TOR_RELAY_MODE'>
291 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
292 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
293 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
294 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
297 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
299 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
303 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
305 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
307 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
309 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
313 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
315 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
317 <td colspan='2'></td>
320 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
321 <td width='75%' colspan='3'>
322 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
331 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
334 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
335 <td width='30%' class='base'>
336 <select name='TOR_RELAY_BANDWIDTH_RATE'>
339 foreach (@bandwidth_limits) {
341 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
343 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
348 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
351 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
353 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
357 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
358 <td width='20%' class='base'>
359 <select name='TOR_RELAY_BANDWIDTH_BURST'>
362 foreach (@bandwidth_limits) {
364 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
366 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
370 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
373 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
375 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
378 foreach (@accounting_periods) {
379 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
396 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
398 <td align='right'> </td>
407 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
413 # If we have a control connection, show the stats.
415 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
417 my @traffic = &TorTrafficStats
($torctrl);
424 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
425 my $fingerprint = &TorRelayFingerprint
($torctrl);
429 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
431 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
438 my $address = TorGetInfo
($torctrl, "address");
442 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
443 <td width='60%'>$address</td>
450 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
452 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
459 my $accounting = &TorAccountingStats
($torctrl);
464 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
468 if ($accounting->{'hibernating'} eq "hard") {
471 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
472 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
476 } elsif ($accounting->{'hibernating'} eq "soft") {
479 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
480 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
488 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
490 $accounting->{'interval-start'} - $accounting->{'interval-end'}
494 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
498 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
499 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
500 print " $Lang::tr{'tor accounting bytes left'})";
509 my @nodes = &TorORConnStatus
($torctrl);
511 my $nodes_length = scalar @nodes;
515 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
516 <td width='60%' colspan='2'>($nodes_length)</td>
520 foreach my $node (@nodes) {
524 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
531 if (exists($node->{'country_code'})) {
532 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>";
536 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
538 <td width='30%' align='right'>
539 ~$node->{'bandwidth_string'}
552 &Header
::closebigbox
();
553 &Header
::closepage
();
556 sub BuildConfiguration
() {
558 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
560 my $torrc = "${General::swroot}/tor/torrc";
562 open(FILE
, ">$torrc");
565 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
567 if ($settings{'TOR_ENABLED'} eq 'on') {
568 my $strict_nodes = 0;
570 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
572 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
574 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
576 print FILE
"SocksPolicy reject *\n" if (@subnets);
578 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
581 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
584 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
587 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
589 print FILE
"ExitNode $_\n";
593 if ($strict_nodes > 0) {
594 print FILE
"StrictNodes 1\n";
598 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
599 # Reject access to private networks.
600 print FILE
"ExitPolicyRejectPrivate 1\n";
602 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
603 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
604 print FILE
" NoAdvertise";
608 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
609 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
612 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
613 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
616 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
617 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
620 # Limit to bridge mode.
623 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
627 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
630 print FILE
"PublishServerDescriptor 0\n";
633 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
634 print FILE
"ExitPolicy accept *:*\n";
637 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
638 print FILE
"ExitPolicy reject *:*\n";
641 if ($is_bridge > 0) {
642 print FILE
"BridgeRelay 1\n";
643 print FILE
"Exitpolicy reject *:*\n";
646 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
647 print FILE
"RelayBandwidthRate ";
648 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
651 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
652 print FILE
"RelayBandwidthBurst ";
653 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
658 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
659 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
661 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
662 print FILE
"AccountingStart day 00:00\n";
663 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
664 print FILE
"AccountingStart week 1 00:00\n";
665 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
666 print FILE
"AccountingStart month 1 00:00\n";
673 # Restart the service.
674 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
675 system("/usr/local/bin/torctrl restart &>/dev/null");
677 system("/usr/local/bin/torctrl stop &>/dev/null");
682 my $socket = new IO
::Socket
::INET
(
683 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
686 $socket->autoflush(1);
689 &TorSendCommand
($socket, "AUTHENTICATE");
694 sub TorSendCommand
() {
695 my ($socket, $cmd) = @_;
697 # Replace line ending with \r\n.
704 while (my $line = <$socket>) {
706 if ($line =~ /^.\r\n$/) {
710 # Command has been successfully executed.
711 if ($line =~ /250 OK/) {
715 } elsif ($line =~ /^5\d+/) {
719 # Remove line endings.
722 push(@output, $line);
729 sub TorSendCommandOneLine
() {
730 my ($tor, $cmd) = @_;
732 my @output = &TorSendCommand
($tor, $cmd);
737 my ($tor, $cmd) = @_;
739 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
741 my ($key, $value) = split("=", $output);
749 $socket->shutdown(2);
753 sub TorTrafficStats
() {
756 my $output_read = &TorGetInfo
($tor, "traffic/read");
757 my $output_written = &TorGetInfo
($tor, "traffic/written");
759 return ($output_read, $output_written);
762 sub TorRelayFingerprint
() {
765 return &TorGetInfo
($tor, "fingerprint");
768 sub TorORConnStatus
() {
772 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
774 $_ =~ s/^250[\+-]orconn-status=//;
777 next unless ($_ =~ /^\$/);
779 my @line = split(" ", $_);
780 my @node = split(/[=~]/, $line[0]);
782 my $node = &TorNodeDescription
($tor, $node[0]);
789 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
794 sub TorNodeDescription
() {
795 my ($tor, $fingerprint) = @_;
796 $fingerprint =~ s/\$//;
799 fingerprint
=> $fingerprint,
803 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
807 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
808 $node->{'name'} = $1;
809 $node->{'address'} = $3;
810 $node->{'port'} = $4;
812 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
813 $node->{'country_code'} = $country_code;
816 } elsif ($_ =~ /^s (.*)$/) {
817 $node->{'flags'} = split(" ", $1);
819 foreach my $flag ($node->{'flags'}) {
820 if ($flag eq "Exit") {
821 $node->{'exit_node'}++;
826 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
827 $node->{'bandwidth'} = $1 * 8;
828 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
832 if (exists($node->{'name'})) {
837 sub TorAccountingStats
() {
841 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
842 if ($enabled ne '1') {
846 my @cmds = ("hibernating", "interval-start", "interval-end");
848 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
851 my @cmds = ("bytes", "bytes-left");
853 my $output = &TorGetInfo
($tor, "accounting/$_");
854 my @bytes = split(" ", $output);
856 $ret->{$_."_read"} = $bytes[0];
857 $ret->{$_."_written"} = $bytes[1];
866 my @units = ("B", "KB", "MB", "GB", "TB");
869 while (($units_index <= $#units) && ($bytes >= 1024)) {
874 return sprintf("%.2f %s", $bytes, $units[$units_index]);
877 sub FormatBitsPerSecond
() {
880 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
883 while (($units_index <= $#units) && ($bits >= 1024)) {
888 return sprintf("%.2f %s", $bits, $units[$units_index]);