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