This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 626e37e5b46dfdb88c5b837f28dd56e81886d812 Author: Benoit TELLIER <btell...@linagora.com> AuthorDate: Sat Nov 23 07:42:50 2024 +0100 JAMES-4091 Add Connection descriptors for SMTP --- .../netty/BasicChannelInboundHandler.java | 4 +++ .../james/modules/protocols/SMTPServerModule.java | 2 ++ .../apache/james/smtpserver/netty/SMTPServer.java | 35 +++++++++++++++++++++- .../james/smtpserver/netty/SMTPServerFactory.java | 13 +++++++- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/protocols/netty/src/main/java/org/apache/james/protocols/netty/BasicChannelInboundHandler.java b/protocols/netty/src/main/java/org/apache/james/protocols/netty/BasicChannelInboundHandler.java index 1dc3bd874d..e6ece0a608 100644 --- a/protocols/netty/src/main/java/org/apache/james/protocols/netty/BasicChannelInboundHandler.java +++ b/protocols/netty/src/main/java/org/apache/james/protocols/netty/BasicChannelInboundHandler.java @@ -25,6 +25,8 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; import java.nio.channels.ClosedChannelException; +import java.time.Clock; +import java.time.Instant; import java.util.Deque; import java.util.LinkedList; import java.util.List; @@ -65,6 +67,7 @@ import io.netty.util.AttributeKey; */ public class BasicChannelInboundHandler extends ChannelInboundHandlerAdapter implements LineHandlerAware { private static final Logger LOGGER = LoggerFactory.getLogger(BasicChannelInboundHandler.class); + public static final AttributeKey<Instant> CONNECTION_DATE = AttributeKey.newInstance("smtpConnectionDate"); public static final ProtocolSession.AttachmentKey<MDCBuilder> MDC_ATTRIBUTE_KEY = ProtocolSession.AttachmentKey.of("bound_MDC", MDCBuilder.class); public static final AttributeKey<CommandDetectionSession> SESSION_ATTRIBUTE_KEY = AttributeKey.valueOf("session"); @@ -97,6 +100,7 @@ public class BasicChannelInboundHandler extends ChannelInboundHandlerAdapter imp @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { MDCBuilder boundMDC = mdcContextFactory.onBound(protocol, ctx); + ctx.channel().attr(CONNECTION_DATE).set(Clock.systemUTC().instant()); try (Closeable closeable = boundMDC.build()) { ProtocolSession session = createSession(ctx); session.setAttachment(MDC_ATTRIBUTE_KEY, boundMDC, Connection); diff --git a/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SMTPServerModule.java b/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SMTPServerModule.java index 9612eb0bf6..b44afd9d81 100644 --- a/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SMTPServerModule.java +++ b/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SMTPServerModule.java @@ -21,6 +21,7 @@ package org.apache.james.modules.protocols; import org.apache.james.ProtocolConfigurationSanitizer; import org.apache.james.RunArguments; +import org.apache.james.core.ConnectionDescriptionSupplier; import org.apache.james.core.Disconnector; import org.apache.james.filesystem.api.FileSystem; import org.apache.james.lifecycle.api.ConfigurationSanitizer; @@ -48,6 +49,7 @@ public class SMTPServerModule extends AbstractModule { Multibinder.newSetBinder(binder(), CertificateReloadable.Factory.class).addBinding().to(SMTPServerFactory.class); Multibinder.newSetBinder(binder(), Disconnector.class).addBinding().to(SMTPServerFactory.class); + Multibinder.newSetBinder(binder(), ConnectionDescriptionSupplier.class).addBinding().to(SMTPServerFactory.class); } @ProvidesIntoSet diff --git a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java index 8ead90f36a..5229643bcb 100644 --- a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java +++ b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java @@ -18,22 +18,28 @@ ****************************************************************/ package org.apache.james.smtpserver.netty; +import static org.apache.james.protocols.netty.BasicChannelInboundHandler.CONNECTION_DATE; import static org.apache.james.protocols.netty.BasicChannelInboundHandler.SESSION_ATTRIBUTE_KEY; import static org.apache.james.smtpserver.netty.SMTPServer.AuthenticationAnnounceMode.ALWAYS; import static org.apache.james.smtpserver.netty.SMTPServer.AuthenticationAnnounceMode.NEVER; +import java.net.InetSocketAddress; import java.net.MalformedURLException; +import java.net.SocketAddress; import java.net.URISyntaxException; import java.util.Locale; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Stream; import jakarta.inject.Inject; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.configuration2.tree.ImmutableNode; +import org.apache.james.core.ConnectionDescription; +import org.apache.james.core.ConnectionDescriptionSupplier; import org.apache.james.core.Disconnector; import org.apache.james.core.Username; import org.apache.james.dnsservice.api.DNSService; @@ -56,6 +62,7 @@ import org.apache.james.util.Size; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import io.netty.buffer.Unpooled; @@ -68,7 +75,7 @@ import io.netty.util.concurrent.GlobalEventExecutor; /** * NIO SMTPServer which use Netty */ -public class SMTPServer extends AbstractProtocolAsyncServer implements SMTPServerMBean, Disconnector { +public class SMTPServer extends AbstractProtocolAsyncServer implements SMTPServerMBean, Disconnector, ConnectionDescriptionSupplier { private static final Logger LOGGER = LoggerFactory.getLogger(SMTPServer.class); private SMTPProtocol transport; @@ -437,4 +444,30 @@ public class SMTPServer extends AbstractProtocolAsyncServer implements SMTPServe }) .forEach(channel -> channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE)); } + + @Override + public Stream<ConnectionDescription> describeConnections() { + return smtpChannelGroup.stream() + .map(channel -> { + Optional<ProtocolSession> pSession = Optional.ofNullable(channel.attr(SESSION_ATTRIBUTE_KEY).get()) + .map(session -> (ProtocolSession) session); + return new ConnectionDescription("SMTP", + jmxName, + Optional.ofNullable(channel.remoteAddress()).map(this::addressAsString), + Optional.ofNullable(channel.attr(CONNECTION_DATE)).flatMap(attribute -> Optional.ofNullable(attribute.get())), + channel.isActive(), + channel.isOpen(), + channel.isWritable(), + pSession.map(p -> p.getSSLSession().isPresent()).orElse(false), + pSession.flatMap(session -> Optional.ofNullable(session.getUsername())), + ImmutableMap.of()); + }); + } + + private String addressAsString(SocketAddress socketAddress) { + if (socketAddress instanceof InetSocketAddress address) { + return address.getAddress().getHostAddress(); + } + return socketAddress.toString(); + } } diff --git a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServerFactory.java b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServerFactory.java index 17b6197660..831a7d4fa4 100644 --- a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServerFactory.java +++ b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServerFactory.java @@ -22,11 +22,14 @@ package org.apache.james.smtpserver.netty; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; +import java.util.stream.Stream; import jakarta.inject.Inject; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.tree.ImmutableNode; +import org.apache.james.core.ConnectionDescription; +import org.apache.james.core.ConnectionDescriptionSupplier; import org.apache.james.core.Disconnector; import org.apache.james.core.Username; import org.apache.james.dnsservice.api.DNSService; @@ -36,7 +39,7 @@ import org.apache.james.protocols.lib.handler.ProtocolHandlerLoader; import org.apache.james.protocols.lib.netty.AbstractConfigurableAsyncServer; import org.apache.james.protocols.lib.netty.AbstractServerFactory; -public class SMTPServerFactory extends AbstractServerFactory implements Disconnector { +public class SMTPServerFactory extends AbstractServerFactory implements Disconnector, ConnectionDescriptionSupplier { protected final DNSService dns; protected final ProtocolHandlerLoader loader; @@ -81,4 +84,12 @@ public class SMTPServerFactory extends AbstractServerFactory implements Disconne .map(server -> (SMTPServer) server) .forEach(smtpServer -> smtpServer.disconnect(username)); } + + @Override + public Stream<ConnectionDescription> describeConnections() { + return getServers() + .stream() + .map(server -> (SMTPServer) server) + .flatMap(SMTPServer::describeConnections); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org