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