Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by updating to new default loop and making Connector optional #100

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 68 additions & 156 deletions README.md

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
"email": "christian@clue.engineering"
}
],
"autoload": {
"psr-4": {"Clue\\React\\Socks\\": "src/"}
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Socks\\": "tests/" }
},
"require": {
"php": ">=5.3",
"react/promise": "^2.1 || ^1.2",
"react/socket": "^1.1"
"react/socket": "^1.8"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/http": "^1.0",
"clue/block-react": "^1.1",
"clue/connection-manager-extra": "^1.0 || ^0.7",
"clue/block-react": "^1.1"
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.2",
"react/http": "^1.4"
},
"autoload": {
"psr-4": { "Clue\\React\\Socks\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Socks\\": "tests/" }
}
}
12 changes: 3 additions & 9 deletions examples/01-https-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@
$url = 'localhost:1080';
}

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\Socks\Client(
$url,
new React\Socket\Connector($loop)
);
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector($loop, array(
$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));

$browser = new React\Http\Browser($loop, $connector);
$browser = new React\Http\Browser(null, $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();
14 changes: 4 additions & 10 deletions examples/02-optional-proxy-https-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,22 @@

require __DIR__ . '/../vendor/autoload.php';

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

$connector = null;
$url = getenv('socks_proxy');
if ($url !== false) {
$proxy = new Clue\React\Socks\Client(
$url,
new React\Socket\Connector($loop)
);
$connector = new React\Socket\Connector($loop, array(
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
}

$browser = new React\Http\Browser($loop, $connector);
$browser = new React\Http\Browser(null, $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();
10 changes: 2 additions & 8 deletions examples/11-proxy-raw-http-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
$url = 'localhost:1080';
}

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\Socks\Client($url);

$proxy = new Clue\React\Socks\Client(
$url,
new React\Socket\Connector($loop)
);
$connector = new React\Socket\Connector($loop, array(
$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand All @@ -47,5 +43,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
13 changes: 5 additions & 8 deletions examples/12-optional-proxy-raw-http-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@

require __DIR__ . '/../vendor/autoload.php';

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

$connector = new React\Socket\Connector($loop);

$url = getenv('socks_proxy');
if ($url !== false) {
$proxy = new Clue\React\Socks\Client($url, $connector);
$connector = new React\Socket\Connector($loop, array(
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
} else {
$connector = new React\Socket\Connector();
}

echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
Expand All @@ -47,5 +46,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
10 changes: 2 additions & 8 deletions examples/13-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
$url = 'localhost:1080';
}

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\Socks\Client($url);

$proxy = new Clue\React\Socks\Client(
$url,
new React\Socket\Connector($loop)
);
$connector = new React\Socket\Connector($loop, array(
$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand All @@ -47,5 +43,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
13 changes: 5 additions & 8 deletions examples/14-optional-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@

require __DIR__ . '/../vendor/autoload.php';

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

$connector = new React\Socket\Connector($loop);

$url = getenv('socks_proxy');
if ($url !== false) {
$proxy = new Clue\React\Socks\Client($url, $connector);
$connector = new React\Socket\Connector($loop, array(
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
} else {
$connector = new React\Socket\Connector();
}

echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
Expand All @@ -47,5 +46,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
10 changes: 3 additions & 7 deletions examples/15-proxy-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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 #21 or try this:
// 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
Expand All @@ -22,17 +22,15 @@
// Alternatively, you can also hard-code this value like this:
//$path = array('127.0.0.1:9051', '127.0.0.1:9052', '127.0.0.1:9053');

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

// set next SOCKS server chain via p1 -> p2 -> p3 -> destination
$connector = new React\Socket\Connector($loop);
$connector = new React\Socket\Connector();
foreach ($path as $proxy) {
$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 React\Socket\Connector($loop, array(
$connector = new React\Socket\Connector(null, array(
'tcp' => $connector,
'timeout' => 3.0,
'dns' => false
Expand All @@ -49,5 +47,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
12 changes: 3 additions & 9 deletions examples/16-local-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@

$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\Socks\Client($url);

// set up DNS server to use (Google's public DNS)
$proxy = new Clue\React\Socks\Client(
$url,
new React\Socket\Connector($loop)
);
$connector = new React\Socket\Connector($loop, array(
$connector = new React\Socket\Connector(null, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => '8.8.8.8'
));

echo 'Demo SOCKS client connecting to SOCKS server ' . $proxy . PHP_EOL;
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;

$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
echo 'connected' . PHP_EOL;
Expand All @@ -37,5 +33,3 @@
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
8 changes: 2 additions & 6 deletions examples/21-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@

require __DIR__ . '/../vendor/autoload.php';

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

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

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(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');
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;

$loop->run();
8 changes: 2 additions & 6 deletions examples/22-server-with-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@

require __DIR__ . '/../vendor/autoload.php';

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

// start a new SOCKS proxy server
// require authentication and hence make this a SOCKS5-only server
$server = new Clue\React\Socks\Server($loop, null, array(
$server = new Clue\React\Socks\Server(null, null, array(
'tom' => 'god',
'user' => 'p@ssw0rd'
));

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(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');
$server->listen($socket);

echo 'SOCKS5 server requiring authentication listening on ' . $socket->getAddress() . PHP_EOL;

$loop->run();
10 changes: 3 additions & 7 deletions examples/23-server-blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

require __DIR__ . '/../vendor/autoload.php';

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

// create a connector that rejects the connection
$reject = new ConnectionManager\Extra\ConnectionManagerReject();

// create an actual connector that establishes real connections
$permit = new React\Socket\Connector($loop);
$permit = new React\Socket\Connector();

// this connector selectively picks one of the the attached connectors depending on the target address
// reject youtube.com and unencrypted HTTP for google.com
Expand All @@ -28,12 +26,10 @@
));

// start a new SOCKS proxy server using our connection manager for outgoing connections
$server = new Clue\React\Socks\Server($loop, $connector);
$server = new Clue\React\Socks\Server(null, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(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');
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;

$loop->run();
10 changes: 3 additions & 7 deletions examples/31-server-proxy-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@
//$listen = '127.0.0.1:9050';
//$path = array('127.0.0.1:9051', '127.0.0.1:9052', '127.0.0.1:9053');

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

// set next SOCKS server chain -> p1 -> p2 -> p3 -> destination
$connector = new React\Socket\Connector($loop);
$connector = new React\Socket\Connector();
foreach ($path as $proxy) {
$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 Clue\React\Socks\Server($loop, $connector);
$server = new Clue\React\Socks\Server(null, $connector);

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

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
echo 'Forwarding via: ' . implode(' -> ', $path) . PHP_EOL;

$loop->run();
10 changes: 3 additions & 7 deletions examples/32-server-proxy-chaining-from-random-pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@
//$listen = '127.0.0.1:9050';
//$pool = array('127.0.0.1:9051', '127.0.0.1:9052', '127.0.0.1:9053');

$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 React\Socket\Connector($loop);
$connector = new React\Socket\Connector();
$proxies = array();
foreach ($pool as $proxy) {
$proxies []= new Clue\React\Socks\Client($proxy, $connector);
}
$connector = new ConnectionManager\Extra\Multiple\ConnectionManagerRandom($proxies);

// start the SOCKS proxy server using our connection manager for outgoing connections
$server = new Clue\React\Socks\Server($loop, $socket, $connector);
$server = new Clue\React\Socks\Server(null, $connector);

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

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
echo 'Randomly picking from: ' . implode(', ', $pool) . PHP_EOL;

$loop->run();
Loading