PHP code example of infostars / socks-react

1. Go to this page and download the library: Download infostars/socks-react library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

infostars / socks-react example snippets


$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);
$client = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);

$client->connect('tcp://www.google.com:80')->then(function (ConnectionInterface $stream) {
    $stream->write("GET / HTTP/1.0\r\n\r\n");
});

$loop->run();

$loop = React\EventLoop\Factory::create();

// start a new SOCKS proxy server
$server = new Clue\React\Socks\Server($loop);

// listen on localhost:1080
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
$server->listen($socket);

$loop->run();

$connector = new React\Socket\Connector($loop);
$client = new Client('127.0.0.1:1080', $connector);

$client = new Client('127.0.0.1', $connector);

$connector = new React\Socket\Connector($loop, array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    )
));

$client = new Client('my-socks-server.local:1080', $connector);

$client->connect('tcp://www.google.com:80')->then(function (ConnectonInterface $stream) {
    echo 'connected to www.google.com:80';
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    // ...
});

$connector = new React\Socket\Connector($loop, array(
    'tcp' => $client,
    'dns' => false
));

$connector->connect('tcp://www.google.com:80')->then(function (ConnectonInterface $stream) {
    echo 'connected to www.google.com:80';
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    // ...
});

$promise = $connector->connect($uri);

$promise->cancel();

$connector = new React\Socket\Connector($loop, array(
    'tcp' => $client,
    'dns' => false
));

// now create an SSL encrypted connection (notice the $ssl instead of $tcp)
$connector->connect('tls://www.google.com:443')->then(function (ConnectionInterface $stream) {
    // proceed with just the plain text data
    // everything is encrypted/decrypted automatically
    echo 'connected to SSL encrypted www.google.com';
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    // ...
});

$connector = new React\Socket\Connector($loop, array(
    'tcp' => $client,
    'tls' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    ),
    'dns' => false
));

// all three forms are equivalent
$client = new Client('127.0.0.1', $connector);
$client = new Client('socks://127.0.0.1', $connector);
$client = new Client('socks5://127.0.0.1', $connector);

$client = new Client('socks4://127.0.0.1', $connector);

$connector = new React\Socket\Connector($loop, array(
    'tcp' => $client,
    'dns' => false
));

// set up Connector which uses Google's public DNS (8.8.8.8)
$connector = new React\Socket\Connector($loop, array(
    'tcp' => $client,
    'dns' => '8.8.8.8'
));

$client = new Client('username:[email protected]', $connector);

$user = 'he:llo';
$pass = 'p@ss';

$client = new Client(
    rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1',
    $connector
);

// throws InvalidArgumentException
new Client('socks4://user:[email protected]', $connector);

// https via the proxy chain  "MiddlemanSocksServer -> TargetSocksServer -> TargetHost"
// please note how the client uses TargetSocksServer (not MiddlemanSocksServer!),
// which in turn then uses MiddlemanSocksServer.
// this creates a TCP/IP connection to MiddlemanSocksServer, which then connects
// to TargetSocksServer, which then connects to the TargetHost
$middle = new Client('127.0.0.1:1080', new Connector($loop));
$target = new Client('example.com:1080', $middle);

$connector = new React\Socket\Connector($loop, array(
    'tcp' => $target,
    'dns' => false
));

$connector->connect('tls://www.google.com:443')->then(function ($stream) {
    // …
});

$connector = new Connector($loop, array(
    'tcp' => $client,
    'dns' => false,
    'timeout' => 3.0
));

$connector->connect('tcp://google.com:80')->then(function ($stream) {
    // connection succeeded within 3.0 seconds
});

$client = new Client('sockss://127.0.0.1:1080', new Connector($loop));

$client = new Client('socks4s://127.0.0.1:1080', new Connector($loop));

$client = new Client('sockss://user:[email protected]:1080', new Connector($loop));

$client = new Client('socks+unix:///tmp/proxy.sock', new Connector($loop));

$client = new Client('socks4+unix:///tmp/proxy.sock', new Connector($loop));

$client = new Client('socks+unix://user:pass@/tmp/proxy.sock', new Connector($loop));

$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

// listen on localhost:$port
$socket = new React\Socket\Server($port, $loop);
$server->listen($socket);

$loop->run();

$connector = new React\Socket\Connector($loop, array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    )
));

$server = new Clue\React\Socks\Server($loop, $connector);

$server = new Clue\React\Socks\Server($loop, null, array(
    'tom' => 'password',
    'admin' => 'root'
));

$server = new Clue\React\Socks\Server($loop, null, function ($user, $pass, $remote) {
    // $remote is a full URI à la socks://user:[email protected]:1234
    // or sockss://user:[email protected]:1234 for SOCKS over TLS
    // or may be null when remote is unknown (SOCKS over Unix Domain Sockets)
    // useful for logging or extracting parts, such as the remote IP
    $ip = parse_url($remote, PHP_URL_HOST);

    return ($user === 'root' && $pass === 'secret' && $ip === '127.0.0.1');
});

$server = new Clue\React\Socks\Server($loop, null, function ($user, $pass) use ($db) {
    // pseudo-code: query database for given authentication details
    return $db->query(
        'SELECT 1 FROM users WHERE name = ? AND password = ?',
        array($username, $password)
    )->then(function (QueryResult $result) {
        // ensure we find exactly one match in the database
        return count($result->resultRows) === 1;
    });
});

$loop = React\EventLoop\Factory::create();

// set next SOCKS server example.com:1080 as target
$connector = new React\Socket\Connector($loop);
$client = new Clue\React\Socks\Client('user:[email protected]:1080', $connector);

// start a new server which forwards all connections to the other SOCKS server
$server = new Clue\React\Socks\Server($loop, $client);

// listen on localhost:1080
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
$server->listen($socket);

$loop->run();

$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

// listen on tls://127.0.0.1:1080 with the given server certificate
$socket = new React\Socket\Server('tls://127.0.0.1:1080', $loop, array(
    'tls' => array(
        'local_cert' => __DIR__ . '/localhost.pem',
    )
));
$server->listen($socket);

$loop->run();

$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

// listen on /tmp/proxy.sock
$socket = new React\Socket\Server('unix:///tmp/proxy.sock', $loop);
$server->listen($socket);

$loop->run();

$client = new Client('127.0.0.1:1080', $connector);

$client = new Client('socks+unix:///tmp/proxy.sock', $connector);

$client = new Client('127.0.0.1:9050', $connector);