|
|
|
@ -3,13 +3,11 @@ |
|
|
|
namespace RouterOS; |
|
|
|
|
|
|
|
use RouterOS\Exceptions\ClientException; |
|
|
|
use RouterOS\Interfaces\ClientInterface; |
|
|
|
use RouterOS\Interfaces\ConfigInterface; |
|
|
|
use RouterOS\Exceptions\ConfigException; |
|
|
|
use RouterOS\Interfaces\QueryInterface; |
|
|
|
|
|
|
|
/** |
|
|
|
* Class Client for RouterOS management |
|
|
|
* |
|
|
|
* @package RouterOS |
|
|
|
* @since 0.1 |
|
|
|
*/ |
|
|
|
@ -17,37 +15,46 @@ class Client implements Interfaces\ClientInterface |
|
|
|
{ |
|
|
|
/** |
|
|
|
* Socket resource |
|
|
|
* |
|
|
|
* @var resource|null |
|
|
|
*/ |
|
|
|
private $_socket; |
|
|
|
|
|
|
|
/** |
|
|
|
* Code of error |
|
|
|
* |
|
|
|
* @var int |
|
|
|
*/ |
|
|
|
private $_socket_err_num; |
|
|
|
|
|
|
|
/** |
|
|
|
* Description of socket error |
|
|
|
* |
|
|
|
* @var string |
|
|
|
*/ |
|
|
|
private $_socket_err_str; |
|
|
|
|
|
|
|
/** |
|
|
|
* Configuration of connection |
|
|
|
* @var ConfigInterface |
|
|
|
* |
|
|
|
* @var \RouterOS\Config |
|
|
|
*/ |
|
|
|
private $_config; |
|
|
|
|
|
|
|
/** |
|
|
|
* Client constructor. |
|
|
|
* |
|
|
|
* @param ConfigInterface $config |
|
|
|
* @param array|\RouterOS\Config $config |
|
|
|
* @throws ConfigException |
|
|
|
* @throws ClientException |
|
|
|
*/ |
|
|
|
public function __construct(ConfigInterface $config) |
|
|
|
public function __construct($config) |
|
|
|
{ |
|
|
|
// If array then need create object
|
|
|
|
if (\is_array($config)) { |
|
|
|
$config = new Config($config); |
|
|
|
} |
|
|
|
|
|
|
|
// Check for important keys
|
|
|
|
$this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); |
|
|
|
|
|
|
|
@ -64,10 +71,10 @@ class Client implements Interfaces\ClientInterface |
|
|
|
* Check for important keys |
|
|
|
* |
|
|
|
* @param array $keys |
|
|
|
* @param ConfigInterface $config |
|
|
|
* @param Config $config |
|
|
|
* @throws ConfigException |
|
|
|
*/ |
|
|
|
private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config) |
|
|
|
private function exceptionIfKeyNotExist(array $keys, Config $config) |
|
|
|
{ |
|
|
|
$parameters = $config->getParameters(); |
|
|
|
foreach ($keys as $key) { |
|
|
|
@ -80,8 +87,9 @@ class Client implements Interfaces\ClientInterface |
|
|
|
/** |
|
|
|
* Get some parameter from config |
|
|
|
* |
|
|
|
* @param string $parameter |
|
|
|
* @param string $parameter Name of required parameter |
|
|
|
* @return mixed |
|
|
|
* @throws ConfigException |
|
|
|
*/ |
|
|
|
private function config(string $parameter) |
|
|
|
{ |
|
|
|
@ -89,6 +97,17 @@ class Client implements Interfaces\ClientInterface |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Return socket resource if is exist |
|
|
|
* |
|
|
|
* @return \RouterOS\Config |
|
|
|
* @since 0.6 |
|
|
|
*/ |
|
|
|
public function getConfig(): Config |
|
|
|
{ |
|
|
|
return $this->_config; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Encode given length in RouterOS format |
|
|
|
* |
|
|
|
* @param string $string |
|
|
|
@ -173,13 +192,27 @@ class Client implements Interfaces\ClientInterface |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Alias for ->write()->read() combination of methods |
|
|
|
* |
|
|
|
* @param Query $query |
|
|
|
* @param bool $parse |
|
|
|
* @return array |
|
|
|
* @throws ClientException |
|
|
|
* @since 0.6 |
|
|
|
*/ |
|
|
|
public function wr(Query $query, bool $parse = true): array |
|
|
|
{ |
|
|
|
return $this->write($query)->read($parse); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Send write query to RouterOS (with or without tag) |
|
|
|
* |
|
|
|
* @param QueryInterface $query |
|
|
|
* @return ClientInterface |
|
|
|
* @param Query $query |
|
|
|
* @return Client |
|
|
|
* @throws ClientException |
|
|
|
*/ |
|
|
|
public function write(QueryInterface $query): ClientInterface |
|
|
|
public function write(Query $query): Client |
|
|
|
{ |
|
|
|
// Send commands via loop to router
|
|
|
|
foreach ($query->getQuery() as $command) { |
|
|
|
@ -295,6 +328,7 @@ class Client implements Interfaces\ClientInterface |
|
|
|
* |
|
|
|
* @return bool |
|
|
|
* @throws ClientException |
|
|
|
* @throws ConfigException |
|
|
|
*/ |
|
|
|
private function login(): bool |
|
|
|
{ |
|
|
|
@ -307,8 +341,7 @@ class Client implements Interfaces\ClientInterface |
|
|
|
// Now need use this hash for authorization
|
|
|
|
$query = (new Query('/login')) |
|
|
|
->add('=name=' . $this->config('user')) |
|
|
|
->add('=response=00' . md5(\chr(0) . $this->config('pass') . pack('H*', |
|
|
|
$response['after']['ret']))); |
|
|
|
->add('=response=00' . md5(\chr(0) . $this->config('pass') . pack('H*', $response['after']['ret']))); |
|
|
|
} else { |
|
|
|
// Just login with our credentials
|
|
|
|
$query = (new Query('/login')) |
|
|
|
@ -328,6 +361,7 @@ class Client implements Interfaces\ClientInterface |
|
|
|
* |
|
|
|
* @return bool |
|
|
|
* @throws ClientException |
|
|
|
* @throws ConfigException |
|
|
|
*/ |
|
|
|
private function connect(): bool |
|
|
|
{ |
|
|
|
@ -385,6 +419,7 @@ class Client implements Interfaces\ClientInterface |
|
|
|
* Initiate socket session |
|
|
|
* |
|
|
|
* @throws ClientException |
|
|
|
* @throws ConfigException |
|
|
|
*/ |
|
|
|
private function openSocket() |
|
|
|
{ |
|
|
|
|