-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |