Hide secret, router forgot ;

This commit is contained in:
Ibnu Maksum 2021-08-19 13:03:55 +07:00
parent b40ffeabbb
commit 4d124bf257
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5
2 changed files with 65 additions and 67 deletions

View File

@ -2,11 +2,11 @@
/** /**
* RouterOS API client implementation. * RouterOS API client implementation.
* *
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts. * RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
* *
* PHP version 5 * PHP version 5
* *
* @category Net * @category Net
* @package PEAR2_Net_RouterOS * @package PEAR2_Net_RouterOS
* @author Vasil Rangelov <boen.robot@gmail.com> * @author Vasil Rangelov <boen.robot@gmail.com>
@ -37,9 +37,9 @@ use Exception as E;
/** /**
* A RouterOS client. * A RouterOS client.
* *
* Provides functionality for easily communicating with a RouterOS host. * Provides functionality for easily communicating with a RouterOS host.
* *
* @category Net * @category Net
* @package PEAR2_Net_RouterOS * @package PEAR2_Net_RouterOS
* @author Vasil Rangelov <boen.robot@gmail.com> * @author Vasil Rangelov <boen.robot@gmail.com>
@ -85,7 +85,7 @@ class Client
* Key is the tag of the request, and the value is the callback for it. * Key is the tag of the request, and the value is the callback for it.
*/ */
protected $callbacks = array(); protected $callbacks = array();
/** /**
* @var Registry A registry for the operations. Particularly helpful at * @var Registry A registry for the operations. Particularly helpful at
* persistent connections. * persistent connections.
@ -99,10 +99,10 @@ class Client
/** /**
* Creates a new instance of a RouterOS API client. * Creates a new instance of a RouterOS API client.
* *
* Creates a new instance of a RouterOS API client with the specified * Creates a new instance of a RouterOS API client with the specified
* settings. * settings.
* *
* @param string $host Hostname (IP or domain) of the RouterOS server. * @param string $host Hostname (IP or domain) of the RouterOS server.
* @param string $username The RouterOS username. * @param string $username The RouterOS username.
* @param string $password The RouterOS password. * @param string $password The RouterOS password.
@ -121,7 +121,7 @@ class Client
* context and your default context uses the value "DEFAULT" for * context and your default context uses the value "DEFAULT" for
* ciphers, "ADH" will be automatically added to the list of ciphers. * ciphers, "ADH" will be automatically added to the list of ciphers.
* @param resource $context A context for the socket. * @param resource $context A context for the socket.
* *
* @see sendSync() * @see sendSync()
* @see sendAsync() * @see sendAsync()
*/ */
@ -156,7 +156,7 @@ class Client
if ((!$persist if ((!$persist
|| !($old = $this->com->getTransmitter()->lock(S::DIRECTION_ALL))) || !($old = $this->com->getTransmitter()->lock(S::DIRECTION_ALL)))
&& $this->com->getTransmitter()->isFresh() && $this->com->getTransmitter()->isFresh()
) { ) {
if (!static::login($this->com, $username, $password, $timeout)) { if (!static::login($this->com, $username, $password, $timeout)) {
$this->com->close(); $this->com->close();
throw new DataFlowException( throw new DataFlowException(
@ -165,29 +165,29 @@ class Client
); );
} }
} }
if (isset($old)) { if (isset($old)) {
$this->com->getTransmitter()->lock($old, true); $this->com->getTransmitter()->lock($old, true);
} }
if ($persist) { if ($persist) {
$this->registry = new Registry("{$host}:{$port}/{$username}"); $this->registry = new Registry("{$host}:{$port}/{$username}");
} }
} }
/** /**
* A shorthand gateway. * A shorthand gateway.
* *
* This is a magic PHP method that allows you to call the object as a * This is a magic PHP method that allows you to call the object as a
* function. Depending on the argument given, one of the other functions in * function. Depending on the argument given, one of the other functions in
* the class is invoked and its returned value is returned by this function. * the class is invoked and its returned value is returned by this function.
* *
* @param mixed $arg Value can be either a {@link Request} to send, which * @param mixed $arg Value can be either a {@link Request} to send, which
* would be sent asynchoniously if it has a tag, and synchroniously if * would be sent asynchoniously if it has a tag, and synchroniously if
* not, a number to loop with or NULL to complete all pending requests. * not, a number to loop with or NULL to complete all pending requests.
* Any other value is converted to string and treated as the tag of a * Any other value is converted to string and treated as the tag of a
* request to complete. * request to complete.
* *
* @return mixed Whatever the long form function would have returned. * @return mixed Whatever the long form function would have returned.
*/ */
public function __invoke($arg = null) public function __invoke($arg = null)
@ -205,13 +205,13 @@ class Client
/** /**
* Login to a RouterOS connection. * Login to a RouterOS connection.
* *
* @param Communicator $com The communicator to attempt to login to. * @param Communicator $com The communicator to attempt to login to.
* @param string $username The RouterOS username. * @param string $username The RouterOS username.
* @param string $password The RouterOS password. * @param string $password The RouterOS password.
* @param int|null $timeout The time to wait for each response. NULL * @param int|null $timeout The time to wait for each response. NULL
* waits indefinetly. * waits indefinetly.
* *
* @return bool TRUE on success, FALSE on failure. * @return bool TRUE on success, FALSE on failure.
*/ */
public static function login( public static function login(
@ -254,17 +254,17 @@ class Client
/** /**
* Login to a RouterOS connection. * Login to a RouterOS connection.
* *
* This is the actual login procedure, applied regardless of persistence and * This is the actual login procedure, applied regardless of persistence and
* charset settings. * charset settings.
* *
* @param Communicator $com The communicator to attempt to login to. * @param Communicator $com The communicator to attempt to login to.
* @param string $username The RouterOS username. * @param string $username The RouterOS username.
* @param string $password The RouterOS password. Potentially parsed * @param string $password The RouterOS password. Potentially parsed
* already by iconv. * already by iconv.
* @param int|null $timeout The time to wait for each response. NULL * @param int|null $timeout The time to wait for each response. NULL
* waits indefinetly. * waits indefinetly.
* *
* @return bool TRUE on success, FALSE on failure. * @return bool TRUE on success, FALSE on failure.
*/ */
private static function _login( private static function _login(
@ -280,13 +280,13 @@ class Client
$request->setArgument('password', $password); $request->setArgument('password', $password);
$request->send($com); $request->send($com);
$response = new Response($com, false, $timeout); $response = new Response($com, false, $timeout);
return $response->getType() === Response::TYPE_FINAL return $response->getType() === Response::TYPE_FINAL;
null === $response->getProperty('ret'); null === $response->getProperty('ret');
} }
/** /**
* Sets the charset(s) for this connection. * Sets the charset(s) for this connection.
* *
* Sets the charset(s) for this connection. The specified charset(s) will be * Sets the charset(s) for this connection. The specified charset(s) will be
* used for all future requests and responses. When sending, * used for all future requests and responses. When sending,
* {@link Communicator::CHARSET_LOCAL} is converted to * {@link Communicator::CHARSET_LOCAL} is converted to
@ -295,7 +295,7 @@ class Client
* {@link Communicator::CHARSET_LOCAL}. Setting NULL to either charset will * {@link Communicator::CHARSET_LOCAL}. Setting NULL to either charset will
* disable charset convertion, and data will be both sent and received "as * disable charset convertion, and data will be both sent and received "as
* is". * is".
* *
* @param mixed $charset The charset to set. If $charsetType is * @param mixed $charset The charset to set. If $charsetType is
* {@link Communicator::CHARSET_ALL}, you can supply either a string to * {@link Communicator::CHARSET_ALL}, you can supply either a string to
* use for all charsets, or an array with the charset types as keys, and * use for all charsets, or an array with the charset types as keys, and
@ -303,7 +303,7 @@ class Client
* @param int $charsetType Which charset to set. Valid values are the * @param int $charsetType Which charset to set. Valid values are the
* Communicator::CHARSET_* constants. Any other value is treated as * Communicator::CHARSET_* constants. Any other value is treated as
* {@link Communicator::CHARSET_ALL}. * {@link Communicator::CHARSET_ALL}.
* *
* @return string|array The old charset. If $charsetType is * @return string|array The old charset. If $charsetType is
* {@link Communicator::CHARSET_ALL}, the old values will be returned as * {@link Communicator::CHARSET_ALL}, the old values will be returned as
* an array with the types as keys, and charsets as values. * an array with the types as keys, and charsets as values.
@ -315,14 +315,14 @@ class Client
) { ) {
return $this->com->setCharset($charset, $charsetType); return $this->com->setCharset($charset, $charsetType);
} }
/** /**
* Gets the charset(s) for this connection. * Gets the charset(s) for this connection.
* *
* @param int $charsetType Which charset to get. Valid values are the * @param int $charsetType Which charset to get. Valid values are the
* Communicator::CHARSET_* constants. Any other value is treated as * Communicator::CHARSET_* constants. Any other value is treated as
* {@link Communicator::CHARSET_ALL}. * {@link Communicator::CHARSET_ALL}.
* *
* @return string|array The current charset. If $charsetType is * @return string|array The current charset. If $charsetType is
* {@link Communicator::CHARSET_ALL}, the current values will be * {@link Communicator::CHARSET_ALL}, the current values will be
* returned as an array with the types as keys, and charsets as values. * returned as an array with the types as keys, and charsets as values.
@ -335,7 +335,7 @@ class Client
/** /**
* Sends a request and waits for responses. * Sends a request and waits for responses.
* *
* @param Request $request The request to send. * @param Request $request The request to send.
* @param callback $callback Optional. A function that is to be executed * @param callback $callback Optional. A function that is to be executed
* when new responses for this request are available. The callback takes * when new responses for this request are available. The callback takes
@ -344,7 +344,7 @@ class Client
* TRUE, the request is canceled. Note that the callback may be executed * TRUE, the request is canceled. Note that the callback may be executed
* one last time after that with a response that notifies about the * one last time after that with a response that notifies about the
* canceling. * canceling.
* *
* @return $this The client object. * @return $this The client object.
* @see completeRequest() * @see completeRequest()
* @see loop() * @see loop()
@ -372,7 +372,7 @@ class Client
UnexpectedValueException::CODE_CALLBACK_INVALID UnexpectedValueException::CODE_CALLBACK_INVALID
); );
} }
$this->send($request); $this->send($request);
if (null === $callback) { if (null === $callback) {
@ -387,14 +387,14 @@ class Client
/** /**
* Checks if a request is active. * Checks if a request is active.
* *
* Checks if a request is active. A request is considered active if it's a * Checks if a request is active. A request is considered active if it's a
* pending request and/or has responses that are not yet extracted. * pending request and/or has responses that are not yet extracted.
* *
* @param string $tag The tag of the request to look for. * @param string $tag The tag of the request to look for.
* @param int $filter One of the FILTER_* consntants. Limits the search * @param int $filter One of the FILTER_* consntants. Limits the search
* to the specified places. * to the specified places.
* *
* @return bool TRUE if the request is active, FALSE otherwise. * @return bool TRUE if the request is active, FALSE otherwise.
* @see getPendingRequestsCount() * @see getPendingRequestsCount()
* @see completeRequest() * @see completeRequest()
@ -413,9 +413,9 @@ class Client
/** /**
* Sends a request and gets the full response. * Sends a request and gets the full response.
* *
* @param Request $request The request to send. * @param Request $request The request to send.
* *
* @return ResponseCollection The received responses as a collection. * @return ResponseCollection The received responses as a collection.
* @see sendAsync() * @see sendAsync()
* @see close() * @see close()
@ -433,13 +433,13 @@ class Client
/** /**
* Completes a specified request. * Completes a specified request.
* *
* Starts an event loop for the RouterOS callbacks and finishes when a * Starts an event loop for the RouterOS callbacks and finishes when a
* specified request is completed. * specified request is completed.
* *
* @param string $tag The tag of the request to complete. Setting NULL * @param string $tag The tag of the request to complete. Setting NULL
* completes all requests. * completes all requests.
* *
* @return ResponseCollection A collection of {@link Response} objects that * @return ResponseCollection A collection of {@link Response} objects that
* haven't been passed to a callback function or previously extracted * haven't been passed to a callback function or previously extracted
* with {@link static::extractNewResponses()}. Returns an empty * with {@link static::extractNewResponses()}. Returns an empty
@ -477,13 +477,13 @@ class Client
/** /**
* Extracts responses for a request. * Extracts responses for a request.
* *
* Gets all new responses for a request that haven't been passed to a * Gets all new responses for a request that haven't been passed to a
* callback and clears the buffer from them. * callback and clears the buffer from them.
* *
* @param string $tag The tag of the request to extract new responses for. * @param string $tag The tag of the request to extract new responses for.
* Specifying NULL with extract new responses for all requests. * Specifying NULL with extract new responses for all requests.
* *
* @return ResponseCollection A collection of {@link Response} objects for * @return ResponseCollection A collection of {@link Response} objects for
* the specified request. * the specified request.
* @see loop() * @see loop()
@ -521,15 +521,15 @@ class Client
/** /**
* Starts an event loop for the RouterOS callbacks. * Starts an event loop for the RouterOS callbacks.
* *
* Starts an event loop for the RouterOS callbacks and finishes when there * Starts an event loop for the RouterOS callbacks and finishes when there
* are no more pending requests or when a specified timeout has passed * are no more pending requests or when a specified timeout has passed
* (whichever comes first). * (whichever comes first).
* *
* @param int $sTimeout Timeout for the loop. If NULL, there is no time * @param int $sTimeout Timeout for the loop. If NULL, there is no time
* limit. * limit.
* @param int $usTimeout Microseconds to add to the time limit. * @param int $usTimeout Microseconds to add to the time limit.
* *
* @return bool TRUE when there are any more pending requests, FALSE * @return bool TRUE when there are any more pending requests, FALSE
* otherwise. * otherwise.
* @see extractNewResponses() * @see extractNewResponses()
@ -579,7 +579,7 @@ class Client
/** /**
* Gets the number of pending requests. * Gets the number of pending requests.
* *
* @return int The number of pending requests. * @return int The number of pending requests.
* @see isRequestActive() * @see isRequestActive()
*/ */
@ -590,16 +590,16 @@ class Client
/** /**
* Cancels a request. * Cancels a request.
* *
* Cancels an active request. Using this function in favor of a plain call * Cancels an active request. Using this function in favor of a plain call
* to the "/cancel" command is highly reccomended, as it also updates the * to the "/cancel" command is highly reccomended, as it also updates the
* counter of pending requests properly. Note that canceling a request also * counter of pending requests properly. Note that canceling a request also
* removes any responses for it that were not previously extracted with * removes any responses for it that were not previously extracted with
* {@link static::extractNewResponses()}. * {@link static::extractNewResponses()}.
* *
* @param string $tag Tag of the request to cancel. Setting NULL will cancel * @param string $tag Tag of the request to cancel. Setting NULL will cancel
* all requests. * all requests.
* *
* @return $this The client object. * @return $this The client object.
* @see sendAsync() * @see sendAsync()
* @see close() * @see close()
@ -662,14 +662,14 @@ class Client
/** /**
* Sets response streaming setting. * Sets response streaming setting.
* *
* Sets whether future responses are streamed. If responses are streamed, * Sets whether future responses are streamed. If responses are streamed,
* the argument values are returned as streams instead of strings. This is * the argument values are returned as streams instead of strings. This is
* particularly useful if you expect a response that may contain one or more * particularly useful if you expect a response that may contain one or more
* very large words. * very large words.
* *
* @param bool $streamingResponses Whether to stream future responses. * @param bool $streamingResponses Whether to stream future responses.
* *
* @return bool The previous value of the setting. * @return bool The previous value of the setting.
* @see isStreamingResponses() * @see isStreamingResponses()
*/ */
@ -682,9 +682,9 @@ class Client
/** /**
* Gets response streaming setting. * Gets response streaming setting.
* *
* Gets whether future responses are streamed. * Gets whether future responses are streamed.
* *
* @return bool The value of the setting. * @return bool The value of the setting.
* @see setStreamingResponses() * @see setStreamingResponses()
*/ */
@ -695,11 +695,11 @@ class Client
/** /**
* Closes the opened connection, even if it is a persistent one. * Closes the opened connection, even if it is a persistent one.
* *
* Closes the opened connection, even if it is a persistent one. Note that * Closes the opened connection, even if it is a persistent one. Note that
* {@link static::extractNewResponses()} can still be used to extract * {@link static::extractNewResponses()} can still be used to extract
* responses collected prior to the closing. * responses collected prior to the closing.
* *
* @return bool TRUE on success, FALSE on failure. * @return bool TRUE on success, FALSE on failure.
*/ */
public function close() public function close()
@ -709,7 +709,7 @@ class Client
* The check below is done because for some unknown reason * The check below is done because for some unknown reason
* (either a PHP or a RouterOS bug) calling "/quit" on an encrypted * (either a PHP or a RouterOS bug) calling "/quit" on an encrypted
* connection makes one end hang. * connection makes one end hang.
* *
* Since encrypted connections only appeared in RouterOS 6.1, and * Since encrypted connections only appeared in RouterOS 6.1, and
* the "/quit" call is needed for all <6.0 versions, problems due * the "/quit" call is needed for all <6.0 versions, problems due
* to its absence should be limited to some earlier 6.* versions * to its absence should be limited to some earlier 6.* versions
@ -737,7 +737,7 @@ class Client
$this->pendingRequestsCount = 0; $this->pendingRequestsCount = 0;
return $result; return $result;
} }
/** /**
* Closes the connection, unless it's a persistent one. * Closes the connection, unless it's a persistent one.
*/ */
@ -754,9 +754,9 @@ class Client
/** /**
* Sends a request to RouterOS. * Sends a request to RouterOS.
* *
* @param Request $request The request to send. * @param Request $request The request to send.
* *
* @return $this The client object. * @return $this The client object.
* @see sendSync() * @see sendSync()
* @see sendAsync() * @see sendAsync()
@ -770,14 +770,14 @@ class Client
/** /**
* Dispatches the next response in queue. * Dispatches the next response in queue.
* *
* Dispatches the next response in queue, i.e. it executes the associated * Dispatches the next response in queue, i.e. it executes the associated
* callback if there is one, or places the response in the response buffer. * callback if there is one, or places the response in the response buffer.
* *
* @param int $sTimeout If a response is not immediatly available, wait * @param int $sTimeout If a response is not immediatly available, wait
* this many seconds. If NULL, wait indefinetly. * this many seconds. If NULL, wait indefinetly.
* @param int $usTimeout Microseconds to add to the waiting time. * @param int $usTimeout Microseconds to add to the waiting time.
* *
* @throws SocketException When there's no response within the time limit. * @throws SocketException When there's no response within the time limit.
* @return Response The dispatched response. * @return Response The dispatched response.
*/ */

View File

@ -7,7 +7,7 @@
<div class="panel-body"> <div class="panel-body">
<div class="md-whiteframe-z1 mb20 text-center" style="padding: 15px"> <div class="md-whiteframe-z1 mb20 text-center" style="padding: 15px">
<div class="col-md-8"> <div class="col-md-8">
<form id="site-search" method="post" action="{$_url}routers/list/"> <form id="site-search" method="post" action="{$_url}routers/list/">
<div class="input-group"> <div class="input-group">
<div class="input-group-addon"> <div class="input-group-addon">
@ -31,7 +31,6 @@
<th>{$_L['Router_Name']}</th> <th>{$_L['Router_Name']}</th>
<th>{$_L['IP_Address']}</th> <th>{$_L['IP_Address']}</th>
<th>{$_L['Username']}</th> <th>{$_L['Username']}</th>
<th>{$_L['Router_Secret']}</th>
<th>{$_L['Description']}</th> <th>{$_L['Description']}</th>
<th>{$_L['Manage']}</th> <th>{$_L['Manage']}</th>
</tr> </tr>
@ -42,7 +41,6 @@
<td>{$ds['name']}</td> <td>{$ds['name']}</td>
<td>{$ds['ip_address']}</td> <td>{$ds['ip_address']}</td>
<td>{$ds['username']}</td> <td>{$ds['username']}</td>
<td>{$ds['password']}</td>
<td>{$ds['description']}</td> <td>{$ds['description']}</td>
<td> <td>
<a href="{$_url}routers/edit/{$ds['id']}" class="btn btn-warning btn-sm">{$_L['Edit']}</a> <a href="{$_url}routers/edit/{$ds['id']}" class="btn btn-warning btn-sm">{$_L['Edit']}</a>