Download the PHP package clue/socks-react without Composer

On this page you can find all versions of the php package clue/socks-react. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

All versions of socks-react with dependencies

Informations about the package socks-react

clue/reactphp-socks

CI status installs on Packagist

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.

The SOCKS proxy protocol family (SOCKS5, SOCKS4 and SOCKS4a) is commonly used to tunnel HTTP(S) traffic through an intermediary ("proxy"), to conceal the origin address (anonymity) or to circumvent address blocking (geoblocking). While many (public) SOCKS proxy servers often limit this to HTTP(S) port 80 and 443 only, this can technically be used to tunnel any TCP/IP-based protocol (HTTP, SMTP, IMAP etc.). This library provides a simple API to create these tunneled connections for you. Because it implements ReactPHP's standard ConnectorInterface, it can simply be used in place of a normal connector. This makes it fairly simple to add SOCKS proxy support to pretty much any existing higher-level protocol implementation. Besides the client side, it also provides a simple SOCKS server implementation which allows you to build your own SOCKS proxy servers with custom business logic.

Table of contents

Support us

We invest a lot of time developing, maintaining and updating our awesome open-source projects. You can help us sustain this high-quality of our work by becoming a sponsor on GitHub. Sponsors get numerous benefits in return, see our sponsoring page for details.

Let's take these projects to the next level together! 🚀

Quickstart example

Once installed, you can use the following code to send a secure HTTPS request to google.com through a local SOCKS proxy server:

If you're not already running any other SOCKS proxy server, you can use the following code to create a SOCKS proxy server listening for connections on 127.0.0.1:1080:

See also the examples.

Usage

Client

The Client is responsible for communication with your SOCKS server instance.

Its constructor simply accepts a SOCKS proxy URI with the SOCKS proxy server address:

You can omit the port if you're using the default SOCKS port 1080:

If you need custom connector settings (DNS resolution, TLS parameters, timeouts, proxy servers etc.), you can explicitly pass a custom instance of the ConnectorInterface:

This is one of the two main classes in this package. Because it implements ReactPHP's standard ConnectorInterface, it can simply be used in place of a normal connector. Accordingly, it provides only a single public method, the connect() method. The connect(string $uri): PromiseInterface<ConnectionInterface, Exception> method can be used to establish a streaming connection. It returns a Promise which either fulfills with a ConnectionInterface on success or rejects with an Exception on error.

This makes it fairly simple to add SOCKS proxy support to pretty much any higher-level component:

Plain TCP connections

SOCKS proxies are most frequently used to issue HTTP(S) requests to your destination. However, this is actually performed on a higher protocol layer and this connector is actually inherently a general-purpose plain TCP/IP connector. As documented above, you can simply invoke its connect() method to establish a streaming plain TCP/IP connection and use any higher level protocol like so:

You can either use the Client directly or you may want to wrap this connector in ReactPHP's Connector:

See also the first example.

The tcp:// scheme can also be omitted. Passing any other scheme will reject the promise.

Pending connection attempts can be cancelled by cancelling its pending promise like so:

Calling cancel() on a pending promise will cancel the underlying TCP/IP connection to the SOCKS server and/or the SOCKS protocol negotiation and reject the resulting promise.

Secure TLS connections

This class can also be used if you want to establish a secure TLS connection (formerly known as SSL) between you and your destination, such as when using secure HTTPS to your destination site. You can simply wrap this connector in ReactPHP's Connector:

See also the second example.

Pending connection attempts can be cancelled by canceling its pending promise as usual.

Note how secure TLS connections are in fact entirely handled outside of this SOCKS client implementation.

You can optionally pass additional SSL context options to the constructor like this:

HTTP requests

This library also allows you to send HTTP requests through a SOCKS proxy server.

In order to send HTTP requests, you first have to add a dependency for ReactPHP's async HTTP client. This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:

See also ReactPHP's HTTP client and any of the examples for more details.

Protocol version

This library supports the SOCKS5 and SOCKS4(a) protocol versions. It focuses on the most commonly used core feature of connecting to a destination host through the SOCKS proxy server. In this mode, a SOCKS proxy server acts as a generic proxy allowing higher level application protocols to work through it.

SOCKS5 SOCKS4(a)
Protocol specification RFC 1928 SOCKS4.protocol / SOCKS4A.protocol
Tunnel outgoing TCP/IP connections
Remote DNS resolution ✗ / ✓
IPv6 addresses
Username/Password authentication ✓ (as per RFC 1929)
Handshake # roundtrips 2 (3 with authentication) 1
Handshake traffic
+ remote DNS
variable (+ auth + IPv6)
+ hostname - 3
17 bytes
+ hostname + 1
Incoming BIND requests not implemented not implemented
UDP datagrams not implemented
GSSAPI authentication not implemented

