]>
git.ipfire.org Git - people/pmueller/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_ADDRESS'} = '';
77 $settings{'TOR_RELAY_PORT'} = 9001;
78 $settings{'TOR_RELAY_NICKNAME'} = '';
79 $settings{'TOR_RELAY_CONTACT_INFO'} = '';
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'} ne '') {
101 if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) {
102 $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}";
106 if (!&General
::validport
($settings{'TOR_SOCKS_PORT'})) {
107 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
110 if (!&General
::validport
($settings{'TOR_RELAY_PORT'})) {
111 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
114 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
115 if ((!&General
::validfqdn
($settings{'TOR_RELAY_ADDRESS'})) && (!&General
::validip
($settings{'TOR_RELAY_ADDRESS'}))) {
116 $errormessage = "$Lang::tr{'tor errmsg invalid relay address'}: $settings{'TOR_RELAY_ADDRESS'}";
120 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
121 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
124 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
125 $settings{'TOR_ALLOWED_SUBNETS'} = "";
127 s/^\s+//g; s/\s+$//g;
129 unless (&General
::validipandmask
($_)) {
130 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
132 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
136 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
137 $settings{'TOR_USE_EXIT_NODES'} = "";
139 s/^\s+//g; s/\s+$//g;
141 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
145 # Burst bandwidth must be less or equal to bandwidth rate.
146 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
147 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
149 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
150 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
153 if ($errormessage eq '') {
154 # Write configuration settings to file.
155 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
157 # Update configuration files.
158 &BuildConfiguration
();
161 # Load settings from file.
162 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
167 # Close Tor control connection.
176 $checked{'TOR_ENABLED'}{'on'} = '';
177 $checked{'TOR_ENABLED'}{'off'} = '';
178 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
180 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
181 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
182 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
184 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
185 &Header
::openbigbox
('100%', 'left', '', $errormessage);
188 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
189 print "<font class='base'>$errormessage </font>\n";
193 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
195 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
200 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
203 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
204 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
205 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
206 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
209 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
210 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
211 <td width='25%' class='base'></td>
212 <td width='20%'></td>
217 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
218 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
220 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
221 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
230 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
233 <td colspan='2' class='base' width='55%'>
234 $Lang::tr{'tor allowed subnets'}:
236 <td colspan='2' width='45%'></td>
239 <td colspan='2' class='base' width='55%'>
240 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
242 <td colspan='2' width='45%'></td>
252 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
255 <td colspan='2' class='base' width='55%'></td>
256 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
259 <td width='50%' colspan='2'>
260 <select name='TOR_EXIT_COUNTRY'>
261 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
264 my @country_names = Locale
::Country
::all_country_names
();
265 foreach my $country_name (sort @country_names) {
266 my $country_code = Locale
::Country
::country2code
($country_name);
267 $country_code = uc($country_code);
268 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
274 <td width='50%' colspan='2'>
275 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
285 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
286 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
287 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
288 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
289 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
291 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
292 foreach (@bandwidth_limits) {
293 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
295 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
297 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
298 foreach (@bandwidth_limits) {
299 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
301 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
303 foreach (@accounting_periods) {
304 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
306 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
308 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
313 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
315 <select name='TOR_RELAY_MODE'>
316 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
317 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
318 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
319 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
322 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
324 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
328 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
330 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
332 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
334 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
338 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
339 <td width='75%' colspan='3'>
340 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' style='width: 98%;' />
349 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
352 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
353 <td width='30%' class='base'>
354 <select name='TOR_RELAY_BANDWIDTH_RATE'>
357 foreach (@bandwidth_limits) {
359 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
361 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
366 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
369 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
371 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
375 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
376 <td width='20%' class='base'>
377 <select name='TOR_RELAY_BANDWIDTH_BURST'>
380 foreach (@bandwidth_limits) {
382 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
384 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
388 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
391 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
393 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
396 foreach (@accounting_periods) {
397 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
413 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
415 <td align='right'> </td>
424 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
430 # If we have a control connection, show the stats.
432 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
434 my @traffic = &TorTrafficStats
($torctrl);
441 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
442 my $fingerprint = &TorRelayFingerprint
($torctrl);
446 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
448 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
455 my $address = TorGetInfo
($torctrl, "address");
459 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
460 <td width='60%'>$address</td>
467 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
469 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
476 my $accounting = &TorAccountingStats
($torctrl);
481 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
485 if ($accounting->{'hibernating'} eq "hard") {
488 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
489 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
493 } elsif ($accounting->{'hibernating'} eq "soft") {
496 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
497 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
505 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
507 $accounting->{'interval-start'} - $accounting->{'interval-end'}
511 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
515 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
516 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
517 print " $Lang::tr{'tor accounting bytes left'})";
526 my @nodes = &TorORConnStatus
($torctrl);
528 my $nodes_length = scalar @nodes;
532 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
533 <td width='60%' colspan='2'>($nodes_length)</td>
537 foreach my $node (@nodes) {
541 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
548 if (exists($node->{'country_code'})) {
549 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>";
553 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
555 <td width='30%' align='right'>
556 ~$node->{'bandwidth_string'}
569 &Header
::closebigbox
();
570 &Header
::closepage
();
573 sub BuildConfiguration
() {
575 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
577 my $torrc = "${General::swroot}/tor/torrc";
579 open(FILE
, ">$torrc");
582 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
584 if ($settings{'TOR_ENABLED'} eq 'on') {
585 my $strict_nodes = 0;
587 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
589 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
591 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
593 print FILE
"SocksPolicy reject *\n" if (@subnets);
595 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
598 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
601 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
604 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
606 print FILE
"ExitNode $_\n";
610 if ($strict_nodes > 0) {
611 print FILE
"StrictNodes 1\n";
615 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
616 # Reject access to private networks.
617 print FILE
"ExitPolicyRejectPrivate 1\n";
619 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}\n";
621 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
622 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
625 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
626 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
629 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
630 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
633 # Limit to bridge mode.
636 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
640 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
643 print FILE
"PublishServerDescriptor 0\n";
646 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
647 print FILE
"ExitPolicy accept *:*\n";
650 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
651 print FILE
"ExitPolicy reject *:*\n";
654 if ($is_bridge > 0) {
655 print FILE
"BridgeRelay 1\n";
656 print FILE
"Exitpolicy reject *:*\n";
659 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
660 print FILE
"RelayBandwidthRate ";
661 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
664 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
665 print FILE
"RelayBandwidthBurst ";
666 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
671 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
672 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
674 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
675 print FILE
"AccountingStart day 00:00\n";
676 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
677 print FILE
"AccountingStart week 1 00:00\n";
678 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
679 print FILE
"AccountingStart month 1 00:00\n";
686 # Restart the service.
687 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
688 system("/usr/local/bin/torctrl restart &>/dev/null");
690 system("/usr/local/bin/torctrl stop &>/dev/null");
695 my $socket = new IO
::Socket
::INET
(
696 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
699 $socket->autoflush(1);
702 &TorSendCommand
($socket, "AUTHENTICATE");
707 sub TorSendCommand
() {
708 my ($socket, $cmd) = @_;
710 # Replace line ending with \r\n.
717 while (my $line = <$socket>) {
719 if ($line =~ /^.\r\n$/) {
723 # Command has been successfully executed.
724 if ($line =~ /250 OK/) {
728 } elsif ($line =~ /^5\d+/) {
732 # Remove line endings.
735 push(@output, $line);
742 sub TorSendCommandOneLine
() {
743 my ($tor, $cmd) = @_;
745 my @output = &TorSendCommand
($tor, $cmd);
750 my ($tor, $cmd) = @_;
752 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
754 my ($key, $value) = split("=", $output);
762 $socket->shutdown(2);
766 sub TorTrafficStats
() {
769 my $output_read = &TorGetInfo
($tor, "traffic/read");
770 my $output_written = &TorGetInfo
($tor, "traffic/written");
772 return ($output_read, $output_written);
775 sub TorRelayFingerprint
() {
778 return &TorGetInfo
($tor, "fingerprint");
781 sub TorORConnStatus
() {
785 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
787 $_ =~ s/^250[\+-]orconn-status=//;
790 next unless ($_ =~ /^\$/);
792 my @line = split(" ", $_);
793 my @node = split(/[=~]/, $line[0]);
795 my $node = &TorNodeDescription
($tor, $node[0]);
802 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
807 sub TorNodeDescription
() {
808 my ($tor, $fingerprint) = @_;
809 $fingerprint =~ s/\$//;
812 fingerprint
=> $fingerprint,
816 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
820 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
821 $node->{'name'} = $1;
822 $node->{'address'} = $3;
823 $node->{'port'} = $4;
825 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
826 $node->{'country_code'} = $country_code;
829 } elsif ($_ =~ /^s (.*)$/) {
830 $node->{'flags'} = split(" ", $1);
832 foreach my $flag ($node->{'flags'}) {
833 if ($flag eq "Exit") {
834 $node->{'exit_node'}++;
839 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
840 $node->{'bandwidth'} = $1 * 8;
841 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
845 if (exists($node->{'name'})) {
850 sub TorAccountingStats
() {
854 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
855 if ($enabled ne '1') {
859 my @cmds = ("hibernating", "interval-start", "interval-end");
861 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
864 my @cmds = ("bytes", "bytes-left");
866 my $output = &TorGetInfo
($tor, "accounting/$_");
867 my @bytes = split(" ", $output);
869 $ret->{$_."_read"} = $bytes[0];
870 $ret->{$_."_written"} = $bytes[1];
879 my @units = ("B", "KB", "MB", "GB", "TB");
882 while (($units_index <= $#units) && ($bytes >= 1024)) {
887 return sprintf("%.2f %s", $bytes, $units[$units_index]);
890 sub FormatBitsPerSecond
() {
893 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
896 while (($units_index <= $#units) && ($bits >= 1024)) {
901 return sprintf("%.2f %s", $bits, $units[$units_index]);