continuous-integration/drone/pr Build is passing
Один LoginGuard на все проверки пароля (SMTP AUTH, IMAP LOGIN/AUTHENTICATE, вход в веб, смена пароля): 10 неверных с IP за 15 мин → блок 15 мин, дальше вдвое дольше до суток; 20 неверных на ящик с разных IP → ящик закрыт, кроме IP, с которых в него уже входили. Во время блока пароль не проверяется (SMTP 421, IMAP BYE + NO [UNAVAILABLE], веб 429 + Retry-After); неверный пароль — ответ через 1 с; 3 неверных в соединении — обрыв. X-Forwarded-For веб-входа — только от прокси в частной сети, иначе заголовок отбрасывается (подменой IP лимит не обойти). Попутно: IMAP терял тегированный ответ после "* BYE" (LOGOUT, обрыв после неудачных входов) — takeUntil теперь после тегированной строки. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
5.2 KiB
Java
88 lines
5.2 KiB
Java
package com.mailserver.config;
|
|
|
|
import com.mailserver.security.LoginGuard;
|
|
import com.mailserver.delivery.SmtpDeliveryClient;
|
|
import com.mailserver.smtp.MailRouter;
|
|
import com.mailserver.smtp.SmtpServer;
|
|
import com.mailserver.smtp.SmtpTls;
|
|
import io.netty.handler.ssl.SslContext;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
import java.nio.file.Path;
|
|
import java.time.Duration;
|
|
import java.util.Locale;
|
|
|
|
/** SMTP server, outbound delivery client and the queue scheduler. */
|
|
@Configuration
|
|
@EnableScheduling
|
|
public class MailServerConfig {
|
|
|
|
/**
|
|
* Certificate for SMTP TLS: mailserver.tls.cert-file + key-file (PEM), or a self-signed one in mailserver.tls.dir.
|
|
* mailserver.tls.enabled=false — no TLS at all.
|
|
*/
|
|
@Bean
|
|
public SmtpTlsContext smtpTlsContext(@Value("${mailserver.tls.enabled:true}") boolean enabled,
|
|
@Value("${mailserver.tls.cert-file:}") String certFile,
|
|
@Value("${mailserver.tls.key-file:}") String keyFile,
|
|
@Value("${mailserver.tls.dir:./tls}") String dir,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname) throws Exception {
|
|
return new SmtpTlsContext(enabled ? SmtpTls.serverContext(certFile, keyFile, Path.of(dir), hostname) : null);
|
|
}
|
|
|
|
public record SmtpTlsContext(SslContext context) {
|
|
}
|
|
|
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
|
@ConditionalOnProperty(name = "mailserver.smtp.enabled", havingValue = "true", matchIfMissing = true)
|
|
public SmtpServer smtpServer(MailRouter router, SmtpTlsContext tls, LoginGuard guard,
|
|
@Value("${mailserver.smtp.host:0.0.0.0}") String host,
|
|
@Value("${mailserver.smtp.port:3025}") int port,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.smtp.max-message-bytes:26214400}") long maxBytes,
|
|
@Value("${mailserver.smtp.auth-without-tls:false}") boolean authWithoutTls,
|
|
@Value("${mailserver.smtp.idle-timeout:5m}") Duration idleTimeout) {
|
|
return new SmtpServer(new SmtpServer.Settings(host, port, hostname, maxBytes, authWithoutTls, idleTimeout,
|
|
tls.context(), false), router, guard);
|
|
}
|
|
|
|
/** Implicit TLS (RFC 8314, usually port 465) — only when mailserver.smtps.port is set and TLS is on. */
|
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
|
@ConditionalOnExpression("${mailserver.smtps.port:0} > 0 and ${mailserver.tls.enabled:true}")
|
|
public SmtpServer smtpsServer(MailRouter router, SmtpTlsContext tls, LoginGuard guard,
|
|
@Value("${mailserver.smtp.host:0.0.0.0}") String host,
|
|
@Value("${mailserver.smtps.port}") int port,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.smtp.max-message-bytes:26214400}") long maxBytes,
|
|
@Value("${mailserver.smtp.idle-timeout:5m}") Duration idleTimeout) {
|
|
return new SmtpServer(new SmtpServer.Settings(host, port, hostname, maxBytes, false, idleTimeout,
|
|
tls.context(), true), router, guard);
|
|
}
|
|
|
|
/**
|
|
* mailserver.outbound.mode: off | mx | smarthost. For smarthost — host, port, username, password,
|
|
* tls (starttls | ssl | none).
|
|
*/
|
|
@Bean
|
|
public SmtpDeliveryClient smtpDeliveryClient(@Value("${mailserver.outbound.mode:off}") String mode,
|
|
@Value("${mailserver.outbound.host:}") String host,
|
|
@Value("${mailserver.outbound.port:587}") int port,
|
|
@Value("${mailserver.outbound.username:}") String username,
|
|
@Value("${mailserver.outbound.password:}") String password,
|
|
@Value("${mailserver.outbound.tls:starttls}") String tls,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.outbound.timeout-ms:30000}") int timeoutMs) {
|
|
SmtpDeliveryClient.Mode m = SmtpDeliveryClient.Mode.valueOf(mode.trim().toUpperCase(Locale.ROOT));
|
|
if (m == SmtpDeliveryClient.Mode.SMARTHOST && host.isBlank()) {
|
|
throw new IllegalStateException("mailserver.outbound.mode=smarthost needs mailserver.outbound.host");
|
|
}
|
|
return new SmtpDeliveryClient(new SmtpDeliveryClient.Settings(m, host, port, username, password,
|
|
SmtpDeliveryClient.Tls.valueOf(tls.trim().toUpperCase(Locale.ROOT)), hostname, timeoutMs, 25));
|
|
}
|
|
}
|