作者 邓超

smtp

@@ -1185,6 +1185,10 @@ class SMTP @@ -1185,6 +1185,10 @@ class SMTP
1185 $this->edebug('CLIENT -> SERVER: ' . $data, self::DEBUG_CLIENT); 1185 $this->edebug('CLIENT -> SERVER: ' . $data, self::DEBUG_CLIENT);
1186 } 1186 }
1187 set_error_handler([$this, 'errorHandler']); 1187 set_error_handler([$this, 'errorHandler']);
  1188 + if($this->proxy_server){
  1189 + // 代理服务器的特殊结束符号
  1190 + $data .="\r:smtp:\r";
  1191 + }
1188 $result = fwrite($this->smtp_conn, $data); 1192 $result = fwrite($this->smtp_conn, $data);
1189 restore_error_handler(); 1193 restore_error_handler();
1190 1194
@@ -23,6 +23,8 @@ class ProxyService @@ -23,6 +23,8 @@ class ProxyService
23 return $msg; 23 return $msg;
24 } 24 }
25 25
  26 + public $eof = "\r:smtp:\r";
  27 +
26 /** 28 /**
27 * @author:dc 29 * @author:dc
28 * @time 2025/3/29 14:34 30 * @time 2025/3/29 14:34
@@ -36,7 +38,9 @@ class ProxyService @@ -36,7 +38,9 @@ class ProxyService
36 SWOOLE_SOCK_TCP//|SWOOLE_SSL 38 SWOOLE_SOCK_TCP//|SWOOLE_SSL
37 ); 39 );
38 $server->set([ 40 $server->set([
39 - 'dispatch_mode' => 2 // 固定模式 41 + 'dispatch_mode' => 2, // 固定模式
  42 + 'open_eof_split' => true,
  43 + 'package_eof' => $this->eof,
40 ]); 44 ]);
41 //监听连接进入事件。 45 //监听连接进入事件。
42 $server->on('Connect', function ($server, $fd) { 46 $server->on('Connect', function ($server, $fd) {
@@ -46,30 +50,37 @@ class ProxyService @@ -46,30 +50,37 @@ class ProxyService
46 50
47 //监听数据接收事件。 51 //监听数据接收事件。
48 $server->on('Receive', function (Swoole\Server $server, $fd, $reactor_id, $data) { 52 $server->on('Receive', function (Swoole\Server $server, $fd, $reactor_id, $data) {
  53 + // 替换掉自定义的结束符号
  54 + $data = str_replace($this->eof,'',$data);
49 // echo "in ".rand(10,99)." > ".$data; 55 // echo "in ".rand(10,99)." > ".$data;
50 // $ridfid = $reactor_id.'_'.$fd; 56 // $ridfid = $reactor_id.'_'.$fd;
51 if(empty($this->clients[$fd])){ 57 if(empty($this->clients[$fd])){
52 $this->clients[$fd] = new SmtpClient(); 58 $this->clients[$fd] = new SmtpClient();
53 } 59 }
54 60
55 - $client = $this->clients[$fd]; 61 + try {
  62 + // $client = $this->clients[$fd];
56 // 加锁 63 // 加锁
57 - $client->lock(); 64 + $this->clients[$fd]->lock();
58 // 处理数据 65 // 处理数据
59 - $result = $client->exec($data); 66 + $result = $this->clients[$fd]->exec($data);
60 // 解锁 67 // 解锁
61 - $client->unlock(); 68 + $this->clients[$fd]->unlock();
62 // 返回结果 69 // 返回结果
63 $server->send($fd, $this->push($result[1])); 70 $server->send($fd, $this->push($result[1]));
64 // 是否关闭连接 71 // 是否关闭连接
65 if($result[0]===false){ 72 if($result[0]===false){
66 // 关闭并释放资源 73 // 关闭并释放资源
67 - $client->close(); 74 + $this->clients[$fd]->close();
68 $this->clients[$fd] = null; 75 $this->clients[$fd] = null;
69 unset($this->clients[$fd]); 76 unset($this->clients[$fd]);
70 77
71 $server->close($fd,true); 78 $server->close($fd,true);
72 } 79 }
  80 +
  81 + }catch (Throwable $e){
  82 + echo $e->getMessage()."\n";
  83 + }
73 }); 84 });
74 85
75 //监听连接关闭事件。 86 //监听连接关闭事件。