You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* SMTP服务器入口文件
*/
require_once __DIR__ . '/SmtpServer.php';
require_once __DIR__ . '/../utils/Config.php';
require_once __DIR__ . '/../utils/Logger.php';
// 解析命令行参数
$options = getopt("h:p:m:", ["host:", "port:", "max-connections:", "help"]);
// 显示帮助信息
if (isset($options['help'])) {
echo "SMTP Server Usage:\n";
echo "php index.php [options]\n";
echo "Options:\n";
echo " -h, --host <host> Listen host (default: 0.0.0.0)\n";
echo " -p, --port <port> Listen port (default: 25)\n";
echo " -m, --max-connections <num> Maximum connections (default: 100)\n";
echo " --help Show this help message\n";
exit(0);
}
// 从配置文件获取默认值
$config = Config::getInstance();
$host = $options['h'] ?? $options['host'] ?? $config->get('smtp.host', '0.0.0.0');
$port = $options['p'] ?? $options['port'] ?? $config->get('smtp.port', 25);
$maxConnections = $options['m'] ?? $options['max-connections'] ?? $config->get('smtp.max_connections', 100);
// 转换端口和最大连接数为整数
$port = (int)$port;
$maxConnections = (int)$maxConnections;
// 验证参数
if ($port < 1 || $port > 65535) {
echo "Invalid port number: $port\n";
exit(1);
}
if ($maxConnections < 1 || $maxConnections > 1000) {
echo "Invalid maximum connections: $maxConnections\n";
exit(1);
}
// 创建并启动SMTP服务器
$server = new SmtpServer($host, $port, $maxConnections);
echo "Starting SMTP Server on $host:$port...\n";
echo "Maximum connections: $maxConnections\n";
echo "Press Ctrl+C to stop the server\n\n";
// 捕获Ctrl+C信号优雅关闭服务器仅在支持PCNTL的系统上
if (function_exists('pcntl_signal')) {
declare(ticks = 1);
pcntl_signal(SIGINT, function() use ($server) {
echo "\nStopping SMTP Server...\n";
$server->stop();
exit(0);
});
}
// 启动服务器
if (!$server->start()) {
echo "Failed to start SMTP Server\n";
exit(1);
}