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