rovazh / phpsocks
SOCKS5 proxy client, written in pure PHP with zero dependencies.
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- mockery/mockery: 1.7.x-dev
- phpunit/phpunit: ^9
- vimeo/psalm: ^5
This package is not auto-updated.
Last update: 2024-05-12 10:41:23 UTC
README
SOCKS5 proxy client, written in pure PHP with zero dependencies.
Features
- Supports SOCKS v5
- Supports CONNECT command
- Supports username/password authentication
Requirements
PHP version 7.4 or higher
Installation
composer require rovazh/phpsocks
Usage
Plain TCP connections
Connecting to example.net
on port 80 through a SOCKS5 proxy server.
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', // SOCKS5 server ipv4 or ipv6 or hostname 'port' => 1080, // SOCKS5 server port ]); try { $stream = $client->connect('tcp://example.com:80'); $stream->write("GET / HTTP/1.0\r\n\r\n"); echo $stream->readAll(); $stream->close(); } catch (\PhpSocks\Exception\PhpSocksException $e) { // Handle exception }
Secure TLS connections
The following example demonstrates establishing a secure TLS connection
to example.net
on port 443 through a SOCKS5 proxy server.
$client = new \PhpSocks\Client(['host' => '127.0.0.1', 'port' => 1080]); try { $stream = $client->connect('tls://example.net:443', [ 'tls' => [ 'peer_name' => 'example.net', ] ]); $stream->write("GET / HTTP/1.0\r\n\r\n"); echo $stream->readAll(); $stream->close(); } catch (\PhpSocks\Exception\PhpSocksException $e) { // Handle exception }
The connect
method accepts an associative array of
SSL context options
that can be used to configure TLS when connecting to a destination host.
Please note that SSL context options do not take any effect when using plain TCP connection tcp://
.
Authentication
The library supports username/password authentication for SOCKS5 servers as defined in RFC 1929.
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, 'auth' => [ 'username' => 'proxy_user', 'password' => 'proxy_pass', ] ]);
Timeout
By default, the library relies on
default_socket_timeout
when connecting to a SOCKS5 server.
To set it at runtime, you can use the connect_timeout
option when creating an instance of the Client
.
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, 'connect_timeout' => 5.0, // 5 seconds ]);
There is also a timeout for sending/receiving data to/from a SOCKS5 server and a destination host.
By default, this timeout is set by the underlying operating system.
To explicitly set it when creating an instance of the Client
, use the timeout
option.
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, 'connect_timeout' => 5.0, // 5 seconds 'timeout' => 3, // 3 seconds ]);
License
The code for PhpSocks is distributed under the terms of the MIT license (see LICENSE).