By default, the Client communicates via SOCKS5 with the SOCKS server. This is done because SOCKS5 is the latest version from the SOCKS protocol family and generally has best support across other vendors. You can also omit the default socks:// URI scheme. Similarly, the socks5:// URI scheme acts as an alias for the default socks:// URI scheme.

If want to explicitly set the protocol version to SOCKS4(a), you can use the URI scheme socks4:// as part of the SOCKS URI:

DNS resolution

By default, the Client does not perform any DNS resolution at all and simply forwards any hostname you're trying to connect to to the SOCKS server. The remote SOCKS server is thus responsible for looking up any hostnames via DNS (this default mode is thus called remote DNS resolution). As seen above, this mode is supported by the SOCKS5 and SOCKS4a protocols, but not the original SOCKS4 protocol, as the protocol lacks a way to communicate hostnames.

On the other hand, all SOCKS protocol versions support sending destination IP addresses to the SOCKS server. In this mode you either have to stick to using IPs only (which is ofen unfeasable) or perform any DNS lookups locally and only transmit the resolved destination IPs (this mode is thus called local DNS resolution).

The default remote DNS resolution is useful if your local Client either can not resolve target hostnames because it has no direct access to the internet or if it should not resolve target hostnames because its outgoing DNS traffic might be intercepted (in particular when using the Tor network).

As noted above, the Client defaults to using remote DNS resolution. However, wrapping the Client in ReactPHP's Connector actually performs local DNS resolution unless explicitly defined otherwise. Given that remote DNS resolution is assumed to be the preferred mode, all other examples explicitly disable DNS resolution like this:

If you want to explicitly use local DNS resolution (such as when explicitly using SOCKS4), you can use the following code:

See also the fourth example.

Pending connection attempts can be cancelled by cancelling its pending promise as usual.

Note how local DNS resolution is in fact entirely handled outside of this SOCKS client implementation.

Authentication

This library supports username/password authentication for SOCKS5 servers as defined in RFC 1929.

On the client side, simply pass your username and password to use for authentication (see below). For each further connection the client will merely send a flag to the server indicating authentication information is available. Only if the server requests authentication during the initial handshake, the actual authentication credentials will be transmitted to the server.

Note that the password is transmitted in cleartext to the SOCKS proxy server, so this methods should not be used on a network where you have to worry about eavesdropping.

You can simply pass the authentication information as part of the SOCKS URI:

Note that both the username and password must be percent-encoded if they contain special characters:

The authentication details will be transmitted in cleartext to the SOCKS proxy server only if it requires username/password authentication. If the authentication details are missing or not accepted by the remote SOCKS proxy server, it is expected to reject each connection attempt with an exception error code of SOCKET_EACCES (13).

Authentication is only supported by protocol version 5 (SOCKS5), so passing authentication to the Client enforces communication with protocol version 5 and complains if you have explicitly set anything else:

Proxy chaining

The Client is responsible for creating connections to the SOCKS server which then connects to the target host.

Sometimes it may be required to establish outgoing connections via another SOCKS server. For example, this can be useful if you want to conceal your origin address.

The Client uses any instance of the ConnectorInterface to establish outgoing connections. In order to connect through another SOCKS server, you can simply use another SOCKS connector from another SOCKS client like this:

See also the third example.

Pending connection attempts can be canceled by canceling its pending promise as usual.

Proxy chaining can happen on the server side and/or the client side:

Connection timeout

By default, the Client does not implement any timeouts for establishing remote connections. Your underlying operating system may impose limits on pending and/or idle TCP/IP connections, anywhere in a range of a few minutes to several hours.

Many use cases require more control over the timeout and likely values much smaller, usually in the range of a few seconds only.

You can use ReactPHP's Connector to decorate any given ConnectorInterface instance. It provides the same connect() method, but will automatically reject the underlying connection attempt if it takes too long:

See also any of the examples.

Pending connection attempts can be cancelled by cancelling its pending promise as usual.

Note how connection timeout is in fact entirely handled outside of this SOCKS client implementation.

SOCKS over TLS

All SOCKS protocol versions support forwarding TCP/IP based connections and higher level protocols. This implies that you can also use secure TLS connections to transfer sensitive data across SOCKS proxy servers. This means that no eavesdropper nor the proxy server will be able to decrypt your data.

