-
Notifications
You must be signed in to change notification settings - Fork 16
/
example.php
74 lines (57 loc) · 1.71 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
// debug
error_reporting(E_ALL);
ini_set('display_errors', true);
require 'src/Weheartwebsites/autoload.php';
use Weheartwebsites\SOCKS5\Client as SOCKS5Client;
use Weheartwebsites\SOCKS5\Methods\None as AuthNone;
$socks_client = new SOCKS5Client('127.0.0.1');
$socks_client->addMethod(new AuthNone);
//$socks_client->setTunnelDNS(true);
$request = [
'GET / HTTP/1.1',
'Host: curlmyip.com',
'Connection: close',
];
try {
$socks_client->connect();
$socks_client->connectTo('curlmyip.com', 80);
var_dump(fwrite($socks_client->socket, implode("\r\n", $request) . "\r\n\r\n"));
$head = $body = $buffer = null;
$is_header = true;
// I know this is shitty, but just for a quick demonstration....
while (true) {
$buffer = fgets($socks_client->socket);
if ($buffer === false) {
usleep(200000);
continue;
}
if ($is_header) {
$head .= $buffer;
if (substr($head, -4) == "\r\n\r\n") {
$is_header = false;
// get rest bytes
$head = rtrim(str_replace("\r\n", "\n", $head));
if (!preg_match('/^content\-length:\s*(\d+)$/im', $head, $matches)) {
echo "error";
break;
}
$rest = (int) $matches[1];
}
} else {
$body .= $buffer;
$rest -= strlen($buffer);
if ($rest <= 0) {
$body = rtrim($body);
break;
}
}
}
var_dump($head);
var_dump($body);
unset($socks_client);
} catch (Exception $e) {
unset($socks_client);
echo $e->getMessage() . PHP_EOL;
exit(1);
}