-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from clue-labs/connect
Support CONNECT method
- Loading branch information
Showing
6 changed files
with
316 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
use React\EventLoop\Factory; | ||
use React\Socket\Server; | ||
use React\Http\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
use React\SocketClient\TcpConnector; | ||
use React\SocketClient\ConnectionInterface; | ||
use React\SocketClient\DnsConnector; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = Factory::create(); | ||
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop); | ||
|
||
$resolver = new \React\Dns\Resolver\Factory(); | ||
$resolver = $resolver->create('8.8.8.8', $loop); | ||
$connector = new DnsConnector(new TcpConnector($loop), $resolver); | ||
|
||
$server = new \React\Http\Server($socket, function (RequestInterface $request) use ($connector) { | ||
if ($request->getMethod() !== 'CONNECT') { | ||
return new Response( | ||
405, | ||
array('Content-Type' => 'text/plain', 'Allow' => 'CONNECT'), | ||
'This is a HTTP CONNECT (secure HTTPS) proxy' | ||
); | ||
} | ||
|
||
// pause consuming request body | ||
$body = $request->getBody(); | ||
$body->pause(); | ||
|
||
$buffer = ''; | ||
$body->on('data', function ($chunk) use (&$buffer) { | ||
$buffer .= $chunk; | ||
}); | ||
|
||
// try to connect to given target host | ||
$promise = $connector->connect($request->getRequestTarget())->then( | ||
function (ConnectionInterface $remote) use ($body, &$buffer) { | ||
// connection established => forward data | ||
$body->pipe($remote); | ||
$body->resume(); | ||
|
||
if ($buffer !== '') { | ||
$remote->write($buffer); | ||
$buffer = ''; | ||
} | ||
|
||
return new Response( | ||
200, | ||
array(), | ||
$remote | ||
); | ||
}, | ||
function ($e) { | ||
return new Response( | ||
502, | ||
array('Content-Type' => 'text/plain'), | ||
'Unable to connect: ' . $e->getMessage() | ||
); | ||
} | ||
); | ||
|
||
// cancel pending connection if request closes prematurely | ||
$body->on('close', function () use ($promise) { | ||
$promise->cancel(); | ||
}); | ||
|
||
return $promise; | ||
}); | ||
|
||
//$server->on('error', 'printf'); | ||
|
||
echo 'Listening on http://' . $socket->getAddress() . PHP_EOL; | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.