]>
git.ipfire.org Git - people/stevee/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_NICKNAME'} = '';
78 $settings{'TOR_RELAY_CONTACT_INFO'} = '';
79 $settings{'TOR_RELAY_NOADVERTISE'} = 'off';
80 $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0;
81 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
82 $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0;
83 $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily';
85 $settings{'ACTION'} = '';
87 my $errormessage = '';
90 &Header
::showhttpheaders
();
93 &Header
::getcgihash
(\
%settings);
95 # Create tor command connection.
96 our $torctrl = &TorConnect
();
98 # Toggle enable/disable field.
99 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
100 if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) {
101 $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}";
104 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
105 $settings{'TOR_ALLOWED_SUBNETS'} = "";
107 s/^\s+//g; s/\s+$//g;
109 unless (&General
::validipandmask
($_)) {
110 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
112 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
116 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
117 $settings{'TOR_USE_EXIT_NODES'} = "";
119 s/^\s+//g; s/\s+$//g;
121 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
125 # Burst bandwidth must be less or equal to bandwidth rate.
126 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
127 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
129 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
130 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
133 if ($errormessage eq '') {
134 # Write configuration settings to file.
135 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
137 # Update configuration files.
138 &BuildConfiguration
();
141 # Load settings from file.
142 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
147 # Close Tor control connection.
156 $checked{'TOR_ENABLED'}{'on'} = '';
157 $checked{'TOR_ENABLED'}{'off'} = '';
158 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
160 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
161 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
162 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
164 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
165 &Header
::openbigbox
('100%', 'left', '', $errormessage);
168 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
169 print "<font class='base'>$errormessage </font>\n";
173 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
175 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
180 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
183 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
184 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
185 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
186 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
189 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
190 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
191 <td width='25%' class='base'></td>
192 <td width='20%'></td>
197 if ($settings{'TOR_ENABLED'} eq 'on') {
198 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
199 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
201 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
202 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
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>
233 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
236 <td colspan='2' class='base' width='55%'></td>
237 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
240 <td width='50%' colspan='2'>
241 <select name='TOR_EXIT_COUNTRY'>
242 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
245 my @country_names = Locale
::Country
::all_country_names
();
246 foreach my $country_name (sort @country_names) {
247 my $country_code = Locale
::Country
::country2code
($country_name);
248 $country_code = uc($country_code);
249 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
255 <td width='50%' colspan='2'>
256 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
266 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
267 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
268 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
269 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
271 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
272 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
273 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
274 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
275 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
277 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
278 foreach (@bandwidth_limits) {
279 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
281 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
283 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
284 foreach (@bandwidth_limits) {
285 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
287 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
289 foreach (@accounting_periods) {
290 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
292 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
294 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
299 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
301 <select name='TOR_RELAY_MODE'>
302 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
303 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
304 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
305 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
308 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
310 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
314 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
316 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
318 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
320 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
324 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
326 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
328 <td colspan='2'></td>
331 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
332 <td width='75%' colspan='3'>
333 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
342 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
345 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
346 <td width='30%' class='base'>
347 <select name='TOR_RELAY_BANDWIDTH_RATE'>
350 foreach (@bandwidth_limits) {
352 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
354 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
359 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
362 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
364 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
368 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
369 <td width='20%' class='base'>
370 <select name='TOR_RELAY_BANDWIDTH_BURST'>
373 foreach (@bandwidth_limits) {
375 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
377 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
381 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
384 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
386 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
389 foreach (@accounting_periods) {
390 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
407 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
409 <td align='right'> </td>
418 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
424 # If we have a control connection, show the stats.
426 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
428 my @traffic = &TorTrafficStats
($torctrl);
435 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
436 my $fingerprint = &TorRelayFingerprint
($torctrl);
440 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
442 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
449 my $address = TorGetInfo
($torctrl, "address");
453 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
454 <td width='60%'>$address</td>
461 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
463 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
470 my $accounting = &TorAccountingStats
($torctrl);
475 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
479 if ($accounting->{'hibernating'} eq "hard") {
482 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
483 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
487 } elsif ($accounting->{'hibernating'} eq "soft") {
490 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
491 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
499 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
501 $accounting->{'interval-start'} - $accounting->{'interval-end'}
505 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
509 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
510 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
511 print " $Lang::tr{'tor accounting bytes left'})";
520 my @nodes = &TorORConnStatus
($torctrl);
522 my $nodes_length = scalar @nodes;
526 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
527 <td width='60%' colspan='2'>($nodes_length)</td>
531 foreach my $node (@nodes) {
535 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
542 if (exists($node->{'country_code'})) {
543 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>";
547 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
549 <td width='30%' align='right'>
550 ~$node->{'bandwidth_string'}
563 &Header
::closebigbox
();
564 &Header
::closepage
();
567 sub BuildConfiguration
() {
569 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
571 my $torrc = "${General::swroot}/tor/torrc";
573 open(FILE
, ">$torrc");
576 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
578 if ($settings{'TOR_ENABLED'} eq 'on') {
579 my $strict_nodes = 0;
581 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
583 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
585 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
587 print FILE
"SocksPolicy reject *\n" if (@subnets);
589 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
592 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
595 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
598 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
600 print FILE
"ExitNode $_\n";
604 if ($strict_nodes > 0) {
605 print FILE
"StrictNodes 1\n";
609 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
610 # Reject access to private networks.
611 print FILE
"ExitPolicyRejectPrivate 1\n";
613 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
614 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
615 print FILE
" NoAdvertise";
619 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
620 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
623 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
624 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
627 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
628 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
631 # Limit to bridge mode.
634 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
638 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
641 print FILE
"PublishServerDescriptor 0\n";
644 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
645 print FILE
"ExitPolicy accept *:*\n";
648 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
649 print FILE
"ExitPolicy reject *:*\n";
652 if ($is_bridge > 0) {
653 print FILE
"BridgeRelay 1\n";
654 print FILE
"Exitpolicy reject *:*\n";
657 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
658 print FILE
"RelayBandwidthRate ";
659 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
662 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
663 print FILE
"RelayBandwidthBurst ";
664 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
669 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
670 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
672 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
673 print FILE
"AccountingStart day 00:00\n";
674 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
675 print FILE
"AccountingStart week 1 00:00\n";
676 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
677 print FILE
"AccountingStart month 1 00:00\n";
684 # Restart the service.
685 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
686 system("/usr/local/bin/torctrl restart &>/dev/null");
688 system("/usr/local/bin/torctrl stop &>/dev/null");
693 my $socket = new IO
::Socket
::INET
(
694 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
697 $socket->autoflush(1);
700 &TorSendCommand
($socket, "AUTHENTICATE");
705 sub TorSendCommand
() {
706 my ($socket, $cmd) = @_;
708 # Replace line ending with \r\n.
715 while (my $line = <$socket>) {
717 if ($line =~ /^.\r\n$/) {
721 # Command has been successfully executed.
722 if ($line =~ /250 OK/) {
726 } elsif ($line =~ /^5\d+/) {
730 # Remove line endings.
733 push(@output, $line);
740 sub TorSendCommandOneLine
() {
741 my ($tor, $cmd) = @_;
743 my @output = &TorSendCommand
($tor, $cmd);
748 my ($tor, $cmd) = @_;
750 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
752 my ($key, $value) = split("=", $output);
760 $socket->shutdown(2);
764 sub TorTrafficStats
() {
767 my $output_read = &TorGetInfo
($tor, "traffic/read");
768 my $output_written = &TorGetInfo
($tor, "traffic/written");
770 return ($output_read, $output_written);
773 sub TorRelayFingerprint
() {
776 return &TorGetInfo
($tor, "fingerprint");
779 sub TorORConnStatus
() {
783 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
785 $_ =~ s/^250[\+-]orconn-status=//;
788 next unless ($_ =~ /^\$/);
790 my @line = split(" ", $_);
791 my @node = split(/[=~]/, $line[0]);
793 my $node = &TorNodeDescription
($tor, $node[0]);
800 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
805 sub TorNodeDescription
() {
806 my ($tor, $fingerprint) = @_;
807 $fingerprint =~ s/\$//;
810 fingerprint
=> $fingerprint,
814 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
818 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
819 $node->{'name'} = $1;
820 $node->{'address'} = $3;
821 $node->{'port'} = $4;
823 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
824 $node->{'country_code'} = $country_code;
827 } elsif ($_ =~ /^s (.*)$/) {
828 $node->{'flags'} = split(" ", $1);
830 foreach my $flag ($node->{'flags'}) {
831 if ($flag eq "Exit") {
832 $node->{'exit_node'}++;
837 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
838 $node->{'bandwidth'} = $1 * 8;
839 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
843 if (exists($node->{'name'})) {
848 sub TorAccountingStats
() {
852 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
853 if ($enabled ne '1') {
857 my @cmds = ("hibernating", "interval-start", "interval-end");
859 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
862 my @cmds = ("bytes", "bytes-left");
864 my $output = &TorGetInfo
($tor, "accounting/$_");
865 my @bytes = split(" ", $output);
867 $ret->{$_."_read"} = $bytes[0];
868 $ret->{$_."_written"} = $bytes[1];
877 my @units = ("B", "KB", "MB", "GB", "TB");
880 while (($units_index <= $#units) && ($bytes >= 1024)) {
885 return sprintf("%.2f %s", $bytes, $units[$units_index]);
888 sub FormatBitsPerSecond
() {
891 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
894 while (($units_index <= $#units) && ($bits >= 1024)) {
899 return sprintf("%.2f %s", $bits, $units[$units_index]);