正在显示
3 个修改的文件
包含
150 行增加
和
65 行删除
| @@ -29,9 +29,13 @@ class SyncToEsCmd { | @@ -29,9 +29,13 @@ class SyncToEsCmd { | ||
| 29 | 29 | ||
| 30 | $es = es(); | 30 | $es = es(); |
| 31 | $db = db(); | 31 | $db = db(); |
| 32 | - | 32 | + $startTime = time(); |
| 33 | while (1){ | 33 | while (1){ |
| 34 | 34 | ||
| 35 | + if(time()-43200 > $startTime){ | ||
| 36 | + break; | ||
| 37 | + } | ||
| 38 | + | ||
| 35 | // 检查是否接收到信号 | 39 | // 检查是否接收到信号 |
| 36 | pcntl_signal_dispatch(); | 40 | pcntl_signal_dispatch(); |
| 37 | 41 |
| @@ -16,9 +16,112 @@ class SmtpClient{ | @@ -16,9 +16,112 @@ class SmtpClient{ | ||
| 16 | */ | 16 | */ |
| 17 | protected $client; | 17 | protected $client; |
| 18 | 18 | ||
| 19 | - public function __construct(string $host) | 19 | + /** |
| 20 | + * 是否验证了 | ||
| 21 | + */ | ||
| 22 | + public $isAuth = false; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * 协程锁 | ||
| 26 | + * @var bool | ||
| 27 | + */ | ||
| 28 | + private bool $_lock = false; | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + public function __construct() | ||
| 20 | { | 32 | { |
| 21 | - $this->host = $host; | 33 | + |
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * 执行 | ||
| 38 | + * @param $data | ||
| 39 | + * @return array | ||
| 40 | + * @author:dc | ||
| 41 | + * @time 2025/4/17 9:03 | ||
| 42 | + */ | ||
| 43 | + public function exec($data):array { | ||
| 44 | + if (!$this->isAuth) { | ||
| 45 | + | ||
| 46 | + try { | ||
| 47 | + $auth = new Auth($data); | ||
| 48 | + }catch (Throwable $e){ | ||
| 49 | + return [false,$e->getMessage()]; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + // 连接客户端 | ||
| 53 | + try { | ||
| 54 | + $this->open($auth->host,$auth->out_ip, $auth->timeOut); | ||
| 55 | + }catch (Throwable $e){ | ||
| 56 | + return [false,'500 ' . $e->getMessage()."\r\n"]; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + $line = $this->readLine(); | ||
| 60 | + | ||
| 61 | + return [true,$line]; | ||
| 62 | + | ||
| 63 | + } // 正式请求转发 | ||
| 64 | + else { | ||
| 65 | + // 没有连接成功 | ||
| 66 | + if(!$this->isAuth){ | ||
| 67 | + return [false,"500 No proxy server.\r\n"]; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + $line = false; | ||
| 71 | + try { | ||
| 72 | + // 请求数据 | ||
| 73 | + // $num = 0; | ||
| 74 | + // foreach (explode("\r\n",$data) as $cmd){ | ||
| 75 | + // if(strlen($cmd) > 0){ | ||
| 76 | + // if($n = self::$clients[$fd]->write($cmd."\r\n")){ | ||
| 77 | + // $num += $n; | ||
| 78 | + // } | ||
| 79 | + // } | ||
| 80 | + | ||
| 81 | + // } | ||
| 82 | + $num = $this->write($data); | ||
| 83 | + | ||
| 84 | + if($num) $line = $this->readLine(); | ||
| 85 | + | ||
| 86 | + }catch (Throwable $e){ | ||
| 87 | + $line = '500 server error '.$e->getMessage()."\r\n"; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + if($line===false){ | ||
| 91 | + $line = "500 server error 2 \r\n"; | ||
| 92 | + } | ||
| 93 | + if(!$line&&$data==".\r\n"){ | ||
| 94 | + $line = "250 Mail OK ok \r\n"; | ||
| 95 | + } | ||
| 96 | + if(empty($line)){ | ||
| 97 | + $line = "250 Mail OK no reply \r\n"; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + return [true,$line]; | ||
| 101 | + | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * 排他锁 | ||
| 107 | + * @return bool | ||
| 108 | + */ | ||
| 109 | + public function lock():void | ||
| 110 | + { | ||
| 111 | + while ($this->_lock){ | ||
| 112 | + co::sleep(0.1); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + $this->_lock = true; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + /** | ||
| 119 | + * 解锁 | ||
| 120 | + * @author:dc | ||
| 121 | + * @time 2025/4/17 9:09 | ||
| 122 | + */ | ||
| 123 | + public function unlock(){ | ||
| 124 | + $this->_lock = false; | ||
| 22 | } | 125 | } |
| 23 | 126 | ||
| 24 | /** | 127 | /** |
| @@ -29,7 +132,8 @@ class SmtpClient{ | @@ -29,7 +132,8 @@ class SmtpClient{ | ||
| 29 | * @author:dc | 132 | * @author:dc |
| 30 | * @time 2025/3/31 10:27 | 133 | * @time 2025/3/31 10:27 |
| 31 | */ | 134 | */ |
| 32 | - public function open(string $out_ip, int $timeout=5){ | 135 | + public function open(string $host, string $out_ip, int $timeout=5){ |
| 136 | + $this->host = $host; | ||
| 33 | $client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP | SWOOLE_SSL); | 137 | $client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP | SWOOLE_SSL); |
| 34 | $client->set([ | 138 | $client->set([ |
| 35 | 'timeout'=> $timeout, | 139 | 'timeout'=> $timeout, |
| @@ -46,6 +150,7 @@ class SmtpClient{ | @@ -46,6 +150,7 @@ class SmtpClient{ | ||
| 46 | 150 | ||
| 47 | if($client->isConnected()){ | 151 | if($client->isConnected()){ |
| 48 | $this->client = $client; | 152 | $this->client = $client; |
| 153 | + $this->isAuth = true; | ||
| 49 | }else{ | 154 | }else{ |
| 50 | throw new Exception($this->host." connection fail. "); | 155 | throw new Exception($this->host." connection fail. "); |
| 51 | } | 156 | } |
| @@ -79,7 +184,7 @@ class SmtpClient{ | @@ -79,7 +184,7 @@ class SmtpClient{ | ||
| 79 | */ | 184 | */ |
| 80 | public function readLine($timeout=5){ | 185 | public function readLine($timeout=5){ |
| 81 | if($this->is_read === 2){ | 186 | if($this->is_read === 2){ |
| 82 | - return '220 DATA OK'; | 187 | + return "250 DATA OK\r\n"; |
| 83 | } | 188 | } |
| 84 | 189 | ||
| 85 | if($this->is_read === 1){ | 190 | if($this->is_read === 1){ |
| @@ -87,11 +192,11 @@ class SmtpClient{ | @@ -87,11 +192,11 @@ class SmtpClient{ | ||
| 87 | } | 192 | } |
| 88 | $online = $this->client->recv($timeout); | 193 | $online = $this->client->recv($timeout); |
| 89 | 194 | ||
| 90 | - return $online===false ? '500 read time out.' : $online; | 195 | + return $online===false ? "500 read time out.\r\n" : $online; |
| 91 | } | 196 | } |
| 92 | 197 | ||
| 93 | 198 | ||
| 94 | - public function __destruct() | 199 | + public function close() |
| 95 | { | 200 | { |
| 96 | if(!empty($this->client)) $this->client->close(); | 201 | if(!empty($this->client)) $this->client->close(); |
| 97 | unset($this->client); | 202 | unset($this->client); |
| @@ -11,11 +11,15 @@ class ProxyService | @@ -11,11 +11,15 @@ class ProxyService | ||
| 11 | * 连接数 | 11 | * 连接数 |
| 12 | * @var SmtpClient[] | 12 | * @var SmtpClient[] |
| 13 | */ | 13 | */ |
| 14 | - protected static $clients = []; | 14 | + private $clients = []; |
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | protected function push($msg){ | 17 | protected function push($msg){ |
| 18 | - echo 'out '.$msg; | 18 | + |
| 19 | + if(substr($msg,-2)!=="\r\n"){ | ||
| 20 | + $msg .= "\r\n"; | ||
| 21 | + } | ||
| 22 | + // echo 'out '.$msg; | ||
| 19 | return $msg; | 23 | return $msg; |
| 20 | } | 24 | } |
| 21 | 25 | ||
| @@ -31,73 +35,45 @@ class ProxyService | @@ -31,73 +35,45 @@ class ProxyService | ||
| 31 | SWOOLE_BASE, | 35 | SWOOLE_BASE, |
| 32 | SWOOLE_SOCK_TCP//|SWOOLE_SSL | 36 | SWOOLE_SOCK_TCP//|SWOOLE_SSL |
| 33 | ); | 37 | ); |
| 34 | - | 38 | + $server->set([ |
| 39 | + 'dispatch_mode' => 2 // 固定模式 | ||
| 40 | + ]); | ||
| 35 | //监听连接进入事件。 | 41 | //监听连接进入事件。 |
| 36 | $server->on('Connect', function ($server, $fd) { | 42 | $server->on('Connect', function ($server, $fd) { |
| 43 | + | ||
| 37 | $server->send($fd, $this->push("220 proxy client ok\r\n")); | 44 | $server->send($fd, $this->push("220 proxy client ok\r\n")); |
| 38 | }); | 45 | }); |
| 39 | 46 | ||
| 40 | //监听数据接收事件。 | 47 | //监听数据接收事件。 |
| 41 | - $server->on('Receive', function ($server, $fd, $reactor_id, $data) { | ||
| 42 | - echo "in > ".$data; | ||
| 43 | - // 建立连接 | ||
| 44 | - if (empty(self::$clients[$fd])) { | ||
| 45 | - | ||
| 46 | - try { | ||
| 47 | - $auth = new Auth($data); | ||
| 48 | - }catch (Throwable $e){ | ||
| 49 | - $server->send($fd, $this->push($e->getMessage())); | ||
| 50 | - $server->close($fd,true); | ||
| 51 | - return; | ||
| 52 | - } | ||
| 53 | - // 创建一个客户端 | ||
| 54 | - self::$clients[$fd] = new SmtpClient($auth->host); | ||
| 55 | - // 连接客户端 | ||
| 56 | - try { | ||
| 57 | - self::$clients[$fd]->open($auth->out_ip, $auth->timeOut); | ||
| 58 | - }catch (Throwable $e){ | ||
| 59 | - $server->send($fd,$this->push('500 ' . $e->getMessage()."\r\n")); | ||
| 60 | - $server->close($fd,true); | ||
| 61 | - return; | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - $line = self::$clients[$fd]->readLine(1); | ||
| 65 | - | ||
| 66 | - $server->send($fd,$this->push($line)); | ||
| 67 | - | ||
| 68 | - } // 正式请求转发 | ||
| 69 | - else { | ||
| 70 | - // 没有连接成功 | ||
| 71 | - if(empty(self::$clients[$fd])){ | ||
| 72 | - $server->send($fd, $this->push("500 No proxy server.\r\n")); | ||
| 73 | - $server->close($fd,true); | ||
| 74 | - return; | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - $line = false; | ||
| 78 | - try { | ||
| 79 | - // 请求数据 | ||
| 80 | - $num = self::$clients[$fd]->write($data); | ||
| 81 | - | ||
| 82 | - if($num) $line = self::$clients[$fd]->readLine(); | ||
| 83 | - | ||
| 84 | - }catch (Throwable $e){ | ||
| 85 | - $line = '500 server error '.$e->getMessage()."\r\n"; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | -// echo 'out '.co::getCid()." => ".$line; | ||
| 89 | - | ||
| 90 | - if($line!==false) $server->send($fd,$this->push($line)); | ||
| 91 | - | 48 | + $server->on('Receive', function (Swoole\Server $server, $fd, $reactor_id, $data) { |
| 49 | + // echo "in ".rand(10,99)." > ".$data; | ||
| 50 | +// $ridfid = $reactor_id.'_'.$fd; | ||
| 51 | + if(empty($this->clients[$fd])){ | ||
| 52 | + $this->clients[$fd] = new SmtpClient(); | ||
| 53 | + } | ||
| 54 | + // 加锁 | ||
| 55 | + $this->clients[$fd]->lock(); | ||
| 56 | + // 处理数据 | ||
| 57 | + $result = $this->clients[$fd]->exec($data); | ||
| 58 | + // 解锁 | ||
| 59 | + $this->clients[$fd]->unlock(); | ||
| 60 | + // 返回结果 | ||
| 61 | + $server->send($fd, $this->push($result[1])); | ||
| 62 | + // 是否关闭连接 | ||
| 63 | + if($result[0]===false){ | ||
| 64 | + // 关闭并释放资源 | ||
| 65 | + $this->clients[$fd]->close(); | ||
| 66 | + $this->clients[$fd] = null; | ||
| 67 | + unset($this->clients[$fd]); | ||
| 68 | + | ||
| 69 | + $server->close($fd,true); | ||
| 92 | } | 70 | } |
| 93 | }); | 71 | }); |
| 94 | 72 | ||
| 95 | //监听连接关闭事件。 | 73 | //监听连接关闭事件。 |
| 96 | $server->on('Close', function ($server, $fd) { | 74 | $server->on('Close', function ($server, $fd) { |
| 97 | -// echo '连接关闭了 => '.$fd."\n"; | ||
| 98 | - // 关闭并释放资源 | ||
| 99 | - self::$clients[$fd] = null; | ||
| 100 | - unset(self::$clients[$fd]); | 75 | + echo '连接关闭了 => '.$fd."\n"; |
| 76 | + unset($this->clients[$fd]); | ||
| 101 | }); | 77 | }); |
| 102 | 78 | ||
| 103 | //启动服务器 | 79 | //启动服务器 |
-
请 注册 或 登录 后发表评论