Skip to content

Instantly share code, notes, and snippets.

@alex-phillips
Last active January 17, 2017 19:02
Show Gist options
  • Save alex-phillips/938695c179d1fbe2e72c987ad306b6e6 to your computer and use it in GitHub Desktop.
Save alex-phillips/938695c179d1fbe2e72c987ad306b6e6 to your computer and use it in GitHub Desktop.
<?php
$settings_base_dir = '/Users/exonintrendo';
$network_interface = 'ppp0';
if (file_exists("$settings_base_dir/.deluge_starter.lock")) {
log_msg("Lock file exists. Exiting.");
exit;
}
touch("$settings_base_dir/.deluge_starter.lock");
$trackers = [
'myanonamouse' => [
'tracker' => 'myanonamouse',
'port' => 58846,
'pid_file' => "$settings_base_dir/.myanonamouse.active",
'client' => "deluge",
'interface' => '',
],
'passtheheadphones' => [
'tracker' => 'pth',
'port' => 58847,
'pid_file' => "$settings_base_dir/.pth.deluge.active",
'client' => "deluge",
'interface' => 'ppp0',
],
'apollo' => [
'tracker' => 'apollo',
'port' => 58848,
'pid_file' => "$settings_base_dir/.apollo.deluge.active",
'client' => "deluge",
'interface' => 'ppp0',
],
];
$interface_ip = `ifconfig $network_interface | grep 'inet ' | awk '{print $2}'`;
$interface_ip = trim($interface_ip);
if (preg_match('#does not exists#', $interface_ip)) {
log_msg("Interface is not connected");
$interface_ip = null;
}
if (!preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $interface_ip)) {
log_msg("'$interface_ip' is not a valid IP address.");
$interface_ip = null;
}
if ($interface_ip) {
log_msg("Current interface IP: $interface_ip");
}
foreach ($trackers as $tracker => $config) {
$config['settings_dir'] = "$settings_base_dir/.{$config['client']}.{$config['tracker']}.d";
$settings = read_config("{$config['settings_dir']}/core.conf", $config['client']);
if (!$config['interface']) {
log_msg("Tracker $tracker should not be bound to interface.");
$pids = null;
if (file_exists($config['pid_file'])) {
exec("pgrep -F {$config['pid_file']}", $pids);
}
if ($pids) {
log_msg(" Tracker $tracker already running. Skipping.");
} else {
log_msg(" Tracker $tracker isn't running. Starting.");
switch ($config['client']) {
case 'transmission':
passthru("transmission-daemon -x \"{$config['pid_file']}\" -g \"{$config['settings_dir']}\"");
break;
case 'deluge':
$cmd = "deluged --config {$config['settings_dir']} --port {$config['port']} --pidfile {$config['pid_file']} > /dev/null 2>&1";
// $cmd = "launchctl load /Users/exonintrendo/deluge_scrpits/$tracker.plist";
log_msg("Starting client with command '$cmd'");
passthru($cmd);
break;
}
}
continue;
}
log_msg("Checking $tracker...");
$settings_interface_ip = null;
switch ($config['client']) {
case 'transmission':
$settings_interface_ip = $settings['bind-address-ipv4'];
break;
case 'deluge':
$settings_interface_ip = $settings[1]['listen_interface'];
break;
}
log_msg(" Bound IP address: $settings_interface_ip");
$pids = null;
if (file_exists($config['pid_file'])) {
exec("pgrep -F {$config['pid_file']}", $pids);
}
if ($pids) {
log_msg(" $tracker client already running.");
log_msg(" Checking bind IP...");
if (!$interface_ip) {
log_msg(" Interface IP not available. Shutting down instance.");
shutdown_instance($tracker, $config);
continue;
}
if ($settings_interface_ip !== $interface_ip) {
log_msg(" IP addresses don't match.");
shutdown_instance($tracker, $config);
} else {
log_msg(" Bound IP address is up to date.");
}
} else {
log_msg(" Checking bind IP...");
if ($settings_interface_ip !== $interface_ip) {
log_msg(" Updating IP bind address from $settings_interface_ip to $interface_ip");
switch ($config['client']) {
case 'transmission':
$settings['bind-address-ipv4'] = $interface_ip;
break;
case 'deluge':
$settings[1]['listen_interface'] = $interface_ip;
break;
}
write_config("{$config['settings_dir']}/core.conf", $settings, $config['client']);
} else {
log_msg(" Bind address is up to date.");
}
log_msg(" Starting $tracker {$config['client']}...");
switch ($config['client']) {
case 'transmission':
passthru("transmission-daemon -x \"{$config['pid_file']}\" -g \"{$config['settings_dir']}\"");
break;
case 'deluge':
$cmd = "deluged --config {$config['settings_dir']} --port {$config['port']} --pidfile {$config['pid_file']} > /dev/null 2>&1";
// $cmd = "launchctl load /Users/exonintrendo/deluge_scrpits/$tracker.plist";
log_msg("Starting client with command '$cmd'");
passthru($cmd);
break;
}
}
}
unlink("$settings_base_dir/.deluge_starter.lock");
log_msg("Done.");
function shutdown_instance($tracker, $config)
{
log_msg("Shutting down $tracker {$config['client']} instance...");
switch ($config['client']) {
case 'transmission':
passthru("transmission-remote {$config['port']} --exit");
break;
case 'deluge':
// passthru("launchctl unload /Users/exonintrendo/deluge_scripts/$tracker.plist");
passthru("deluge-console --config {$config['settings_dir']} \"connect localhost:{$config['port']}; halt\"");
break;
}
}
function read_config($file, $client)
{
switch ($client) {
case 'transmission':
return json_decode(file_get_contents($file), true);
case 'deluge':
$contents = file_get_contents($file);
$contents = "[" . str_replace('}{', '},{', $contents) . "]";
$contents = json_decode($contents, true);
return $contents;
}
}
function write_config($file, $data, $client)
{
switch ($client) {
case 'transmission':
file_put_contents($file, json_encode($data, JSON_UNESCAPED_SLASHES));
break;
case 'deluge':
$data = json_encode($data[0]) . json_encode($data[1], JSON_UNESCAPED_SLASHES);
file_put_contents($file, $data);
break;
}
}
function log_msg($msg)
{
echo "$msg\n";
}
/*
NOTES:
deluge startup: deluged --config ~/.deluge.myanonamouse.d --port 58846 --pidfile ~/.myanonamouse.active
deluge startup with bound IP: deluged --config ~/.deluge.myanonamouse.d --port 58846 --pidfile ~/.myanonamouse.active --interface 10.10.1.3
send commands to deluge without entering console: deluge-console --config ~/.deluge.myanonamouse.d halt
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment