mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 07:04:07 +01:00
Controllers are being splited into commands to better dependency management. Signed-off-by: Rubén D <nuxsmin@syspass.org>
146 lines
3.5 KiB
PHP
146 lines
3.5 KiB
PHP
<?php
|
|
/*
|
|
* sysPass
|
|
*
|
|
* @author nuxsmin
|
|
* @link https://syspass.org
|
|
* @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org
|
|
*
|
|
* This file is part of sysPass.
|
|
*
|
|
* sysPass is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* sysPass is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace SP\Util;
|
|
|
|
use SP\Core\Exceptions\SPException;
|
|
|
|
/**
|
|
* Class Connection para crear conexiones TCP o UDP
|
|
*
|
|
* @package SP\Util
|
|
*/
|
|
final class Connection implements ConnectionInterface
|
|
{
|
|
/**
|
|
* @var resource|false
|
|
*/
|
|
protected $socket;
|
|
|
|
protected string $host;
|
|
protected int $port;
|
|
protected int $errorno = 0;
|
|
protected string $errorstr = '';
|
|
|
|
/**
|
|
* @param $host string El host a conectar
|
|
* @param $port int El puerto a conectar
|
|
*/
|
|
public function __construct(string $host, int $port)
|
|
{
|
|
$this->host = gethostbyname($host);
|
|
$this->port = $port;
|
|
}
|
|
|
|
/**
|
|
* Obtener un socket
|
|
*
|
|
* @param $type int EL tipo de socket TCP/UDP
|
|
*
|
|
* @return resource
|
|
* @throws SPException
|
|
*/
|
|
public function getSocket(int $type)
|
|
{
|
|
if ($type === self::TYPE_UDP) {
|
|
$this->socket = $this->getUDPSocket();
|
|
} else {
|
|
$this->socket = $this->getTCPSocket();
|
|
}
|
|
|
|
if ($this->socket === false) {
|
|
throw new SPException($this->getSocketError(), SPException::WARNING);
|
|
}
|
|
|
|
stream_set_timeout($this->socket, self::SOCKET_TIMEOUT);
|
|
|
|
return $this->socket;
|
|
}
|
|
|
|
/**
|
|
* Obtener un socket del tipo UDP
|
|
*
|
|
* @return resource
|
|
*/
|
|
private function getUDPSocket()
|
|
{
|
|
return stream_socket_client(
|
|
'udp://'.$this->host.':'.$this->port,
|
|
$this->errorno,
|
|
$this->errorstr,
|
|
self::SOCKET_TIMEOUT
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Obtener un socket del tipo TCP
|
|
*
|
|
* @return resource
|
|
*/
|
|
private function getTCPSocket()
|
|
{
|
|
return stream_socket_client(
|
|
'tcp://'.$this->host.':'.$this->port,
|
|
$this->errorno,
|
|
$this->errorstr,
|
|
self::SOCKET_TIMEOUT
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Obtener el último error del socket
|
|
*/
|
|
public function getSocketError(): string
|
|
{
|
|
return sprintf('%s (%d)', $this->errorstr, $this->errorno);
|
|
}
|
|
|
|
/**
|
|
* Cerrar el socket
|
|
*/
|
|
public function closeSocket()
|
|
{
|
|
fclose($this->socket);
|
|
}
|
|
|
|
/**
|
|
* Enviar un mensaje al socket
|
|
*
|
|
* @throws SPException
|
|
*/
|
|
public function send(string $message): int
|
|
{
|
|
if (!is_resource($this->socket)) {
|
|
throw new SPException(__u('Socket not initialized'), SPException::WARNING);
|
|
}
|
|
|
|
$nBytes = @fwrite($this->socket, $message);
|
|
|
|
if ($nBytes === false) {
|
|
throw new SPException(__u('Error while sending the data'), SPException::WARNING, $this->getSocketError());
|
|
}
|
|
|
|
return $nBytes;
|
|
}
|
|
} |