| | 1455 | # Subs: Tor support |
| | 1456 | # ----------------------------------------- |
| | 1457 | |
| | 1458 | sub start_tor |
| | 1459 | { |
| | 1460 | # do we have any components requesting the use of tor? |
| | 1461 | my $want_tor = 0; |
| | 1462 | foreach (query_grabbers()) { |
| | 1463 | unless ($components->{$_}->{disabled}) { |
| | 1464 | $want_tor++ if (query_config($_, 'option_anon_socks')); |
| | 1465 | } |
| | 1466 | } |
| | 1467 | |
| | 1468 | return if ($want_tor == 0); |
| | 1469 | |
| | 1470 | # try to find tor |
| | 1471 | my $searchpath = ".:".$ENV{PATH}; |
| | 1472 | my $found_tor; |
| | 1473 | foreach my $dir (split(/:/,$searchpath)) { |
| | 1474 | if (-x "$dir/tor") { |
| | 1475 | $found_tor = "$dir/tor"; |
| | 1476 | last; |
| | 1477 | } |
| | 1478 | } |
| | 1479 | |
| | 1480 | if (!defined $found_tor) { |
| | 1481 | printf "WARNING: $want_tor components wanted to use Tor but could not find it.\n"; |
| | 1482 | printf "This may cause data collection to run slower than it otherwise would.\n\n"; |
| | 1483 | return; |
| | 1484 | } |
| | 1485 | |
| | 1486 | printf "Starting Tor ($found_tor) in the background (wanted by $want_tor components).\n"; |
| | 1487 | my $pid = fork; |
| | 1488 | if (!defined $pid) { |
| | 1489 | # failed |
| | 1490 | printf "Failed to start $found_tor: $!\n"; |
| | 1491 | return; |
| | 1492 | } elsif ($pid > 0) { |
| | 1493 | # parent |
| | 1494 | sleep 2; # wait a few seconds for Tor to start |
| | 1495 | |
| | 1496 | # test that it is running |
| | 1497 | if (!kill 0, $pid) { |
| | 1498 | printf "Tor doesn't seem to be running on pid $pid anymore, ignoring Tor option.\n"; |
| | 1499 | } else { |
| | 1500 | printf "Tor appears to have successfully started (pid $pid).\n"; |
| | 1501 | $plugin_data->{tor_address} = "127.0.0.1:9050"; |
| | 1502 | $plugin_data->{tor_pid} = $pid; |
| | 1503 | } |
| | 1504 | } else { |
| | 1505 | # child |
| | 1506 | exec $found_tor "MaxCircuitDirtiness 30 SocksListenAddress 127.0.0.1:9050"; |
| | 1507 | exit(1); # we won't reach this |
| | 1508 | } |
| | 1509 | } |
| | 1510 | |
| | 1511 | |
| | 1512 | sub stop_tor |
| | 1513 | { |
| | 1514 | if (defined $plugin_data->{tor_pid}) { |
| | 1515 | # INTR sig stops tor |
| | 1516 | kill 2,$plugin_data->{tor_pid}; |
| | 1517 | } |
| | 1518 | } |
| | 1519 | |
| | 1520 | # ----------------------------------------- |