指定されたドメインのSSL証明書パラメータを取得し、返します。

@param	string		$domain_name	SSL証明書のパラメータを取得するドメイン
@param	int			$port			SSLで接続するポート デフォルトは443
@param	int			$timeout		コネクションタイムアウトまでの秒数
@return	array|bool	SSL証明書のパラメータを取得出来た場合はSSL証明書のパラメータの配列、そうでない場合はfalse
185705
public static function GetSSLCertificateParameters ($domain_name, $port = 443, $timeout = 30) {
		$sp = stream_socket_client(
			sprintf('ssl://%s:%d', $domain_name, $port),
			$errno,
			$errstr,
			$timeout,
			\STREAM_CLIENT_CONNECT,
			stream_context_create(['ssl' => [
				'verify_peer'				=> false,
				'verify_peer_name'			=> false,
				'allow_self_signed'			=> true,
				'capture_peer_cert'			=> true,
				'capture_peer_cert_chain'	=> true,
				'SNI_enabled'				=> true
			]])
		);

		if (!is_resource($sp)) {
			return false;
		}

		$parameters = stream_context_get_params($sp);
		fclose($sp);

		if (!isset($parameters['options']['ssl']['peer_certificate'])) {
			return false;
		}
		$peer_certificate = openssl_x509_parse($parameters['options']['ssl']['peer_certificate']);

		$peer_certificate_chain_list = [];
		foreach ($parameters['options']['ssl']['peer_certificate_chain'] as $peer_certificate_chain) {
			$peer_certificate_chain_list[] = openssl_x509_parse($peer_certificate_chain);
		}

		return [
			'peer_certificate'				=> $peer_certificate,
			'peer_certificate_chain_list'	=> $peer_certificate_chain_list,
			'self_signed_certificate'		=> $peer_certificate['issuer']['CN'] === $domain_name,
		];
	}
831185
protected function getSocket($host, $port, $timeout)
    {
        return stream_socket_client("tcp://{$host}:{$port}", $error, $message, $timeout, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
    }
1109723
public function checkAtOnce(string $addr, int $port = 0, int $timeout = 3): Result {
        try {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if (!$socket) {
                $errno = socket_last_error();
                throw new \Exception("socket create error:" . socket_strerror($errno), $errno);
            }
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
            socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $timeout, 'usec' => 0));
            $ok = @socket_connect($socket, $addr, $port);
            $err = "";
            if (!$ok) {
                $err = socket_strerror(socket_last_error());
            }
            socket_close($socket);
        } catch (\Exception $e) {
            $ok = false;
            $err = $e->getMessage();
        }
        return new Result(static::class, $addr, $port, $ok, $err);
    }
197277
public function open($host, $port, $timeout)
    {
        if (($this->socket = @fsockopen($host, $port, $errno, $errstr, ($timeout / 1000))) === false) {
            return false;
        }

        return true;
    }
481653
public function connect($host, $port, $timeout = 1)
    {
        $this->socket = @stream_socket_client(
            "tcp://$host:$port",
            $errorNumber,
            $errorMessage,
            $timeout,
            STREAM_CLIENT_CONNECT,
            stream_context_create($this->options)
        );

        return (bool) $this->socket;
    }
141068
public function queryServer($server, $domain)
    {
        $port = 43;
        $timeout = 10;
        $fp = @fsockopen($server, $port, $errno, $errstr, $timeout);
        if ( !$fp ) {
            throw new Exception("Socket Error " . $errno . " - " . $errstr);
        }
        // if($server == "whois.verisign-grs.com") $domain = "=".$domain; // whois.verisign-grs.com requires the equals sign ("=") or it returns any result containing the searched string.
        fputs($fp, $domain . "\r\n");
        $out = "";
        while (!feof($fp)) {
            $out .= fgets($fp);
        }
        fclose($fp);

        $res = "";
        if ((strpos(strtolower($out), "error") === false) && (strpos(strtolower($out), "not allocated") === false)) {
            $rows = explode("\n", $out);
            foreach ($rows as $row) {
                $row = trim($row);
                if (($row != '') && ($row{0} != '#') && ($row{0} != '%')) {
                    $res .= $row."\n";
                }
            }
        }
        return $res;
    }
