clue/socks-react
Async SOCKS5/SOCKS4(a) proxy client and server for ReactPHP. Tunnel any TCP/IP protocol (HTTP, SMTP, IMAP, etc.) through a SOCKS proxy using the standard ConnectorInterface, enabling easy drop-in proxy support and parallel connections.
Installation:
composer require clue/socks-react
Add to composer.json if using Laravel:
"require": {
"clue/socks-react": "^1.0"
}
Basic Usage:
use Clue\React\Socks\Client;
use React\Socket\Connector;
// Initialize SOCKS client
$proxy = new Client('socks://your-proxy-host:port');
// Wrap in ReactPHP Connector
$connector = new Connector(['tcp' => $proxy, 'dns' => false]);
// Use with HTTP client (e.g., Guzzle or ReactPHP HTTP)
$browser = new React\Http\Browser($connector);
$browser->get('https://example.com')->then(...);
First Use Case: Route Laravel HTTP client requests through a SOCKS proxy:
$proxy = new Client('socks://proxy.example.com:1080');
$connector = new Connector(['tcp' => $proxy, 'dns' => false]);
$client = new React\Http\Browser($connector);
$response = $client->get('https://api.example.com/data');
examples/ for practical implementations.ConnectorInterface for integration patterns.Replace Laravel’s default HTTP client (Guzzle) with a SOCKS-aware ReactPHP HTTP client:
// In a Laravel service provider
public function register()
{
$proxy = new Client('socks://proxy.example.com:1080');
$connector = new Connector(['tcp' => $proxy, 'dns' => false]);
$this->app->singleton('react.http.client', function () use ($connector) {
return new React\Http\Browser($connector);
});
}
Use Case: Scrape websites or access geo-restricted APIs anonymously.
Proxy raw TCP connections (e.g., for custom protocols like SMTP/IMAP):
$proxy = new Client('socks://proxy.example.com:1080');
$proxy->connect('tcp://mail.example.com:25')->then(function ($connection) {
$connection->write("EHLO example.com\r\n");
// Handle response...
});
Use Case: Route Laravel queue workers (e.g., tcp://) through a proxy.
Chain multiple proxies (e.g., SOCKS → Tor → Destination):
$innerProxy = new Client('socks://tor-exit-node:9050');
$outerProxy = new Client('socks://your-proxy:1080', new Connector(['tcp' => $innerProxy]));
Use Case: Multi-layer anonymization for sensitive operations.
Implement a custom SOCKS server in Laravel (e.g., for internal routing):
$socks = new \Clue\React\Socks\Server();
$socket = new \React\Socket\SocketServer('0.0.0.0:1080');
$socks->listen($socket);
Use Case: Route internal traffic through a local proxy for logging/monitoring.
Service Container Binding: Bind the SOCKS client to Laravel’s container for dependency injection:
$this->app->bind(Client::class, function () {
return new Client(config('proxy.socks_uri'));
});
Configurable Proxy:
Store proxy settings in config/proxy.php:
return [
'socks_uri' => env('SOCKS_PROXY', 'socks://proxy.example.com:1080'),
'timeout' => 10.0,
];
Middleware for Proxy Routing: Create middleware to route specific requests through the proxy:
public function handle($request, Closure $next)
{
if ($request->isProxyRoute()) {
$connector = new Connector(['tcp' => app(Client::class)]);
$client = new React\Http\Browser($connector);
return $client->get($request->url());
}
return $next($request);
}
Promise Chaining: Chain promises for sequential proxy operations:
$proxy->connect('tcp://target:port')
->then(function ($conn) {
return $conn->write('DATA');
})
->then(function () use ($conn) {
return $conn->end();
});
Event-Driven Workflows: Use ReactPHP’s event loop for async proxy operations:
$loop = React\EventLoop\Factory::create();
$proxy = new Client('socks://proxy:1080');
$loop->run();
Custom DNS Resolution: Override DNS resolution for proxy-specific domains:
$connector = new Connector([
'tcp' => $proxy,
'dns' => function ($host) {
return $host === 'proxy.example.com' ? '192.168.1.100' : null;
}
]);
DNS Leaks:
socks5://) or configure the proxy to handle DNS.EHOSTUNREACH errors.Timeout Handling:
Connector:
$connector = new Connector([
'tcp' => $proxy,
'timeout' => 30.0, // seconds
]);
TLS Handshake Failures:
$connector = new Connector([
'tls' => ['verify_peer' => false],
'tcp' => $proxy,
]);
Proxy Authentication:
$proxy = new Client('socks://user:pass@proxy.example.com:1080');
IPv6 Limitations:
socks5://) for IPv6 targets.Enable Verbose Logging:
$proxy = new Client('socks://proxy:1080', null, [
'debug' => true,
]);
Packet Inspection:
Use Wireshark or tcpdump to inspect SOCKS traffic:
tcpdump -i any -w socks_traffic.pcap port 1080
Promise Errors: Always handle promise rejections:
$proxy->connect('tcp://target')
->then(...)
->otherwise(function (Exception $e) {
\Log::error('Proxy failed: ' . $e->getMessage());
});
Custom Proxy URI Parsing:
Extend the Client to support custom URI schemes:
$proxy = new Client('custom://user:pass@proxy:1080');
Proxy Rotation: Implement a rotating proxy pool:
$proxies = ['socks://p1:1080', 'socks://p2:1080'];
$currentProxy = new Client($proxies[array_rand($proxies)]);
Server-Side Extensions:
Extend the Server class to add custom auth logic:
$socks = new class extends \Clue\React\Socks\Server {
protected function onAuthenticate($username, $password) {
return $username === 'admin' && $password === 'secret';
}
};
Protocol Extensions: Add support for UDP (not natively supported):
// Requires custom implementation (
How can I help you explore Laravel packages today?