]>
git.ipfire.org Git - people/teissler/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'} ne '') {
102 if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) {
103 $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}";
107 if (!&General
::validport
($settings{'TOR_SOCKS_PORT'})) {
108 $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}";
111 if (!&General
::validport
($settings{'TOR_RELAY_PORT'})) {
112 $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}";
115 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
116 if ((!&General
::validfqdn
($settings{'TOR_RELAY_ADDRESS'})) && (!&General
::validip
($settings{'TOR_RELAY_ADDRESS'}))) {
117 $errormessage = "$Lang::tr{'tor errmsg invalid relay address'}: $settings{'TOR_RELAY_ADDRESS'}";
121 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) {
122 $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}";
125 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
126 $settings{'TOR_ALLOWED_SUBNETS'} = "";
128 s/^\s+//g; s/\s+$//g;
130 unless (&General
::validipandmask
($_)) {
131 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
133 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
137 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
138 $settings{'TOR_USE_EXIT_NODES'} = "";
140 s/^\s+//g; s/\s+$//g;
142 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
146 # Burst bandwidth must be less or equal to bandwidth rate.
147 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
148 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
150 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
151 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
154 if ($errormessage eq '') {
155 # Write configuration settings to file.
156 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
158 # Update configuration files.
159 &BuildConfiguration
();
162 # Load settings from file.
163 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
168 # Close Tor control connection.
177 $checked{'TOR_ENABLED'}{'on'} = '';
178 $checked{'TOR_ENABLED'}{'off'} = '';
179 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
181 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
182 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
183 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
185 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
186 &Header
::openbigbox
('100%', 'left', '', $errormessage);
189 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
190 print "<font class='base'>$errormessage </font>\n";
194 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
196 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
201 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
204 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
205 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
206 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
207 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
210 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
211 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
212 <td width='25%' class='base'></td>
213 <td width='20%'></td>
218 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
219 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
221 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
222 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
231 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
234 <td colspan='2' class='base' width='55%'>
235 $Lang::tr{'tor allowed subnets'}:
237 <td colspan='2' width='45%'></td>
240 <td colspan='2' class='base' width='55%'>
241 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
243 <td colspan='2' width='45%'></td>
253 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
256 <td colspan='2' class='base' width='55%'></td>
257 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
260 <td width='50%' colspan='2'>
261 <select name='TOR_EXIT_COUNTRY'>
262 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
265 my @country_names = Locale
::Country
::all_country_names
();
266 foreach my $country_name (sort @country_names) {
267 my $country_code = Locale
::Country
::country2code
($country_name);
268 $country_code = uc($country_code);
269 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
275 <td width='50%' colspan='2'>
276 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
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>";
425 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
427 <td align='right'> </td>
436 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
442 # If we have a control connection, show the stats.
444 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
446 my @traffic = &TorTrafficStats
($torctrl);
453 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
454 my $fingerprint = &TorRelayFingerprint
($torctrl);
458 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
460 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
467 my $address = TorGetInfo
($torctrl, "address");
471 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
472 <td width='60%'>$address</td>
479 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
481 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
488 my $accounting = &TorAccountingStats
($torctrl);
493 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
497 if ($accounting->{'hibernating'} eq "hard") {
500 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
501 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
505 } elsif ($accounting->{'hibernating'} eq "soft") {
508 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
509 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
517 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
519 $accounting->{'interval-start'} - $accounting->{'interval-end'}
523 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
527 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
528 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
529 print " $Lang::tr{'tor accounting bytes left'})";
538 my @nodes = &TorORConnStatus
($torctrl);
540 my $nodes_length = scalar @nodes;
544 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
545 <td width='60%' colspan='2'>($nodes_length)</td>
549 foreach my $node (@nodes) {
553 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
560 if (exists($node->{'country_code'})) {
561 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>";
565 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
567 <td width='30%' align='right'>
568 ~$node->{'bandwidth_string'}
581 &Header
::closebigbox
();
582 &Header
::closepage
();
585 sub BuildConfiguration
() {
587 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
589 my $torrc = "${General::swroot}/tor/torrc";
591 open(FILE
, ">$torrc");
594 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
596 if ($settings{'TOR_ENABLED'} eq 'on') {
597 my $strict_nodes = 0;
599 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
601 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
603 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
605 print FILE
"SocksPolicy reject *\n" if (@subnets);
607 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
610 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
613 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
616 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
618 print FILE
"ExitNode $_\n";
622 if ($strict_nodes > 0) {
623 print FILE
"StrictNodes 1\n";
627 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
628 # Reject access to private networks.
629 print FILE
"ExitPolicyRejectPrivate 1\n";
631 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
632 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
633 print FILE
" NoAdvertise";
637 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
638 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
641 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
642 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
645 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
646 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
649 # Limit to bridge mode.
652 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
656 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
659 print FILE
"PublishServerDescriptor 0\n";
662 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
663 print FILE
"ExitPolicy accept *:*\n";
666 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
667 print FILE
"ExitPolicy reject *:*\n";
670 if ($is_bridge > 0) {
671 print FILE
"BridgeRelay 1\n";
672 print FILE
"Exitpolicy reject *:*\n";
675 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
676 print FILE
"RelayBandwidthRate ";
677 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
680 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
681 print FILE
"RelayBandwidthBurst ";
682 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
687 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
688 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
690 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
691 print FILE
"AccountingStart day 00:00\n";
692 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
693 print FILE
"AccountingStart week 1 00:00\n";
694 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
695 print FILE
"AccountingStart month 1 00:00\n";
702 # Restart the service.
703 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
704 system("/usr/local/bin/torctrl restart &>/dev/null");
706 system("/usr/local/bin/torctrl stop &>/dev/null");
711 my $socket = new IO
::Socket
::INET
(
712 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
715 $socket->autoflush(1);
718 &TorSendCommand
($socket, "AUTHENTICATE");
723 sub TorSendCommand
() {
724 my ($socket, $cmd) = @_;
726 # Replace line ending with \r\n.
733 while (my $line = <$socket>) {
735 if ($line =~ /^.\r\n$/) {
739 # Command has been successfully executed.
740 if ($line =~ /250 OK/) {
744 } elsif ($line =~ /^5\d+/) {
748 # Remove line endings.
751 push(@output, $line);
758 sub TorSendCommandOneLine
() {
759 my ($tor, $cmd) = @_;
761 my @output = &TorSendCommand
($tor, $cmd);
766 my ($tor, $cmd) = @_;
768 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
770 my ($key, $value) = split("=", $output);
778 $socket->shutdown(2);
782 sub TorTrafficStats
() {
785 my $output_read = &TorGetInfo
($tor, "traffic/read");
786 my $output_written = &TorGetInfo
($tor, "traffic/written");
788 return ($output_read, $output_written);
791 sub TorRelayFingerprint
() {
794 return &TorGetInfo
($tor, "fingerprint");
797 sub TorORConnStatus
() {
801 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
803 $_ =~ s/^250[\+-]orconn-status=//;
806 next unless ($_ =~ /^\$/);
808 my @line = split(" ", $_);
809 my @node = split(/[=~]/, $line[0]);
811 my $node = &TorNodeDescription
($tor, $node[0]);
818 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
823 sub TorNodeDescription
() {
824 my ($tor, $fingerprint) = @_;
825 $fingerprint =~ s/\$//;
828 fingerprint
=> $fingerprint,
832 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
836 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
837 $node->{'name'} = $1;
838 $node->{'address'} = $3;
839 $node->{'port'} = $4;
841 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
842 $node->{'country_code'} = $country_code;
845 } elsif ($_ =~ /^s (.*)$/) {
846 $node->{'flags'} = split(" ", $1);
848 foreach my $flag ($node->{'flags'}) {
849 if ($flag eq "Exit") {
850 $node->{'exit_node'}++;
855 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
856 $node->{'bandwidth'} = $1 * 8;
857 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
861 if (exists($node->{'name'})) {
866 sub TorAccountingStats
() {
870 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
871 if ($enabled ne '1') {
875 my @cmds = ("hibernating", "interval-start", "interval-end");
877 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
880 my @cmds = ("bytes", "bytes-left");
882 my $output = &TorGetInfo
($tor, "accounting/$_");
883 my @bytes = split(" ", $output);
885 $ret->{$_."_read"} = $bytes[0];
886 $ret->{$_."_written"} = $bytes[1];
895 my @units = ("B", "KB", "MB", "GB", "TB");
898 while (($units_index <= $#units) && ($bytes >= 1024)) {
903 return sprintf("%.2f %s", $bytes, $units[$units_index]);
906 sub FormatBitsPerSecond
() {
909 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
912 while (($units_index <= $#units) && ($bits >= 1024)) {
917 return sprintf("%.2f %s", $bits, $units[$units_index]);