1097527
public static function HeadRequest ($url, $timeout = 30, $protocol_version = self::VERSION_1_1) {
		$parsed_url = parse_url($url);

		$host = $parsed_url['host'];
		if (!isset($parsed_url['port'])) {
			$port = $parsed_url['scheme'] === 'https' ? 443 : 80;
		}
		$path = $parsed_url['path'];

		$request_headers = [
			sprintf('HEAD %s HTTP/%s', $path, $protocol_version),
			'User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)',
			sprintf('Host: %s', $host),
			'Connection: Close',
		];
		$request_header = implode("\r\n", $request_headers) . "\r\n\r\n";

		$schema = $parsed_url['scheme'] === 'https' ? 'ssl://' : '';

		$fp = fsockopen($schema . $host, $port, $errno, $errstr, $timeout);
		if (!$fp) {
			CoreException::ScrubbedThrow(Status::SystemError(sprintf('%s (%s)', $errstr, $errno)));
		}

		fwrite($fp, $request_header);

		$return_data = [];
		while (!feof($fp)) {
			$return_data[] = trim(fgets($fp, 4000));
		}
		fclose($fp);

		$result = [];
		foreach ($return_data as $row) {
			if (preg_match("/^HTTP\/(\d+.\d+) (\d+) (.+)$/", $row, $matches) === 1) {
				$result['http'] = [
					'version'	=> $matches[1],
					'status'	=> [
						'code'		=> $matches[2],
						'message'	=> $matches[3],
					],
				];
				continue;
			}
			if (preg_match("/^([^:]+): (.+)/", $row, $matches) === 1) {
				$result[$matches[1]] = $matches[2];
				continue;
			}
		}
		return $result;
	}
345635
public function setProxy(
        $host,
        $port,
        $ssl = false,
        $username = null,
        $password = null
    ) {
        $scheme = ($ssl ? 'https://' : 'http://');

        if ( ! is_null($username)) {
            return $this->options['proxy'] = sprintf('%s%s:%s@%s:%s', $scheme, $username, $password, $host, $port);
        }

        return $this->options['proxy'] = sprintf('%s%s:%s', $scheme, $host, $port);
    }
1265642
public function checkAtOnce(string $addr, int $port = 0, int $timeout = 3): Result {
        try {
            $res = $this->http()->get((0 === strpos($addr, 'http') ? $addr : 'http://' . $addr) . ($port ? ':' . $port : ''), [
                'timeout' => $timeout,
                'connect_timeout' => $timeout,
            ])->getBody()->getContents();
            $ok = true;
            $err = '';
            if (false === strpos($res, 'Jetbrains License Server')) {
                $ok = false;
                $err = 'response body not contain substr, body: ' . $res;
            }
        } catch (\Exception $e) {
            $ok = false;
            $err = $e->getMessage();
        }
        return new Result(static::class, $addr, $port, $ok, $err);
    }
