mirror of
https://github.com/netbirdio/plugins.git
synced 2026-05-22 18:44:07 -07:00
net/wol to mvc (#570)
This commit is contained in:
committed by
Franco Fichtner
parent
f735af193d
commit
52d93653e2
+1
-2
@@ -1,6 +1,5 @@
|
||||
PLUGIN_NAME= wol
|
||||
PLUGIN_VERSION= 1.0
|
||||
PLUGIN_REVISION= 1
|
||||
PLUGIN_VERSION= 2.0.d
|
||||
PLUGIN_DEPENDS= wol
|
||||
PLUGIN_COMMENT= Wake on LAN Service
|
||||
PLUGIN_MAINTAINER= franco@opnsense.org
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2017 Fabian Franz
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OPNsense\Wol\Api;
|
||||
|
||||
use OPNsense\Base\ApiMutableModelControllerBase;
|
||||
use OPNsense\Wol\Wol;
|
||||
use OPNsense\Core\Config;
|
||||
use OPNsense\Core\Backend;
|
||||
|
||||
class WolController extends ApiMutableModelControllerBase
|
||||
{
|
||||
static protected $internalModelName = 'wol';
|
||||
static protected $internalModelClass = '\OPNsense\Wol\Wol';
|
||||
|
||||
public function setAction() {
|
||||
$result = array();
|
||||
if ($this->request->isPost()) {
|
||||
/* input validation */
|
||||
$wol = $this->getModel();
|
||||
$wolent = $wol->wolentry->Add();
|
||||
if ($this->request->hasPost('uuid')) {
|
||||
$uuid = $this->request->getPost('uuid');
|
||||
$tmp = $wol->getNodeByReference('wolentry.' . $uuid);
|
||||
if ($tmp) {
|
||||
$wolent = $tmp;
|
||||
$this->wakeHostByNode($wolent, $result);
|
||||
}
|
||||
} else {
|
||||
$wolent->setNodes($this->request->getPost('wake'));
|
||||
if ($wol->performValidation()->count() == 0) {
|
||||
$this->wakeHostByNode($wolent, $result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function delHostAction($uuid) {
|
||||
$this->delBase('host', $uuid);
|
||||
}
|
||||
public function searchHostAction()
|
||||
{
|
||||
return $this->searchBase('wolentry', array("interface", "mac", "descr"));
|
||||
}
|
||||
public function getHostAction($uuid = null)
|
||||
{
|
||||
$this->sessionClose();
|
||||
return $this->getBase('host', 'wolentry', $uuid);
|
||||
}
|
||||
public function getwakeAction()
|
||||
{
|
||||
return $this->getBase('wake', 'wolentry', null);
|
||||
}
|
||||
public function addHostAction()
|
||||
{
|
||||
return $this->addBase('host', 'wolentry');
|
||||
}
|
||||
public function setHostAction($uuid)
|
||||
{
|
||||
return $this->setBase('host', 'wolentry', $uuid);
|
||||
}
|
||||
public function wakeallAction() {
|
||||
if (!$this->request->isPost()) {
|
||||
return array('error' => 'Must be called via POST');
|
||||
}
|
||||
$results = array('results' => array());
|
||||
$wol = $this->getModel()->wolentry->__items;
|
||||
foreach ($wol as $wolent) {
|
||||
$result = array('mac' => (string)$wolent->mac);
|
||||
$this->wakeHostByNode($wolent, $result);
|
||||
$results['results'][] = $result;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
private function wakeHostByNode($wolent, &$result) {
|
||||
$backend = new Backend();
|
||||
/* determine broadcast address */
|
||||
$cidr = $this->getInterfaceSubnet($wolent->interface);
|
||||
$ipaddr = $this->getInterfaceIP($wolent->interface);
|
||||
if (empty($cidr) || empty($ipaddr)) {
|
||||
$result['status'] = 'error';
|
||||
$result['error_msg'] = 'Incorrect IPv4 configuration on interface';
|
||||
return $result;
|
||||
}
|
||||
$broadcast_ip = escapeshellarg($this->calculateSubnetBroadcast($ipaddr, $cidr));
|
||||
$result['status'] = trim($backend->configdRun("wol wake {$broadcast_ip} ". escapeshellarg((string)$wolent->mac)));
|
||||
}
|
||||
private function getInterfaceIP($if) {
|
||||
$cfg = Config::getInstance()->object();
|
||||
try {
|
||||
$tmp = (string)$cfg->interfaces->{$if}->ipaddr;
|
||||
// for example DHCP
|
||||
if (!filter_var($tmp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
return null;
|
||||
}
|
||||
return $tmp;
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private function getInterfaceSubnet($if) {
|
||||
$cfg = Config::getInstance()->object();
|
||||
try {
|
||||
return (string)$cfg->interfaces->{$if}->subnet;
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private function calculateSubnetBroadcast($ip_addr, $cidr) {
|
||||
// TODO undefined offset
|
||||
$parts = explode('.', $ip_addr);
|
||||
$int_ip = ((int)$parts[0]) << 24 | ((int)$parts[1]) << 16 | ((int)$parts[2]) << 8 | (int)$parts[3];
|
||||
$hostmask = (2 << (31 - $cidr)) - 1;
|
||||
$int_bcast = $int_ip | $hostmask;
|
||||
return implode('.', array(
|
||||
$int_bcast >> 24,
|
||||
($int_bcast >> 16) & 255,
|
||||
($int_bcast >> 8) & 255,
|
||||
$int_bcast & 255,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/*
|
||||
Copyright (C) 2017 Fabian Franz
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace OPNsense\Wol;
|
||||
|
||||
class IndexController extends \OPNsense\Base\IndexController
|
||||
{
|
||||
public function indexAction()
|
||||
{
|
||||
$this->view->wakeForm = $this->getForm("wake");
|
||||
$this->view->hostForm = $this->getForm("host");
|
||||
$this->view->pick('OPNsense/Wol/index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<form>
|
||||
<field>
|
||||
<id>host.interface</id>
|
||||
<label>Interface</label>
|
||||
<type>dropdown</type>
|
||||
<help>Choose which interface this host is connected to.</help>
|
||||
</field>
|
||||
<field>
|
||||
<id>host.mac</id>
|
||||
<label>MAC address</label>
|
||||
<type>text</type>
|
||||
<help>Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx</help>
|
||||
</field>
|
||||
<field>
|
||||
<id>host.description</id>
|
||||
<label>Description</label>
|
||||
<type>text</type>
|
||||
<help>You may enter a description here for your reference (not parsed).</help>
|
||||
</field>
|
||||
</form>
|
||||
@@ -0,0 +1,14 @@
|
||||
<form>
|
||||
<field>
|
||||
<id>wake.interface</id>
|
||||
<label>Interface</label>
|
||||
<type>dropdown</type>
|
||||
<help>Choose which interface the host to be woken up is connected to.</help>
|
||||
</field>
|
||||
<field>
|
||||
<id>wake.mac</id>
|
||||
<label>MAC address</label>
|
||||
<type>text</type>
|
||||
<help>Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx.</help>
|
||||
</field>
|
||||
</form>
|
||||
@@ -1,14 +0,0 @@
|
||||
<acl>
|
||||
<page-services-wakeonlan>
|
||||
<name>Services: Wake on LAN</name>
|
||||
<patterns>
|
||||
<pattern>services_wol.php*</pattern>
|
||||
</patterns>
|
||||
</page-services-wakeonlan>
|
||||
<page-services-wakeonlan-edit>
|
||||
<name>Services: Wake on LAN: Edit</name>
|
||||
<patterns>
|
||||
<pattern>services_wol_edit.php*</pattern>
|
||||
</patterns>
|
||||
</page-services-wakeonlan-edit>
|
||||
</acl>
|
||||
@@ -1,8 +0,0 @@
|
||||
<menu>
|
||||
<Services>
|
||||
<WoL VisibleName="Wake on LAN" url="/services_wol.php" cssClass="fa fa-power-off fa-fw">
|
||||
<WoLEdit url="/services_wol_edit.php*" visibility="hidden"/>
|
||||
<Wol url="/services_wol.php*" visibility="hidden"/>
|
||||
</WoL>
|
||||
</Services>
|
||||
</menu>
|
||||
@@ -0,0 +1,9 @@
|
||||
<acl>
|
||||
<page-services-wakeonlan>
|
||||
<name>Services: Wake on LAN</name>
|
||||
<patterns>
|
||||
<pattern>ui/wol*</pattern>
|
||||
<pattern>api/wol*</pattern>
|
||||
</patterns>
|
||||
</page-services-wakeonlan>
|
||||
</acl>
|
||||
@@ -0,0 +1,5 @@
|
||||
<menu>
|
||||
<Services>
|
||||
<WoL VisibleName="Wake on LAN" url="/ui/wol/" cssClass="fa fa-power-off fa-fw" />
|
||||
</Services>
|
||||
</menu>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*
|
||||
Copyright (C) 2018 Fabian Franz
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
namespace OPNsense\Wol;
|
||||
|
||||
use OPNsense\Base\BaseModel;
|
||||
|
||||
class Wol extends BaseModel
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<model>
|
||||
<mount>//wol</mount>
|
||||
<description>Wake On Lan configuration</description>
|
||||
<version>1.0.0</version>
|
||||
<items>
|
||||
<wolentry type="ArrayField">
|
||||
<interface type="InterfaceField">
|
||||
<Required>Y</Required>
|
||||
<multiple>N</multiple>
|
||||
<default></default>
|
||||
<filters>
|
||||
<enable>/^(?!0).*$/</enable>
|
||||
</filters>
|
||||
</interface>
|
||||
<mac type="TextField">
|
||||
<Required>Y</Required>
|
||||
<multiple>N</multiple>
|
||||
<mask>/^((?:[a-f0-9]{2}:){5}(?:[a-f0-9]{2}))$/</mask>
|
||||
<default>00:00:00:00:00:00</default>
|
||||
</mac>
|
||||
<descr type="TextField">
|
||||
<Required>N</Required>
|
||||
<multiple>N</multiple>
|
||||
<default></default>
|
||||
</descr>
|
||||
</wolentry>
|
||||
</items>
|
||||
</model>
|
||||
@@ -0,0 +1,104 @@
|
||||
<script>
|
||||
$( document ).ready(function() {
|
||||
// delete host action
|
||||
$("#act_wake_all").click(function(event){
|
||||
event.preventDefault();
|
||||
$.post('/api/wol/wol/wakeall', {}, function(data) {
|
||||
BootstrapDialog.show({
|
||||
type:BootstrapDialog.TYPE_INFO,
|
||||
title: "{{ lang._("Result") }}",
|
||||
message: '<ul>' + (data['results'].map(function(element) {
|
||||
return `<li>${element.mac}: ${element.status}</li>`
|
||||
}).join('')) + '</ul>'
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var grid = $("#grid-wol-settings").UIBootgrid(
|
||||
{ 'search':'/api/wol/wol/searchHost',
|
||||
'get':'/api/wol/wol/getHost/',
|
||||
'set':'/api/wol/wol/setHost/',
|
||||
'add':'/api/wol/wol/addHost/',
|
||||
'del':'/api/wol/wol/delHost/',
|
||||
'options':{
|
||||
selection:false,
|
||||
multiSelect:false,
|
||||
formatters: {
|
||||
"commandswithwake": function (column, row) {
|
||||
return "<button type=\"button\" class=\"btn btn-xs btn-default command-wake\" data-row-id=\"" + row.uuid + "\"><span class=\"fa fa-clock-o\"></span></button> " +
|
||||
"<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.uuid + "\"><span class=\"fa fa-pencil\"></span></button> " +
|
||||
"<button type=\"button\" class=\"btn btn-xs btn-default command-copy\" data-row-id=\"" + row.uuid + "\"><span class=\"fa fa-clone\"></span></button>" +
|
||||
"<button type=\"button\" class=\"btn btn-xs btn-default command-delete\" data-row-id=\"" + row.uuid + "\"><span class=\"fa fa-trash-o\"></span></button>";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
grid.on("loaded.rs.jquery.bootgrid", function(){
|
||||
grid.find('.command-wake').click(function(event) {
|
||||
event.preventDefault();
|
||||
$.post('/api/wol/wol/set', {'uuid': this.dataset['rowId']}, function(data) {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function msg_not_successful(wolent) {
|
||||
return '{{ lang._('Please check the %ssystem log%s, the wol command for %s (%s) did not complete successfully.') | format( '<a href="/diag_logs.php">', '</a>', '%s', '%s') }}'
|
||||
.replace('%s', wolent['descr']).replace('%s',$('<pre>').text(wolent['mac']).html());
|
||||
}
|
||||
function msg_successful(wolent) {
|
||||
return 'Sent magic packet to %s (%s).'.replace('%s', wolent['descr']).replace('%s',$('<pre>').text(wolent['mac']).html());
|
||||
}
|
||||
|
||||
|
||||
$( document ).ready(function() {
|
||||
var data_get_map = {'frm_wol_wake':"/api/wol/wol/getwake"};
|
||||
mapDataToFormUI(data_get_map).done(function(data){
|
||||
formatTokenizersUI();
|
||||
$('.selectpicker').selectpicker('refresh');
|
||||
});
|
||||
|
||||
// link save button to API set action
|
||||
$("#wakeAct").click(function(){
|
||||
saveFormToEndpoint(url="/api/wol/wol/set", formid='frm_wol_wake',callback_ok=function(data){}, true);
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
<div class="content-box" style="padding-bottom: 1.5em;">
|
||||
{{ partial("layout_partials/base_form",['fields':wakeForm,'id':'frm_wol_wake'])}}
|
||||
<div class="col-md-12">
|
||||
<hr />
|
||||
<button class="btn btn-primary" id="wakeAct" type="button"><b>{{ lang._('Wake up') }}</b> <i id="wakeAct_progress"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content-box" style="padding-bottom: 1.5em;">
|
||||
|
||||
<table id="grid-wol-settings" class="table table-responsive" data-editDialog="frm_wol_settings">
|
||||
<thead>
|
||||
<tr>
|
||||
<th data-column-id="interface" data-type="string" data-visible="true">{{ lang._('Interface') }}</th>
|
||||
<th data-column-id="mac" data-type="string" data-visible="true">{{ lang._('MAC') }}</th>
|
||||
<th data-column-id="descr" data-type="string" data-identifier="true">{{ lang._('Description') }}</th>
|
||||
<th data-column-id="commands" data-formatter="commandswithwake" data-sortable="false">{{ lang._('Commands') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="3"></td>
|
||||
<td>
|
||||
<button data-action="add" type="button" class="btn btn-xs btn-default"><span class="fa fa-plus"></span></button>
|
||||
<button type="button" class="btn btn-xs reload_btn btn-primary" id="act_wake_all"><span class="fa wakeallAct_progress"></span> {{ lang._('Wake All') }}</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
|
||||
{{ partial("layout_partials/base_dialog",['fields': hostForm,'id':'frm_wol_settings', 'label':lang._('Edit WOL Host')]) }}
|
||||
@@ -0,0 +1,4 @@
|
||||
[wake]
|
||||
command:/usr/local/bin/wol -i
|
||||
parameters: %s %s
|
||||
type:script
|
||||
@@ -1,267 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (C) 2014-2015 Deciso B.V.
|
||||
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
require_once("guiconfig.inc");
|
||||
require_once("interfaces.inc");
|
||||
|
||||
$a_wol = &config_read_array('wol', 'wolentry');
|
||||
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// delete entry
|
||||
if (isset($_POST['act']) && $_POST['act'] == "del" && isset($_POST['id'])) {
|
||||
if (!empty($a_wol[$_POST['id']])) {
|
||||
unset($a_wol[$_POST['id']]);
|
||||
write_config();
|
||||
}
|
||||
exit;
|
||||
} elseif (isset($_POST['act']) && $_POST['act'] == "wakeall") {
|
||||
$savemsg = "";
|
||||
$result = array();
|
||||
foreach ($a_wol as $wolent) {
|
||||
$if = $wolent['interface'];
|
||||
$ipaddr = get_interface_ip($if);
|
||||
if (!is_ipaddr($ipaddr)) {
|
||||
continue;
|
||||
}
|
||||
$bcip = escapeshellarg(gen_subnet_max($ipaddr, get_interface_subnet($if)));
|
||||
/* Execute wol command and check return code. */
|
||||
if (!mwexec("/usr/local/bin/wol -i {$bcip} ". escapeshellarg($wolent['mac']))) {
|
||||
$result[] = sprintf(gettext('Sent magic packet to %s (%s).'), htmlspecialchars($wolent['mac']), $wolent['descr']);
|
||||
} else {
|
||||
$result[] = sprintf(gettext('Please check the %ssystem log%s, the wol command for %s (%s) did not complete successfully.'), '<a href="/diag_logs.php">', '</a>', $wolent['descr'], htmlspecialchars($wolent['mac']));
|
||||
}
|
||||
}
|
||||
echo json_encode($result);
|
||||
exit;
|
||||
} elseif (isset($_POST['mac'])) {
|
||||
/* input validation */
|
||||
if (empty($_POST['mac']) || !is_macaddr($_POST['mac'])) {
|
||||
$input_errors[] = gettext("A valid MAC address must be specified.");
|
||||
}
|
||||
if (empty($_POST['if'])) {
|
||||
$input_errors[] = gettext("A valid interface must be specified.");
|
||||
} else {
|
||||
$ipaddr = get_interface_ip($_POST['if']);
|
||||
if (!is_ipaddr($ipaddr)) {
|
||||
$input_errors[] = gettext("A valid ip could not be found!");
|
||||
}
|
||||
}
|
||||
|
||||
if (count($input_errors) == 0) {
|
||||
/* determine broadcast address */
|
||||
$bcip = escapeshellarg(gen_subnet_max($ipaddr, get_interface_subnet($_POST['if'])));
|
||||
/* Execute wol command and check return code. */
|
||||
if(!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($_POST['mac']))) {
|
||||
$savemsg = sprintf(gettext('Sent magic packet to %s.'), $_POST['mac']);
|
||||
} else {
|
||||
$savemsg = sprintf(gettext('Please check the %ssystem log%s, the wol command for %s did not complete successfully.'), '<a href="/diag_logs.php">', '</a>', $_POST['mac']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
include("head.inc");
|
||||
?>
|
||||
|
||||
<body>
|
||||
<script>
|
||||
$( document ).ready(function() {
|
||||
// delete host action
|
||||
$(".act_delete_entry").click(function(event){
|
||||
event.preventDefault();
|
||||
var id = $(this).data("id");
|
||||
// delete single
|
||||
BootstrapDialog.show({
|
||||
type:BootstrapDialog.TYPE_DANGER,
|
||||
title: "<?= gettext("Wake on LAN");?>",
|
||||
message: "<?=gettext("Do you really want to delete this entry?");?>",
|
||||
buttons: [{
|
||||
label: "<?= gettext("No");?>",
|
||||
action: function(dialogRef) {
|
||||
dialogRef.close();
|
||||
}}, {
|
||||
label: "<?= gettext("Yes");?>",
|
||||
action: function(dialogRef) {
|
||||
$.post(window.location, {act: 'del', id:id}, function(data) {
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
}]
|
||||
});
|
||||
});
|
||||
$("#act_wake_all").click(function(event){
|
||||
event.preventDefault();
|
||||
$.post(window.location, {act: 'wakeall'}, function(data) {
|
||||
BootstrapDialog.show({
|
||||
type:BootstrapDialog.TYPE_INFO,
|
||||
title: "<?= gettext("Result");?>",
|
||||
message: JSON.parse(data).join('<br/>')
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php include("fbegin.inc"); ?>
|
||||
<section class="page-content-main">
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
|
||||
<?php if (isset($savemsg)) print_info_box($savemsg); ?>
|
||||
<section class="col-xs-12">
|
||||
<div class="content-box">
|
||||
<div class="content-box-main">
|
||||
<form method="post" name="iform" id="iform">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped opnsense_standard_table_form">
|
||||
<thead>
|
||||
<tr>
|
||||
<td style="width:22%">
|
||||
<strong><?=gettext("Wake on LAN");?></strong>
|
||||
</td>
|
||||
<td style="width:78%; text-align:right">
|
||||
<small><?=gettext("full help"); ?> </small>
|
||||
<i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page"></i>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><a id="help_for_interface" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Interface");?></td>
|
||||
<td>
|
||||
<select name="if" class="selectpicker">
|
||||
<?php
|
||||
if (!empty($_POST['if'])) {
|
||||
$if = $_POST['if'];
|
||||
} elseif (!empty($_GET['if'])) {
|
||||
$if = $_GET['if'];
|
||||
} else {
|
||||
$if = null;
|
||||
}
|
||||
foreach (get_configured_interface_with_descr() as $iface => $ifacename): ?>
|
||||
<option value="<?=$iface;?>" <?=$iface == $if ? "selected=\"selected\"" : ""; ?>>
|
||||
<?=htmlspecialchars($ifacename);?>
|
||||
</option>
|
||||
<?php
|
||||
endforeach; ?>
|
||||
</select>
|
||||
<output class="hidden" for="help_for_interface">
|
||||
<?=gettext("Choose which interface the host to be woken up is connected to.");?>
|
||||
</output>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a id="help_for_mac" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("MAC address");?></td>
|
||||
<td>
|
||||
<input name="mac" type="text" id="mac" value="<?=!empty($_GET['mac']) ? htmlspecialchars(strtolower(str_replace("-", ":", $_GET['mac']))) : "";?>" />
|
||||
<output class="hidden" for="help_for_mac">
|
||||
<?=sprintf(gettext("Enter a MAC address %sin the following format: xx:xx:xx:xx:xx:xx%s"),'<strong>','</strong>');?>
|
||||
</output>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> </td>
|
||||
<td>
|
||||
<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Send");?>" />
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<?=gettext("Wake all clients at once:");?>
|
||||
<button type="button" class="btn btn-default btn-xs" id="act_wake_all">
|
||||
<span class="glyphicon glyphicon-time"></span>
|
||||
</button>
|
||||
<?=gettext("Or Click the MAC address to wake up an individual device:");?>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section class="col-xs-12">
|
||||
<div class="content-box">
|
||||
<div class="content-box-main ">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<td><?=gettext("Interface");?></td>
|
||||
<td><?=gettext("MAC address");?></td>
|
||||
<td><?=gettext("Description");?></td>
|
||||
<td>
|
||||
<a href="services_wol_edit.php" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span></a>
|
||||
</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ($a_wol as $wolent): ?>
|
||||
<tr>
|
||||
<td>
|
||||
<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($wolent['interface']));?>
|
||||
</td>
|
||||
<td>
|
||||
<a href="?mac=<?=$wolent['mac'];?>&if=<?=$wolent['interface'];?>"><?=strtolower($wolent['mac']);?></a>
|
||||
</td>
|
||||
<td>
|
||||
<?=htmlspecialchars($wolent['descr']);?>
|
||||
</td>
|
||||
<td>
|
||||
<a href="services_wol_edit.php?id=<?=$i;?>" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-pencil"></span></a>
|
||||
<button data-id="<?=$i;?>" type="button" class="act_delete_entry btn btn-xs btn-default"><span class="fa fa-trash text-muted"></span></button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach; ?>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="4">
|
||||
<span class="text-danger"><strong><?=gettext("Note:");?><br /></strong></span>
|
||||
<?= gettext('This service can be used to wake up (power on) computers by sending special "Magic Packets". The NIC in the computer that is to be woken up must support Wake on LAN and has to be configured properly (WOL cable, BIOS settings).');?>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<?php include("foot.inc"); ?>
|
||||
@@ -1,169 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (C) 2014-2016 Deciso B.V.
|
||||
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
require_once("guiconfig.inc");
|
||||
require_once("interfaces.inc");
|
||||
|
||||
function wolcmp($a, $b) {
|
||||
return strcmp($a['descr'], $b['descr']);
|
||||
}
|
||||
|
||||
|
||||
$a_wol = &config_read_array('wol', 'wolentry');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
if (isset($_GET['id']) && !empty($a_wol[$_GET['id']])) {
|
||||
$id = $_GET['id'];
|
||||
}
|
||||
$pconfig = array();
|
||||
foreach (array('interface', 'mac', 'descr') as $fieldname) {
|
||||
if (isset($id) && isset($a_wol[$id][$fieldname])) {
|
||||
$pconfig[$fieldname] = $a_wol[$id][$fieldname];
|
||||
} elseif (isset($_GET[$fieldname])) {
|
||||
$pconfig[$fieldname] = $_GET[$fieldname];
|
||||
} else {
|
||||
$pconfig[$fieldname] = null;
|
||||
}
|
||||
}
|
||||
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (isset($_POST['id']) && !empty($a_wol[$_POST['id']])) {
|
||||
$id = $_POST['id'];
|
||||
}
|
||||
$pconfig = $_POST;
|
||||
$input_errors = array();
|
||||
|
||||
/* input validation */
|
||||
$reqdfields = explode(" ", "interface mac");
|
||||
$reqdfieldsn = array(gettext("Interface"),gettext("MAC address"));
|
||||
|
||||
do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);
|
||||
|
||||
/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
|
||||
$pconfig['mac'] = strtolower(str_replace("-", ":", $pconfig['mac']));
|
||||
|
||||
if (!empty($pconfig['mac']) && !is_macaddr($_POST['mac'])) {
|
||||
$input_errors[] = gettext("A valid MAC address must be specified.");
|
||||
}
|
||||
if (count($input_errors) == 0) {
|
||||
$wolent = array();
|
||||
$wolent['interface'] = $_POST['interface'];
|
||||
$wolent['mac'] = $_POST['mac'];
|
||||
$wolent['descr'] = $_POST['descr'];
|
||||
|
||||
if (isset($id)) {
|
||||
$a_wol[$id] = $wolent;
|
||||
} else {
|
||||
$a_wol[] = $wolent;
|
||||
}
|
||||
usort($config['wol']['wolentry'], "wolcmp");
|
||||
write_config();
|
||||
|
||||
header(url_safe('Location: /services_wol.php'));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
include("head.inc");
|
||||
legacy_html_escape_form_data($pconfig);
|
||||
?>
|
||||
|
||||
<body>
|
||||
<?php include("fbegin.inc"); ?>
|
||||
<section class="page-content-main">
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
|
||||
<section class="col-xs-12">
|
||||
<div class="content-box">
|
||||
<form method="post" name="iform" id="iform">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped opnsense_standard_table_form">
|
||||
<tr>
|
||||
<td style="width:22%">
|
||||
<strong><?=gettext("Edit WOL entry");?></strong>
|
||||
</td>
|
||||
<td style="width:78%; text-align:right">
|
||||
<small><?=gettext("full help"); ?> </small>
|
||||
<i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page"></i>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a id="help_for_interface" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Interface");?></td>
|
||||
<td>
|
||||
<select name="interface" class="selectpicker">
|
||||
<?php
|
||||
foreach (get_configured_interface_with_descr() as $iface => $ifacename): ?>
|
||||
<option value="<?=$iface;?>" <?= !link_interface_to_bridge($iface) && $iface == $pconfig['interface'] ? "selected=\"selected\"" : ""; ?>>
|
||||
<?=htmlspecialchars($ifacename);?>
|
||||
</option>
|
||||
<?php
|
||||
endforeach; ?>
|
||||
</select>
|
||||
<output class="hidden" for="help_for_interface">
|
||||
<?=gettext("Choose which interface this host is connected to.");?>
|
||||
</output>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a id="help_for_mac" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("MAC address");?></td>
|
||||
<td>
|
||||
<input name="mac" type="text" value="<?=$pconfig['mac'];?>" />
|
||||
<output class="hidden" for="help_for_mac">
|
||||
<?=gettext("Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx");?>
|
||||
</output>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a id="help_for_descr" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Description");?></td>
|
||||
<td>
|
||||
<input name="descr" type="text" value="<?=$pconfig['descr'];?>" />
|
||||
<output class="hidden" for="help_for_descr">
|
||||
<?=gettext("You may enter a description here for your reference (not parsed).");?>
|
||||
</output>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> </td>
|
||||
<td>
|
||||
<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
|
||||
<input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='/services_wol.php'" />
|
||||
<?php if (isset($id)): ?>
|
||||
<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<?php include("foot.inc"); ?>
|
||||
@@ -30,12 +30,8 @@ require_once("guiconfig.inc");
|
||||
require_once("widgets/include/wake_on_lan.inc");
|
||||
require_once("interfaces.inc");
|
||||
|
||||
if (isset($config['wol']['wolentry'])) {
|
||||
$wolcomputers = $config['wol']['wolentry'];
|
||||
} else {
|
||||
$wolcomputers = array();
|
||||
}
|
||||
|
||||
use OPNsense\Wol\Wol;
|
||||
$wol = new Wol();
|
||||
?>
|
||||
<table class="table table-striped table-condensed">
|
||||
<thead>
|
||||
@@ -48,24 +44,24 @@ if (isset($config['wol']['wolentry'])) {
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($wolcomputers as $wolent):
|
||||
$is_active = exec("/usr/sbin/arp -an |/usr/bin/grep {$wolent['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");?>
|
||||
foreach ($wol->wolentry->__items as $wolent):
|
||||
$is_active = exec("/usr/sbin/arp -an |/usr/bin/grep {$wolent->mac}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");?>
|
||||
<tr>
|
||||
<td><?=$wolent['descr'];?><br/><?=$wolent['mac'];?></td>
|
||||
<td><?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($wolent['interface']));?></td>
|
||||
<td><?= $wolent->descr ?><br/><?= $wolent->mac ?></td>
|
||||
<td><?=htmlspecialchars(convert_friendly_interface_to_friendly_descr((string)$wolent->interface));?></td>
|
||||
<td>
|
||||
<span class="glyphicon glyphicon-<?=$is_active == 1 ? "play" : "remove";?> text-<?=$is_active == 1 ? "success" : "danger";?>" ></span>
|
||||
<?=$is_active == 1 ? gettext("Online") : gettext("Offline");?>
|
||||
</td>
|
||||
<td>
|
||||
<a href="services_wol.php?mac=<?=$wolent['mac'];?>&if=<?=$wolent['interface'];?>">
|
||||
<button class="btn btn-primary btn-sm wakeupbtn" data-mac="<?= $wolent->mac ?>" data-interface="<?= $wolent->interface ?>" data-uuid="<?= $wolent->getAttributes()['uuid'] ?>">
|
||||
<span class="glyphicon glyphicon-flash" title="<?=gettext("Wake Up");?>"></span>
|
||||
</a>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
endforeach;
|
||||
if (count($wolcomputers) == 0):?>
|
||||
if (count($wol->wolentry->__items) == 0):?>
|
||||
<tr>
|
||||
<td colspan="4" ><?=gettext("No saved WoL addresses");?></td>
|
||||
</tr>
|
||||
@@ -78,3 +74,11 @@ if (isset($config['wol']['wolentry'])) {
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
<script>
|
||||
$('.wakeupbtn').click(function(event) {
|
||||
event.preventDefault();
|
||||
var data = this.dataset;
|
||||
$.post('/api/wol/wol/set', {'uuid': data['uuid']}, function(result) {
|
||||
});
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user