rovazh/phpsocks
Composer 安装命令:
composer require rovazh/phpsocks
包简介
SOCKS5 proxy client, written in pure PHP with zero dependencies.
README 文档
README
PhpSocks
SOCKS5 proxy client, written in pure PHP with zero dependencies.
Features
- Supports SOCKS5 protocol
- Implements the CONNECT command
- Implements UDP ASSOCIATE command
- Supports username/password authentication (RFC 1929)
Requirements
- PHP 7.4 or higher
- Sockets extension enabled
Installation
Install via Composer:
composer require rovazh/phpsocks
Usage
Tunneling TCP connections through a SOCKS5 server (CONNECT)
Plain TCP connections
The following example demonstrates connecting to example.net on port 80 via a SOCKS5 proxy server:
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', // SOCKS5 server (IPv4, IPv6, or hostname) 'port' => 1080, // SOCKS5 server port ]); try { $stream = $client->connect('tcp://example.net:80'); $stream->write("GET / HTTP/1.0\r\n\r\n"); echo $stream->read(1024); $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 via a SOCKS5 proxy server:
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, ]); try { $stream = $client->connect('tls://example.net:443'); $stream->write("GET / HTTP/1.0\r\n\r\n"); echo $stream->read(1024); $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 settings when connecting to a destination host.
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, ]); try { $stream = $client->connect('tls://example.net:443', [ 'tls' => [ 'verify_peer' => false, ] ]); $stream->write("GET / HTTP/1.0\r\n\r\n"); echo $stream->read(1024); $stream->close(); } catch (\PhpSocks\Exception\PhpSocksException $e) { // Handle exception }
Note: SSL context options have no effect when using a plain TCP connection (tcp://).
Authentication
PhpSocks 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 Settings
By default, the library relies on the system's
default_socket_timeout
when connecting to a SOCKS5 server.
To set a custom timeout at runtime, use the connect_timeout option:
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', 'port' => 1080, 'connect_timeout' => 5.0, // 5 seconds ]);
Additionally, you can set a timeout for sending and receiving data. By default,
this is determined by the operating system. To explicitly define it, 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 ]);
Relaying UDP Datagrams through a SOCKS5 Server (UDP ASSOCIATE)
The following example establishes a SOCKS5 UDP association to enable relaying UDP
datagrams to example.net on port 5023 via a SOCKS5 proxy server:
$client = new \PhpSocks\Client([ 'host' => '127.0.0.1', // SOCKS5 server (IPv4, IPv6, or hostname) 'port' => 1080, // SOCKS5 server port ]); try { $stream = $client->associate('udp://example.net:5023'); $stream->write("Hello"); echo $stream->read(1024); $stream->close(); } catch (\PhpSocks\Exception\PhpSocksException $e) { // Handle exception }
License
PhpSocks is distributed under the terms of the MIT License. See LICENSE file for details.
rovazh/phpsocks 适用场景与选型建议
rovazh/phpsocks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.38k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「socks client」 「php proxy」 「socks」 「socks5」 「php socks」 「php socks5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rovazh/phpsocks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rovazh/phpsocks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rovazh/phpsocks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Async SOCKS proxy connector client and server implementation, tunnel any TCP/IP-based protocol through a SOCKS5 or SOCKS4(a) proxy server, built on top of ReactPHP.
Async SOCKS proxy connector client and server implementation, tunnel any TCP/IP-based protocol through a SOCKS5 or SOCKS4(a) proxy server, built on top of ReactPHP.
LeProxy is the HTTP/SOCKS proxy server for everybody!
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Async SOCKS proxy client and server (SOCKS4, SOCKS4a and SOCKS5)
统计信息
- 总下载量: 11.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-02