However, the initial SOCKS communication between the client and the proxy is usually via an unencrypted, plain TCP/IP connection. This means that an eavesdropper may be able to see where you connect to and may also be able to see your SOCKS authentication details in cleartext.

As an alternative, you may establish a secure TLS connection to your SOCKS proxy before starting the initial SOCKS communication. This means that no eavesdroppper will be able to see the destination address you want to connect to or your SOCKS authentication details.

You can use the sockss:// URI scheme or use an explicit SOCKS protocol version like this:

See also example 32.

Similarly, you can also combine this with authentication like this:

Note that for most use cases, secure TLS connections should be used instead. SOCKS over TLS is considered advanced usage and is used very rarely in practice. In particular, the SOCKS server has to accept secure TLS connections, see also Server SOCKS over TLS for more details. Also, PHP does not support "double encryption" over a single connection. This means that enabling secure TLS connections over a communication channel that has been opened with SOCKS over TLS may not be supported.

Note that the SOCKS protocol does not support the notion of TLS. The above works reasonably well because TLS is only used for the connection between client and proxy server and the SOCKS protocol data is otherwise identical. This implies that this may also have only limited support for proxy chaining over multiple TLS paths.

Unix domain sockets

All SOCKS protocol versions support forwarding TCP/IP based connections and higher level protocols. In some advanced cases, it may be useful to let your SOCKS server listen on a Unix domain socket (UDS) path instead of a IP:port combination. For example, this allows you to rely on file system permissions instead of having to rely on explicit authentication.

You can use the socks+unix:// URI scheme or use an explicit SOCKS protocol version like this:

Similarly, you can also combine this with authentication like this:

Note that Unix domain sockets (UDS) are considered advanced usage and PHP only has limited support for this. In particular, enabling secure TLS may not be supported.

Note that the SOCKS protocol does not support the notion of UDS paths. The above works reasonably well because UDS is only used for the connection between client and proxy server and the path will not actually passed over the protocol. This implies that this does also not support proxy chaining over multiple UDS paths.

Server

The Server is responsible for accepting incoming communication from SOCKS clients and forwarding the requested connection to the target host. It supports the SOCKS5 and SOCKS4(a) protocol versions by default. You can start listening on an underlying TCP/IP socket server like this:

This class takes an optional LoopInterface|null $loop parameter that can be used to pass the event loop instance to use for this object. You can use a null value here in order to use the default loop. This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

Additionally, the Server constructor accepts optional parameters to explicitly configure the connector to use and to require authentication. For more details, read on...

Server connector

The Server uses an instance of ReactPHP's ConnectorInterface to establish outgoing connections for each incoming connection request.

If you need custom connector settings (DNS resolution, TLS parameters, timeouts, proxy servers etc.), you can explicitly pass a custom instance of the ConnectorInterface:

If you want to forward the outgoing connection through another SOCKS proxy, you may also pass a Client instance as a connector, see also server proxy chaining for more details.

Internally, the Server uses ReactPHP's normal connect() method, but it also passes the original client IP as the ?source={remote} parameter. The source parameter contains the full remote URI, including the protocol and any authentication details, for example socks://alice:[email protected]:5678 or socks4://1.2.3.4:5678 for legacy SOCKS4(a). You can use this parameter for logging purposes or to restrict connection requests for certain clients by providing a custom implementation of the ConnectorInterface.

Server authentication

By default, the Server does not require any authentication from the clients. You can enable authentication support so that clients need to pass a valid username and password before forwarding any connections.

Setting authentication on the Server enforces each further connected client to use protocol version 5 (SOCKS5). If a client tries to use any other protocol version, does not send along authentication details or if authentication details can not be verified, the connection will be rejected.

If you only want to accept static authentication details, you can simply pass an additional assoc array with your authentication details to the Server like this:

See also example #12.

If you want more control over authentication, you can pass an authenticator function that should return a bool value like this synchronous example:

Because your authentication mechanism might take some time to actually check the provided authentication credentials (like querying a remote database or webservice), the server also supports a Promise-based interface. While this might seem more complex at first, it actually provides a very powerful way of handling a large number of connections concurrently without ever blocking any connections. You can return a Promise from the authenticator function that will fulfill with a bool value like this async example:

Server proxy chaining

The Server is responsible for creating connections to the target host.

