Skip to content

Commit

Permalink
feat: Fix WoL not sent because of missing ARP entry and add more log …
Browse files Browse the repository at this point in the history
…output
  • Loading branch information
fussel178 committed Mar 6, 2022
1 parent a4bf865 commit 90c5152
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Sends a WOL packet to the specified host on website request and redirects to the
## `config.json` definition

| Key | Description |
|------------|----------------------------------------------------------------------------------|
| `host.mac` | The MAC address of the adapter that you want to wake (get it with `ip addr`) |
| `host.ip` | The IP address of the device you want to wake (only used for routing) |
| `timeout` | The timeout between refreshes in the browser (`0` disables automatic refreshing) |
| `details` | Show details to the client (e.g. try count, destination url, etc.) |
| Key | Description |
|------------|---------------------------------------------------------------------------------------------|
| `host.mac` | The MAC address of the adapter that you want to wake (get it with `ip addr`) |
| `host.ip` | The IP address of the device you want to wake (only used for ICMP ping) |
| `timeout` | The timeout between refreshes in the browser (`0` disables automatic refreshing) |
| `details` | Show details to the client (e.g. try count, destination url, etc.) |
| `log` | When `true` write log messages via the `error_log` function for debugging output (optional) |
3 changes: 2 additions & 1 deletion config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"ip": "192.168.0.2"
},
"timeout": 5,
"details": false
"details": false,
"log": false
}
63 changes: 44 additions & 19 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,46 @@
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!");
die("Configuration file doesn't exist. Please create JSON file at \"" . CONFIG_PATH . "\" and try again.");
}

$config = json_decode(file_get_contents(CONFIG_PATH));
$log_enabled = $config->{"log"} ?? false;

/// logging section

function notice(string $message, string $scope = null): void {
global $log_enabled;
if (!$log_enabled) return;
$scope = isset($scope) ? " [" . $scope . "]" : "";
error_log("notice" . $scope . ": " . $message . "\n");
}

function warn(string $message, string $scope = null): void {
global $log_enabled;
if (!$log_enabled) return;
$scope = isset($scope) ? " [" . $scope . "]" : "";
error_log("warn" . $scope . ": " . $message . "\n");
}

function error(string $message, string $scope = null): void {
global $log_enabled;
if (!$log_enabled) return;
$scope = isset($scope) ? " [" . $scope . "]" : "";
error_log("error" . $scope . ": " . $message . "\n");
}

/// command section

/**
* Same as {@link exec()} but additionally logs everything to the error log.
*/
function execute_command(string $command, array &$output, int &$result_code, string $scope = null) {
$result = exec($command, $output, $result_code);
notice("executed: " . $command . ", got: " . $output[0] . ", rc: " . $result_code, $scope);
return $result;
}

/**
* Checks if a given command (UNIX binary) is in the PATH environment.
*
Expand All @@ -23,8 +56,7 @@ function command_exists(string $command): bool
{
$output = [];
$result_code = 0;
$cmd = "command -v " . $command;
exec($cmd, $output, $result_code);
execute_command("command -v " . escapeshellarg($command), $output, $result_code, "command_exists");
return $result_code == 0;
}

Expand All @@ -38,13 +70,11 @@ function get_command_path(string $command): string
{
$output = [];
$result_code = 0;
$cmd = "command -v " . $command;
exec($cmd, $output, $result_code);
execute_command("command -v " . escapeshellarg($command), $output, $result_code, "get_command_path");

if ($result_code > 0) {
die("Requested command " . $command . " doesn't exist. Please install the required package and try again.");
}

return $output[0];
}

Expand All @@ -65,8 +95,7 @@ function is_up(): bool
$output = [];
$result_code = 0;
$cmd = get_command_path("ping") . " -q -c 1 -w 1 " . $config->{"host"}->{"ip"};
exec($cmd, $output, $result_code);

execute_command($cmd, $output, $result_code, "is_up");
return $result_code == 0;
}

Expand All @@ -79,23 +108,19 @@ function wake(): bool
{
global $config;

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

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

0 comments on commit 90c5152

Please sign in to comment.