Skip to content

Commit 788d4eb

Browse files
committed
Correctly fire conn fail event for TLS exceptions (e.g. cert expired)
1 parent 4969f03 commit 788d4eb

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/main/java/org/kitteh/irc/client/library/defaults/feature/network/NettyConnection.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.checkerframework.checker.nullness.qual.Nullable;
4848
import org.kitteh.irc.client.library.Client;
4949
import org.kitteh.irc.client.library.event.connection.ClientConnectionClosedEvent;
50+
import org.kitteh.irc.client.library.event.connection.ClientConnectionEndedEvent;
5051
import org.kitteh.irc.client.library.event.connection.ClientConnectionEstablishedEvent;
5152
import org.kitteh.irc.client.library.event.connection.ClientConnectionFailedEvent;
5253
import org.kitteh.irc.client.library.exception.KittehConnectionException;
@@ -87,6 +88,7 @@ public final class NettyConnection implements ClientConnection {
8788
private @Nullable ScheduledFuture<?> ping;
8889

8990
private volatile String lastMessage;
91+
private volatile Throwable lastCause;
9092

9193
private boolean alive = true;
9294

@@ -187,12 +189,17 @@ protected void channelRead0(ChannelHandlerContext ctx, String msg) {
187189
// The presence of the two latter arguments enables SNI.
188190
final SslHandler sslHandler = sslContext.newHandler(this.channel.alloc(), addr.getHost(), addr.getPort());
189191
sslHandler.handshakeFuture().addListener(handshakeFuture -> {
190-
if (!handshakeFuture.isSuccess() && NettyConnection.this.client.getStsMachine().isPresent()) {
191-
StsMachine machine = NettyConnection.this.client.getStsMachine().get();
192-
if (machine.getCurrentState() == StsClientState.STS_PRESENT_RECONNECTING) {
193-
NettyConnection.this.shutdown(DefaultMessageType.STS_FAILURE, false);
194-
machine.setCurrentState(StsClientState.STS_PRESENT_CANNOT_CONNECT);
195-
throw new KittehStsException("Handshake failure, aborting STS-protected connection attempt.", handshakeFuture.cause());
192+
this.lastCause = null;
193+
if (!handshakeFuture.isSuccess()) {
194+
if (NettyConnection.this.client.getStsMachine().isPresent()) {
195+
StsMachine machine = NettyConnection.this.client.getStsMachine().get();
196+
if (machine.getCurrentState() == StsClientState.STS_PRESENT_RECONNECTING) {
197+
NettyConnection.this.shutdown(DefaultMessageType.STS_FAILURE, false);
198+
machine.setCurrentState(StsClientState.STS_PRESENT_CANNOT_CONNECT);
199+
throw new KittehStsException("Handshake failure, aborting STS-protected connection attempt.", handshakeFuture.cause());
200+
}
201+
} else {
202+
this.lastCause = handshakeFuture.cause();
196203
}
197204
}
198205
});
@@ -227,7 +234,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
227234
this.ping.cancel(true);
228235
}
229236
NettyConnection.this.alive = false;
230-
ClientConnectionClosedEvent event = new ClientConnectionClosedEvent(NettyConnection.this.client, NettyConnection.this.reconnect, future.cause(), this.lastMessage);
237+
ClientConnectionEndedEvent event;
238+
if (this.lastCause == null ) {
239+
event = new ClientConnectionClosedEvent(this.client, this.reconnect, future.cause(), this.lastMessage);
240+
} else {
241+
event = new ClientConnectionFailedEvent(this.client, this.reconnect, this.lastCause);
242+
}
231243
NettyConnection.this.client.getEventManager().callEvent(event);
232244
if (event.willAttemptReconnect()) {
233245
this.scheduleReconnect(event.getReconnectionDelay());

0 commit comments

Comments
 (0)