Skip to content

Commit 20c9879

Browse files
committed
Add RemoteConnectionActiveEvent
1 parent 4f3ec33 commit 20c9879

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.azisaba.simpleProxy.api.event.connection;
2+
3+
import io.netty.channel.Channel;
4+
import net.azisaba.simpleProxy.api.event.Event;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.util.Objects;
8+
9+
public abstract class ChannelEvent extends Event {
10+
protected final Channel channel;
11+
12+
public ChannelEvent(@NotNull Channel channel) {
13+
Objects.requireNonNull(channel, "channel");
14+
this.channel = channel;
15+
}
16+
17+
@NotNull
18+
public Channel getChannel() {
19+
return channel;
20+
}
21+
}

api/src/main/java/net/azisaba/simpleProxy/api/event/connection/ConnectionInitEvent.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@
22

33
import io.netty.channel.Channel;
44
import net.azisaba.simpleProxy.api.config.ListenerInfo;
5-
import net.azisaba.simpleProxy.api.event.Event;
65
import org.jetbrains.annotations.NotNull;
76

8-
public class ConnectionInitEvent extends Event {
7+
public class ConnectionInitEvent extends ChannelEvent {
98
private final ListenerInfo listenerInfo;
10-
private final Channel channel;
119

1210
public ConnectionInitEvent(@NotNull ListenerInfo listenerInfo, @NotNull Channel channel) {
11+
super(channel);
1312
this.listenerInfo = listenerInfo;
14-
this.channel = channel;
1513
}
1614

1715
@NotNull
1816
public ListenerInfo getListenerInfo() {
1917
return listenerInfo;
2018
}
21-
22-
@NotNull
23-
public Channel getChannel() {
24-
return channel;
25-
}
2619
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.azisaba.simpleProxy.api.event.connection;
2+
3+
import io.netty.channel.Channel;
4+
import net.azisaba.simpleProxy.api.config.ListenerInfo;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.net.SocketAddress;
8+
9+
/**
10+
* Fired when the remote connection is active and now ready to send/receive data.
11+
*/
12+
public class RemoteConnectionActiveEvent extends RemoteConnectionInitEvent {
13+
public RemoteConnectionActiveEvent(@NotNull ListenerInfo listenerInfo, @NotNull Channel channel, @NotNull Channel sourceChannel, @NotNull SocketAddress sourceAddress) {
14+
super(listenerInfo, channel, sourceChannel, sourceAddress);
15+
}
16+
}

proxy/src/main/java/net/azisaba/simpleProxy/proxy/connection/MessageForwarder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.netty.handler.codec.haproxy.HAProxyMessage;
99
import net.azisaba.simpleProxy.api.config.ListenerInfo;
1010
import net.azisaba.simpleProxy.api.config.ServerInfo;
11+
import net.azisaba.simpleProxy.api.event.connection.RemoteConnectionActiveEvent;
1112
import net.azisaba.simpleProxy.proxy.ProxyInstance;
1213
import net.azisaba.simpleProxy.proxy.config.ProxyConfigInstance;
1314
import org.apache.logging.log4j.LogManager;
@@ -51,6 +52,25 @@ public void channelActive(@NotNull ChannelHandlerContext ctx) throws Exception {
5152
}
5253
}
5354

55+
@Override
56+
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
57+
if (ctx.channel().isActive()) {
58+
// handler added while the channel is active (added by plugin)
59+
ctx.read();
60+
if (ProxyConfigInstance.debug) {
61+
LOGGER.info("Forwarder: Established connection: " + ctx.channel());
62+
}
63+
if (remote == null && !remoteConnecting) {
64+
remoteConnecting = true;
65+
ChannelFuture future = ProxyInstance.getInstance()
66+
.getConnectionListener()
67+
.connect(this, remoteServerInfo);
68+
remote = future.channel();
69+
}
70+
}
71+
super.handlerAdded(ctx);
72+
}
73+
5474
@Override
5575
public void channelInactive(@NotNull ChannelHandlerContext ctx) throws Exception {
5676
super.channelInactive(ctx);
@@ -117,6 +137,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
117137
void remoteActive() {
118138
isRemoteActive = true;
119139
flushQueue();
140+
new RemoteConnectionActiveEvent(listenerInfo, remote, channel, sourceAddress).callEvent();
120141
}
121142

122143
public void flushQueue() {

0 commit comments

Comments
 (0)