454738
function Net_SFTP($host, $port = 22, $timeout = 10)
    {
        parent::Net_SSH2($host, $port, $timeout);
        $this->packet_types = array(
            1  => 'NET_SFTP_INIT',
            2  => 'NET_SFTP_VERSION',
            /* the format of SSH_FXP_OPEN changed between SFTPv4 and SFTPv5+:
                   SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.1
               pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 */
            3  => 'NET_SFTP_OPEN',
            4  => 'NET_SFTP_CLOSE',
            5  => 'NET_SFTP_READ',
            6  => 'NET_SFTP_WRITE',
            7  => 'NET_SFTP_LSTAT',
            9  => 'NET_SFTP_SETSTAT',
            11 => 'NET_SFTP_OPENDIR',
            12 => 'NET_SFTP_READDIR',
            13 => 'NET_SFTP_REMOVE',
            14 => 'NET_SFTP_MKDIR',
            15 => 'NET_SFTP_RMDIR',
            16 => 'NET_SFTP_REALPATH',
            17 => 'NET_SFTP_STAT',
            /* the format of SSH_FXP_RENAME changed between SFTPv4 and SFTPv5+:
                   SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
               pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.5 */
            18 => 'NET_SFTP_RENAME',

            101=> 'NET_SFTP_STATUS',
            102=> 'NET_SFTP_HANDLE',
            /* the format of SSH_FXP_NAME changed between SFTPv3 and SFTPv4+:
                   SFTPv4+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.4
               pre-SFTPv4 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-7 */
            103=> 'NET_SFTP_DATA',
            104=> 'NET_SFTP_NAME',
            105=> 'NET_SFTP_ATTRS',

            200=> 'NET_SFTP_EXTENDED'
        );
        $this->status_codes = array(
            0 => 'NET_SFTP_STATUS_OK',
            1 => 'NET_SFTP_STATUS_EOF',
            2 => 'NET_SFTP_STATUS_NO_SUCH_FILE',
            3 => 'NET_SFTP_STATUS_PERMISSION_DENIED',
            4 => 'NET_SFTP_STATUS_FAILURE',
            5 => 'NET_SFTP_STATUS_BAD_MESSAGE',
            6 => 'NET_SFTP_STATUS_NO_CONNECTION',
            7 => 'NET_SFTP_STATUS_CONNECTION_LOST',
            8 => 'NET_SFTP_STATUS_OP_UNSUPPORTED'
        );
        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1
        // the order, in this case, matters quite a lot - see Net_SFTP::_parseAttributes() to understand why
        $this->attributes = array(
            0x00000001 => 'NET_SFTP_ATTR_SIZE',
            0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+
            0x00000004 => 'NET_SFTP_ATTR_PERMISSIONS',
            0x00000008 => 'NET_SFTP_ATTR_ACCESSTIME',
            // 0x80000000 will yield a floating point on 32-bit systems and converting floating points to integers
            // yields inconsistent behavior depending on how php is compiled.  so we left shift -1 (which, in 
            // two's compliment, consists of all 1 bits) by 31.  on 64-bit systems this'll yield 0xFFFFFFFF80000000.
            // that's not a problem, however, and 'anded' and a 32-bit number, as all the leading 1 bits are ignored.
              -1 << 31 => 'NET_SFTP_ATTR_EXTENDED'
        );
        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3
        // the flag definitions change somewhat in SFTPv5+.  if SFTPv5+ support is added to this library, maybe name
        // the array for that $this->open5_flags and similarily alter the constant names.
        $this->open_flags = array(
            0x00000001 => 'NET_SFTP_OPEN_READ',
            0x00000002 => 'NET_SFTP_OPEN_WRITE',
            0x00000004 => 'NET_SFTP_OPEN_APPEND',
            0x00000008 => 'NET_SFTP_OPEN_CREATE',
            0x00000010 => 'NET_SFTP_OPEN_TRUNCATE'
        );
        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2
        // see Net_SFTP::_parseLongname() for an explanation
        $this->file_types = array(
            1 => 'NET_SFTP_TYPE_REGULAR',
            2 => 'NET_SFTP_TYPE_DIRECTORY',
            3 => 'NET_SFTP_TYPE_SYMLINK',
            4 => 'NET_SFTP_TYPE_SPECIAL'
        );
        $this->_define_array(
            $this->packet_types,
            $this->status_codes,
            $this->attributes,
            $this->open_flags,
            $this->file_types
        );
    }
