]>
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_NOADVERTISE'} = 'off';
81 $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0;
82 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
83 $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0;
84 $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily';
86 $settings{'ACTION'} = '';
88 my $errormessage = '';
91 &Header
::showhttpheaders
();
94 &Header
::getcgihash
(\
%settings);
96 # Create tor command connection.
97 our $torctrl = &TorConnect
();
99 # Toggle enable/disable field.
100 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
101 if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) {
102 $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}";
105 if (!&General
::validport
($settings{'TOR_SOCKS_PORT'})) {
106 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
109 if (!&General
::validport
($settings{'TOR_RELAY_PORT'})) {
110 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
113 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
114 if ((!&General
::validfqdn
($settings{'TOR_RELAY_ADDRESS'})) && (!&General
::validip
($settings{'TOR_RELAY_ADDRESS'}))) {
115 $errormessage = "$Lang::tr{'tor errmsg invalid relay address'}: $settings{'TOR_RELAY_ADDRESS'}";
119 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
120 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
123 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
124 $settings{'TOR_ALLOWED_SUBNETS'} = "";
126 s/^\s+//g; s/\s+$//g;
128 unless (&General
::validipandmask
($_)) {
129 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
131 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
135 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
136 $settings{'TOR_USE_EXIT_NODES'} = "";
138 s/^\s+//g; s/\s+$//g;
140 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
144 # Burst bandwidth must be less or equal to bandwidth rate.
145 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
146 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
148 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
149 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
152 if ($errormessage eq '') {
153 # Write configuration settings to file.
154 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
156 # Update configuration files.
157 &BuildConfiguration
();
160 # Load settings from file.
161 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
166 # Close Tor control connection.
175 $checked{'TOR_ENABLED'}{'on'} = '';
176 $checked{'TOR_ENABLED'}{'off'} = '';
177 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
179 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
180 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
181 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
183 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
184 &Header
::openbigbox
('100%', 'left', '', $errormessage);
187 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
188 print "<font class='base'>$errormessage </font>\n";
192 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
194 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
199 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
202 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
203 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
204 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
205 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
208 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
209 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
210 <td width='25%' class='base'></td>
211 <td width='20%'></td>
216 if ($settings{'TOR_ENABLED'} eq 'on') {
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 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
286 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
287 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
288 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
290 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
291 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
292 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
293 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
294 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
296 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
297 foreach (@bandwidth_limits) {
298 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
300 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
302 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
303 foreach (@bandwidth_limits) {
304 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
306 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
308 foreach (@accounting_periods) {
309 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
311 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
313 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
318 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
320 <select name='TOR_RELAY_MODE'>
321 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
322 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
323 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
324 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
327 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
329 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
333 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
335 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
337 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
339 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
343 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
345 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
347 <td colspan='2'></td>
350 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
351 <td width='75%' colspan='3'>
352 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
361 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
364 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
365 <td width='30%' class='base'>
366 <select name='TOR_RELAY_BANDWIDTH_RATE'>
369 foreach (@bandwidth_limits) {
371 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
373 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
378 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
381 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
383 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
387 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
388 <td width='20%' class='base'>
389 <select name='TOR_RELAY_BANDWIDTH_BURST'>
392 foreach (@bandwidth_limits) {
394 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
396 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
400 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
403 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
405 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
408 foreach (@accounting_periods) {
409 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
426 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
428 <td align='right'> </td>
437 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
443 # If we have a control connection, show the stats.
445 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
447 my @traffic = &TorTrafficStats
($torctrl);
454 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
455 my $fingerprint = &TorRelayFingerprint
($torctrl);
459 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
461 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
468 my $address = TorGetInfo
($torctrl, "address");
472 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
473 <td width='60%'>$address</td>
480 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
482 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
489 my $accounting = &TorAccountingStats
($torctrl);
494 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
498 if ($accounting->{'hibernating'} eq "hard") {
501 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
502 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
506 } elsif ($accounting->{'hibernating'} eq "soft") {
509 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
510 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
518 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
520 $accounting->{'interval-start'} - $accounting->{'interval-end'}
524 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
528 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
529 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
530 print " $Lang::tr{'tor accounting bytes left'})";
539 my @nodes = &TorORConnStatus
($torctrl);
541 my $nodes_length = scalar @nodes;
545 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
546 <td width='60%' colspan='2'>($nodes_length)</td>
550 foreach my $node (@nodes) {
554 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
561 if (exists($node->{'country_code'})) {
562 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>";
566 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
568 <td width='30%' align='right'>
569 ~$node->{'bandwidth_string'}
582 &Header
::closebigbox
();
583 &Header
::closepage
();
586 sub BuildConfiguration
() {
588 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
590 my $torrc = "${General::swroot}/tor/torrc";
592 open(FILE
, ">$torrc");
595 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
597 if ($settings{'TOR_ENABLED'} eq 'on') {
598 my $strict_nodes = 0;
600 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
602 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
604 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
606 print FILE
"SocksPolicy reject *\n" if (@subnets);
608 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
611 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
614 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
617 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
619 print FILE
"ExitNode $_\n";
623 if ($strict_nodes > 0) {
624 print FILE
"StrictNodes 1\n";
628 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
629 # Reject access to private networks.
630 print FILE
"ExitPolicyRejectPrivate 1\n";
632 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
633 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
634 print FILE
" NoAdvertise";
638 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
639 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
642 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
643 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
646 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
647 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
650 # Limit to bridge mode.
653 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
657 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
660 print FILE
"PublishServerDescriptor 0\n";
663 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
664 print FILE
"ExitPolicy accept *:*\n";
667 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
668 print FILE
"ExitPolicy reject *:*\n";
671 if ($is_bridge > 0) {
672 print FILE
"BridgeRelay 1\n";
673 print FILE
"Exitpolicy reject *:*\n";
676 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
677 print FILE
"RelayBandwidthRate ";
678 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
681 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
682 print FILE
"RelayBandwidthBurst ";
683 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
688 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
689 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
691 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
692 print FILE
"AccountingStart day 00:00\n";
693 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
694 print FILE
"AccountingStart week 1 00:00\n";
695 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
696 print FILE
"AccountingStart month 1 00:00\n";
703 # Restart the service.
704 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
705 system("/usr/local/bin/torctrl restart &>/dev/null");
707 system("/usr/local/bin/torctrl stop &>/dev/null");
712 my $socket = new IO
::Socket
::INET
(
713 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
716 $socket->autoflush(1);
719 &TorSendCommand
($socket, "AUTHENTICATE");
724 sub TorSendCommand
() {
725 my ($socket, $cmd) = @_;
727 # Replace line ending with \r\n.
734 while (my $line = <$socket>) {
736 if ($line =~ /^.\r\n$/) {
740 # Command has been successfully executed.
741 if ($line =~ /250 OK/) {
745 } elsif ($line =~ /^5\d+/) {
749 # Remove line endings.
752 push(@output, $line);
759 sub TorSendCommandOneLine
() {
760 my ($tor, $cmd) = @_;
762 my @output = &TorSendCommand
($tor, $cmd);
767 my ($tor, $cmd) = @_;
769 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
771 my ($key, $value) = split("=", $output);
779 $socket->shutdown(2);
783 sub TorTrafficStats
() {
786 my $output_read = &TorGetInfo
($tor, "traffic/read");
787 my $output_written = &TorGetInfo
($tor, "traffic/written");
789 return ($output_read, $output_written);
792 sub TorRelayFingerprint
() {
795 return &TorGetInfo
($tor, "fingerprint");
798 sub TorORConnStatus
() {
802 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
804 $_ =~ s/^250[\+-]orconn-status=//;
807 next unless ($_ =~ /^\$/);
809 my @line = split(" ", $_);
810 my @node = split(/[=~]/, $line[0]);
812 my $node = &TorNodeDescription
($tor, $node[0]);
819 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
824 sub TorNodeDescription
() {
825 my ($tor, $fingerprint) = @_;
826 $fingerprint =~ s/\$//;
829 fingerprint
=> $fingerprint,
833 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
837 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
838 $node->{'name'} = $1;
839 $node->{'address'} = $3;
840 $node->{'port'} = $4;
842 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
843 $node->{'country_code'} = $country_code;
846 } elsif ($_ =~ /^s (.*)$/) {
847 $node->{'flags'} = split(" ", $1);
849 foreach my $flag ($node->{'flags'}) {
850 if ($flag eq "Exit") {
851 $node->{'exit_node'}++;
856 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
857 $node->{'bandwidth'} = $1 * 8;
858 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
862 if (exists($node->{'name'})) {
867 sub TorAccountingStats
() {
871 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
872 if ($enabled ne '1') {
876 my @cmds = ("hibernating", "interval-start", "interval-end");
878 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
881 my @cmds = ("bytes", "bytes-left");
883 my $output = &TorGetInfo
($tor, "accounting/$_");
884 my @bytes = split(" ", $output);
886 $ret->{$_."_read"} = $bytes[0];
887 $ret->{$_."_written"} = $bytes[1];
896 my @units = ("B", "KB", "MB", "GB", "TB");
899 while (($units_index <= $#units) && ($bytes >= 1024)) {
904 return sprintf("%.2f %s", $bytes, $units[$units_index]);
907 sub FormatBitsPerSecond
() {
910 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
913 while (($units_index <= $#units) && ($bits >= 1024)) {
918 return sprintf("%.2f %s", $bits, $units[$units_index]);