<?php
namespace WebBundle\Helper;
/**
* class: ProxyHelper
* ssh -p 8105 te@136.243.143.197
* -----------------------------------------------------
* Created by MihailShirnin on 05.06.2017.
* @package WebBundle\Helper
*/
class ProxyHelper
{
/**
* Список имен серверов, для удаленной чистки кеша
* @var array
*/
public static array $mirrorList = ['site1', 'site3', 'site4'];
private static array $serverList = [
'img' => [
'name' => 'сервер картинок img.tile.expert',
'host' => [
'dev' => '35.158.222.127',
'prod' => '35.158.222.127',
],
'port' => '22',
'user' => 'te',
'mirror' => null,
],
'8106' => [
'name' => 'админка 8106',
'host' => [
'dev' => '136.243.143.197',
'prod' => '136.243.143.197',
],
'port' => '8106',
'user' => 'te',
'mirror' => '0',
],
];
public const ADMIN_SERV_ID = '8106';
private static $rsa = [
'dev' => 'd:\OSPanel\.ssh\te\id_rsa', // для консоли так /d/OSPanel/.ssh/te/id_rsa
'prod' => '/home/te/.ssh/id_rsa',
];
public static $error = null;
/**
* @return bool
*/
public static function hasError()
{
return boolval(static::$error);
}
/**
* Получаем туннель через ssh2
* @param int|string $servId указываем ID серверов по их порту
* @return null|resource
*/
public static function getConnection($servId)
{
$server = static::$serverList[$servId];
$host = App::isLocEnv() ? $server['host']['dev'] : $server['host']['prod'];
$rsa = App::isLocEnv() ? static::$rsa['dev'] : static::$rsa['prod'];
$connection = ssh2_connect($host, $server['port'], ['hostkey' => 'ssh-rsa']);
if (ssh2_auth_pubkey_file($connection, $server['user'], $rsa . '.pub', $rsa, 'secret')) {
return $connection;
}
static::$error = 'Public Key Authentication Failed';
return null;
}
/**
* Отправляем SSH команду на указанный сервер
* @param $servId
* @param $command
* @return bool|null|string
*/
public static function sendCommand($servId, $command)
{
$connection = static::getConnection($servId);
if ($connection) {
$command = is_array($command) ? implode('; ', $command) : $command;
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$res = fread($stream, 4096);
fclose($stream);
return $res;
}
return null;
}
/**
* Отправляем файлы по scp
* @param $servId
* @param $from
* @param $to
* @return bool
*/
public static function loadFile($servId, $from, $to)
{
// фикс для тестирования под WIN
$from = str_replace(['D:\\', 'C:\\', '\\'], ['/D/', '/C/', '/'], $from);
$to = str_replace(['D:\\', 'C:\\', '\\'], ['/D/', '/C/', '/'], $to);
$server = static::$serverList[$servId];
$host = App::isLocEnv() ? $server['host']['dev'] : $server['host']['prod'];
$rsa = App::isLocEnv() ? static::$rsa['dev'] : static::$rsa['prod'];
$command = "scp -r -P {$server['port']} -i {$rsa} {$from} {$server['user']}@{$host}:{$to}";
system($command);
return true;
}
/**
* Проверяем на ответ сервера
* @param $link
* @param bool $self
* @return array
*/
public static function getHttpCode($link, $self = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; CrawlBot/1.0.0)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 15);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$code = $info['http_code'];
$url = !empty($info['redirect_url']) ? $info['redirect_url'] : null;
if ($code == 301 or $code == 302) {
if (!$self) {
$http = self::getHttpCode($info['redirect_url'], true);
if ($http['code'] != 200) {
$code = $http['code'];
$url = $http['url'];
}
}
}
return [
'code' => $code,
'url' => $url,
];
}
public static function getFullPathByRelative(string $relativePath): string
{
return rtrim(ROOT_DIR, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR
. ltrim($relativePath, DIRECTORY_SEPARATOR);
}
}