smtp.php
2.1 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
<?php
require_once "Auth.php";
require_once "SmtpClient.php";
class ProxyService
{
/**
* 连接数
* @var SmtpClient[]
*/
private $clients = [];
protected function push($msg){
if(substr($msg,-2)!=="\r\n"){
$msg .= "\r\n";
}
// echo 'out '.$msg;
return $msg;
}
/**
* @author:dc
* @time 2025/3/29 14:34
*/
public function run()
{
//创建Server对象,监听 127.0.0.1:9501 端口。
$server = new Swoole\Server(
'0.0.0.0', 9527,
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 (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();
}
$client = $this->clients[$fd];
// 加锁
$client->lock();
// 处理数据
$result = $client->exec($data);
// 解锁
$client->unlock();
// 返回结果
$server->send($fd, $this->push($result[1]));
// 是否关闭连接
if($result[0]===false){
// 关闭并释放资源
$client->close();
$this->clients[$fd] = null;
unset($this->clients[$fd]);
$server->close($fd,true);
}
});
//监听连接关闭事件。
$server->on('Close', function ($server, $fd) {
echo '连接关闭了 => '.$fd."\n";
$this->clients[$fd] = null;
unset($this->clients[$fd]);
});
//启动服务器
$server->start();
}
}
(new ProxyService())->run();