Sometimes it may be required to establish outgoing connections via another SOCKS server. For example, this can be useful if your target SOCKS server requires authentication, but your client does not support sending authentication information (e.g. like most webbrowser).

The Server uses any instance of the ConnectorInterface to establish outgoing connections. In order to connect through another SOCKS server, you can simply use the Client SOCKS connector from above. You can create a SOCKS Client instance like this:

See also example #21.

Proxy chaining can happen on the server side and/or the client side:

Server SOCKS over TLS

Both SOCKS5 and SOCKS4(a) protocol versions support forwarding TCP/IP based connections and higher level protocols. This implies that you can also use secure TLS connections to transfer sensitive data across SOCKS proxy servers. This means that no eavesdropper nor the proxy server will be able to decrypt your data.

However, the initial SOCKS communication between the client and the proxy is usually via an unencrypted, plain TCP/IP connection. This means that an eavesdropper may be able to see where the client connects to and may also be able to see the SOCKS authentication details in cleartext.

As an alternative, you may listen for SOCKS over TLS connections so that the client has to establish a secure TLS connection to your SOCKS proxy before starting the initial SOCKS communication. This means that no eavesdroppper will be able to see the destination address the client wants to connect to or their SOCKS authentication details.

You can simply start your listening socket on the tls:// URI scheme like this:

See also example 31.

Note that for most use cases, secure TLS connections should be used instead. SOCKS over TLS is considered advanced usage and is used very rarely in practice.

Note that the SOCKS protocol does not support the notion of TLS. The above works reasonably well because TLS is only used for the connection between client and proxy server and the SOCKS protocol data is otherwise identical. This implies that this does also not support proxy chaining over multiple TLS paths.

Server Unix domain sockets

Both SOCKS5 and SOCKS4(a) protocol versions support forwarding TCP/IP based connections and higher level protocols. In some advanced cases, it may be useful to let your SOCKS server listen on a Unix domain socket (UDS) path instead of a IP:port combination. For example, this allows you to rely on file system permissions instead of having to rely on explicit authentication.

You can simply start your listening socket on the unix:// URI scheme like this:

Note that Unix domain sockets (UDS) are considered advanced usage and that the SOCKS protocol does not support the notion of UDS paths. The above works reasonably well because UDS is only used for the connection between client and proxy server and the path will not actually passed over the protocol. This implies that this does also not support proxy chaining over multiple UDS paths.

Servers

Using a PHP SOCKS server

Using SSH as a SOCKS server

If you already have an SSH server set up, you can easily use it as a SOCKS tunnel end point. On your client, simply start your SSH client and use the -D <port> option to start a local SOCKS server (quoting the man page: a local "dynamic" application-level port forwarding).

You can start a local SOCKS server by creating a loopback connection to your local system if you already run an SSH daemon:

Alternatively, you can start a local SOCKS server tunneling through a given remote host that runs an SSH daemon:

Now you can simply use this SSH SOCKS server like this:

Note that the above will allow all users on the local system to connect over your SOCKS server without authentication which may or may not be what you need. As an alternative, recent OpenSSH client versions also support Unix domain sockets (UDS) paths so that you can rely on Unix file system permissions instead:

Now you can simply use this SSH SOCKS server like this:

As an alternative to requiring this manual setup, you may also want to look into using clue/reactphp-ssh-proxy which automatically creates this SSH tunnel for you. It provides an implementation of the same ConnectorInterface so that supporting either proxy protocol should be fairly trivial.

Using the Tor (anonymity network) to tunnel SOCKS connections

The Tor anonymity network client software is designed to encrypt your traffic and route it over a network of several nodes to conceal its origin. It presents a SOCKS5 and SOCKS4(a) interface on TCP port 9050 by default which allows you to tunnel any traffic through the anonymity network:

In most common scenarios you probably want to stick to default remote DNS resolution and don't want your client to resolve the target hostnames, because you would leak DNS information to anybody observing your local traffic. Also, Tor provides hidden services through an .onion pseudo top-level domain which have to be resolved by Tor.

Install

The recommended way to install this library is through Composer. New to Composer?

This project follows SemVer. This will install the latest supported version:

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. It's highly recommended to use the latest supported PHP version for this project.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

To run the test suite, go to the project root and run:

The test suite contains a number of tests that rely on a working internet connection, alternatively you can also run it like this:

License

This project is released under the permissive MIT license.

Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.

More


Version
Requires php Version >=5.3
react/promise Version ^2.1 || ^1.2
react/socket Version ^1.9

The package clue/socks-react contains the following files

Loading the files please wait ....