-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_socks5.php
122 lines (106 loc) · 4.83 KB
/
start_socks5.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
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
<?php
require __DIR__ . '/vendor/autoload.php';
use Socks5\Protocols\Command\Command;
use Socks5\Protocols\Socks5;
use Socks5\Worker;
use Socks5\TcpConnection;
use Socks5\Protocols\Command\InitCommand;
use Socks5\Protocols\Command\Message\MessageClosed;
use Socks5\Protocols\Command\Message\MessageSock;
use Socks5\Connection\AsyncTcpConnection;
Worker::$onMasterStart = function () {
echo " ____ ____ _ ____
/ ___|_ _____ _ __ ___ __ _ _ __ / ___| ___ ___| | _____ | ___|
\___ \ \ /\ / / _ \| '_ ` _ \ / _` | '_ \ \___ \ / _ \ / __| |/ / __| |___ \
___) \ V V / (_) | | | | | | (_| | | | | ___) | (_) | (__| <\__ \ ___) |
|____/ \_/\_/ \___/|_| |_| |_|\__,_|_| |_| |____/ \___/ \___|_|\_\___/ |____/
\n{ Socks5 } 多进程、高性能Socks5代理服务器\n";
Worker::debug('Master started success.');
};
Worker::$processTitle = 'Socks5-Server';
$worker = new Worker('tcp://0.0.0.0:1090');
$worker->layerProtocol = Socks5::class;
$worker->user = 'meows';
$worker->group = 'meows';
$worker->count = 4;
$worker->onWorkerStart = function () {
Worker::debug('Worker started success.');
};
$worker->onConnect = function (TcpConnection $connection) {
if (!isset($connection->state)) {
$connection->state = InitCommand::COMMAND;
}
$connection->onSocks5Auth = function ($username, $password) {
return $password === 'pass';
};
};
$worker->onMessage = function (TcpConnection $connection, $message) {
Worker::debug($connection->getRemoteAddress() . " -> " . $message);
$message = json_decode($message, true);
if (!$message) {
$connection->close(
new MessageClosed(Command::COMMAND_SERVER_ERROR, '0.0.0.0', '0')
);
return;
}
// var_dump($message);
switch ($command = $message['command']) {
case 'CONNECT':
$proxyClient = new AsyncTcpConnection(
"tcp://" . $message["dest_address"] . ":" . $message["port"]
);
$proxyClient->onConnect = function (AsyncTcpConnection $proxyClient) use ($connection, $message) {
$connection->send(
new MessageSock(
Command::COMMAND_CONNECT_SUCCESS,
$connection->getRemoteHost(),
$connection->getRemotePort(),
(int)$message['addr_type'],
)
);
Worker::debug($connection->getRemoteAddress() . " -> 代理通道已连接资源主机 ".$proxyClient->getRemoteAddress());
$proxyClient->onMessage = function (AsyncTcpConnection $proxyClient, $data) use ($connection) {
$connection->send($data);
Worker::debug(
$proxyClient->getRemoteAddress() . " -> 资源主机转发流量到代理客户端 " . $connection->getRemoteAddress().'.'
);
};
$proxyClient->onBufferFull = function (AsyncTcpConnection $proxyClient) {
$proxyClient->pauseRecv();
};
$proxyClient->onBufferDrain = function (AsyncTcpConnection $proxyClient) {
$proxyClient->resumeRecv();
};
$connection->onMessage = function (TcpConnection $connection, $data) use ($proxyClient) {
$proxyClient->send($data);
Worker::debug($connection->getRemoteAddress()." -> 代理客户端流量转发到资源主机 ".$proxyClient->getRemoteAddress().".");
};
$connection->onBufferFull = function (TcpConnection $connection) {
$connection->pauseRecv();
};
$connection->onBufferDrain = function (TcpConnection $connection) {
$connection->resumeRecv();
};
$connection->onClose = function ($connection) use ($proxyClient) {
$proxyClient->close();
Worker::debug($proxyClient->getRemoteAddress() . " -> 关闭资源主机连接.");
};
$proxyClient->onClose = function ($proxyClient) use ($connection) {
$connection->close();
Worker::debug($connection->getRemoteAddress() . " -> 关闭代理通道.");
};
};
$proxyClient->connect();
break;
default:
Worker::debug("[ {$command} ] 未知命令.");
$connection->close(
new MessageClosed(Command::COMMAND_UNKNOWN, '0.0.0.0', '0')
);
break;
}
};
$worker->onClose = function () {
var_dump('关闭连接.');
};
$worker->start();