]>
Commit | Line | Data |
---|---|---|
13b5ce6e MT |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
04f93219 | 5 | # Copyright (C) 2013-2019 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'> | |
319 | <select name='TOR_EXIT_COUNTRY'> | |
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"); |
8b452573 SS |
323 | foreach my $country_code (@country_codes) { |
324 | # Convert country code into upper case format. | |
a03547fe | 325 | $country_code = uc($country_code); |
8b452573 SS |
326 | |
327 | # Get country name. | |
5fe798b5 | 328 | my $country_name = &Location::Functions::get_full_country_name($country_code); |
8b452573 | 329 | |
26cce22d MT |
330 | print "<option value='$country_code'"; |
331 | ||
332 | if ($settings{'TOR_EXIT_COUNTRY'} eq $country_code) { | |
333 | print " selected"; | |
334 | } | |
335 | ||
336 | print ">$country_name ($country_code)</option>\n"; | |
a03547fe | 337 | } |
13b5ce6e | 338 | |
a03547fe MT |
339 | print <<END; |
340 | </select> | |
341 | </td> | |
342 | <td width='50%' colspan='2'> | |
343 | <textarea name='TOR_USE_EXIT_NODES' cols='32' rows='3' wrap='off'>$settings{'TOR_USE_EXIT_NODES'}</textarea> | |
344 | </td> | |
345 | </tr> | |
346 | </table> | |
13b5ce6e | 347 | END |
13b5ce6e | 348 | |
005db206 MT |
349 | &Header::closebox(); |
350 | ||
a03547fe | 351 | # Tor relay box |
a03547fe MT |
352 | $selected{'TOR_RELAY_MODE'}{'bridge'} = ''; |
353 | $selected{'TOR_RELAY_MODE'}{'exit'} = ''; | |
354 | $selected{'TOR_RELAY_MODE'}{'private-bridge'} = ''; | |
355 | $selected{'TOR_RELAY_MODE'}{'relay'} = ''; | |
356 | $selected{'TOR_RELAY_MODE'}{$settings{'TOR_RELAY_MODE'}} = 'selected'; | |
13b5ce6e | 357 | |
a03547fe MT |
358 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'} = ''; |
359 | foreach (@bandwidth_limits) { | |
360 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_} = ''; | |
361 | } | |
362 | $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$settings{'TOR_RELAY_BANDWIDTH_RATE'}} = 'selected'; | |
13b5ce6e | 363 | |
a03547fe MT |
364 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'} = ''; |
365 | foreach (@bandwidth_limits) { | |
366 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_} = ''; | |
367 | } | |
368 | $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$settings{'TOR_RELAY_BANDWIDTH_BURST'}} = 'selected'; | |
13b5ce6e | 369 | |
a03547fe MT |
370 | foreach (@accounting_periods) { |
371 | $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_} = ''; | |
372 | } | |
373 | $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$settings{'TOR_RELAY_ACCOUNTING_PERIOD'}} = 'selected'; | |
374 | ||
3387469b | 375 | &Header::openbox('100%', 'center', $Lang::tr{'tor relay configuration'}); |
a03547fe MT |
376 | |
377 | print <<END; | |
3387469b | 378 | <table width='95%'> |
a03547fe MT |
379 | <tr> |
380 | <td width='25%' class='base'>$Lang::tr{'tor relay mode'}:</td> | |
381 | <td width='30%'> | |
382 | <select name='TOR_RELAY_MODE'> | |
383 | <option value='exit' $selected{'TOR_RELAY_MODE'}{'exit'}>$Lang::tr{'tor relay mode exit'}</option> | |
384 | <option value='relay' $selected{'TOR_RELAY_MODE'}{'relay'}>$Lang::tr{'tor relay mode relay'}</option> | |
385 | <option value='bridge' $selected{'TOR_RELAY_MODE'}{'bridge'}>$Lang::tr{'tor relay mode bridge'}</option> | |
386 | <option value='private-bridge' $selected{'TOR_RELAY_MODE'}{'private-bridge'}>$Lang::tr{'tor relay mode private bridge'}</option> | |
387 | </select> | |
388 | </td> | |
e3edceeb | 389 | <td width='25%' class='base'>$Lang::tr{'tor relay nickname'}:</td> |
a03547fe | 390 | <td width='20%'> |
b31af085 | 391 | <input type='text' name='TOR_RELAY_NICKNAME' value='$settings{'TOR_RELAY_NICKNAME'}' maxlength='19' /> |
a03547fe MT |
392 | </td> |
393 | </tr> | |
394 | <tr> | |
e3edceeb | 395 | <td width='25%' class='base'>$Lang::tr{'tor relay address'}:</td> |
a03547fe MT |
396 | <td width='30%'> |
397 | <input type='text' name='TOR_RELAY_ADDRESS' value='$settings{'TOR_RELAY_ADDRESS'}' /> | |
398 | </td> | |
e3edceeb | 399 | <td width='25%' class='base'>$Lang::tr{'tor relay port'}: <img src='/blob.gif' alt='*' /></td> |
a03547fe | 400 | <td width='20%'> |
919a5020 | 401 | <input type='text' name='TOR_RELAY_PORT' value='$settings{'TOR_RELAY_PORT'}' size='5' /> |
a03547fe | 402 | </td> |
a03547fe | 403 | </tr> |
4245fe34 JPT |
404 | <tr> |
405 | <td width='25%'> </td> | |
406 | <td width='30%'> </td> | |
e3edceeb | 407 | <td width='25%' class='base'>$Lang::tr{'tor directory port'}: <img src='/blob.gif' alt='*' /></td> |
4245fe34 JPT |
408 | <td width='20%'> |
409 | <input type='text' name='TOR_RELAY_DIRPORT' value='$settings{'TOR_RELAY_DIRPORT'}' size='5' /> $Lang::tr{'tor 0 = disabled'} | |
410 | </td> | |
411 | </tr> | |
a03547fe | 412 | <tr> |
e3edceeb | 413 | <td width='25%' class='base'>$Lang::tr{'tor contact info'}:</td> |
a03547fe | 414 | <td width='75%' colspan='3'> |
919a5020 | 415 | <input type='text' name='TOR_RELAY_CONTACT_INFO' value='$settings{'TOR_RELAY_CONTACT_INFO'}' style='width: 98%;' /> |
a03547fe MT |
416 | </td> |
417 | </tr> | |
418 | </table> | |
419 | ||
3387469b | 420 | <br> |
a03547fe | 421 | |
3387469b | 422 | <table width='95%'> |
a03547fe | 423 | <tr> |
3387469b | 424 | <td colspan='4' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'tor bandwidth settings'}</b></td> |
a03547fe MT |
425 | </tr> |
426 | <tr> | |
427 | <td width='25%' class='base'>$Lang::tr{'tor bandwidth rate'}:</td> | |
428 | <td width='30%' class='base'> | |
429 | <select name='TOR_RELAY_BANDWIDTH_RATE'> | |
13b5ce6e MT |
430 | END |
431 | ||
a03547fe MT |
432 | foreach (@bandwidth_limits) { |
433 | if ($_ >= 1024) { | |
f00699e8 | 434 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>". $_ / 1024 ." Mbit/s</option>\n"; |
a03547fe | 435 | } else { |
f00699e8 | 436 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{$_}>$_ kbit/s</option>\n"; |
13b5ce6e | 437 | } |
a03547fe | 438 | } |
13b5ce6e | 439 | |
a03547fe MT |
440 | print <<END; |
441 | <option value='0' $selected{'TOR_RELAY_BANDWIDTH_RATE'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option> | |
442 | </select> | |
443 | </td> | |
e3edceeb | 444 | <td width='25%' class='base'>$Lang::tr{'tor accounting limit'}: <img src='/blob.gif' alt='*' /></td> |
a03547fe MT |
445 | <td width='20%'> |
446 | <input type='text' name='TOR_RELAY_ACCOUNTING_LIMIT' value='$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}' size='12' /> | |
447 | </td> | |
448 | </tr> | |
449 | <tr> | |
450 | <td width='25%' class='base'>$Lang::tr{'tor bandwidth burst'}:</td> | |
451 | <td width='20%' class='base'> | |
452 | <select name='TOR_RELAY_BANDWIDTH_BURST'> | |
13b5ce6e MT |
453 | END |
454 | ||
a03547fe MT |
455 | foreach (@bandwidth_limits) { |
456 | if ($_ >= 1024) { | |
f00699e8 | 457 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>". $_ / 1024 ." Mbit/s</option>\n"; |
a03547fe | 458 | } else { |
f00699e8 | 459 | print "<option value='$_' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{$_}>$_ kbit/s</option>\n"; |
13b5ce6e | 460 | } |
a03547fe MT |
461 | } |
462 | print <<END; | |
463 | <option value='0' $selected{'TOR_RELAY_BANDWIDTH_BURST'}{'0'}>$Lang::tr{'tor bandwidth unlimited'}</option> | |
464 | </select> | |
465 | </td> | |
466 | <td width='25%' class='base'>$Lang::tr{'tor accounting period'}:</td> | |
467 | <td width='20%'> | |
468 | <select name='TOR_RELAY_ACCOUNTING_PERIOD'> | |
13b5ce6e MT |
469 | END |
470 | ||
a03547fe MT |
471 | foreach (@accounting_periods) { |
472 | print "<option value='$_' $selected{'TOR_RELAY_ACCOUNTING_PERIOD'}{$_}>$Lang::tr{'tor accounting period '.$_}</option>"; | |
473 | } | |
13b5ce6e | 474 | |
a03547fe MT |
475 | print <<END; |
476 | </select> | |
477 | </td> | |
478 | </tr> | |
479 | </table> | |
13b5ce6e MT |
480 | END |
481 | ||
a03547fe | 482 | &Header::closebox(); |
13b5ce6e MT |
483 | |
484 | print <<END; | |
3387469b | 485 | <table width='95%'> |
13b5ce6e | 486 | <tr> |
e3edceeb | 487 | <td><img src='/blob.gif' align='top' alt='*' /> <font class='base'>$Lang::tr{'required field'}</font></td> |
13b5ce6e MT |
488 | <td align='right'> </td> |
489 | </tr> | |
490 | </table> | |
491 | ||
492 | <hr> | |
493 | ||
3387469b | 494 | <table width='95%'> |
13b5ce6e MT |
495 | <tr> |
496 | <td> </td> | |
497 | <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td> | |
498 | <td> </td> | |
499 | </tr> | |
500 | </table> | |
501 | END | |
502 | ||
503 | # If we have a control connection, show the stats. | |
504 | if ($torctrl) { | |
3387469b | 505 | &Header::openbox('100%', 'center', $Lang::tr{'tor stats'}); |
13b5ce6e MT |
506 | |
507 | my @traffic = &TorTrafficStats($torctrl); | |
508 | ||
509 | if (@traffic) { | |
510 | print <<END; | |
3387469b | 511 | <table width='95%'> |
13b5ce6e MT |
512 | END |
513 | ||
514 | if ($settings{'TOR_RELAY_ENABLED'} eq 'on') { | |
515 | my $fingerprint = &TorRelayFingerprint($torctrl); | |
516 | if ($fingerprint) { | |
517 | print <<END; | |
518 | <tr> | |
519 | <td width='40%' class='base'>$Lang::tr{'tor relay fingerprint'}:</td> | |
520 | <td width='60%'> | |
0675a66d | 521 | <a href='https://metrics.torproject.org/rs.html#details/$fingerprint' target='_blank'>$fingerprint</a> |
13b5ce6e MT |
522 | </td> |
523 | </tr> | |
524 | END | |
525 | } | |
526 | } | |
527 | ||
528 | my $address = TorGetInfo($torctrl, "address"); | |
529 | if ($address) { | |
530 | print <<END; | |
531 | <tr> | |
532 | <td width='40%' class='base'>$Lang::tr{'tor relay external address'}:</td> | |
533 | <td width='60%'>$address</td> | |
534 | </tr> | |
535 | END | |
536 | } | |
537 | ||
538 | print <<END; | |
539 | <tr> | |
540 | <td width='40%'>$Lang::tr{'tor traffic read written'}:</td> | |
541 | END | |
542 | print "<td width='60%'>" . &FormatBytes($traffic[0]) ."/". &FormatBytes($traffic[1]) . "</td>"; | |
543 | print <<END; | |
544 | </tr> | |
545 | </table> | |
546 | END | |
547 | } | |
548 | ||
549 | my $accounting = &TorAccountingStats($torctrl); | |
550 | if ($accounting) { | |
551 | print <<END; | |
3387469b | 552 | <table width='95%'> |
13b5ce6e MT |
553 | <tr> |
554 | <td colspan='2' class='base'><b>$Lang::tr{'tor accounting'}</b></td> | |
555 | </tr> | |
556 | END | |
557 | ||
558 | if ($accounting->{'hibernating'} eq "hard") { | |
559 | print <<END; | |
560 | <tr> | |
561 | <td class='base' colspan='2' bgcolor="$Header::colourred" align='center'> | |
562 | <font color='white'>$Lang::tr{'tor traffic limit hard'}</font> | |
563 | </td> | |
564 | </tr> | |
565 | END | |
566 | } elsif ($accounting->{'hibernating'} eq "soft") { | |
567 | print <<END; | |
568 | <tr> | |
569 | <td class='base' colspan='2' bgcolor="$Header::colourorange" align='center'> | |
570 | <font color='white'>$Lang::tr{'tor traffic limit soft'}</font> | |
571 | </td> | |
572 | </tr> | |
573 | END | |
574 | } | |
575 | ||
576 | print <<END; | |
577 | <tr> | |
578 | <td width='40%' class='base'>$Lang::tr{'tor accounting interval'}</td> | |
579 | <td width='60%'> | |
580 | $accounting->{'interval-start'} - $accounting->{'interval-end'} | |
581 | </td> | |
582 | </tr> | |
583 | <tr> | |
584 | <td width='40%' class='base'>$Lang::tr{'tor accounting bytes'}</td> | |
585 | <td width='60%'> | |
586 | END | |
587 | ||
588 | print &FormatBytes($accounting->{'bytes_read'}) . "/" . &FormatBytes($accounting->{'bytes_written'}); | |
589 | print " (" . &FormatBytes($accounting->{'bytes-left_read'}) . "/" . &FormatBytes($accounting->{'bytes-left_written'}); | |
590 | print " $Lang::tr{'tor accounting bytes left'})"; | |
591 | ||
592 | print <<END; | |
593 | </td> | |
594 | </tr> | |
595 | </table> | |
596 | END | |
597 | } | |
598 | ||
599 | my @nodes = &TorORConnStatus($torctrl); | |
600 | if (@nodes) { | |
f16bcc3e | 601 | my $nodes_length = scalar @nodes; |
13b5ce6e | 602 | print <<END; |
3387469b | 603 | <table width='95%'> |
13b5ce6e | 604 | <tr> |
f16bcc3e MT |
605 | <td width='40%' class='base'><b>$Lang::tr{'tor connected relays'}</b></td> |
606 | <td width='60%' colspan='2'>($nodes_length)</td> | |
13b5ce6e MT |
607 | </tr> |
608 | END | |
609 | ||
610 | foreach my $node (@nodes) { | |
611 | print <<END; | |
612 | <tr> | |
613 | <td width='40%'> | |
0675a66d | 614 | <a href='https://metrics.torproject.org/rs.html#details/$node->{'fingerprint'}' target='_blank'> |
13b5ce6e MT |
615 | $node->{'name'} |
616 | </a> | |
617 | </td> | |
618 | <td width='30%'> | |
619 | END | |
620 | ||
621 | if (exists($node->{'country_code'})) { | |
a9a28430 | 622 | # Get the flag icon of the country. |
0893eef4 | 623 | my $flag_icon = &Location::Functions::get_flag_icon($node->{'country_code'}); |
a9a28430 SS |
624 | |
625 | # Check if a flag for the given country is available. | |
626 | if ($flag_icon) { | |
627 | print "<a href='country.cgi#$node->{'country_code'}'><img src='$flag_icon' border='0' align='absmiddle' alt='$node->{'country_code'}'></a>"; | |
3387469b | 628 | } else { |
a9a28430 | 629 | print "<img src='/images/flags/blank.png' border='0' align='absmiddle'/>"; |
3387469b | 630 | } |
13b5ce6e MT |
631 | } |
632 | ||
633 | print <<END; | |
634 | <a href='ipinfo.cgi?ip=$node->{'address'}'>$node->{'address'}</a>:$node->{'port'} | |
635 | </td> | |
636 | <td width='30%' align='right'> | |
637 | ~$node->{'bandwidth_string'} | |
638 | </td> | |
639 | </tr> | |
640 | END | |
641 | } | |
642 | print "</table>"; | |
643 | } | |
644 | ||
645 | &Header::closebox(); | |
646 | } | |
647 | ||
648 | print "</form>\n"; | |
649 | ||
650 | &Header::closebigbox(); | |
651 | &Header::closepage(); | |
652 | } | |
653 | ||
654 | sub BuildConfiguration() { | |
655 | my %settings = (); | |
656 | &General::readhash("${General::swroot}/tor/settings", \%settings); | |
657 | ||
658 | my $torrc = "${General::swroot}/tor/torrc"; | |
659 | ||
660 | open(FILE, ">$torrc"); | |
661 | ||
662 | # Global settings. | |
663 | print FILE "ControlPort $TOR_CONTROL_PORT\n"; | |
664 | ||
665 | if ($settings{'TOR_ENABLED'} eq 'on') { | |
666 | my $strict_nodes = 0; | |
667 | ||
668 | print FILE "SocksPort 0.0.0.0:$settings{'TOR_SOCKS_PORT'}\n"; | |
669 | ||
670 | my @subnets = split(",", $settings{'TOR_ALLOWED_SUBNETS'}); | |
671 | foreach (@subnets) { | |
672 | print FILE "SocksPolicy accept $_\n" if (&General::validipandmask($_)); | |
673 | } | |
674 | print FILE "SocksPolicy reject *\n" if (@subnets); | |
675 | ||
676 | if ($settings{'TOR_EXIT_COUNTRY'} ne '') { | |
677 | $strict_nodes = 1; | |
678 | ||
679 | print FILE "ExitNodes {$settings{'TOR_EXIT_COUNTRY'}}\n"; | |
680 | } | |
681 | ||
682 | if ($settings{'TOR_USE_EXIT_NODES'} ne '') { | |
683 | $strict_nodes = 1; | |
684 | ||
685 | my @nodes = split(",", $settings{'TOR_USE_EXIT_NODES'}); | |
686 | foreach (@nodes) { | |
687 | print FILE "ExitNode $_\n"; | |
688 | } | |
689 | } | |
690 | ||
691 | if ($strict_nodes > 0) { | |
692 | print FILE "StrictNodes 1\n"; | |
693 | } | |
694 | } | |
695 | ||
696 | if ($settings{'TOR_RELAY_ENABLED'} eq 'on') { | |
697 | # Reject access to private networks. | |
698 | print FILE "ExitPolicyRejectPrivate 1\n"; | |
699 | ||
919a5020 | 700 | print FILE "ORPort $settings{'TOR_RELAY_PORT'}\n"; |
13b5ce6e | 701 | |
4245fe34 JPT |
702 | if ($settings{'TOR_RELAY_DIRPORT'} ne '0') { |
703 | print FILE "DirPort $settings{'TOR_RELAY_DIRPORT'}\n"; | |
704 | } | |
705 | ||
13b5ce6e MT |
706 | if ($settings{'TOR_RELAY_ADDRESS'} ne '') { |
707 | print FILE "Address $settings{'TOR_RELAY_ADDRESS'}\n"; | |
708 | } | |
709 | ||
710 | if ($settings{'TOR_RELAY_NICKNAME'} ne '') { | |
711 | print FILE "Nickname $settings{'TOR_RELAY_NICKNAME'}\n"; | |
712 | } | |
713 | ||
714 | if ($settings{'TOR_RELAY_CONTACT_INFO'} ne '') { | |
715 | print FILE "ContactInfo $settings{'TOR_RELAY_CONTACT_INFO'}\n"; | |
716 | } | |
717 | ||
718 | # Limit to bridge mode. | |
719 | my $is_bridge = 0; | |
720 | ||
721 | if ($settings{'TOR_RELAY_MODE'} eq 'bridge') { | |
722 | $is_bridge++; | |
723 | ||
724 | # Private bridge. | |
725 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'private-bridge') { | |
726 | $is_bridge++; | |
727 | ||
728 | print FILE "PublishServerDescriptor 0\n"; | |
729 | ||
730 | # Exit node. | |
731 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'exit') { | |
732 | print FILE "ExitPolicy accept *:*\n"; | |
733 | ||
734 | # Relay only. | |
735 | } elsif ($settings{'TOR_RELAY_MODE'} eq 'relay') { | |
736 | print FILE "ExitPolicy reject *:*\n"; | |
737 | } | |
738 | ||
739 | if ($is_bridge > 0) { | |
740 | print FILE "BridgeRelay 1\n"; | |
741 | print FILE "Exitpolicy reject *:*\n"; | |
742 | } | |
743 | ||
744 | if ($settings{'TOR_RELAY_BANDWIDTH_RATE'} > 0) { | |
745 | print FILE "RelayBandwidthRate "; | |
746 | print FILE $settings{'TOR_RELAY_BANDWIDTH_RATE'} / 8; | |
747 | print FILE " KB\n"; | |
748 | ||
749 | if ($settings{'TOR_RELAY_BANDWIDTH_BURST'} > 0) { | |
750 | print FILE "RelayBandwidthBurst "; | |
751 | print FILE $settings{'TOR_RELAY_BANDWIDTH_BURST'} / 8; | |
752 | print FILE " KB\n"; | |
753 | } | |
754 | } | |
755 | ||
756 | if ($settings{'TOR_RELAY_ACCOUNTING_LIMIT'} > 0) { | |
757 | print FILE "AccountingMax ".$settings{'TOR_RELAY_ACCOUNTING_LIMIT'}." MB\n"; | |
758 | ||
759 | if ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'daily') { | |
760 | print FILE "AccountingStart day 00:00\n"; | |
761 | } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'weekly') { | |
762 | print FILE "AccountingStart week 1 00:00\n"; | |
763 | } elsif ($settings{'TOR_RELAY_ACCOUNTING_PERIOD'} eq 'monthly') { | |
764 | print FILE "AccountingStart month 1 00:00\n"; | |
765 | } | |
766 | } | |
767 | } | |
768 | ||
769 | close(FILE); | |
770 | ||
771 | # Restart the service. | |
772 | if (($settings{'TOR_ENABLED'} eq 'on') || ($settings{'TOR_RELAY_ENABLED'} eq 'on')) { | |
005db206 | 773 | system("/usr/local/bin/torctrl restart &>/dev/null"); |
13b5ce6e | 774 | } else { |
005db206 | 775 | system("/usr/local/bin/torctrl stop &>/dev/null"); |
13b5ce6e | 776 | } |
3387469b JPT |
777 | # Update pid and memory |
778 | daemonstats(); | |
13b5ce6e MT |
779 | } |
780 | ||
781 | sub TorConnect() { | |
782 | my $socket = new IO::Socket::INET( | |
783 | Proto => 'tcp', PeerAddr => '127.0.0.1', PeerPort => $TOR_CONTROL_PORT, | |
784 | ) or return; | |
785 | ||
786 | $socket->autoflush(1); | |
787 | ||
788 | # Authenticate. | |
789 | &TorSendCommand($socket, "AUTHENTICATE"); | |
790 | ||
791 | return $socket; | |
792 | } | |
793 | ||
794 | sub TorSendCommand() { | |
795 | my ($socket, $cmd) = @_; | |
796 | ||
797 | # Replace line ending with \r\n. | |
798 | chomp $cmd; | |
799 | $cmd .= "\r\n"; | |
800 | ||
801 | $socket->send($cmd); | |
802 | ||
803 | my @output = (); | |
804 | while (my $line = <$socket>) { | |
805 | # Skip empty lines. | |
806 | if ($line =~ /^.\r\n$/) { | |
807 | next; | |
808 | } | |
809 | ||
810 | # Command has been successfully executed. | |
811 | if ($line =~ /250 OK/) { | |
812 | last; | |
813 | ||
814 | # Error. | |
815 | } elsif ($line =~ /^5\d+/) { | |
816 | last; | |
817 | ||
818 | } else { | |
819 | # Remove line endings. | |
820 | $line =~ s/\r\n$//; | |
821 | ||
822 | push(@output, $line); | |
823 | } | |
824 | } | |
825 | ||
826 | return @output; | |
827 | } | |
828 | ||
829 | sub TorSendCommandOneLine() { | |
830 | my ($tor, $cmd) = @_; | |
831 | ||
832 | my @output = &TorSendCommand($tor, $cmd); | |
833 | return $output[0]; | |
834 | } | |
835 | ||
836 | sub TorGetInfo() { | |
837 | my ($tor, $cmd) = @_; | |
838 | ||
839 | my $output = &TorSendCommandOneLine($tor, "GETINFO ".$cmd); | |
840 | ||
841 | my ($key, $value) = split("=", $output); | |
842 | return $value; | |
843 | } | |
844 | ||
845 | sub TorClose() { | |
846 | my $socket = shift; | |
847 | ||
848 | if ($socket) { | |
849 | $socket->shutdown(2); | |
850 | } | |
851 | } | |
852 | ||
853 | sub TorTrafficStats() { | |
854 | my $tor = shift; | |
855 | ||
856 | my $output_read = &TorGetInfo($tor, "traffic/read"); | |
857 | my $output_written = &TorGetInfo($tor, "traffic/written"); | |
858 | ||
859 | return ($output_read, $output_written); | |
860 | } | |
861 | ||
862 | sub TorRelayFingerprint() { | |
863 | my $tor = shift; | |
864 | ||
865 | return &TorGetInfo($tor, "fingerprint"); | |
866 | } | |
867 | ||
868 | sub TorORConnStatus() { | |
869 | my $tor = shift; | |
870 | my @nodes = (); | |
871 | ||
872 | my @output = &TorSendCommand($tor, "GETINFO orconn-status"); | |
873 | foreach (@output) { | |
874 | $_ =~ s/^250[\+-]orconn-status=//; | |
875 | next if ($_ eq ""); | |
876 | last if ($_ eq "."); | |
877 | next unless ($_ =~ /^\$/); | |
878 | ||
879 | my @line = split(" ", $_); | |
880 | my @node = split(/[=~]/, $line[0]); | |
881 | ||
882 | my $node = &TorNodeDescription($tor, $node[0]); | |
883 | if ($node) { | |
884 | push(@nodes, $node); | |
885 | } | |
886 | } | |
887 | ||
888 | # Sort by names. | |
889 | @nodes = sort { $a->{'name'} cmp $b->{'name'} } @nodes; | |
890 | ||
891 | return @nodes; | |
892 | } | |
893 | ||
894 | sub TorNodeDescription() { | |
895 | my ($tor, $fingerprint) = @_; | |
896 | $fingerprint =~ s/\$//; | |
897 | ||
898 | my $node = { | |
899 | fingerprint => $fingerprint, | |
900 | exit_node => 0, | |
901 | }; | |
902 | ||
903 | my @output = &TorSendCommand($tor, "GETINFO ns/id/$node->{'fingerprint'}"); | |
904 | ||
905 | foreach (@output) { | |
906 | # Router | |
907 | if ($_ =~ /^r (\w+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\d+)/) { | |
908 | $node->{'name'} = $1; | |
909 | $node->{'address'} = $3; | |
910 | $node->{'port'} = $4; | |
911 | ||
07e42be9 | 912 | my $country_code = &Location::Functions::lookup_country_code($node->{'address'}); |
13b5ce6e MT |
913 | $node->{'country_code'} = $country_code; |
914 | ||
915 | # Flags | |
916 | } elsif ($_ =~ /^s (.*)$/) { | |
917 | $node->{'flags'} = split(" ", $1); | |
918 | ||
919 | foreach my $flag ($node->{'flags'}) { | |
920 | if ($flag eq "Exit") { | |
921 | $node->{'exit_node'}++; | |
922 | } | |
923 | } | |
924 | ||
925 | # Bandwidth | |
926 | } elsif ($_ =~ /^w Bandwidth=(\d+)/) { | |
927 | $node->{'bandwidth'} = $1 * 8; | |
928 | $node->{'bandwidth_string'} = &FormatBitsPerSecond($node->{'bandwidth'}); | |
929 | } | |
930 | } | |
931 | ||
932 | if (exists($node->{'name'})) { | |
933 | return $node; | |
934 | } | |
935 | } | |
936 | ||
937 | sub TorAccountingStats() { | |
938 | my $tor = shift; | |
939 | my $ret = {}; | |
940 | ||
941 | my $enabled = &TorGetInfo($tor, "accounting/enabled"); | |
942 | if ($enabled ne '1') { | |
943 | return; | |
944 | } | |
945 | ||
946 | my @cmds = ("hibernating", "interval-start", "interval-end"); | |
947 | foreach (@cmds) { | |
948 | $ret->{$_} = &TorGetInfo($tor, "accounting/$_"); | |
949 | } | |
950 | ||
951 | my @cmds = ("bytes", "bytes-left"); | |
952 | foreach (@cmds) { | |
953 | my $output = &TorGetInfo($tor, "accounting/$_"); | |
954 | my @bytes = split(" ", $output); | |
955 | ||
956 | $ret->{$_."_read"} = $bytes[0]; | |
957 | $ret->{$_."_written"} = $bytes[1]; | |
958 | } | |
959 | ||
960 | return $ret; | |
961 | } | |
962 | ||
963 | sub FormatBytes() { | |
964 | my $bytes = shift; | |
965 | ||
966 | my @units = ("B", "KB", "MB", "GB", "TB"); | |
967 | my $units_index = 0; | |
968 | ||
969 | while (($units_index <= $#units) && ($bytes >= 1024)) { | |
970 | $units_index++; | |
971 | $bytes /= 1024; | |
972 | } | |
973 | ||
974 | return sprintf("%.2f %s", $bytes, $units[$units_index]); | |
975 | } | |
976 | ||
977 | sub FormatBitsPerSecond() { | |
978 | my $bits = shift; | |
979 | ||
f00699e8 | 980 | my @units = ("bit/s", "kbit/s", "Mbit/s", "Gbit/s", "Tbit/s"); |
13b5ce6e MT |
981 | my $units_index = 0; |
982 | ||
983 | while (($units_index <= $#units) && ($bits >= 1024)) { | |
984 | $units_index++; | |
985 | $bits /= 1024; | |
986 | } | |
987 | ||
988 | return sprintf("%.2f %s", $bits, $units[$units_index]); | |
989 | } |