作者 邓超

smtp

... ... @@ -1185,6 +1185,10 @@ class SMTP
$this->edebug('CLIENT -> SERVER: ' . $data, self::DEBUG_CLIENT);
}
set_error_handler([$this, 'errorHandler']);
if($this->proxy_server){
// 代理服务器的特殊结束符号
$data .="\r:smtp:\r";
}
$result = fwrite($this->smtp_conn, $data);
restore_error_handler();
... ...
... ... @@ -23,6 +23,8 @@ class ProxyService
return $msg;
}
public $eof = "\r:smtp:\r";
/**
* @author:dc
* @time 2025/3/29 14:34
... ... @@ -36,7 +38,9 @@ class ProxyService
SWOOLE_SOCK_TCP//|SWOOLE_SSL
);
$server->set([
'dispatch_mode' => 2 // 固定模式
'dispatch_mode' => 2, // 固定模式
'open_eof_split' => true,
'package_eof' => $this->eof,
]);
//监听连接进入事件。
$server->on('Connect', function ($server, $fd) {
... ... @@ -46,30 +50,37 @@ class ProxyService
//监听数据接收事件。
$server->on('Receive', function (Swoole\Server $server, $fd, $reactor_id, $data) {
// 替换掉自定义的结束符号
$data = str_replace($this->eof,'',$data);
// echo "in ".rand(10,99)." > ".$data;
// $ridfid = $reactor_id.'_'.$fd;
if(empty($this->clients[$fd])){
$this->clients[$fd] = new SmtpClient();
}
$client = $this->clients[$fd];
try {
// $client = $this->clients[$fd];
// 加锁
$client->lock();
$this->clients[$fd]->lock();
// 处理数据
$result = $client->exec($data);
$result = $this->clients[$fd]->exec($data);
// 解锁
$client->unlock();
$this->clients[$fd]->unlock();
// 返回结果
$server->send($fd, $this->push($result[1]));
// 是否关闭连接
if($result[0]===false){
// 关闭并释放资源
$client->close();
$this->clients[$fd]->close();
$this->clients[$fd] = null;
unset($this->clients[$fd]);
$server->close($fd,true);
}
}catch (Throwable $e){
echo $e->getMessage()."\n";
}
});
//监听连接关闭事件。
... ...