用PHP检查SOCKS4或5是否存在?

时间:2011-12-12 07:21:20

标签: php socks proxies

基本上,我有一个代理列表。我想把它们分成SOCKS4和SOCKS5。我想编写一个小的PHP脚本来为我做这个。我将如何检测它在PHP中的类型?

3 个答案:

答案 0 :(得分:1)

您需要自己编写一些尝试与任何代理连接并检查socks版本的小代码。 wikipedia page about SOCKS上记录了不同版本和错误代码的连接协议。

考虑到这一点,其余部分或多或少是与PHP的标准套接字连接。

示例:

$proxies = array( '66.135.131.74:1681', '172.52.61.244:48943',
                  '75.101.237.217:1080', '76.68.128.165:39879',);

foreach ($proxies as $index => $proxy)
{
    $type = SOCKSVersion::getType($proxy);
    $typeName = SOCKSVersion::getTypeName($type);
    printf("Proxy #%d: %s\n", $index, $typeName);
}

输出:

Proxy #0: SOCKS4
Proxy #1: SOCKS4
Proxy #2: Unknown
Proxy #3: SOCKS4

此示例性实现仅检查SOCKS4,但可以通过添加类似于isSocks4()的方法轻松扩展以测试SOCK4a和SOCKS5:

/**
 * SOCKS server identifiation class.
 */
class SOCKSVersion
{
    const TYPE_UNKNOWN = 0;
    const TYPE_SOCKS4 = 1;
    const TYPE_SOCKS4a = 2;
    const TYPE_SOCKS5 = 3;

    /**
     * @var string[]
     */
    private static $typeNames = array(
        self::TYPE_UNKNOWN => 'Unknown',
        self::TYPE_SOCKS4 => 'SOCKS4',
        self::TYPE_SOCKS4a => 'SOCKS4a',
        self::TYPE_SOCKS5 => 'SOCKS5',
    );

    /**
     * @var int
     */
    private $timeout = 30;

    /**
     * @var int
     */
    private $host, $port;

    /**
     * @var string[]
     */
    private $errors;

    /**
     * @var string[]
     */
    private $socks4Errors = array(
        91 => "Request rejected or failed",
        92 => "Request failed because client is not running identd (or not reachable from the server)",
        93 => "Request failed because client's identd could not confirm the user ID string in the request",
    );

    public function __construct($endpoint)
    {
        $this->setEndpoint($endpoint);
    }

    /**
     * @static
     * @param string $proxy
     * @return int any of the TYPE_* constants
     */
    public static function getType($proxy)
    {
        $socks = new self($proxy);
        return $socks->getSocksVersion();
    }

    /**
     * @static
     * @param int $type
     * @return string
     */
    public static function getTypeName($type)
    {
        $typeNames = self::$typeNames;
        if (isset($typeNames[$type])) {
            return $typeNames[$type];
        }
        return $typeNames[self::TYPE_UNKNOWN];
    }

    public function setEndpoint($endpoint)
    {
        if (!$parts = parse_url('http://' . $endpoint)) {
            throw new InvalidArgumentException(sprintf('Unable to parse endpoint "%s".', $endpoint));
        }
        if (empty($parts['host'])) {
            throw new InvalidArgumentException('No host given.');
        }
        if (empty($parts['port'])) {
            throw new InvalidArgumentException('No port given.');
        }
        $this->host = $parts['host'];
        $this->port = $parts['port'];
    }

    /**
     * @return int any of the TYPE_* constants
     */
    public function getSocksVersion()
    {
        try {
            if ($this->isSocks4()) {
                return self::TYPE_SOCKS4;
            }
        } catch (BadFunctionCallException $e) {
            $this->errors[] = sprintf("SOCKS4 Test: ", $this->host, $e->getMessage());
        }
        return self::TYPE_UNKNOWN;
    }

    public function isSocks4()
    {
        $socket = stream_socket_client("tcp://" . $this->host . ":" . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT);
        if (!$socket) {
            throw new BadFunctionCallException(sprintf('Socket-Error #%d: %s', $errno, $errstr));
        }

        // SOCKS4; @link <http://en.wikipedia.org/wiki/SOCKS#Protocol>
        $userId = "";
        $packet = "\x04\x01" . pack("n", $this->port) . pack("H*", dechex(ip2long($this->host))) . $userId . "\0";
        fwrite($socket, $packet, strlen($packet));
        $response = fread($socket, 9);
        if (strlen($response) == 8 && (ord($response[0]) == 0 || ord($response[0]) == 4)) {
            $status = ord($response[1]);
            if ($status != 90) {
                throw new BadFunctionCallException(sprintf("Error from SOCKS4 server: %s.", $this->socks4Errors[$status]));
            }
        } else {
            throw new BadFunctionCallException("The SOCKS server returned an invalid response");
        }
        fclose($socket);

        return TRUE;
    }
}

希望这有帮助。如果您引入了多个版本,则应该改进错误处理,如果在先前的测试中连接失败,则不要多次连接到同一主机。

答案 1 :(得分:0)

我认为您可以做的最好的事情是首先尝试通过尝试最高版本来建立CURL连接 - 5.

  curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

这将为您提供答案。执行后检查curl_error。如果没有错误,则使用SOCKS5,否则使用SOCKS4。

答案 2 :(得分:-1)

根据RFC1928,要建立SOCKS连接,首先要将这些字节发送到服务器:

1 byte      SOCKS version
1 byte      Number of authentication methods (n)
n bytes     List of method identifiers

服务器以

响应
1 byte      SOCKS version
1 byte      Accepted method

这在SOCKS的第4版和第5版之间很常见。因此,如果服务器没有相应的响应,您可以从一个版本(例如5)开始并回退到另一个版本。