]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/tor.cgi
ed8964cdbd8f989dfd9812ff9462451e43bbd11b
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 if (!&General
::validport
($settings{'TOR_SOCKS_PORT'})) {
105 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
108 if (!&General
::validport
($settings{'TOR_RELAY_PORT'})) {
109 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
112 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
113 $settings{'TOR_ALLOWED_SUBNETS'} = "";
115 s/^\s+//g; s/\s+$//g;
117 unless (&General
::validipandmask
($_)) {
118 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
120 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
124 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
125 $settings{'TOR_USE_EXIT_NODES'} = "";
127 s/^\s+//g; s/\s+$//g;
129 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
133 # Burst bandwidth must be less or equal to bandwidth rate.
134 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
135 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
137 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
138 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
141 if ($errormessage eq '') {
142 # Write configuration settings to file.
143 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
145 # Update configuration files.
146 &BuildConfiguration
();
149 # Load settings from file.
150 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
155 # Close Tor control connection.
164 $checked{'TOR_ENABLED'}{'on'} = '';
165 $checked{'TOR_ENABLED'}{'off'} = '';
166 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
168 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
169 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
170 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
172 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
173 &Header
::openbigbox
('100%', 'left', '', $errormessage);
176 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
177 print "<font class='base'>$errormessage </font>\n";
181 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
183 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
188 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
191 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
192 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
193 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
194 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
197 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
198 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
199 <td width='25%' class='base'></td>
200 <td width='20%'></td>
205 if ($settings{'TOR_ENABLED'} eq 'on') {
206 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
207 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
209 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
210 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
219 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
222 <td colspan='2' class='base' width='55%'>
223 $Lang::tr{'tor allowed subnets'}:
225 <td colspan='2' width='45%'></td>
228 <td colspan='2' class='base' width='55%'>
229 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
231 <td colspan='2' width='45%'></td>
241 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
244 <td colspan='2' class='base' width='55%'></td>
245 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
248 <td width='50%' colspan='2'>
249 <select name='TOR_EXIT_COUNTRY'>
250 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
253 my @country_names = Locale
::Country
::all_country_names
();
254 foreach my $country_name (sort @country_names) {
255 my $country_code = Locale
::Country
::country2code
($country_name);
256 $country_code = uc($country_code);
257 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
263 <td width='50%' colspan='2'>
264 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
274 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
275 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
276 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
277 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
279 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
280 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
281 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
282 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
283 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
285 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
286 foreach (@bandwidth_limits) {
287 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
289 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
291 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
292 foreach (@bandwidth_limits) {
293 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
295 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
297 foreach (@accounting_periods) {
298 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
300 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
302 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
307 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
309 <select name='TOR_RELAY_MODE'>
310 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
311 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
312 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
313 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
316 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
318 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
322 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
324 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
326 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
328 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
332 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
334 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
336 <td colspan='2'></td>
339 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
340 <td width='75%' colspan='3'>
341 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
350 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
353 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
354 <td width='30%' class='base'>
355 <select name='TOR_RELAY_BANDWIDTH_RATE'>
358 foreach (@bandwidth_limits) {
360 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
362 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
367 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
370 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
372 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
376 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
377 <td width='20%' class='base'>
378 <select name='TOR_RELAY_BANDWIDTH_BURST'>
381 foreach (@bandwidth_limits) {
383 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
385 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
389 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
392 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
394 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
397 foreach (@accounting_periods) {
398 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
415 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
417 <td align='right'> </td>
426 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
432 # If we have a control connection, show the stats.
434 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
436 my @traffic = &TorTrafficStats
($torctrl);
443 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
444 my $fingerprint = &TorRelayFingerprint
($torctrl);
448 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
450 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
457 my $address = TorGetInfo
($torctrl, "address");
461 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
462 <td width='60%'>$address</td>
469 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
471 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
478 my $accounting = &TorAccountingStats
($torctrl);
483 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
487 if ($accounting->{'hibernating'} eq "hard") {
490 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
491 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
495 } elsif ($accounting->{'hibernating'} eq "soft") {
498 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
499 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
507 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
509 $accounting->{'interval-start'} - $accounting->{'interval-end'}
513 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
517 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
518 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
519 print " $Lang::tr{'tor accounting bytes left'})";
528 my @nodes = &TorORConnStatus
($torctrl);
530 my $nodes_length = scalar @nodes;
534 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
535 <td width='60%' colspan='2'>($nodes_length)</td>
539 foreach my $node (@nodes) {
543 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
550 if (exists($node->{'country_code'})) {
551 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>";
555 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
557 <td width='30%' align='right'>
558 ~$node->{'bandwidth_string'}
571 &Header
::closebigbox
();
572 &Header
::closepage
();
575 sub BuildConfiguration
() {
577 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
579 my $torrc = "${General::swroot}/tor/torrc";
581 open(FILE
, ">$torrc");
584 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
586 if ($settings{'TOR_ENABLED'} eq 'on') {
587 my $strict_nodes = 0;
589 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
591 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
593 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
595 print FILE
"SocksPolicy reject *\n" if (@subnets);
597 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
600 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
603 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
606 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
608 print FILE
"ExitNode $_\n";
612 if ($strict_nodes > 0) {
613 print FILE
"StrictNodes 1\n";
617 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
618 # Reject access to private networks.
619 print FILE
"ExitPolicyRejectPrivate 1\n";
621 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
622 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
623 print FILE
" NoAdvertise";
627 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
628 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
631 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
632 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
635 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
636 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
639 # Limit to bridge mode.
642 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
646 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
649 print FILE
"PublishServerDescriptor 0\n";
652 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
653 print FILE
"ExitPolicy accept *:*\n";
656 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
657 print FILE
"ExitPolicy reject *:*\n";
660 if ($is_bridge > 0) {
661 print FILE
"BridgeRelay 1\n";
662 print FILE
"Exitpolicy reject *:*\n";
665 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
666 print FILE
"RelayBandwidthRate ";
667 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
670 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
671 print FILE
"RelayBandwidthBurst ";
672 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
677 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
678 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
680 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
681 print FILE
"AccountingStart day 00:00\n";
682 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
683 print FILE
"AccountingStart week 1 00:00\n";
684 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
685 print FILE
"AccountingStart month 1 00:00\n";
692 # Restart the service.
693 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
694 system("/usr/local/bin/torctrl restart &>/dev/null");
696 system("/usr/local/bin/torctrl stop &>/dev/null");
701 my $socket = new IO
::Socket
::INET
(
702 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
705 $socket->autoflush(1);
708 &TorSendCommand
($socket, "AUTHENTICATE");
713 sub TorSendCommand
() {
714 my ($socket, $cmd) = @_;
716 # Replace line ending with \r\n.
723 while (my $line = <$socket>) {
725 if ($line =~ /^.\r\n$/) {
729 # Command has been successfully executed.
730 if ($line =~ /250 OK/) {
734 } elsif ($line =~ /^5\d+/) {
738 # Remove line endings.
741 push(@output, $line);
748 sub TorSendCommandOneLine
() {
749 my ($tor, $cmd) = @_;
751 my @output = &TorSendCommand
($tor, $cmd);
756 my ($tor, $cmd) = @_;
758 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
760 my ($key, $value) = split("=", $output);
768 $socket->shutdown(2);
772 sub TorTrafficStats
() {
775 my $output_read = &TorGetInfo
($tor, "traffic/read");
776 my $output_written = &TorGetInfo
($tor, "traffic/written");
778 return ($output_read, $output_written);
781 sub TorRelayFingerprint
() {
784 return &TorGetInfo
($tor, "fingerprint");
787 sub TorORConnStatus
() {
791 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
793 $_ =~ s/^250[\+-]orconn-status=//;
796 next unless ($_ =~ /^\$/);
798 my @line = split(" ", $_);
799 my @node = split(/[=~]/, $line[0]);
801 my $node = &TorNodeDescription
($tor, $node[0]);
808 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
813 sub TorNodeDescription
() {
814 my ($tor, $fingerprint) = @_;
815 $fingerprint =~ s/\$//;
818 fingerprint
=> $fingerprint,
822 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
826 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
827 $node->{'name'} = $1;
828 $node->{'address'} = $3;
829 $node->{'port'} = $4;
831 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
832 $node->{'country_code'} = $country_code;
835 } elsif ($_ =~ /^s (.*)$/) {
836 $node->{'flags'} = split(" ", $1);
838 foreach my $flag ($node->{'flags'}) {
839 if ($flag eq "Exit") {
840 $node->{'exit_node'}++;
845 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
846 $node->{'bandwidth'} = $1 * 8;
847 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
851 if (exists($node->{'name'})) {
856 sub TorAccountingStats
() {
860 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
861 if ($enabled ne '1') {
865 my @cmds = ("hibernating", "interval-start", "interval-end");
867 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
870 my @cmds = ("bytes", "bytes-left");
872 my $output = &TorGetInfo
($tor, "accounting/$_");
873 my @bytes = split(" ", $output);
875 $ret->{$_."_read"} = $bytes[0];
876 $ret->{$_."_written"} = $bytes[1];
885 my @units = ("B", "KB", "MB", "GB", "TB");
888 while (($units_index <= $#units) && ($bytes >= 1024)) {
893 return sprintf("%.2f %s", $bytes, $units[$units_index]);
896 sub FormatBitsPerSecond
() {
899 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
902 while (($units_index <= $#units) && ($bits >= 1024)) {
907 return sprintf("%.2f %s", $bits, $units[$units_index]);