Wake On Lan with pfSense

I saw a project with an arduino turning a computer on and off, and thought it would be handy to be able to do. But instead of an arduino, I was trying to make pfSense do the job instead, by sending a magic package to my computer.

To do that, I made a little php script with a simple login.

That php script will then log into my pfSense router, and post the mac address to it.

index.php

<?php
session_start();

require_once("config.inc.php");

if (!isset($_SESSION['basic_is_logged_in']) || $_SESSION['basic_is_logged_in'] !== true)
{

	$errorMessage = '';
	if (isset($_POST['txtUserId']) && isset($_POST['txtPassword']))
	{
		if ($_POST['txtUserId'] === $loginName && $_POST['txtPassword'] === $loginPass)
		{
			$_SESSION['basic_is_logged_in'] = true;
			header('Location: .');
			exit;
		}
		else
		{
			$errorMessage = 'Sorry, wrong user id / password';
		}
	}
?>

<form method="post" name="frmLogin" id="frmLogin" action=".">
	<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
		<tr>
			<td width="150">User Id</td>
			<td><input name="txtUserId" type="text" id="txtUserId"></td>
		</tr>
		<tr>
			<td width="150">Password</td>
			<td><input name="txtPassword" type="password" id="txtPassword"></td>
		</tr>
			<tr>
			<td width="150">&nbsp;</td>
			<td><input type="submit" name="btnLogin" value="Login"></td>
		</tr>
	</table>
</form>

<? echo " $errorMessage ";
}
else
{
	function getBetween($content,$start,$end)
	{
		$a1 = strrpos($content,$start);
		$content = substr($content,$a1 + strlen($start));
		while($a2 = strrpos($content,$end))
		{
			$content = substr($content,0,$a2);
		}
		return $content;
	}

	function wol($mac=0)
	{
		global $username,$password,$pfsenseprot,$pfsenseip;

		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");

		curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);

		curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ":" . $password);

		curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($curl_handle, CURLOPT_URL, $pfsenseprot . "://" . $pfsenseip . "/services_wol.php");

		curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "interface=lan&mac=" . $mac);

		$buffer = curl_exec($curl_handle);

		if(curl_errno($curl_handle))
		{
			echo 'Curl error: ' . curl_error($curl_handle);
		}
		else
		{
			if (empty($buffer))
			{
				print "Returend nothing?";
			}
			else
			{
				$begin = "Sent magic packet to";
				$end = ".</b></font>";
				print "Sent magic packet to " . getBetween($buffer, $begin, $end);
			}
		}

		curl_close($curl_handle);
	}

	if (isset($_GET["mac"]))
	{
		echo "pfSense reply: ";
		wol($_GET["mac"]);
		echo "<br /><br />";
	}

	echo "Send magic packet to: <a href='?mac=00:23:54:48:3f:ea'>Desktop</a>";
}

config.inc.php

<?php
//page configuration
	$loginName = "user";
	$loginPass = "password";

//pfSense configuration
	$username = "user";		//username for pfSense
	$password = "password";		//password for pfSense
	$pfsenseip = "192.168.1.1";	//ip of pfSense
	$pfsenseprot = "https";		//http or https
?>

.htaccess

<Files config.inc.php>
  order allow,deny
  deny from all
</Files>

Leave a Reply