...
|
...
|
@@ -11,11 +11,15 @@ class ProxyService |
|
|
* 连接数
|
|
|
* @var SmtpClient[]
|
|
|
*/
|
|
|
protected static $clients = [];
|
|
|
private $clients = [];
|
|
|
|
|
|
|
|
|
protected function push($msg){
|
|
|
echo 'out '.$msg;
|
|
|
|
|
|
if(substr($msg,-2)!=="\r\n"){
|
|
|
$msg .= "\r\n";
|
|
|
}
|
|
|
// echo 'out '.$msg;
|
|
|
return $msg;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -31,73 +35,45 @@ class ProxyService |
|
|
SWOOLE_BASE,
|
|
|
SWOOLE_SOCK_TCP//|SWOOLE_SSL
|
|
|
);
|
|
|
|
|
|
$server->set([
|
|
|
'dispatch_mode' => 2 // 固定模式
|
|
|
]);
|
|
|
//监听连接进入事件。
|
|
|
$server->on('Connect', function ($server, $fd) {
|
|
|
|
|
|
$server->send($fd, $this->push("220 proxy client ok\r\n"));
|
|
|
});
|
|
|
|
|
|
//监听数据接收事件。
|
|
|
$server->on('Receive', function ($server, $fd, $reactor_id, $data) {
|
|
|
echo "in > ".$data;
|
|
|
// 建立连接
|
|
|
if (empty(self::$clients[$fd])) {
|
|
|
|
|
|
try {
|
|
|
$auth = new Auth($data);
|
|
|
}catch (Throwable $e){
|
|
|
$server->send($fd, $this->push($e->getMessage()));
|
|
|
$server->close($fd,true);
|
|
|
return;
|
|
|
$server->on('Receive', function (Swoole\Server $server, $fd, $reactor_id, $data) {
|
|
|
// echo "in ".rand(10,99)." > ".$data;
|
|
|
// $ridfid = $reactor_id.'_'.$fd;
|
|
|
if(empty($this->clients[$fd])){
|
|
|
$this->clients[$fd] = new SmtpClient();
|
|
|
}
|
|
|
// 创建一个客户端
|
|
|
self::$clients[$fd] = new SmtpClient($auth->host);
|
|
|
// 连接客户端
|
|
|
try {
|
|
|
self::$clients[$fd]->open($auth->out_ip, $auth->timeOut);
|
|
|
}catch (Throwable $e){
|
|
|
$server->send($fd,$this->push('500 ' . $e->getMessage()."\r\n"));
|
|
|
$server->close($fd,true);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
$line = self::$clients[$fd]->readLine(1);
|
|
|
|
|
|
$server->send($fd,$this->push($line));
|
|
|
// 加锁
|
|
|
$this->clients[$fd]->lock();
|
|
|
// 处理数据
|
|
|
$result = $this->clients[$fd]->exec($data);
|
|
|
// 解锁
|
|
|
$this->clients[$fd]->unlock();
|
|
|
// 返回结果
|
|
|
$server->send($fd, $this->push($result[1]));
|
|
|
// 是否关闭连接
|
|
|
if($result[0]===false){
|
|
|
// 关闭并释放资源
|
|
|
$this->clients[$fd]->close();
|
|
|
$this->clients[$fd] = null;
|
|
|
unset($this->clients[$fd]);
|
|
|
|
|
|
} // 正式请求转发
|
|
|
else {
|
|
|
// 没有连接成功
|
|
|
if(empty(self::$clients[$fd])){
|
|
|
$server->send($fd, $this->push("500 No proxy server.\r\n"));
|
|
|
$server->close($fd,true);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
$line = false;
|
|
|
try {
|
|
|
// 请求数据
|
|
|
$num = self::$clients[$fd]->write($data);
|
|
|
|
|
|
if($num) $line = self::$clients[$fd]->readLine();
|
|
|
|
|
|
}catch (Throwable $e){
|
|
|
$line = '500 server error '.$e->getMessage()."\r\n";
|
|
|
}
|
|
|
|
|
|
// echo 'out '.co::getCid()." => ".$line;
|
|
|
|
|
|
if($line!==false) $server->send($fd,$this->push($line));
|
|
|
|
|
|
}
|
|
|
});
|
|
|
|
|
|
//监听连接关闭事件。
|
|
|
$server->on('Close', function ($server, $fd) {
|
|
|
// echo '连接关闭了 => '.$fd."\n";
|
|
|
// 关闭并释放资源
|
|
|
self::$clients[$fd] = null;
|
|
|
unset(self::$clients[$fd]);
|
|
|
echo '连接关闭了 => '.$fd."\n";
|
|
|
unset($this->clients[$fd]);
|
|
|
});
|
|
|
|
|
|
//启动服务器
|
...
|
...
|
|