A computer has long been replaced by many for television, and what is missing from a computer for comfortable watching movies, TV shows and other things? I, personally, did not have enough remote control.
I am a little keen on web programming in my free time. It was decided to write my bike, and at the same time combine business with pleasure. The possibility of using a console to control a computer was studied, and first of all I was interested in the following minimum amount of tasks (which increased slightly afterwards):
- Ability to send a PC to sleep
- Ability to turn off the volume
With the help of a well-known search engine I found a console program,
NirCmd , the program allows you to perform quite a lot of actions other than those mentioned above. With the main part, we decided. As I said, I study php a bit, my web server is usually constantly running, so I didn’t think of anything specific in terms of the server. The server is written in php and consists of two classes: Control, which represents the methods that execute console commands, and Route, which validates incoming requests and executes methods of the Control class.
')
Control looks like this (the code is of the same type, so I have shortened it):
class Control implements ActionControl { protected $Path;
Second class - Route. Actually, the client refers to it. The class first validates the request, and, if the request is correct, calls the methods of the Control class.
It looks like this:
class Route implements ActionRoute { protected $possible = []; protected $ControlObj; function __construct($obj) { $this->possible = get_class_methods($obj); $this->ControlObj = $obj; } function route($arr) { forEach($arr as $key => $value) { if (in_array($key, $this->possible) && $value == 'true') { $this->execute($key); } else { Message::sent('wrong method'); } } } function execute($c) { $this->ControlObj->$c(); Message::sent('executed'); } }
To succeed in which case it is easy to expand the number of methods in the Control class, validation in the Route class is not rigidly tied to a specific list of methods of the Control class, more precisely tied, but all possible methods are extracted from the object itself, and the incoming data is compared with them.
The file itself that the client accesses looks like this:
<? define("PATH", "C:/nircmd/nircmd.exe"); function __autoload($name) { require "class/$name.class.php"; } $obj = new Control(PATH); $route = new Route($obj); if ($_GET) { $route->route($_GET); } ?>
If a GET request arrives, then we give the entire array to the route method.
The client part is a control button and one handler, by which we send data using ajax to the server.
var wrapper = document.querySelector(".wrapper"); function getXmlHttpRequest(){ if (window.XMLHttpRequest) { try { return new XMLHttpRequest(); } catch (e){ } } else if (window.ActiveXObject) { try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e){} try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e){} } return null; } wrapper.addEventListener('click', function(e){ var target = e.target; if(target.tagName!= 'BUTTON') return; if (target.getAttribute('data')=='qestion') { var yn = confirm("?"); if (!yn) return ajaxf(target.id); } ajaxf(target.id); }) function ajaxf($com) { var command = $com+'=true'; var xhr = new getXmlHttpRequest(); xhr.open('GET', 'remote.php?'+command, true); xhr.send(); xhr.onreadystatechange = function() { if (xhr.readyState != 4) return; if (xhr.status != 200) { console.log(xhr.status + ': ' + xhr.statusText); } else { console.log(xhr.responseText); } } }
At present, the following management functionality has been implemented:
- sleep mode
- hibernation
- will reboot
- turn off
- go out
- turn off the monitor
- enable / disable trigger sound
- more volume
- volume less
- Media buttons, stop / play next / prev
Everything I wanted to get, I got, but, as they say, appetite comes with eating. I would like to have feedback from the server, i.e. When you first enter the page, make a request for the current volume level, for example. Unfortunately, nirCmd does not return values when executed, so with what we have now, I do not know how to accomplish this.
A safety issue. Although this is all spinning on the local machine, it will be very sad if the attacker has access to the execution of the nirCmd commands, since You can create a lot of destructive actions.
Ready project on
GIthub .