-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocks5h.php
More file actions
140 lines (116 loc) · 3.47 KB
/
socks5h.php
File metadata and controls
140 lines (116 loc) · 3.47 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* SOCKS5 proxy with request + traffic logging
* Works with Composer, curl, browsers
* PHP 8+
*/
set_time_limit(0);
error_reporting(E_ALL);
$LISTEN_ADDR = '127.0.0.1';
$LISTEN_PORT = 10080;
$LOG_FILE = __DIR__ . '/logs/socks.log';
/* -------------------- LOGGER -------------------- */
function log_line(string $msg): void
{
global $LOG_FILE;
file_put_contents(
$LOG_FILE,
'[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL,
FILE_APPEND | LOCK_EX
);
}
/* -------------------- SERVER -------------------- */
$server = stream_socket_server(
"tcp://{$LISTEN_ADDR}:{$LISTEN_PORT}",
$errno,
$errstr
);
if (!$server) {
die("Server error: $errstr\n");
}
echo "✅ SOCKS5 proxy listening on {$LISTEN_ADDR}:{$LISTEN_PORT}\n";
log_line("SERVER started on {$LISTEN_ADDR}:{$LISTEN_PORT}");
/* -------------------- MAIN LOOP -------------------- */
while ($client = stream_socket_accept($server)) {
$peer = stream_socket_get_name($client, true);
log_line("CLIENT connected from {$peer}");
/* ---- 1) GREETING ---- */
$greeting = fread($client, 262);
if (!$greeting || ord($greeting[0]) !== 0x05) {
log_line("ERROR invalid SOCKS version");
fclose($client);
continue;
}
// No authentication
fwrite($client, "\x05\x00");
/* ---- 2) REQUEST ---- */
$request = fread($client, 262);
if (!$request || ord($request[1]) !== 0x01) {
log_line("ERROR unsupported command");
fclose($client);
continue;
}
$atype = ord($request[3]);
$offset = 4;
if ($atype === 0x01) { // IPv4
$addr = inet_ntop(substr($request, $offset, 4));
$offset += 4;
} elseif ($atype === 0x03) { // DOMAIN
$len = ord($request[$offset]);
$offset++;
$addr = substr($request, $offset, $len);
$offset += $len;
} else {
log_line("ERROR address type not supported");
fclose($client);
continue;
}
$port = unpack('n', substr($request, $offset, 2))[1];
log_line("CONNECT {$addr}:{$port}");
/* ---- 3) CONNECT TARGET ---- */
$remote = @stream_socket_client(
"tcp://{$addr}:{$port}",
$e,
$es,
10
);
if (!$remote) {
log_line("ERROR connect failed {$addr}:{$port}");
fwrite($client, "\x05\x01\x00\x01\0\0\0\0\0\0");
fclose($client);
continue;
}
// Success reply
fwrite($client, "\x05\x00\x00\x01\0\0\0\0\0\0");
/* ---- 4) TUNNEL + LOGGING ---- */
stream_set_blocking($client, false);
stream_set_blocking($remote, false);
$bytesUp = 0;
$bytesDown = 0;
$write=null;
$ecept=null;
$start = microtime(true);
while (!feof($client) && !feof($remote)) {
$read = [$client, $remote];
if (stream_select($read, $write, $ecept, 1) === false) {
break;
}
foreach ($read as $sock) {
$data = fread($sock, 8192);
if ($data === '' || $data === false) {
continue;
}
if ($sock === $client) {
$bytesUp += strlen($data);
fwrite($remote, $data);
} else {
$bytesDown += strlen($data);
fwrite($client, $data);
}
}
}
$duration = round(microtime(true) - $start, 3);
log_line("DONE {$addr}:{$port} ↑{$bytesUp}B ↓{$bytesDown}B {$duration}s");
fclose($client);
fclose($remote);
}