]>
Commit | Line | Data |
---|---|---|
13b5ce6e MT |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5efe8957 | 5 | # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> # |
13b5ce6e MT |
6 | # # |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | use strict; | |
13b5ce6e MT |
23 | |
24 | # enable only the following on debugging purpose | |
ac2fdbb1 EK |
25 | #use warnings; |
26 | #use CGI::Carp 'fatalsToBrowser'; | |
13b5ce6e MT |
27 | |
28 | require '/var/ipfire/general-functions.pl'; | |
0893eef4 | 29 | require "${General::swroot}/location-functions.pl"; |
13b5ce6e MT |
30 | require "${General::swroot}/lang.pl"; |
31 | require "${General::swroot}/header.pl"; | |
32 | ||
33 | #workaround to suppress a warning when a variable is used only once | |
34 | my @dummy = ( ${Header::colouryellow} ); | |
35 | undef (@dummy); | |
36 | ||
37 | my @bandwidth_limits = ( | |
04f93219 | 38 | 1000 * 1024, # 1 GBit/s |
13b5ce6e MT |
39 | 500 * 1024, |
40 | 200 * 1024, | |
04f93219 | 41 | 100 * 1024, # 100 MBit/s |
13b5ce6e MT |
42 | 64 * 1024, |
43 | 50 * 1024, | |
44 | 25 * 1024, | |
45 | 20 * 1024, | |
46 | 16 * 1024, | |
47 | 10 * 1024, | |
48 | 8 * 1024, | |
49 | 4 * 1024, | |
50 | 2 * 1024, | |
04f93219 | 51 | 1024 # 1 MBit/s |
13b5ce6e MT |
52 | ); |
53 | my @accounting_periods = ('daily', 'weekly', 'monthly'); | |
54 | ||
55 | my $TOR_CONTROL_PORT = 9051; | |
56 | ||
3387469b JPT |
57 | my $string=(); |
58 | my $memory=(); | |
59 | my @memory=(); | |
60 | my @pid=(); | |
61 | my @tor=(); | |
62 | sub daemonstats | |
63 | { | |
64 | $memory = 0; | |
65 | # for pid and memory | |
66 | open(FILE, '/usr/local/bin/addonctrl tor status | '); | |
67 | @tor = <FILE>; | |
68 | close(FILE); | |
69 | $string = join("", @tor); | |
70 | $string =~ s/[a-z_]//gi; | |
71 | $string =~ s/\[[0-1]\;[0-9]+//gi; | |
72 | $string =~ s/[\(\)\.]//gi; | |
73 | $string =~ s/ //gi; | |
74 | $string =~ s/\e//gi; | |
75 | @pid = split(/\s/,$string); | |
76 | if (open(FILE, "/proc/$pid[0]/statm")){ | |
77 | my $temp = <FILE>; | |
78 | @memory = split(/ /,$temp); | |
79 | close(FILE); | |
80 | } | |
81 | $memory+=$memory[0]; | |
82 | } | |
83 | daemonstats(); | |
84 | ||
13b5ce6e MT |
85 | our %netsettings = (); |
86 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
87 | ||
3387469b JPT |
88 | our %color = (); |
89 | our %mainsettings = (); | |
90 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
91 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
92 | ||
13b5ce6e MT |
93 | our %settings = (); |
94 | ||
95 | $settings{'TOR_ENABLED'} = 'off'; | |
96 | $settings{'TOR_SOCKS_PORT'} = 9050; | |
97 | $settings{'TOR_EXIT_COUNTRY'} = ''; | |
98 | $settings{'TOR_USE_EXIT_NODES'} = ''; | |
99 | $settings{'TOR_ALLOWED_SUBNETS'} = "$netsettings{'GREEN_NETADDRESS'}\/$netsettings{'GREEN_NETMASK'}"; | |
100 | if (&Header::blue_used()) { | |
101 | $settings{'TOR_ALLOWED_SUBNETS'} .= ",$netsettings{'BLUE_NETADDRESS'}\/$netsettings{'BLUE_NETMASK'}"; | |
102 | } | |
103 | ||
104 | $settings{'TOR_RELAY_ENABLED'} = 'off'; | |
eea4969d | 105 | $settings{'TOR_RELAY_MODE'} = 'relay'; |
bd8b0330 | 106 | $settings{'TOR_RELAY_ADDRESS'} = ''; |
13b5ce6e | 107 | $settings{'TOR_RELAY_PORT'} = 9001; |
4245fe34 | 108 | $settings{'TOR_RELAY_DIRPORT'} = 0; |
b0449403 MT |
109 | $settings{'TOR_RELAY_NICKNAME'} = ''; |
110 | $settings{'TOR_RELAY_CONTACT_INFO'} = ''; | |
13b5ce6e MT |
111 | $settings{'TOR_RELAY_BANDWIDTH_RATE'} = 0; |
112 | $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0; | |
113 | $settings{'TOR_RELAY_ACCOUNTING_LIMIT'} = 0; | |
114 | $settings{'TOR_RELAY_ACCOUNTING_PERIOD'} = 'daily'; | |
115 | ||
116 | $settings{'ACTION'} = ''; | |
117 | ||
118 | my $errormessage = ''; | |
119 | my $warnmessage = ''; | |
120 | ||
121 | &Header::showhttpheaders(); | |
122 | ||
13b5ce6e MT |
123 | # Get GUI values. |
124 | &Header::getcgihash(\%settings); | |
125 | ||
126 | # Create tor command connection. | |
127 | our $torctrl = &TorConnect(); | |
128 | ||
129 | # Toggle enable/disable field. | |
130 | if ($settings{'ACTION'} eq $Lang::tr{'save'}) { | |
a03547fe MT |
131 | if ($settings{'TOR_RELAY_NICKNAME'} ne '') { |
132 | if ($settings{'TOR_RELAY_NICKNAME'} !~ /^[a-zA-Z0-9]+$/) { | |
133 | $errormessage = "$Lang::tr{'tor errmsg invalid relay name'}: $settings{'TOR_RELAY_NICKNAME'}"; | |
134 | } | |
b0449403 MT |
135 | } |
136 | ||
818f47d0 MT |
137 | if (!&General::validport($settings{'TOR_SOCKS_PORT'})) { |
138 | $errormessage = "$Lang::tr{'tor errmsg invalid socks port'}: $settings{'TOR_SOCKS_PORT'}"; | |
139 | } | |
140 | ||
141 | if (!&General::validport($settings{'TOR_RELAY_PORT'})) { | |
142 | $errormessage = "$Lang::tr{'tor errmsg invalid relay port'}: $settings{'TOR_RELAY_PORT'}"; | |
143 | } | |
4245fe34 JPT |
144 | if ($settings{'TOR_RELAY_DIRPORT'} ne '0') { |
145 | if (!&General::validport($settings{'TOR_RELAY_DIRPORT'})) { | |
146 | $errormessage = "$Lang::tr{'tor errmsg invalid directory port'}: $settings{'TOR_RELAY_DIRPORT'}"; | |
147 | } | |
148 | } | |
818f47d0 | 149 | |
bd8b0330 MT |
150 | if ($settings{'TOR_RELAY_ADDRESS'} ne '') { |
151 | if ((!&General::validfqdn($settings{'TOR_RELAY_ADDRESS'})) && (!&General::validip($settings{'TOR_RELAY_ADDRESS'}))) { | |
152 | $errormessage = "$Lang::tr{'tor errmsg invalid relay address'}: $settings{'TOR_RELAY_ADDRESS'}"; | |
153 | } | |
154 | } | |
155 | ||
56bf9f21 MT |
156 | if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} !~ /^\d+$/) { |
157 | $errormessage = "$Lang::tr{'tor errmsg invalid accounting limit'}: $settings{'TOR_RELAY_ACCOUNTING_LIMIT'}"; | |
158 | } | |
159 | ||
13b5ce6e MT |
160 | my @temp = split(/[\n,]/,$settings{'TOR_ALLOWED_SUBNETS'}); |
161 | $settings{'TOR_ALLOWED_SUBNETS'} = ""; | |
162 | foreach (@temp) { | |
163 | s/^\s+//g; s/\s+$//g; | |
164 | if ($_) { | |
165 | unless (&General::validipandmask($_)) { | |
166 | $errormessage = "$Lang::tr{'tor errmsg invalid ip or mask'}: $_"; | |
167 | } | |
168 | $settings{'TOR_ALLOWED_SUBNETS'} .= $_.","; | |
169 | } | |
170 | } | |
171 | ||
172 | @temp = split(/[\n,]/,$settings{'TOR_USE_EXIT_NODES'}); | |
173 | $settings{'TOR_USE_EXIT_NODES'} = ""; | |
174 | foreach (@temp) { | |
175 | s/^\s+//g; s/\s+$//g; | |
176 | if ($_) { | |
177 | $settings{'TOR_USE_EXIT_NODES'} .= $_.","; | |
178 | } | |
179 | } | |
180 | ||
3308f8d0 MT |
181 | # Burst bandwidth must be less or equal to bandwidth rate. |
182 | if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} == 0) { | |
183 | $settings{'TOR_RELAY_BANDWIDTH_BURST'} = 0; | |
184 | ||
185 | } elsif ($settings{'TOR_RELAY_BANDWIDTH_BURST'} < $settings{'TOR_RELAY_BANDWIDTH_RATE'}) { | |
186 | $settings{'TOR_RELAY_BANDWIDTH_BURST'} = $settings{'TOR_RELAY_BANDWIDTH_RATE'}; | |
187 | } | |
188 | ||
13b5ce6e MT |
189 | if ($errormessage eq '') { |
190 | # Write configuration settings to file. | |
191 | &General::writehash("${General::swroot}/tor/settings", \%settings); | |
192 | ||
193 | # Update configuration files. | |
194 | &BuildConfiguration(); | |
195 | } | |
b0449403 MT |
196 | } else { |
197 | # Load settings from file. | |
198 | &General::readhash("${General::swroot}/tor/settings", \%settings); | |
13b5ce6e MT |
199 | } |
200 | ||
201 | &showMainBox(); | |
202 | ||
203 | # Close Tor control connection. | |
204 | &TorClose($torctrl); | |
205 | ||
206 | # Functions | |
207 | ||
208 | sub showMainBox() { | |
209 | my %checked = (); | |
210 | my %selected = (); | |
211 | ||
212 | $checked{'TOR_ENABLED'}{'on'} = ''; | |
213 | $checked{'TOR_ENABLED'}{'off'} = ''; | |
214 | $checked{'TOR_ENABLED'}{$settings{'TOR_ENABLED'}} = 'checked'; | |
215 | ||
216 | $checked{'TOR_RELAY_ENABLED'}{'on'} = ''; | |
217 | $checked{'TOR_RELAY_ENABLED'}{'off'} = ''; | |
218 | $checked{'TOR_RELAY_ENABLED'}{$settings{'TOR_RELAY_ENABLED'}} = 'checked'; | |
219 | ||
220 | &Header::openpage($Lang::tr{'tor configuration'}, 1, ''); | |
221 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
222 | ||
223 | if ($errormessage) { | |
224 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
225 | print "<font class='base'>$errormessage </font>\n"; | |
226 | &Header::closebox(); | |
227 | } | |
228 | ||
229 | print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n"; | |
230 | ||
3387469b JPT |
231 | &Header::openbox('100%', 'center', $Lang::tr{'tor'}); |
232 | ||
233 | ||
234 | if ( ($memory != 0) && (@pid[0] ne "///") ){ | |
f76b104c AM |
235 | print "<table width='95%' cellspacing='0' class='tbl'>"; |
236 | print "<tr><th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'tor service'}</strong></th></tr>"; | |
3387469b JPT |
237 | print "<tr><td class='base'>$Lang::tr{'tor daemon'}</td>"; |
238 | print "<td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td></tr>"; | |
239 | print "<tr><td class='base'></td>"; | |
240 | print "<td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>"; | |
241 | print "<td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td></tr>"; | |
242 | print "<tr><td class='base'></td>"; | |
243 | print "<td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>"; | |
244 | print "<td bgcolor='$color{'color22'}' align='center'>$memory KB</td></tr>"; | |
245 | print "</table>"; | |
246 | } else { | |
f76b104c AM |
247 | print "<table width='95%' cellspacing='0' class='tbl'>"; |
248 | print "<tr><th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'tor service'}</strong></th></tr>"; | |
3387469b JPT |
249 | print "<tr><td class='base'>$Lang::tr{'tor daemon'}</td>"; |
250 | print "<td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td></tr>"; | |
251 | print "</table>"; | |
252 | } | |
253 | ||
254 | &Header::closebox(); | |
255 | ||
256 | &Header::openbox('100%', 'center', $Lang::tr{'tor configuration'}); | |
13b5ce6e MT |
257 | |
258 | print <<END; | |
3387469b | 259 | <table width='95%'> |
13b5ce6e | 260 | <tr> |
3387469b | 261 | <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor common settings'}</b></td> |
13b5ce6e MT |
262 | </tr> |
263 | <tr> | |
264 | <td width='25%' class='base'>$Lang::tr{'tor enabled'}:</td> | |
005db206 | 265 | <td width='30%'><input type='checkbox' name='TOR_ENABLED' $checked{'TOR_ENABLED'}{'on'} /></td> |
e3edceeb | 266 | <td width='25%' class='base'>$Lang::tr{'tor socks port'}: <img src='/blob.gif' alt='*' /></td> |
005db206 | 267 | <td width='20%'><input type='text' name='TOR_SOCKS_PORT' value='$settings{'TOR_SOCKS_PORT'}' size='5' /></td> |
13b5ce6e MT |
268 | </tr> |
269 | <tr> | |
270 | <td width='25%' class='base'>$Lang::tr{'tor relay enabled'}:</td> | |
005db206 | 271 | <td width='30%'><input type='checkbox' name='TOR_RELAY_ENABLED' $checked{'TOR_RELAY_ENABLED'}{'on'} /></td> |
13b5ce6e | 272 | <td width='25%' class='base'></td> |
005db206 | 273 | <td width='20%'></td> |
13b5ce6e MT |
274 | </tr> |
275 | </table> | |
276 | END | |
277 | ||
a03547fe MT |
278 | my @temp = split(",", $settings{'TOR_ALLOWED_SUBNETS'}); |
279 | $settings{'TOR_ALLOWED_SUBNETS'} = join("\n", @temp); | |
280 | ||
281 | @temp = split(",", $settings{'TOR_USE_EXIT_NODES'}); | |
282 | $settings{'TOR_USE_EXIT_NODES'} = join("\n", @temp); | |
283 | ||
284 | print <<END; | |
285 | <br> | |
a03547fe MT |
286 | <br> |
287 | ||
3387469b | 288 | <table width='95%'> |
a03547fe | 289 | <tr> |
3387469b | 290 | <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor acls'}</b></td> |
a03547fe MT |
291 | </tr> |
292 | <tr> | |
293 | <td colspan='2' class='base' width='55%'> | |
294 | $Lang::tr{'tor allowed subnets'}: | |
295 | </td> | |
296 | <td colspan='2' width='45%'></td> | |
297 | </tr> | |
298 | <tr> | |
299 | <td colspan='2' class='base' width='55%'> | |
300 | <textarea name='TOR_ALLOWED_SUBNETS' cols='32' rows='3' wrap='off'>$settings{'TOR_ALLOWED_SUBNETS'}</textarea> | |
301 | </td> | |
302 | <td colspan='2' width='45%'></td> | |
303 | </tr> | |
304 | </table> | |
305 | ||
306 | <br> | |
a03547fe MT |
307 | <br> |
308 | ||
3387469b | 309 | <table width='95%'> |
a03547fe | 310 | <tr> |
3387469b | 311 | <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor exit nodes'}</b></td> |
a03547fe MT |
312 | </tr> |
313 | <tr> | |
314 | <td colspan='2' class='base' width='55%'></td> | |
315 | <td colspan='2' class='base' width='45%'>$Lang::tr{'tor use exit nodes'}:</td> | |
316 | </tr> | |
317 | <tr> | |
318 | <td width='50%' colspan='2'> | |
5efe8957 | 319 | <select name='TOR_EXIT_COUNTRY' multiple='multiple'> |
a03547fe | 320 | <option value=''>- $Lang::tr{'tor exit country any'} -</option> |
13b5ce6e | 321 | END |
07e42be9 | 322 | my @country_codes = &Location::Functions::get_locations("no_special_locations"); |
5efe8957 PM |
323 | |
324 | # Convert Exit/Guard country strings into lists to make comparison easier | |
325 | my @exit_countries; | |
326 | if ($settings{'TOR_EXIT_COUNTRY'} ne '') { | |
327 | @exit_countries = split(/\|/, $settings{'TOR_EXIT_COUNTRY'}); | |
328 | } | |
329 | ||
8b452573 SS |
330 | foreach my $country_code (@country_codes) { |
331 | # Convert country code into upper case format. | |
a03547fe | 332 | $country_code = uc($country_code); |
8b452573 SS |
333 | |
334 | # Get country name. | |
5fe798b5 | 335 | my $country_name = &Location::Functions::get_full_country_name($country_code); |
8b452573 | 336 | |
26cce22d MT |
337 | print "<option value='$country_code'"; |
338 | ||
5efe8957 PM |
339 | if ($settings{'TOR_EXIT_COUNTRY'} ne '') { |
340 | print " selected" if grep /$country_code/, @exit_countries; | |
26cce22d MT |
341 | } |
342 | ||
343 | print ">$country_name ($country_code)</option>\n"; | |
a03547fe | 344 | } |
13b5ce6e | 345 | |
a03547fe MT |
346 | print <<END; |
347 | </select> | |
348 | </td> | |
349 | <td width='50%' colspan='2'> | |
350 | <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea> | |
351 | </td> | |
352 | </tr> | |
353 | </table> | |
13b5ce6e | 354 | END |
13b5ce6e | 355 | |
005db206 MT |
356 | &Header::closebox(); |
357 | ||
a03547fe | 358 | # Tor relay box |
a03547fe MT |
359 | $selected{'TOR_RELAY_MODE'}{'bridge'} = ''; |
360 | $selected{'TOR_RELAY_MODE'}{'exit'} = ''; | |
361 | $selected{'TOR_RELAY_MODE'}{'private-bridge'} = ''; | |
362 | $selected{'TOR_RELAY_MODE'}{'relay'} = ''; | |
363 | $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected'; | |
13b5ce6e | 364 | |
a03547fe MT |
365 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = ''; |
366 | foreach (@bandwidth_limits) { | |
367 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = ''; | |
368 | } | |
369 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected'; | |
13b5ce6e | 370 | |
a03547fe MT |
371 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = ''; |
372 | foreach (@bandwidth_limits) { | |
373 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = ''; | |
374 | } | |
375 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected'; | |
13b5ce6e | 376 | |
a03547fe MT |
377 | foreach (@accounting_periods) { |
378 | $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = ''; | |
379 | } | |
380 | $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected'; | |
381 | ||
3387469b | 382 | &Header::openbox('100%', 'center', $Lang::tr{'tor relay configuration'}); |
a03547fe MT |
383 | |
384 | print <<END; | |
3387469b | 385 | <table width='95%'> |
a03547fe MT |
386 | <tr> |
387 | <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td> | |
388 | <td width='30%'> | |
389 | <select name='TOR_RELAY_MODE'> | |
390 | <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option> | |
391 | <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option> | |
392 | <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option> | |
393 | <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option> | |
394 | </select> | |
395 | </td> | |
e3edceeb | 396 | <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}:</td> |
a03547fe | 397 | <td width='20%'> |
b31af085 | 398 | <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' maxlength='19' /> |
a03547fe MT |
399 | </td> |
400 | </tr> | |
401 | <tr> | |
e3edceeb | 402 | <td width='25%' class='base'>$Lang::tr{'tor relay address'}:</td> |
a03547fe MT |
403 | <td width='30%'> |
404 | <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' /> | |
405 | </td> | |
e3edceeb | 406 | <td width='25%' class='base'>$Lang::tr{'tor relay port'}: <img src='/blob.gif' alt='*' /></td> |
a03547fe | 407 | <td width='20%'> |
919a5020 | 408 | <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' /> |
a03547fe | 409 | </td> |
a03547fe | 410 | </tr> |
4245fe34 JPT |
411 | <tr> |
412 | <td width='25%'> </td> | |
413 | <td width='30%'> </td> | |
e3edceeb | 414 | <td width='25%' class='base'>$Lang::tr{'tor directory port'}: <img src='/blob.gif' alt='*' /></td> |
4245fe34 JPT |
415 | <td width='20%'> |
416 | <input type='text' name='TOR_RELAY_DIRPORT' value='$settings{'TOR_RELAY_DIRPORT'}' size='5' /> $Lang::tr{'tor 0 = disabled'} | |
417 | </td> | |
418 | </tr> | |
a03547fe | 419 | <tr> |
e3edceeb | 420 | <td width='25%' class='base'>$Lang::tr{'tor contact info'}:</td> |
a03547fe | 421 | <td width='75%' colspan='3'> |
919a5020 | 422 | <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' style='width: 98%;' /> |
a03547fe MT |
423 | </td> |
424 | </tr> | |
425 | </table> | |
426 | ||
3387469b | 427 | <br> |
a03547fe | 428 | |
3387469b | 429 | <table width='95%'> |
a03547fe | 430 | <tr> |
3387469b | 431 | <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor bandwidth settings'}</b></td> |
a03547fe MT |
432 | </tr> |
433 | <tr> | |
434 | <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td> | |
435 | <td width='30%' class='base'> | |
436 | <select name='TOR_RELAY_BANDWIDTH_RATE'> | |
13b5ce6e MT |
437 | END |
438 | ||
a03547fe MT |
439 | foreach (@bandwidth_limits) { |
440 | if ($_ >= 1024) { | |
f00699e8 | 441 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." Mbit/s</option>\n"; |
a03547fe | 442 | } else { |
f00699e8 | 443 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kbit/s</option>\n"; |
13b5ce6e | 444 | } |
a03547fe | 445 | } |
13b5ce6e | 446 | |
a03547fe MT |
447 | print <<END; |
448 | <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option> | |
449 | </select> | |
450 | </td> | |
e3edceeb | 451 | <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}: <img src='/blob.gif' alt='*' /></td> |
a03547fe MT |
452 | <td width='20%'> |
453 | <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' /> | |
454 | </td> | |
455 | </tr> | |
456 | <tr> | |
457 | <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td> | |
458 | <td width='20%' class='base'> | |
459 | <select name='TOR_RELAY_BANDWIDTH_BURST'> | |
13b5ce6e MT |
460 | END |
461 | ||
a03547fe MT |
462 | foreach (@bandwidth_limits) { |
463 | if ($_ >= 1024) { | |
f00699e8 | 464 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." Mbit/s</option>\n"; |
a03547fe | 465 | } else { |
f00699e8 | 466 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kbit/s</option>\n"; |
13b5ce6e | 467 | } |
a03547fe MT |
468 | } |
469 | print <<END; | |
470 | <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option> | |
471 | </select> | |
472 | </td> | |
473 | <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td> | |
474 | <td width='20%'> | |
475 | <select name='TOR_RELAY_ACCOUNTING_PERIOD'> | |
13b5ce6e MT |
476 | END |
477 | ||
a03547fe MT |
478 | foreach (@accounting_periods) { |
479 | print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>"; | |
480 | } | |
13b5ce6e | 481 | |
a03547fe MT |
482 | print <<END; |
483 | </select> | |
484 | </td> | |
485 | </tr> | |
486 | </table> | |
13b5ce6e MT |
487 | END |
488 | ||
a03547fe | 489 | &Header::closebox(); |
13b5ce6e MT |
490 | |
491 | print <<END; | |
3387469b | 492 | <table width='95%'> |
13b5ce6e | 493 | <tr> |
e3edceeb | 494 | <td><img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'required field'}</font></td> |
13b5ce6e MT |
495 | <td align='right'> </td> |
496 | </tr> | |
497 | </table> | |
498 | ||
499 | <hr> | |
500 | ||
3387469b | 501 | <table width='95%'> |
13b5ce6e MT |
502 | <tr> |
503 | <td> </td> | |
504 | <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td> | |
505 | <td> </td> | |
506 | </tr> | |
507 | </table> | |
508 | END | |
509 | ||
510 | # If we have a control connection, show the stats. | |
511 | if ($torctrl) { | |
3387469b | 512 | &Header::openbox('100%', 'center', $Lang::tr{'tor stats'}); |
13b5ce6e MT |
513 | |
514 | my @traffic = &TorTrafficStats($torctrl); | |
515 | ||
516 | if (@traffic) { | |
517 | print <<END; | |
3387469b | 518 | <table width='95%'> |
13b5ce6e MT |
519 | END |
520 | ||
521 | if ($settings{'TOR_RELAY_ENABLED'} eq 'on') { | |
522 | my $fingerprint = &TorRelayFingerprint($torctrl); | |
523 | if ($fingerprint) { | |
524 | print <<END; | |
525 | <tr> | |
526 | <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td> | |
527 | <td width='60%'> | |
0675a66d | 528 | <a href='https://metrics.torproject.org/rs.html#details/$fingerprint' target='_blank'>$fingerprint</a> |
13b5ce6e MT |
529 | </td> |
530 | </tr> | |
531 | END | |
532 | } | |
533 | } | |
534 | ||
535 | my $address = TorGetInfo($torctrl, "address"); | |
536 | if ($address) { | |
537 | print <<END; | |
538 | <tr> | |
539 | <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td> | |
540 | <td width='60%'>$address</td> | |
541 | </tr> | |
542 | END | |
543 | } | |
544 | ||
545 | print <<END; | |
546 | <tr> | |
547 | <td width='40%'>$Lang::tr{'tor traffic read written'}:</td> | |
548 | END | |
549 | print "<td width='60%'>" . &FormatBytes($traffic[0]) ."/". &FormatBytes($traffic[1]) . "</td>"; | |
550 | print <<END; | |
551 | </tr> | |
552 | </table> | |
553 | END | |
554 | } | |
555 | ||
556 | my $accounting = &TorAccountingStats($torctrl); | |
557 | if ($accounting) { | |
558 | print <<END; | |
3387469b | 559 | <table width='95%'> |
13b5ce6e MT |
560 | <tr> |
561 | <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td> | |
562 | </tr> | |
563 | END | |
564 | ||
565 | if ($accounting->{'hibernating'} eq "hard") { | |
566 | print <<END; | |
567 | <tr> | |
568 | <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'> | |
569 | <font color='white'>$Lang::tr{'tor traffic limit hard'}</font> | |
570 | </td> | |
571 | </tr> | |
572 | END | |
573 | } elsif ($accounting->{'hibernating'} eq "soft") { | |
574 | print <<END; | |
575 | <tr> | |
576 | <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'> | |
577 | <font color='white'>$Lang::tr{'tor traffic limit soft'}</font> | |
578 | </td> | |
579 | </tr> | |
580 | END | |
581 | } | |
582 | ||
583 | print <<END; | |
584 | <tr> | |
585 | <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td> | |
586 | <td width='60%'> | |
587 | $accounting->{'interval-start'} - $accounting->{'interval-end'} | |
588 | </td> | |
589 | </tr> | |
590 | <tr> | |
591 | <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td> | |
592 | <td width='60%'> | |
593 | END | |
594 | ||
595 | print &FormatBytes($accounting->{'bytes_read'}) . "/" . &FormatBytes($accounting->{'bytes_written'}); | |
596 | print " (" . &FormatBytes($accounting->{'bytes-left_read'}) . "/" . &FormatBytes($accounting->{'bytes-left_written'}); | |
597 | print " $Lang::tr{'tor accounting bytes left'})"; | |
598 | ||
599 | print <<END; | |
600 | </td> | |
601 | </tr> | |
602 | </table> | |
603 | END | |
604 | } | |
605 | ||
606 | my @nodes = &TorORConnStatus($torctrl); | |
607 | if (@nodes) { | |
f16bcc3e | 608 | my $nodes_length = scalar @nodes; |
13b5ce6e | 609 | print <<END; |
3387469b | 610 | <table width='95%'> |
13b5ce6e | 611 | <tr> |
f16bcc3e MT |
612 | <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td> |
613 | <td width='60%' colspan='2'>($nodes_length)</td> | |
13b5ce6e MT |
614 | </tr> |
615 | END | |
616 | ||
617 | foreach my $node (@nodes) { | |
618 | print <<END; | |
619 | <tr> | |
620 | <td width='40%'> | |
0675a66d | 621 | <a href='https://metrics.torproject.org/rs.html#details/$node->{'fingerprint'}' target='_blank'> |
13b5ce6e MT |
622 | $node->{'name'} |
623 | </a> | |
624 | </td> | |
625 | <td width='30%'> | |
626 | END | |
627 | ||
628 | if (exists($node->{'country_code'})) { | |
a9a28430 | 629 | # Get the flag icon of the country. |
0893eef4 | 630 | my $flag_icon = &Location::Functions::get_flag_icon($node->{'country_code'}); |
a9a28430 SS |
631 | |
632 | # Check if a flag for the given country is available. | |
633 | if ($flag_icon) { | |
634 | print "<a href='country.cgi#$node->{'country_code'}'><img src='$flag_icon' border='0' align='absmiddle' alt='$node->{'country_code'}'></a>"; | |
3387469b | 635 | } else { |
a9a28430 | 636 | print "<img src='/images/flags/blank.png' border='0' align='absmiddle'/>"; |
3387469b | 637 | } |
13b5ce6e MT |
638 | } |
639 | ||
640 | print <<END; | |
641 | <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'} | |
642 | </td> | |
643 | <td width='30%' align='right'> | |
644 | ~$node->{'bandwidth_string'} | |
645 | </td> | |
646 | </tr> | |
647 | END | |
648 | } | |
649 | print "</table>"; | |
650 | } | |
651 | ||
652 | &Header::closebox(); | |
653 | } | |
654 | ||
655 | print "</form>\n"; | |
656 | ||
657 | &Header::closebigbox(); | |
658 | &Header::closepage(); | |
659 | } | |
660 | ||
661 | sub BuildConfiguration() { | |
662 | my %settings = (); | |
663 | &General::readhash("${General::swroot}/tor/settings", \%settings); | |
664 | ||
665 | my $torrc = "${General::swroot}/tor/torrc"; | |
666 | ||
667 | open(FILE, ">$torrc"); | |
668 | ||
669 | # Global settings. | |
670 | print FILE "ControlPort $TOR_CONTROL_PORT\n"; | |
671 | ||
672 | if ($settings{'TOR_ENABLED'} eq 'on') { | |
673 | my $strict_nodes = 0; | |
674 | ||
675 | print FILE "SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n"; | |
676 | ||
677 | my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'}); | |
678 | foreach (@subnets) { | |
679 | print FILE "SocksPolicy accept $_\n" if (&General::validipandmask($_)); | |
680 | } | |
681 | print FILE "SocksPolicy reject *\n" if (@subnets); | |
682 | ||
683 | if ($settings{'TOR_EXIT_COUNTRY'} ne '') { | |
684 | $strict_nodes = 1; | |
5efe8957 PM |
685 | my $countrylist; |
686 | ||
687 | for my $singlecountry (split(/\|/, $settings{'TOR_EXIT_COUNTRY'})) { | |
688 | if ($countrylist eq '') { | |
689 | $countrylist = "{" . lc $singlecountry . "}"; | |
690 | } else { | |
691 | $countrylist = $countrylist . "," . "{" . lc $singlecountry . "}"; | |
692 | } | |
693 | } | |
13b5ce6e | 694 | |
5efe8957 | 695 | print FILE "ExitNodes $countrylist\n"; |
13b5ce6e MT |
696 | } |
697 | ||
698 | if ($settings{'TOR_USE_EXIT_NODES'} ne '') { | |
699 | $strict_nodes = 1; | |
700 | ||
701 | my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'}); | |
702 | foreach (@nodes) { | |
703 | print FILE "ExitNode $_\n"; | |
704 | } | |
705 | } | |
706 | ||
707 | if ($strict_nodes > 0) { | |
708 | print FILE "StrictNodes 1\n"; | |
709 | } | |
710 | } | |
711 | ||
712 | if ($settings{'TOR_RELAY_ENABLED'} eq 'on') { | |
713 | # Reject access to private networks. | |
714 | print FILE "ExitPolicyRejectPrivate 1\n"; | |
715 | ||
919a5020 | 716 | print FILE "ORPort $settings{'TOR_RELAY_PORT'}\n"; |
13b5ce6e | 717 | |
4245fe34 JPT |
718 | if ($settings{'TOR_RELAY_DIRPORT'} ne '0') { |
719 | print FILE "DirPort $settings{'TOR_RELAY_DIRPORT'}\n"; | |
720 | } | |
721 | ||
13b5ce6e MT |
722 | if ($settings{'TOR_RELAY_ADDRESS'} ne '') { |
723 | print FILE "Address $settings{'TOR_RELAY_ADDRESS'}\n"; | |
724 | } | |
725 | ||
726 | if ($settings{'TOR_RELAY_NICKNAME'} ne '') { | |
727 | print FILE "Nickname $settings{'TOR_RELAY_NICKNAME'}\n"; | |
728 | } | |
729 | ||
730 | if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') { | |
731 | print FILE "ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n"; | |
732 | } | |
733 | ||
734 | # Limit to bridge mode. | |
735 | my $is_bridge = 0; | |
736 | ||
737 | if ($settings{'TOR_RELAY_MODE'} eq 'bridge') { | |
738 | $is_bridge++; | |
739 | ||
740 | # Private bridge. | |
741 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') { | |
742 | $is_bridge++; | |
743 | ||
744 | print FILE "PublishServerDescriptor 0\n"; | |
745 | ||
746 | # Exit node. | |
747 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') { | |
748 | print FILE "ExitPolicy accept *:*\n"; | |
749 | ||
750 | # Relay only. | |
751 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') { | |
752 | print FILE "ExitPolicy reject *:*\n"; | |
753 | } | |
754 | ||
755 | if ($is_bridge > 0) { | |
756 | print FILE "BridgeRelay 1\n"; | |
757 | print FILE "Exitpolicy reject *:*\n"; | |
758 | } | |
759 | ||
760 | if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) { | |
761 | print FILE "RelayBandwidthRate "; | |
762 | print FILE $settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8; | |
763 | print FILE " KB\n"; | |
764 | ||
765 | if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) { | |
766 | print FILE "RelayBandwidthBurst "; | |
767 | print FILE $settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8; | |
768 | print FILE " KB\n"; | |
769 | } | |
770 | } | |
771 | ||
772 | if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) { | |
773 | print FILE "AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n"; | |
774 | ||
775 | if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') { | |
776 | print FILE "AccountingStart day 00:00\n"; | |
777 | } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') { | |
778 | print FILE "AccountingStart week 1 00:00\n"; | |
779 | } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') { | |
780 | print FILE "AccountingStart month 1 00:00\n"; | |
781 | } | |
782 | } | |
783 | } | |
784 | ||
785 | close(FILE); | |
786 | ||
787 | # Restart the service. | |
788 | if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) { | |
005db206 | 789 | system("/usr/local/bin/torctrl restart &>/dev/null"); |
13b5ce6e | 790 | } else { |
005db206 | 791 | system("/usr/local/bin/torctrl stop &>/dev/null"); |
13b5ce6e | 792 | } |
3387469b JPT |
793 | # Update pid and memory |
794 | daemonstats(); | |
13b5ce6e MT |
795 | } |
796 | ||
797 | sub TorConnect() { | |
798 | my $socket = new IO::Socket::INET( | |
799 | Proto => 'tcp', PeerAddr => '127.0.0.1', PeerPort => $TOR_CONTROL_PORT, | |
800 | ) or return; | |
801 | ||
802 | $socket->autoflush(1); | |
803 | ||
804 | # Authenticate. | |
805 | &TorSendCommand($socket, "AUTHENTICATE"); | |
806 | ||
807 | return $socket; | |
808 | } | |
809 | ||
810 | sub TorSendCommand() { | |
811 | my ($socket, $cmd) = @_; | |
812 | ||
813 | # Replace line ending with \r\n. | |
814 | chomp $cmd; | |
815 | $cmd .= "\r\n"; | |
816 | ||
817 | $socket->send($cmd); | |
818 | ||
819 | my @output = (); | |
820 | while (my $line = <$socket>) { | |
821 | # Skip empty lines. | |
822 | if ($line =~ /^.\r\n$/) { | |
823 | next; | |
824 | } | |
825 | ||
826 | # Command has been successfully executed. | |
827 | if ($line =~ /250 OK/) { | |
828 | last; | |
829 | ||
830 | # Error. | |
831 | } elsif ($line =~ /^5\d+/) { | |
832 | last; | |
833 | ||
834 | } else { | |
835 | # Remove line endings. | |
836 | $line =~ s/\r\n$//; | |
837 | ||
838 | push(@output, $line); | |
839 | } | |
840 | } | |
841 | ||
842 | return @output; | |
843 | } | |
844 | ||
845 | sub TorSendCommandOneLine() { | |
846 | my ($tor, $cmd) = @_; | |
847 | ||
848 | my @output = &TorSendCommand($tor, $cmd); | |
849 | return $output[0]; | |
850 | } | |
851 | ||
852 | sub TorGetInfo() { | |
853 | my ($tor, $cmd) = @_; | |
854 | ||
855 | my $output = &TorSendCommandOneLine($tor, "GETINFO ".$cmd); | |
856 | ||
857 | my ($key, $value) = split("=", $output); | |
858 | return $value; | |
859 | } | |
860 | ||
861 | sub TorClose() { | |
862 | my $socket = shift; | |
863 | ||
864 | if ($socket) { | |
865 | $socket->shutdown(2); | |
866 | } | |
867 | } | |
868 | ||
869 | sub TorTrafficStats() { | |
870 | my $tor = shift; | |
871 | ||
872 | my $output_read = &TorGetInfo($tor, "traffic/read"); | |
873 | my $output_written = &TorGetInfo($tor, "traffic/written"); | |
874 | ||
875 | return ($output_read, $output_written); | |
876 | } | |
877 | ||
878 | sub TorRelayFingerprint() { | |
879 | my $tor = shift; | |
880 | ||
881 | return &TorGetInfo($tor, "fingerprint"); | |
882 | } | |
883 | ||
884 | sub TorORConnStatus() { | |
885 | my $tor = shift; | |
886 | my @nodes = (); | |
887 | ||
888 | my @output = &TorSendCommand($tor, "GETINFO orconn-status"); | |
889 | foreach (@output) { | |
890 | $_ =~ s/^250[\+-]orconn-status=//; | |
891 | next if ($_ eq ""); | |
892 | last if ($_ eq "."); | |
893 | next unless ($_ =~ /^\$/); | |
894 | ||
895 | my @line = split(" ", $_); | |
896 | my @node = split(/[=~]/, $line[0]); | |
897 | ||
898 | my $node = &TorNodeDescription($tor, $node[0]); | |
899 | if ($node) { | |
900 | push(@nodes, $node); | |
901 | } | |
902 | } | |
903 | ||
904 | # Sort by names. | |
905 | @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes; | |
906 | ||
907 | return @nodes; | |
908 | } | |
909 | ||
910 | sub TorNodeDescription() { | |
911 | my ($tor, $fingerprint) = @_; | |
912 | $fingerprint =~ s/\$//; | |
913 | ||
914 | my $node = { | |
915 | fingerprint => $fingerprint, | |
916 | exit_node => 0, | |
917 | }; | |
918 | ||
919 | my @output = &TorSendCommand($tor, "GETINFO ns/id/$node->{'fingerprint'}"); | |
920 | ||
921 | foreach (@output) { | |
922 | # Router | |
923 | if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) { | |
924 | $node->{'name'} = $1; | |
925 | $node->{'address'} = $3; | |
926 | $node->{'port'} = $4; | |
927 | ||
07e42be9 | 928 | my $country_code = &Location::Functions::lookup_country_code($node->{'address'}); |
13b5ce6e MT |
929 | $node->{'country_code'} = $country_code; |
930 | ||
931 | # Flags | |
932 | } elsif ($_ =~ /^s (.*)$/) { | |
933 | $node->{'flags'} = split(" ", $1); | |
934 | ||
935 | foreach my $flag ($node->{'flags'}) { | |
936 | if ($flag eq "Exit") { | |
937 | $node->{'exit_node'}++; | |
938 | } | |
939 | } | |
940 | ||
941 | # Bandwidth | |
942 | } elsif ($_ =~ /^w Bandwidth=(\d+)/) { | |
943 | $node->{'bandwidth'} = $1 * 8; | |
944 | $node->{'bandwidth_string'} = &FormatBitsPerSecond($node->{'bandwidth'}); | |
945 | } | |
946 | } | |
947 | ||
948 | if (exists($node->{'name'})) { | |
949 | return $node; | |
950 | } | |
951 | } | |
952 | ||
953 | sub TorAccountingStats() { | |
954 | my $tor = shift; | |
955 | my $ret = {}; | |
956 | ||
957 | my $enabled = &TorGetInfo($tor, "accounting/enabled"); | |
958 | if ($enabled ne '1') { | |
959 | return; | |
960 | } | |
961 | ||
962 | my @cmds = ("hibernating", "interval-start", "interval-end"); | |
963 | foreach (@cmds) { | |
964 | $ret->{$_} = &TorGetInfo($tor, "accounting/$_"); | |
965 | } | |
966 | ||
967 | my @cmds = ("bytes", "bytes-left"); | |
968 | foreach (@cmds) { | |
969 | my $output = &TorGetInfo($tor, "accounting/$_"); | |
970 | my @bytes = split(" ", $output); | |
971 | ||
972 | $ret->{$_."_read"} = $bytes[0]; | |
973 | $ret->{$_."_written"} = $bytes[1]; | |
974 | } | |
975 | ||
976 | return $ret; | |
977 | } | |
978 | ||
979 | sub FormatBytes() { | |
980 | my $bytes = shift; | |
981 | ||
982 | my @units = ("B", "KB", "MB", "GB", "TB"); | |
983 | my $units_index = 0; | |
984 | ||
985 | while (($units_index <= $#units) && ($bytes >= 1024)) { | |
986 | $units_index++; | |
987 | $bytes /= 1024; | |
988 | } | |
989 | ||
990 | return sprintf("%.2f %s", $bytes, $units[$units_index]); | |
991 | } | |
992 | ||
993 | sub FormatBitsPerSecond() { | |
994 | my $bits = shift; | |
995 | ||
f00699e8 | 996 | my @units = ("bit/s", "kbit/s", "Mbit/s", "Gbit/s", "Tbit/s"); |
13b5ce6e MT |
997 | my $units_index = 0; |
998 | ||
999 | while (($units_index <= $#units) && ($bits >= 1024)) { | |
1000 | $units_index++; | |
1001 | $bits /= 1024; | |
1002 | } | |
1003 | ||
1004 | return sprintf("%.2f %s", $bits, $units[$units_index]); | |
1005 | } |