]>
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'} !~ /^[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 my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'});
120 $settings{'TOR_ALLOWED_SUBNETS'} = "";
122 s/^\s+//g; s/\s+$//g;
124 unless (&General
::validipandmask
($_)) {
125 $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_";
127 $settings{'TOR_ALLOWED_SUBNETS'} .= $_.",";
131 @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'});
132 $settings{'TOR_USE_EXIT_NODES'} = "";
134 s/^\s+//g; s/\s+$//g;
136 $settings{'TOR_USE_EXIT_NODES'} .= $_.",";
140 # Burst bandwidth must be less or equal to bandwidth rate.
141 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) {
142 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0;
144 } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) {
145 $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'};
148 if ($errormessage eq '') {
149 # Write configuration settings to file.
150 &General
::writehash
("${General::swroot}/tor/settings", \
%settings);
152 # Update configuration files.
153 &BuildConfiguration
();
156 # Load settings from file.
157 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
162 # Close Tor control connection.
171 $checked{'TOR_ENABLED'}{'on'} = '';
172 $checked{'TOR_ENABLED'}{'off'} = '';
173 $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked';
175 $checked{'TOR_RELAY_ENABLED'}{'on'} = '';
176 $checked{'TOR_RELAY_ENABLED'}{'off'} = '';
177 $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked';
179 &Header
::openpage
($Lang::tr
{'tor configuration'}, 1, '');
180 &Header
::openbigbox
('100%', 'left', '', $errormessage);
183 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
184 print "<font class='base'>$errormessage </font>\n";
188 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
190 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor configuration'});
195 <td colspan='4' class='base'><b>$Lang::tr{'tor common settings'}</b></td>
198 <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td>
199 <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td>
200 <td width='25%' class='base'>$Lang::tr{'tor socks port'}:</td>
201 <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td>
204 <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td>
205 <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td>
206 <td width='25%' class='base'></td>
207 <td width='20%'></td>
212 if ($settings{'TOR_ENABLED'} eq 'on') {
213 my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
214 $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp);
216 @temp = split(",", $settings{'TOR_USE_EXIT_NODES'});
217 $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp);
226 <td colspan='4' class='base'><b>$Lang::tr{'tor acls'}</b></td>
229 <td colspan='2' class='base' width='55%'>
230 $Lang::tr{'tor allowed subnets'}:
232 <td colspan='2' width='45%'></td>
235 <td colspan='2' class='base' width='55%'>
236 <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea>
238 <td colspan='2' width='45%'></td>
248 <td colspan='4' class='base'><b>$Lang::tr{'tor exit nodes'}</b></td>
251 <td colspan='2' class='base' width='55%'></td>
252 <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td>
255 <td width='50%' colspan='2'>
256 <select name='TOR_EXIT_COUNTRY'>
257 <option value=''>- $Lang::tr{'tor exit country any'} -</option>
260 my @country_names = Locale
::Country
::all_country_names
();
261 foreach my $country_name (sort @country_names) {
262 my $country_code = Locale
::Country
::country2code
($country_name);
263 $country_code = uc($country_code);
264 print "<option value='$country_code'>$country_name ($country_code)</option>\n";
270 <td width='50%' colspan='2'>
271 <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea>
281 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
282 $checked{'TOR_RELAY_NOADVERTISE'}{'on'} = '';
283 $checked{'TOR_RELAY_NOADVERTISE'}{'off'} = '';
284 $checked{'TOR_RELAY_NOADVERTISE'}{$settings{'TOR_RELAY_NOADVERTISE'}} = 'checked';
286 $selected{'TOR_RELAY_MODE'}{'bridge'} = '';
287 $selected{'TOR_RELAY_MODE'}{'exit'} = '';
288 $selected{'TOR_RELAY_MODE'}{'private-bridge'} = '';
289 $selected{'TOR_RELAY_MODE'}{'relay'} = '';
290 $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected';
292 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = '';
293 foreach (@bandwidth_limits) {
294 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = '';
296 $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected';
298 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = '';
299 foreach (@bandwidth_limits) {
300 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = '';
302 $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected';
304 foreach (@accounting_periods) {
305 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = '';
307 $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected';
309 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor relay configuration'});
314 <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td>
316 <select name='TOR_RELAY_MODE'>
317 <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option>
318 <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option>
319 <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option>
320 <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option>
323 <td width='25%' class='base'>$Lang::tr{'tor relay port'}:</td>
325 <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' />
329 <td width='25%' class='base'>$Lang::tr{'tor relay address'}: <img src='/blob.gif' alt='*' /></td>
331 <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' />
333 <td width='25%' class='base'>$Lang::tr{'tor do not advertise relay'}:</td>
335 <input type='checkbox' name='TOR_RELAY_NOADVERTISE' $checked{'TOR_RELAY_NOADVERTISE'}{'on'} />
339 <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}: <img src='/blob.gif' alt='*' /></td>
341 <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' />
343 <td colspan='2'></td>
346 <td width='25%' class='base'>$Lang::tr{'tor contact info'}: <img src='/blob.gif' alt='*' /></td>
347 <td width='75%' colspan='3'>
348 <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' size='60' />
357 <td colspan='4' class='base'><b>$Lang::tr{'tor bandwidth settings'}</b></td>
360 <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td>
361 <td width='30%' class='base'>
362 <select name='TOR_RELAY_BANDWIDTH_RATE'>
365 foreach (@bandwidth_limits) {
367 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
369 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kBit
/s</option
>\n";
374 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
377 <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}:</td>
379 <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' />
383 <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td>
384 <td width='20%' class='base'>
385 <select name='TOR_RELAY_BANDWIDTH_BURST'>
388 foreach (@bandwidth_limits) {
390 print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." MBit/s</option
>\n";
392 print "<option value
='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kBit
/s</option
>\n";
396 <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option>
399 <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td>
401 <select name='TOR_RELAY_ACCOUNTING_PERIOD'>
404 foreach (@accounting_periods) {
405 print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>";
422 <img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'this field may be blank'}</font>
424 <td align='right'> </td>
433 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
439 # If we have a control connection, show the stats.
441 &Header
::openbox
('100%', 'left', $Lang::tr
{'tor stats'});
443 my @traffic = &TorTrafficStats
($torctrl);
450 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
451 my $fingerprint = &TorRelayFingerprint
($torctrl);
455 <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td>
457 <a href='https://atlas.torproject.org/#details/$fingerprint' target='_blank'>$fingerprint</a>
464 my $address = TorGetInfo
($torctrl, "address");
468 <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td>
469 <td width='60%'>$address</td>
476 <td width='40%'>$Lang::tr{'tor traffic read written'}:</td>
478 print "<td width='60%'>" . &FormatBytes
($traffic[0]) ."/". &FormatBytes
($traffic[1]) . "</td>";
485 my $accounting = &TorAccountingStats
($torctrl);
490 <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td>
494 if ($accounting->{'hibernating'} eq "hard") {
497 <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'>
498 <font color='white'>$Lang::tr{'tor traffic limit hard'}</font>
502 } elsif ($accounting->{'hibernating'} eq "soft") {
505 <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'>
506 <font color='white'>$Lang::tr{'tor traffic limit soft'}</font>
514 <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td>
516 $accounting->{'interval-start'} - $accounting->{'interval-end'}
520 <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td>
524 print &FormatBytes
($accounting->{'bytes_read'}) . "/" . &FormatBytes
($accounting->{'bytes_written'});
525 print " (" . &FormatBytes
($accounting->{'bytes-left_read'}) . "/" . &FormatBytes
($accounting->{'bytes-left_written'});
526 print " $Lang::tr{'tor accounting bytes left'})";
535 my @nodes = &TorORConnStatus
($torctrl);
537 my $nodes_length = scalar @nodes;
541 <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td>
542 <td width='60%' colspan='2'>($nodes_length)</td>
546 foreach my $node (@nodes) {
550 <a href='https://atlas.torproject.org/#details/$node->{'fingerprint'}' target='_blank'>
557 if (exists($node->{'country_code'})) {
558 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>";
562 <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'}
564 <td width='30%' align='right'>
565 ~$node->{'bandwidth_string'}
578 &Header
::closebigbox
();
579 &Header
::closepage
();
582 sub BuildConfiguration
() {
584 &General
::readhash
("${General::swroot}/tor/settings", \
%settings);
586 my $torrc = "${General::swroot}/tor/torrc";
588 open(FILE
, ">$torrc");
591 print FILE
"ControlPort $TOR_CONTROL_PORT\n";
593 if ($settings{'TOR_ENABLED'} eq 'on') {
594 my $strict_nodes = 0;
596 print FILE
"SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n";
598 my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'});
600 print FILE
"SocksPolicy accept $_\n" if (&General
::validipandmask
($_));
602 print FILE
"SocksPolicy reject *\n" if (@subnets);
604 if ($settings{'TOR_EXIT_COUNTRY'} ne '') {
607 print FILE
"ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n";
610 if ($settings{'TOR_USE_EXIT_NODES'} ne '') {
613 my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'});
615 print FILE
"ExitNode $_\n";
619 if ($strict_nodes > 0) {
620 print FILE
"StrictNodes 1\n";
624 if ($settings{'TOR_RELAY_ENABLED'} eq 'on') {
625 # Reject access to private networks.
626 print FILE
"ExitPolicyRejectPrivate 1\n";
628 print FILE
"ORPort $settings{'TOR_RELAY_PORT'}";
629 if ($settings{'TOR_RELAY_NOADVERTISE'} eq 'on') {
630 print FILE
" NoAdvertise";
634 if ($settings{'TOR_RELAY_ADDRESS'} ne '') {
635 print FILE
"Address $settings{'TOR_RELAY_ADDRESS'}\n";
638 if ($settings{'TOR_RELAY_NICKNAME'} ne '') {
639 print FILE
"Nickname $settings{'TOR_RELAY_NICKNAME'}\n";
642 if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') {
643 print FILE
"ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n";
646 # Limit to bridge mode.
649 if ($settings{'TOR_RELAY_MODE'} eq 'bridge') {
653 } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') {
656 print FILE
"PublishServerDescriptor 0\n";
659 } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') {
660 print FILE
"ExitPolicy accept *:*\n";
663 } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') {
664 print FILE
"ExitPolicy reject *:*\n";
667 if ($is_bridge > 0) {
668 print FILE
"BridgeRelay 1\n";
669 print FILE
"Exitpolicy reject *:*\n";
672 if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) {
673 print FILE
"RelayBandwidthRate ";
674 print FILE
$settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8;
677 if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) {
678 print FILE
"RelayBandwidthBurst ";
679 print FILE
$settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8;
684 if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) {
685 print FILE
"AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n";
687 if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') {
688 print FILE
"AccountingStart day 00:00\n";
689 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') {
690 print FILE
"AccountingStart week 1 00:00\n";
691 } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') {
692 print FILE
"AccountingStart month 1 00:00\n";
699 # Restart the service.
700 if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) {
701 system("/usr/local/bin/torctrl restart &>/dev/null");
703 system("/usr/local/bin/torctrl stop &>/dev/null");
708 my $socket = new IO
::Socket
::INET
(
709 Proto
=> 'tcp', PeerAddr
=> '127.0.0.1', PeerPort
=> $TOR_CONTROL_PORT,
712 $socket->autoflush(1);
715 &TorSendCommand
($socket, "AUTHENTICATE");
720 sub TorSendCommand
() {
721 my ($socket, $cmd) = @_;
723 # Replace line ending with \r\n.
730 while (my $line = <$socket>) {
732 if ($line =~ /^.\r\n$/) {
736 # Command has been successfully executed.
737 if ($line =~ /250 OK/) {
741 } elsif ($line =~ /^5\d+/) {
745 # Remove line endings.
748 push(@output, $line);
755 sub TorSendCommandOneLine
() {
756 my ($tor, $cmd) = @_;
758 my @output = &TorSendCommand
($tor, $cmd);
763 my ($tor, $cmd) = @_;
765 my $output = &TorSendCommandOneLine
($tor, "GETINFO ".$cmd);
767 my ($key, $value) = split("=", $output);
775 $socket->shutdown(2);
779 sub TorTrafficStats
() {
782 my $output_read = &TorGetInfo
($tor, "traffic/read");
783 my $output_written = &TorGetInfo
($tor, "traffic/written");
785 return ($output_read, $output_written);
788 sub TorRelayFingerprint
() {
791 return &TorGetInfo
($tor, "fingerprint");
794 sub TorORConnStatus
() {
798 my @output = &TorSendCommand
($tor, "GETINFO orconn-status");
800 $_ =~ s/^250[\+-]orconn-status=//;
803 next unless ($_ =~ /^\$/);
805 my @line = split(" ", $_);
806 my @node = split(/[=~]/, $line[0]);
808 my $node = &TorNodeDescription
($tor, $node[0]);
815 @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes;
820 sub TorNodeDescription
() {
821 my ($tor, $fingerprint) = @_;
822 $fingerprint =~ s/\$//;
825 fingerprint
=> $fingerprint,
829 my @output = &TorSendCommand
($tor, "GETINFO ns/id/$node->{'fingerprint'}");
833 if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) {
834 $node->{'name'} = $1;
835 $node->{'address'} = $3;
836 $node->{'port'} = $4;
838 my $country_code = &TorGetInfo
($tor, "ip-to-country/$node->{'address'}");
839 $node->{'country_code'} = $country_code;
842 } elsif ($_ =~ /^s (.*)$/) {
843 $node->{'flags'} = split(" ", $1);
845 foreach my $flag ($node->{'flags'}) {
846 if ($flag eq "Exit") {
847 $node->{'exit_node'}++;
852 } elsif ($_ =~ /^w Bandwidth=(\d+)/) {
853 $node->{'bandwidth'} = $1 * 8;
854 $node->{'bandwidth_string'} = &FormatBitsPerSecond
($node->{'bandwidth'});
858 if (exists($node->{'name'})) {
863 sub TorAccountingStats
() {
867 my $enabled = &TorGetInfo
($tor, "accounting/enabled");
868 if ($enabled ne '1') {
872 my @cmds = ("hibernating", "interval-start", "interval-end");
874 $ret->{$_} = &TorGetInfo
($tor, "accounting/$_");
877 my @cmds = ("bytes", "bytes-left");
879 my $output = &TorGetInfo
($tor, "accounting/$_");
880 my @bytes = split(" ", $output);
882 $ret->{$_."_read"} = $bytes[0];
883 $ret->{$_."_written"} = $bytes[1];
892 my @units = ("B", "KB", "MB", "GB", "TB");
895 while (($units_index <= $#units) && ($bytes >= 1024)) {
900 return sprintf("%.2f %s", $bytes, $units[$units_index]);
903 sub FormatBitsPerSecond
() {
906 my @units = ("Bit/s", "KBit/s", "MBit/s", "GBit/s", "TBit/s");
909 while (($units_index <= $#units) && ($bits >= 1024)) {
914 return sprintf("%.2f %s", $bits, $units[$units_index]);