889565
function (host, port, cb) {
    var err,
        data = {};

    var openssl = spawn('openssl', ['s_client', '-connect', host + ':' + port, '-servername', host]);

    // Clear timeout when execution was successful
    openssl.on('exit', function(){
      clearTimeout(timeoutTimer);
    });

    // Catch stderr and search for possible errors
    openssl.stderr.on('data', function (out) {
      // Search for possible errors in stderr
      var errorRegexp = /(Connection refused)|(Can't assign requested address)|(gethostbyname failure)|(getaddrinfo: nodename)/;
      var regexTester = errorRegexp.test(out);

      // If match, raise error
      if (regexTester) {
        err = new Error(out.toString().replace(/^\s+|\s+$/g, ''));

        // Callback and return array
        return cb(err, data);
      }
    });

    openssl.stdout.on('data', function (out) {
      var data = out.toString();

      // Search for certificate in stdout
      var matches = data.match(/-----BEGIN CERTIFICATE-----([\s\S.]*)-----END CERTIFICATE-----/);

      try {
        data = matches[0];
      } catch (e) {
        // ... otherwise raise error
        err = new Error('Couldn\'t extract certificate for ' + host + ':' + port);
      }

      // ... callback and return certificate
      return cb(err, data);
    });

    // End stdin (otherwise it'll run indefinitely)
  	openssl.stdin.end();

    // Timeout function to kill in case of errors
    var timeoutTimer = setTimeout(function(){
      openssl.kill();
      // ... otherwise throw error
      err = new Error('Time out while trying to extract certificate for ' + host + ':' + port);

      // and return
      return cb(err, data);
    }, 5000);
  }
820434
public static function phpGet($url, $timeout = 5, $header = "")
    {
        $header = empty($header) ? self::defaultHeader() : $header;
        $opts = [
            'http' => [
                'protocol_version' => '1.0', //http协议版本(若不指定php5.2系默认为http1.0)
                'method' => "GET", //获取方式
                'timeout' => $timeout, //超时时间
                'header' => $header
            ]
        ];
        $context = stream_context_create($opts);
        return @file_get_contents($url, false, $context);
    }
1145880
public static function UrlExsist ($url, $timeout = 30, $protocol_version = self::VERSION_1_1) {
		$result = static::HeadRequest($url, $timeout, $protocol_version);
		return isset($result['http']['status']['code']) && $result['http']['status']['code'] !== '404';
	}
457324
public function connect($timeout = 0.5)
    {
        if (extension_loaded('swoole'))
        {
            $type = preg_match('/^[0-9\.]+$/', $this->host) ? SWOOLE_SOCK_TCP : SWOOLE_SOCK_UNIX_STREAM;
            if ($this->ssl)
            {
                $type |= SWOOLE_SSL;
            }
            $this->socket = new \swoole_client($type);
            if ($this->ssl_key_file)
            {
                $this->socket->set(array(
                    'ssl_key_file' => $this->ssl_key_file,
                    'ssl_cert_file' => $this->ssl_cert_file
                ));
            }
        }
        else
        {
            $this->socket = new TCP;
        }
        //建立连接
        if (!$this->socket->connect($this->host, $this->port, $timeout))
        {
            return false;
        }
        $this->connected = true;
        //WebSocket握手
        if ($this->socket->send($this->createHeader()) === false)
        {
            return false;
        }
        $headerBuffer = '';
        while(true)
        {
            $_tmp = $this->socket->recv();
            if ($_tmp)
            {
                $headerBuffer .= $_tmp;
                $offset = stripos($headerBuffer,"\r\n\r\n");
                if ($offset === false) {
                    continue;
                }
                $header = substr($headerBuffer, 0, $offset+4);
                $this->buffer = substr($headerBuffer, $offset+4);
            }
            else
            {
                return false;
            }
            return $this->doHandShake($header);
        }
        return false;
    }
1330114
function connect($host, $port, $timeout = 0.1, $nonblock = false)
    {
        //判断超时为0或负数
        if (empty($host) or empty($port) or $timeout <= 0)
        {
            $this->errCode = -10001;
            $this->errMsg = "param error";
            return false;
        }
        $this->host = $host;
        $this->port = $port;
        //创建socket
        $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($this->sock === false)
        {
            $this->set_error();
            return false;
        }
        //设置connect超时
        $this->set_timeout($timeout, $timeout);
        $this->setopt(SO_REUSEADDR, 1);
        //非阻塞模式下connect将立即返回
        if ($nonblock)
        {
            socket_set_nonblock($this->sock);
            @socket_connect($this->sock, $this->host, $this->port);
            return true;
        }
        else
        {
            //这里的错误信息没有任何意义，所以屏蔽掉
            if (@socket_connect($this->sock, $this->host, $this->port))
            {
                $this->connected = true;
                return true;
            }
            elseif ($this->try_reconnect)
            {
                if (@socket_connect($this->sock, $this->host, $this->port))
                {
                    $this->connected = true;
                    return true;
                }
            }
        }
        $this->set_error();
        trigger_error("connect server[{$this->host}:{$this->port}] fail. Error: {$this->errMsg}[{$this->errCode}].");
        return false;
    }
4079
public function setServerParams($host, $port = 11211, $timeout = NULL, $retry_interval = false, $status = NULL, $failure_callback = NULL)
    {
    }
289234
protected static function getHostAndPortFromHeader(&$host, &$port, $headerHost)
    {
        if (is_array($headerHost)) {
            $headerHost = implode(', ', $headerHost);
        }

        $host = $headerHost;

        if (preg_match('|\:(\d+)$|', $host, $matches)) {
            $host = substr($host, 0, -1 * (strlen($matches[1]) + 1));
            $port = (int) $matches[1];
        }
    }