Skip to content

Commit 1f24fd4

Browse files
committed
add connectOnActive listener config
1 parent 8aec32a commit 1f24fd4

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ listeners:
2626
proxyProtocol: true # optional (default: false): whether the proxy protocol should be enabled for this listener
2727
timeout: 3000 # custom timeout duration in milliseconds (default: 30000 = 30 seconds)
2828
# ^ don't use 3 seconds as timeout in production because it is (probably) too short
29+
connectOnActive: true # required for mysql servers
2930
servers: # server is selected randomly when a user tries to connect
3031
- host: 10.0.0.1
3132
port: 25565

api/src/main/java/net/azisaba/simpleproxy/api/config/ListenerInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public interface ListenerInfo {
2727
@Nullable
2828
String getType();
2929

30+
boolean isConnectOnActive();
31+
3032
/**
3133
* Returns the raw config object.
3234
* @return the config

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "net.azisaba.simpleproxy"
9-
version = "2.0.1"
9+
version = "2.1.0"
1010

1111
extra.set("log4jVersion", "2.23.1")
1212

proxy/src/main/java/net/azisaba/simpleproxy/proxy/config/ListenerInfoImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ListenerInfoImpl implements ListenerInfo {
2525
private final int timeout;
2626
private final Protocol protocol;
2727
private final String type;
28+
private final boolean connectOnActive;
2829

2930
public ListenerInfoImpl(@NotNull YamlObject config,
3031
@NotNull String host,
@@ -34,7 +35,8 @@ public ListenerInfoImpl(@NotNull YamlObject config,
3435
int initialTimeout,
3536
int timeout,
3637
@NotNull Protocol protocol,
37-
@Nullable String type) {
38+
@Nullable String type,
39+
boolean connectOnActive) {
3840
if (listenPort <= 0 || listenPort > 65535) throw new RuntimeException("Port is out of range: " + listenPort);
3941
Objects.requireNonNull(config, "config cannot be null");
4042
Objects.requireNonNull(host, "host cannot be null");
@@ -48,6 +50,7 @@ public ListenerInfoImpl(@NotNull YamlObject config,
4850
this.timeout = timeout;
4951
this.protocol = protocol;
5052
this.type = type;
53+
this.connectOnActive = connectOnActive;
5154
}
5255

5356
public ListenerInfoImpl(@NotNull YamlObject obj) {
@@ -62,7 +65,8 @@ public ListenerInfoImpl(@NotNull YamlObject obj) {
6265
obj.getInt("initialTimeout", 1000 * 5), // 5 seconds
6366
obj.getInt("timeout", 1000 * 30), // 30 seconds
6467
Protocol.valueOf(obj.getString("protocol", "tcp").toUpperCase(Locale.ROOT)),
65-
obj.getString("type")
68+
obj.getString("type"),
69+
obj.getBoolean("connectOnActive", false)
6670
);
6771
}
6872

@@ -110,6 +114,11 @@ public String getType() {
110114
return type;
111115
}
112116

117+
@Override
118+
public boolean isConnectOnActive() {
119+
return connectOnActive;
120+
}
121+
113122
@Override
114123
public @NotNull YamlObject getConfig() {
115124
return config;

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,39 @@ public void channelActive(@NotNull ChannelHandlerContext ctx) throws Exception {
5353
super.channelActive(ctx);
5454
ctx.read();
5555
if (ProxyConfigInstance.debug) {
56-
LOGGER.info("Forwarder: Established connection: " + ctx.channel());
56+
LOGGER.info("Forwarder: Established connection: {}", ctx.channel());
5757
}
5858
active = true;
59+
if (listenerInfo.isConnectOnActive()) {
60+
connectToRemote();
61+
}
5962
}
6063

6164
public void activate() {
6265
if (!active) {
6366
channel.read();
6467
if (ProxyConfigInstance.debug) {
65-
LOGGER.info("Forwarder: Established connection: " + channel);
68+
LOGGER.info("Forwarder: Established connection: {}", channel);
6669
}
6770
}
6871
if (remote == null && !remoteConnecting) {
69-
if (channel.pipeline().context(ReadTimeoutHandler.class) != null) {
70-
channel.pipeline().remove(ReadTimeoutHandler.class);
71-
channel.pipeline().addFirst(new ReadTimeoutHandler(listenerInfo.getTimeout(), TimeUnit.MILLISECONDS));
72-
}
73-
remoteConnecting = true;
74-
if (ProxyConfigInstance.debug) {
75-
LOGGER.info("Forwarder: Connecting to remote server: " + remoteServerInfo);
76-
}
77-
ChannelFuture future = ProxyInstance.getInstance()
78-
.getConnectionListener()
79-
.connect(this, remoteServerInfo);
80-
remote = future.channel();
72+
connectToRemote();
73+
}
74+
}
75+
76+
private void connectToRemote() {
77+
if (channel.pipeline().context(ReadTimeoutHandler.class) != null) {
78+
channel.pipeline().remove(ReadTimeoutHandler.class);
79+
channel.pipeline().addFirst(new ReadTimeoutHandler(listenerInfo.getTimeout(), TimeUnit.MILLISECONDS));
80+
}
81+
remoteConnecting = true;
82+
if (ProxyConfigInstance.debug) {
83+
LOGGER.info("Forwarder: Connecting to remote server: {}", remoteServerInfo);
8184
}
85+
ChannelFuture future = ProxyInstance.getInstance()
86+
.getConnectionListener()
87+
.connect(this, remoteServerInfo);
88+
remote = future.channel();
8289
}
8390

8491
public void deactivate() {

0 commit comments

Comments
 (0)