clue / php-socks

[maintenance] look at clue/socks-react instead
MIT License
62 stars 11 forks source link

Improve examples #4

Closed clue closed 10 years ago

clue commented 10 years ago

The current set of examples is a bit convoluted and not particularly straightforward. The examples should probably be part of the test suite instead.

Each of the server examples is blocking and hence should be run from an independent terminal:

$ php examples/server.php
$ php examples/server-middleman.php
$ php examples/client.php

Instead, I'd like to provide some simpler examples.

cursedcoder commented 10 years ago

This didn't work with custom proxies and these I found on free proxy lists, although they work with curl and that lib https://github.com/maxia/php-socks5socket.

<?php

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

use React\HttpClient\Client as HttpClient;

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

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$factory = new Socks\Factory($loop, $dns);

$client = $factory->createClient('custom_proxy', 1080);
$client->setTimeout(3.0);
$client->setResolveLocal(false);
$client->setProtocolVersion(5);
$client->setAuth('usr', 'pwd');

$httpClient = $client->createHttpClient();

$request = $httpClient->request('GET', 'http://wtfismyip.com/text', array('user-agent'=>'none'));
$request->on('response', function (Response $response) {
    //var_dump($response->getHeaders());
    $response->on('data', function ($data) {
        echo $data;
    });
});
$request->end();
clue commented 10 years ago

Thanks for stopping by and providing your script. The one thing that's missing from your example is actually starting the event loop. Without it, it won't do much at all :) So make sure the following call is placed at the end:

$loop->run();

With this line in place, the above script works just fine (tested both master and latest tag v0.4.0). I have tested this against a number of SOCKS proxy servers and am not aware of any limitations.

This didn't work…

Perhaps you can elaborate on what exactly didn't work? :) Possibly might be related to your public proxy list being outdated or restricted?

cursedcoder commented 10 years ago

Yeah, I've missed loop->run while copying. As I have said my custom proxy worked at the same time with cURL and socks5socket lib. To ensure that it doesn't work I've tested also some public proxies too.

Given above example will out in an empty screen.

cursedcoder commented 10 years ago

my log of running examples suite https://gist.github.com/cursedcoder/9890536

cursedcoder commented 10 years ago

interesting, running this on ubuntu works :wolf: so the issue is in MacOS incompatibility

clue commented 10 years ago
$ php examples/server.php &
[1] 51255
…
$ php examples/client.php
Demo SOCKS client connecting to SOCKS server 127.0.0.1:9051
FAIL: connection to www.google.com:80 failed: message: Unable to connect to socks server
previous: Unable to connect to socks server
previous: Connection refused
…

The provided gist seems to indicate you've suspended the execution of the server by running it in the background (…/server.php &). I'm not sure how much influence the platform has on this behavior :)

Could you check running both servers in the foreground? Otherwise, you may also use your SSH client to open a (temporary) SOCKS server to any machine you have access to (including your localhost):

$ ssh -D 9051 localhost
cursedcoder commented 10 years ago

@clue same behavior running it in foreground.

But it works on virtual Ubuntu 13.10. And my example mentioned above works here too.

I will try to find the difference between my php packages.

cursedcoder commented 10 years ago

@clue okay, found it.

When I use LibEvent loop it fails, but it works with StreamSelectLoop

p.s. LibEvLoop same issue as with LibEvent

clue commented 10 years ago

Alright, thanks for keeping me posted! Unfortunately I don't have a Mac to test against.

When I use LibEvent loop it fails, but it works with StreamSelectLoop

Now that's really unexpected behavior, but I'll look into this later.

It might also be related to IPv6/IPv4 sockets. The servers bind to localhost:9050 and localhost:9051 respectively, while the client connects to 127.0.0.1:9051. The output seems to indicate the servers started just fine and yet the client is unable to connect to the server.

This can be caused if your system maps localhost to the IPv6 loopback address (::1) instead of the IPv4 one (127.0.0.1) which the client always uses. You could either modify your /etc/hosts or simply adjust the examples to always use 127.0.0.1 instead. See also http://superuser.com/questions/30827/safari-is-unable-to-reach-localhost-127-0-0-1.

clue commented 10 years ago

Will be resolved as part of clue/reactphp-socks#3