Skip to content

Commit

Permalink
feat: Add first working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
fussel178 committed Mar 5, 2022
1 parent bca20e0 commit 15a3854
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
13 changes: 13 additions & 0 deletions encode_url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php7
<?php

require_once "lib.php";

if ($argc < 2) {
echo "Usage: encode_url.php <url>\n";
exit(1);
}

echo "Append the following line to your link to the PHP WoL redirector:\n\n";
echo " ?" . GET_PARAM_REDIRECT_URL . "=" . urlencode($argv[1]) . "\n\n";
exit(0);
39 changes: 39 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
require_once "lib.php";
global $config;

session_start();

$dest = $_GET[GET_PARAM_REDIRECT_URL];
$is_up = is_up();
$timeout = $config->{"timeout"};
$try_count = $_SESSION[SESSION_PARAM_TRY_COUNT] ?? 0;

if ($dest != null && $is_up) {
header("Location: " . urldecode($dest));
exit(0);
}

// well, host is down -> try to wake up
$wol_state = wake();
$try_count++;
$_SESSION[SESSION_PARAM_TRY_COUNT] = $try_count;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Check Homeserver Response</title>
<meta http-equiv="refresh" content="<?= $timeout ?>" />
</head>
<body>

<p>Destination host: <?= $config->{"host"}->{"ip"} ?></p>
<p>Host State: <?= $is_up ? "Up" : "Down" ?></p>
<p>Destination URL: <?= htmlspecialchars(urldecode($dest)) ?></p>
<p>Timeout: <?= $timeout ?></p>
<p>WoL packet sent: <?= $wol_state ? "successfully" : "failed" ?></p>
<p>Try count: <?= $try_count ?></p>
<p>Please wait for the host to come up...</p>
</body>
</html>
76 changes: 76 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/// config section

const CONFIG_PATH = "config.json";
const GET_PARAM_REDIRECT_URL = "dest";
const SESSION_PARAM_TRY_COUNT = "count";

if (!file_exists(CONFIG_PATH)) {
die("Configuration file doesn't exist. Please create JSON file at " . CONFIG_PATH . " and try again!");
}

$config = json_decode(file_get_contents(CONFIG_PATH));

/// function section

/**
* Checks if the configured host is up and running.
*
* @return bool <code>true</code> when the host is up
*/
function is_up(): bool {
global $config;

// send one ICMP ping packet to host
// when answered -> host is up
// when timeout -> host is down
$output = [];
$result_code = 0;
$cmd = "/usr/bin/ping -q -c 1 -w 1 " . $config->{"host"}->{"ip"};
exec($cmd, $output, $result_code);

return $result_code == 0;
}

/**
* Checks if a given command (UNIX binary) is in the PATH environment.
*
* @return bool <code>true</code> when the command/binary exists
*/
function command_exists(string $command): bool {
$output = [];
$result_code = 0;
$cmd = "command -v " . $command;
exec($cmd, $output, $result_code);
return $result_code == 0;
}

/**
* Wakes the configured host with a Wake-On-Lan packet.
*
* @return bool <code>true</code> when the WoL packet was sent successfully
*/
function wake(): bool {
global $config;

// send one WOL packet to the host
if (command_exists("/usr/bin/wol")) {
$wol_command = sprintf("/usr/bin/wol -i \"%s\" \"%s\"",
$config->{"host"}->{"ip"},
$config->{"host"}->{"mac"}
);
} else if (command_exists("/usr/bin/wakeonlan")) {
$wol_command = sprintf("/usr/bin/wakeonlan -i \"%s\" \"%s\"",
$config->{"host"}->{"ip"},
$config->{"host"}->{"mac"}
);
} else {
die("No wakeonlan binaries found. Please install \"wakeonlan\" or \"wol\" package.");
}

$output = [];
$result_code = 0;
exec($wol_command, $output, $result_code);
return $result_code == 0;
}

0 comments on commit 15a3854

Please sign in to comment.