smtp.php
2.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
require_once "Auth.php";
require_once "SmtpClient.php";
class ProxyService
{
    /**
     * 连接数
     * @var SmtpClient[]
     */
    protected static $clients = [];
    protected function push(...$params){
        echo co::getCid()." => ";
        echo implode(' => ',$params);
        return $params;
    }
    /**
     * @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->on('Connect', function ($server, $fd) {
            $server->send($fd, "220 proxy client ok\r\n");
        });
        //监听数据接收事件。
        $server->on('Receive', function ($server, $fd, $reactor_id, $data) {
//            echo "in ".co::getCid()." ==> ".$data."\n";
            // 建立连接
            if (empty(self::$clients[$fd])) {
                try {
                    $auth = new Auth($data);
                }catch (Throwable $e){
                    $server->send($fd, $e->getMessage());
                    $server->close($fd,true);
                    return;
                }
                // 创建一个客户端
                self::$clients[$fd] = new SmtpClient($auth->host);
                // 连接客户端
                try {
                    self::$clients[$fd]->open($auth->out_ip, $auth->timeOut);
                }catch (Throwable $e){
                    $server->send($fd, '500 ' . $e->getMessage()."\r\n");
                    $server->close($fd,true);
                    return;
                }
                $line = self::$clients[$fd]->readLine();
                if($line) $server->send($fd,$line);
                // 发送成功消息
//                $server->send($fd, "200 OK The proxy server is successfully connected.\r\n");
            } // 正式请求转发
            else {
                // 没有连接成功
                if(empty(self::$clients[$fd])){
                    $server->send($fd, "500 No proxy server.\r\n");
                    $server->close($fd,true);
                    return;
                }
                // 请求数据
                self::$clients[$fd]->write($data);
                $line = self::$clients[$fd]->readLine();
//                echo 'out '.co::getCid()." => ".$line;
                if($line!==false) $server->send($fd,$line);
            }
        });
        //监听连接关闭事件。
        $server->on('Close', function ($server, $fd) {
//            echo '连接关闭了 => '.$fd."\n";
            // 关闭并释放资源
            self::$clients[$fd] = null;
            unset(self::$clients[$fd]);
        });
        //启动服务器
        $server->start();
    }
}
(new ProxyService())->run();