src/WebBundle/Helper/ProxyHelper.php line 71

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Helper;
  3. /**
  4.  * class: ProxyHelper
  5.  * ssh -p 8105 te@136.243.143.197
  6.  * -----------------------------------------------------
  7.  * Created by MihailShirnin on 05.06.2017.
  8.  * @package WebBundle\Helper
  9.  */
  10. class ProxyHelper
  11. {
  12.     /**
  13.      * Список имен серверов, для удаленной чистки кеша
  14.      * @var array
  15.      */
  16.     public static array $mirrorList = ['site1''site3''site4'];
  17.     private static array $serverList = [
  18.         'img' => [
  19.             'name' => 'сервер картинок img.tile.expert',
  20.             'host' => [
  21.                 'dev' => '35.158.222.127',
  22.                 'prod' => '35.158.222.127',
  23.             ],
  24.             'port' => '22',
  25.             'user' => 'te',
  26.             'mirror' => null,
  27.         ],
  28.         '8106' => [
  29.             'name' => 'админка 8106',
  30.             'host' => [
  31.                 'dev' => '136.243.143.197',
  32.                 'prod' => '136.243.143.197',
  33.             ],
  34.             'port' => '8106',
  35.             'user' => 'te',
  36.             'mirror' => '0',
  37.         ],
  38.     ];
  39.     public const ADMIN_SERV_ID '8106';
  40.     private static $rsa = [
  41.         'dev' => 'd:\OSPanel\.ssh\te\id_rsa'// для консоли так /d/OSPanel/.ssh/te/id_rsa
  42.         'prod' => '/home/te/.ssh/id_rsa',
  43.     ];
  44.     public static $error null;
  45.     /**
  46.      * @return bool
  47.      */
  48.     public static function hasError()
  49.     {
  50.         return boolval(static::$error);
  51.     }
  52.     /**
  53.      * Получаем туннель через ssh2
  54.      * @param int|string $servId указываем ID серверов по их порту
  55.      * @return null|resource
  56.      */
  57.     public static function getConnection($servId)
  58.     {
  59.         $server = static::$serverList[$servId];
  60.         $host App::isLocEnv() ? $server['host']['dev'] : $server['host']['prod'];
  61.         $rsa App::isLocEnv() ? static::$rsa['dev'] : static::$rsa['prod'];
  62.         $connection ssh2_connect($host$server['port'], ['hostkey' => 'ssh-rsa']);
  63.         if (ssh2_auth_pubkey_file($connection$server['user'], $rsa '.pub'$rsa'secret')) {
  64.             return $connection;
  65.         }
  66.         static::$error 'Public Key Authentication Failed';
  67.         return null;
  68.     }
  69.     /**
  70.      * Отправляем SSH команду на указанный сервер
  71.      * @param $servId
  72.      * @param $command
  73.      * @return bool|null|string
  74.      */
  75.     public static function sendCommand($servId$command)
  76.     {
  77.         $connection = static::getConnection($servId);
  78.         if ($connection) {
  79.             $command is_array($command) ? implode('; '$command) : $command;
  80.             $stream ssh2_exec($connection$command);
  81.             stream_set_blocking($streamtrue);
  82.             $res fread($stream4096);
  83.             fclose($stream);
  84.             return $res;
  85.         }
  86.         return null;
  87.     }
  88.     /**
  89.      * Отправляем файлы по scp
  90.      * @param $servId
  91.      * @param $from
  92.      * @param $to
  93.      * @return bool
  94.      */
  95.     public static function loadFile($servId$from$to)
  96.     {
  97.         // фикс для тестирования под WIN
  98.         $from str_replace(['D:\\''C:\\''\\'], ['/D/''/C/''/'], $from);
  99.         $to str_replace(['D:\\''C:\\''\\'], ['/D/''/C/''/'], $to);
  100.         $server = static::$serverList[$servId];
  101.         $host App::isLocEnv() ? $server['host']['dev'] : $server['host']['prod'];
  102.         $rsa App::isLocEnv() ? static::$rsa['dev'] : static::$rsa['prod'];
  103.         $command "scp -r -P {$server['port']} -i {$rsa} {$from} {$server['user']}@{$host}:{$to}";
  104.         system($command);
  105.         return true;
  106.     }
  107.     /**
  108.      * Проверяем на ответ сервера
  109.      * @param      $link
  110.      * @param bool $self
  111.      * @return array
  112.      */
  113.     public static function getHttpCode($link$self false)
  114.     {
  115.         $ch curl_init();
  116.         curl_setopt($chCURLOPT_URL$link);
  117.         curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (compatible; CrawlBot/1.0.0)');
  118.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  119.         curl_setopt($chCURLOPT_HEADERtrue);
  120.         curl_setopt($chCURLOPT_CONNECTTIMEOUT15);
  121.         curl_setopt($chCURLOPT_TIMEOUT15);
  122.         curl_setopt($chCURLOPT_ENCODING"");
  123.         curl_setopt($chCURLOPT_AUTOREFERERtrue);
  124.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  125.         curl_setopt($chCURLOPT_MAXREDIRS15);
  126.         curl_exec($ch);
  127.         $info curl_getinfo($ch);
  128.         curl_close($ch);
  129.         $code $info['http_code'];
  130.         $url = !empty($info['redirect_url']) ? $info['redirect_url'] : null;
  131.         if ($code == 301 or $code == 302) {
  132.             if (!$self) {
  133.                 $http self::getHttpCode($info['redirect_url'], true);
  134.                 if ($http['code'] != 200) {
  135.                     $code $http['code'];
  136.                     $url $http['url'];
  137.                 }
  138.             }
  139.         }
  140.         return [
  141.             'code' => $code,
  142.             'url' => $url,
  143.         ];
  144.     }
  145.     public static function getFullPathByRelative(string $relativePath): string
  146.     {
  147.         return rtrim(ROOT_DIRDIRECTORY_SEPARATOR)
  148.             . DIRECTORY_SEPARATOR
  149.             ltrim($relativePathDIRECTORY_SEPARATOR);
  150.     }
  151. }