diff --git a/composer.json b/composer.json index 1471ca1..45d224f 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "require-dev": { "phpunit/phpunit": "^9.0 || ^7.0 || ^6.0 || ^5.7 || ^4.8.35", "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/http": "^1.0", "clue/connection-manager-extra": "^1.0 || ^0.7", "clue/block-react": "^1.1" } diff --git a/examples/01-http.php b/examples/01-http.php deleted file mode 100644 index 37a88a3..0000000 --- a/examples/01-http.php +++ /dev/null @@ -1,38 +0,0 @@ - $client, - 'timeout' => 3.0, - 'dns' => false -)); - -echo 'Demo SOCKS client connecting to SOCKS server ' . $proxy . PHP_EOL; - -$connector->connect('tcp://www.google.com:80')->then(function (ConnectionInterface $stream) { - echo 'connected' . PHP_EOL; - $stream->write("GET / HTTP/1.0\r\n\r\n"); - $stream->on('data', function ($data) { - echo $data; - }); -}, 'printf'); - -$loop->run(); diff --git a/examples/01-https-request.php b/examples/01-https-request.php new file mode 100644 index 0000000..7f9661d --- /dev/null +++ b/examples/01-https-request.php @@ -0,0 +1,41 @@ + $client, + 'timeout' => 3.0, + 'dns' => false +)); + +$browser = new React\Http\Browser($loop, $connector); + +$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/02-https.php b/examples/02-https.php deleted file mode 100644 index 5af865d..0000000 --- a/examples/02-https.php +++ /dev/null @@ -1,38 +0,0 @@ - $client, - 'timeout' => 3.0, - 'dns' => false -)); - -echo 'Demo SOCKS client connecting to SOCKS server ' . $proxy . PHP_EOL; - -$connector->connect('tls://www.google.com:443')->then(function (ConnectionInterface $stream) { - echo 'connected' . PHP_EOL; - $stream->write("GET / HTTP/1.0\r\n\r\n"); - $stream->on('data', function ($data) { - echo $data; - }); -}, 'printf'); - -$loop->run(); diff --git a/examples/02-optional-proxy-https-request.php b/examples/02-optional-proxy-https-request.php new file mode 100644 index 0000000..4c1a7a0 --- /dev/null +++ b/examples/02-optional-proxy-https-request.php @@ -0,0 +1,40 @@ + $client, + 'timeout' => 3.0, + 'dns' => false + )); +} + +$browser = new React\Http\Browser($loop, $connector); + +$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/11-proxy-raw-http-protocol.php b/examples/11-proxy-raw-http-protocol.php new file mode 100644 index 0000000..90028d3 --- /dev/null +++ b/examples/11-proxy-raw-http-protocol.php @@ -0,0 +1,48 @@ + $client, + 'timeout' => 3.0, + 'dns' => false +)); + +echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL; + +$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) { + echo 'connected' . PHP_EOL; + $stream->write("GET / HTTP/1.0\r\n\r\n"); + $stream->on('data', function ($data) { + echo $data; + }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/12-optional-proxy-raw-http-protocol.php b/examples/12-optional-proxy-raw-http-protocol.php new file mode 100644 index 0000000..f24f94e --- /dev/null +++ b/examples/12-optional-proxy-raw-http-protocol.php @@ -0,0 +1,51 @@ + $client, + 'timeout' => 3.0, + 'dns' => false + )); +} + +echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL; + +$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) { + echo 'connected' . PHP_EOL; + $stream->write("GET / HTTP/1.0\r\n\r\n"); + $stream->on('data', function ($data) { + echo $data; + }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/13-proxy-raw-https-protocol.php b/examples/13-proxy-raw-https-protocol.php new file mode 100644 index 0000000..4373258 --- /dev/null +++ b/examples/13-proxy-raw-https-protocol.php @@ -0,0 +1,48 @@ + $client, + 'timeout' => 3.0, + 'dns' => false +)); + +echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL; + +$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) { + echo 'connected' . PHP_EOL; + $stream->write("GET / HTTP/1.0\r\n\r\n"); + $stream->on('data', function ($data) { + echo $data; + }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/14-optional-proxy-raw-https-protocol.php b/examples/14-optional-proxy-raw-https-protocol.php new file mode 100644 index 0000000..51f7162 --- /dev/null +++ b/examples/14-optional-proxy-raw-https-protocol.php @@ -0,0 +1,51 @@ + $client, + 'timeout' => 3.0, + 'dns' => false + )); +} + +echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL; + +$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) { + echo 'connected' . PHP_EOL; + $stream->write("GET / HTTP/1.0\r\n\r\n"); + $stream->on('data', function ($data) { + echo $data; + }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/examples/03-proxy-chaining.php b/examples/15-proxy-chaining.php similarity index 73% rename from examples/03-proxy-chaining.php rename to examples/15-proxy-chaining.php index ece6de8..1410bb0 100644 --- a/examples/03-proxy-chaining.php +++ b/examples/15-proxy-chaining.php @@ -3,14 +3,11 @@ // A more advanced example which requests http://www.google.com/ through a chain of SOCKS proxy servers. // The proxy servers can be given as arguments. // -// Not already running a SOCKS proxy server? See also example #11 or try this: `ssh -D 1080 localhost` +// Not already running a SOCKS proxy server? See also example #21 or try this: +// $ ssh -D 1080 localhost // // For illustration purposes only. If you want to send HTTP requests in a real -// world project, take a look at https://github.com/clue/reactphp-buzz#socks-proxy - -use Clue\React\Socks\Client; -use React\Socket\Connector; -use React\Socket\ConnectionInterface; +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. require __DIR__ . '/../vendor/autoload.php'; @@ -28,14 +25,14 @@ $loop = React\EventLoop\Factory::create(); // set next SOCKS server chain via p1 -> p2 -> p3 -> destination -$connector = new Connector($loop); +$connector = new React\Socket\Connector($loop); foreach ($path as $proxy) { - $connector = new Client($proxy, $connector); + $connector = new Clue\React\Socks\Client($proxy, $connector); } // please note how the client uses p3 (not p1!), which in turn then uses the complete chain // this creates a TCP/IP connection to p1, which then connects to p2, then to p3, which then connects to the target -$connector = new Connector($loop, array( +$connector = new React\Socket\Connector($loop, array( 'tcp' => $connector, 'timeout' => 3.0, 'dns' => false @@ -43,12 +40,14 @@ echo 'Demo SOCKS client connecting to SOCKS proxy server chain ' . implode(' -> ', $path) . PHP_EOL; -$connector->connect('tls://www.google.com:443')->then(function (ConnectionInterface $stream) { +$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) { echo 'connected' . PHP_EOL; $stream->write("GET / HTTP/1.0\r\n\r\n"); $stream->on('data', function ($data) { echo $data; }); -}, 'printf'); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); $loop->run(); diff --git a/examples/04-local-dns.php b/examples/16-local-dns.php similarity index 64% rename from examples/04-local-dns.php rename to examples/16-local-dns.php index f876e4e..300f42b 100644 --- a/examples/04-local-dns.php +++ b/examples/16-local-dns.php @@ -3,14 +3,11 @@ // A simple example which requests https://www.google.com/ through a SOCKS proxy with local DNS resolution. // The proxy can be given as first argument and defaults to localhost:1080 otherwise. // -// Not already running a SOCKS proxy server? See also example #11 or try this: `ssh -D 1080 localhost` +// Not already running a SOCKS proxy server? See also example #21 or try this: +// $ ssh -D 1080 localhost // // For illustration purposes only. If you want to send HTTP requests in a real -// world project, take a look at https://github.com/clue/reactphp-buzz#socks-proxy - -use Clue\React\Socks\Client; -use React\Socket\Connector; -use React\Socket\ConnectionInterface; +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. require __DIR__ . '/../vendor/autoload.php'; @@ -19,8 +16,8 @@ $loop = React\EventLoop\Factory::create(); // set up DNS server to use (Google's public DNS) -$client = new Client($proxy, new Connector($loop)); -$connector = new Connector($loop, array( +$client = new Clue\React\Socks\Client($proxy, new React\Socket\Connector($loop)); +$connector = new React\Socket\Connector($loop, array( 'tcp' => $client, 'timeout' => 3.0, 'dns' => '8.8.8.8' @@ -28,12 +25,14 @@ echo 'Demo SOCKS client connecting to SOCKS server ' . $proxy . PHP_EOL; -$connector->connect('tls://www.google.com:443')->then(function (ConnectionInterface $stream) { +$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) { echo 'connected' . PHP_EOL; $stream->write("GET / HTTP/1.0\r\n\r\n"); $stream->on('data', function ($data) { echo $data; }); -}, 'printf'); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); $loop->run(); diff --git a/examples/11-server.php b/examples/21-server.php similarity index 66% rename from examples/11-server.php rename to examples/21-server.php index 0e8dfe3..1d5802c 100644 --- a/examples/11-server.php +++ b/examples/21-server.php @@ -3,20 +3,17 @@ // A simple example which runs a SOCKS proxy server. // The listen address can be given as first argument and defaults to localhost:1080 otherwise. // -// See also examples #01 and #02 for the client side. - -use Clue\React\Socks\Server; -use React\Socket\Server as Socket; +// See also examples #12 and #14 for the client side. require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); // start a new SOCKS proxy server -$server = new Server($loop); +$server = new Clue\React\Socks\Server($loop); // listen on 127.0.0.1:1080 or first argument -$socket = new Socket(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); +$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); $server->listen($socket); echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL; diff --git a/examples/12-server-with-password.php b/examples/22-server-with-password.php similarity index 74% rename from examples/12-server-with-password.php rename to examples/22-server-with-password.php index 88117e7..eec40ce 100644 --- a/examples/12-server-with-password.php +++ b/examples/22-server-with-password.php @@ -3,16 +3,13 @@ // A simple example which runs a SOCKS proxy server with hard-coded authentication details. // The listen address can be given as first argument and defaults to localhost:1080 otherwise. // -// See also examples #01 and #02 for the client side. +// See also examples #12 and #14 for the client side. // // Note that the client examples do not pass any authentication details by default // and as such will fail to authenticate against this example server. You can // explicitly pass authentication details to the client example like this: // -// $ php examples/01-http.php tom:god@localhost:1080 - -use Clue\React\Socks\Server; -use React\Socket\Server as Socket; +// $ php examples/12-optional-proxy-raw-http-protocol.php tom:god@localhost:1080 require __DIR__ . '/../vendor/autoload.php'; @@ -20,13 +17,13 @@ // start a new SOCKS proxy server // require authentication and hence make this a SOCKS5-only server -$server = new Server($loop, null, array( +$server = new Clue\React\Socks\Server($loop, null, array( 'tom' => 'god', 'user' => 'p@ssw0rd' )); // listen on 127.0.0.1:1080 or first argument -$socket = new Socket(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); +$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); $server->listen($socket); echo 'SOCKS5 server requiring authentication listening on ' . $socket->getAddress() . PHP_EOL; diff --git a/examples/13-server-blacklist.php b/examples/23-server-blacklist.php similarity index 57% rename from examples/13-server-blacklist.php rename to examples/23-server-blacklist.php index 3ec0221..70d8f0f 100644 --- a/examples/13-server-blacklist.php +++ b/examples/23-server-blacklist.php @@ -4,41 +4,34 @@ // to some domains (blacklist /filtering). // The listen address can be given as first argument and defaults to localhost:1080 otherwise. // -// See also examples #01 and #02 for the client side. -// Client example #01 is expected to fail because port 80 is blocked in this server example. -// Client example #02 is expected to succceed because it is not blacklisted. - -use React\EventLoop\Factory as LoopFactory; -use ConnectionManager\Extra\Multiple\ConnectionManagerSelective; -use React\Socket\Server as Socket; -use Clue\React\Socks\Server; -use ConnectionManager\Extra\ConnectionManagerReject; -use React\Socket\Connector; +// See also examples #12 and #14 for the client side. +// Client example #12 is expected to fail because port 80 is blocked in this server example. +// Client example #14 is expected to succceed because it is not blacklisted. require __DIR__ . '/../vendor/autoload.php'; -$loop = LoopFactory::create(); +$loop = React\EventLoop\Factory::create(); // create a connector that rejects the connection -$reject = new ConnectionManagerReject(); +$reject = new ConnectionManager\Extra\ConnectionManagerReject(); // create an actual connector that establishes real connections -$permit = new Connector($loop); +$permit = new React\Socket\Connector($loop); // this connector selectively picks one of the the attached connectors depending on the target address // reject youtube.com and unencrypted HTTP for google.com // default connctor: permit everything -$connector = new ConnectionManagerSelective(array( +$connector = new ConnectionManager\Extra\Multiple\ConnectionManagerSelective(array( '*.youtube.com' => $reject, 'www.google.com:80' => $reject, '*' => $permit )); // start a new SOCKS proxy server using our connection manager for outgoing connections -$server = new Server($loop, $connector); +$server = new Clue\React\Socks\Server($loop, $connector); // listen on 127.0.0.1:1080 or first argument -$socket = new Socket(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); +$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop); $server->listen($socket); echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL; diff --git a/examples/21-server-proxy-chaining.php b/examples/31-server-proxy-chaining.php similarity index 77% rename from examples/21-server-proxy-chaining.php rename to examples/31-server-proxy-chaining.php index 1fb757a..05902a7 100644 --- a/examples/21-server-proxy-chaining.php +++ b/examples/31-server-proxy-chaining.php @@ -5,12 +5,7 @@ // The listen address can be given as first argument. // The upstream proxy servers can be given as additional arguments. // -// See also examples #01 and #02 for the client side. - -use Clue\React\Socks\Client; -use Clue\React\Socks\Server; -use React\Socket\Server as Socket; -use React\Socket\Connector; +// See also examples #12 and #14 for the client side. require __DIR__ . '/../vendor/autoload.php'; @@ -30,16 +25,16 @@ $loop = React\EventLoop\Factory::create(); // set next SOCKS server chain -> p1 -> p2 -> p3 -> destination -$connector = new Connector($loop); +$connector = new React\Socket\Connector($loop); foreach ($path as $proxy) { - $connector = new Client($proxy, $connector); + $connector = new Clue\React\Socks\Client($proxy, $connector); } // start a new SOCKS proxy server which forwards all connections to the other SOCKS server -$server = new Server($loop, $connector); +$server = new Clue\React\Socks\Server($loop, $connector); // listen on 127.0.0.1:1080 or first argument -$socket = new Socket($listen, $loop); +$socket = new React\Socket\Server($listen, $loop); $server->listen($socket); echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL; diff --git a/examples/22-server-proxy-chaining-from-random-pool.php b/examples/32-server-proxy-chaining-from-random-pool.php similarity index 69% rename from examples/22-server-proxy-chaining-from-random-pool.php rename to examples/32-server-proxy-chaining-from-random-pool.php index 99b0796..1172b02 100644 --- a/examples/22-server-proxy-chaining-from-random-pool.php +++ b/examples/32-server-proxy-chaining-from-random-pool.php @@ -5,14 +5,7 @@ // The listen address can be given as first argument. // The upstream proxy servers can be given as additional arguments. // -// See also examples #01 and #02 for the client side. - -use React\EventLoop\Factory as LoopFactory; -use ConnectionManager\Extra\Multiple\ConnectionManagerRandom; -use React\Socket\Server as Socket; -use Clue\React\Socks\Server; -use Clue\React\Socks\Client; -use React\Socket\Connector; +// See also examples #12 and #14 for the client side. require __DIR__ . '/../vendor/autoload.php'; @@ -29,22 +22,22 @@ //$listen = '127.0.0.1:9050'; //$pool = array('127.0.0.1:9051', '127.0.0.1:9052', '127.0.0.1:9053'); -$loop = LoopFactory::create(); +$loop = React\EventLoop\Factory::create(); // forward to socks server listening on 127.0.0.1:9051-9053 // this connector randomly picks one of the the attached connectors from the pool -$connector = new Connector($loop); +$connector = new React\Socket\Connector($loop); $clients = array(); foreach ($pool as $proxy) { - $clients []= new Client($proxy, $connector); + $clients []= new Clue\React\Socks\Client($proxy, $connector); } -$connector = new ConnectionManagerRandom($clients); +$connector = new ConnectionManager\Extra\Multiple\ConnectionManagerRandom($clients); // start the SOCKS proxy server using our connection manager for outgoing connections -$server = new Server($loop, $socket, $connector); +$server = new Clue\React\Socks\Server($loop, $socket, $connector); // listen on 127.0.0.1:1080 or first argument -$socket = new Socket($listen, $loop); +$socket = new React\Socket\Server($listen, $loop); $server->listen($socket); echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL; diff --git a/examples/31-server-secure.php b/examples/41-server-secure.php similarity index 74% rename from examples/31-server-secure.php rename to examples/41-server-secure.php index c76adec..d6d2cad 100644 --- a/examples/31-server-secure.php +++ b/examples/41-server-secure.php @@ -3,21 +3,18 @@ // A more advanced example which runs a secure SOCKS over TLS proxy server. // The listen address can be given as first argument and defaults to localhost:1080 otherwise. // -// See also example #32 for the client side. - -use Clue\React\Socks\Server; -use React\Socket\Server as Socket; +// See also example #42 for the client side. require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); // start a new SOCKS proxy server -$server = new Server($loop); +$server = new Clue\React\Socks\Server($loop); // listen on tls://127.0.0.1:1080 or first argument $listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080'; -$socket = new Socket('tls://' . $listen, $loop, array('tls' => array( +$socket = new React\Socket\Server('tls://' . $listen, $loop, array('tls' => array( 'local_cert' => __DIR__ . '/localhost.pem', ))); diff --git a/examples/32-http-secure.php b/examples/42-http-secure.php similarity index 61% rename from examples/32-http-secure.php rename to examples/42-http-secure.php index 0f68054..6205db0 100644 --- a/examples/32-http-secure.php +++ b/examples/42-http-secure.php @@ -3,14 +3,10 @@ // A more advanced example which requests http://google.com/ through a secure SOCKS over TLS proxy. // The proxy can be given as first argument and defaults to localhost:1080 otherwise. // -// See also example #31 for the server side. +// See also example #41 for the server side. // // For illustration purposes only. If you want to send HTTP requests in a real -// world project, take a look at https://github.com/clue/reactphp-buzz#socks-proxy - -use Clue\React\Socks\Client; -use React\Socket\Connector; -use React\Socket\ConnectionInterface; +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. require __DIR__ . '/../vendor/autoload.php'; @@ -18,11 +14,11 @@ $loop = React\EventLoop\Factory::create(); -$client = new Client('sockss://' . $proxy, new Connector($loop, array('tls' => array( +$client = new Clue\React\Socks\Client('sockss://' . $proxy, new React\Socket\Connector($loop, array('tls' => array( 'verify_peer' => false, 'verify_peer_name' => false )))); -$connector = new Connector($loop, array( +$connector = new React\Socket\Connector($loop, array( 'tcp' => $client, 'timeout' => 3.0, 'dns' => false @@ -30,12 +26,14 @@ echo 'Demo SOCKS over TLS client connecting to secure SOCKS server ' . $proxy . PHP_EOL; -$connector->connect('tcp://www.google.com:80')->then(function (ConnectionInterface $stream) { +$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) { echo 'connected' . PHP_EOL; $stream->write("GET / HTTP/1.0\r\n\r\n"); $stream->on('data', function ($data) { echo $data; }); -}, 'printf'); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); $loop->run();