Skip to content

Commit

Permalink
add connectOnActive listener config
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Oct 14, 2024
1 parent 8aec32a commit 1f24fd4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ listeners:
proxyProtocol: true # optional (default: false): whether the proxy protocol should be enabled for this listener
timeout: 3000 # custom timeout duration in milliseconds (default: 30000 = 30 seconds)
# ^ don't use 3 seconds as timeout in production because it is (probably) too short
connectOnActive: true # required for mysql servers
servers: # server is selected randomly when a user tries to connect
- host: 10.0.0.1
port: 25565
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface ListenerInfo {
@Nullable
String getType();

boolean isConnectOnActive();

/**
* Returns the raw config object.
* @return the config
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "net.azisaba.simpleproxy"
version = "2.0.1"
version = "2.1.0"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ListenerInfoImpl implements ListenerInfo {
private final int timeout;
private final Protocol protocol;
private final String type;
private final boolean connectOnActive;

public ListenerInfoImpl(@NotNull YamlObject config,
@NotNull String host,
Expand All @@ -34,7 +35,8 @@ public ListenerInfoImpl(@NotNull YamlObject config,
int initialTimeout,
int timeout,
@NotNull Protocol protocol,
@Nullable String type) {
@Nullable String type,
boolean connectOnActive) {
if (listenPort <= 0 || listenPort > 65535) throw new RuntimeException("Port is out of range: " + listenPort);
Objects.requireNonNull(config, "config cannot be null");
Objects.requireNonNull(host, "host cannot be null");
Expand All @@ -48,6 +50,7 @@ public ListenerInfoImpl(@NotNull YamlObject config,
this.timeout = timeout;
this.protocol = protocol;
this.type = type;
this.connectOnActive = connectOnActive;
}

public ListenerInfoImpl(@NotNull YamlObject obj) {
Expand All @@ -62,7 +65,8 @@ public ListenerInfoImpl(@NotNull YamlObject obj) {
obj.getInt("initialTimeout", 1000 * 5), // 5 seconds
obj.getInt("timeout", 1000 * 30), // 30 seconds
Protocol.valueOf(obj.getString("protocol", "tcp").toUpperCase(Locale.ROOT)),
obj.getString("type")
obj.getString("type"),
obj.getBoolean("connectOnActive", false)
);
}

Expand Down Expand Up @@ -110,6 +114,11 @@ public String getType() {
return type;
}

@Override
public boolean isConnectOnActive() {
return connectOnActive;
}

@Override
public @NotNull YamlObject getConfig() {
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,39 @@ public void channelActive(@NotNull ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.read();
if (ProxyConfigInstance.debug) {
LOGGER.info("Forwarder: Established connection: " + ctx.channel());
LOGGER.info("Forwarder: Established connection: {}", ctx.channel());
}
active = true;
if (listenerInfo.isConnectOnActive()) {
connectToRemote();
}
}

public void activate() {
if (!active) {
channel.read();
if (ProxyConfigInstance.debug) {
LOGGER.info("Forwarder: Established connection: " + channel);
LOGGER.info("Forwarder: Established connection: {}", channel);
}
}
if (remote == null && !remoteConnecting) {
if (channel.pipeline().context(ReadTimeoutHandler.class) != null) {
channel.pipeline().remove(ReadTimeoutHandler.class);
channel.pipeline().addFirst(new ReadTimeoutHandler(listenerInfo.getTimeout(), TimeUnit.MILLISECONDS));
}
remoteConnecting = true;
if (ProxyConfigInstance.debug) {
LOGGER.info("Forwarder: Connecting to remote server: " + remoteServerInfo);
}
ChannelFuture future = ProxyInstance.getInstance()
.getConnectionListener()
.connect(this, remoteServerInfo);
remote = future.channel();
connectToRemote();
}
}

private void connectToRemote() {
if (channel.pipeline().context(ReadTimeoutHandler.class) != null) {
channel.pipeline().remove(ReadTimeoutHandler.class);
channel.pipeline().addFirst(new ReadTimeoutHandler(listenerInfo.getTimeout(), TimeUnit.MILLISECONDS));
}
remoteConnecting = true;
if (ProxyConfigInstance.debug) {
LOGGER.info("Forwarder: Connecting to remote server: {}", remoteServerInfo);
}
ChannelFuture future = ProxyInstance.getInstance()
.getConnectionListener()
.connect(this, remoteServerInfo);
remote = future.channel();
}

public void deactivate() {
Expand Down

0 comments on commit 1f24fd4

Please sign in to comment.