Skip to content

Commit

Permalink
update G4J and proxy configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUmbrella committed Jan 16, 2023
1 parent 155e9c8 commit ae90dd6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 71 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>vip.floatationdevice</groupId>
<artifactId>mgbridge</artifactId>
<version>0.9.6</version>
<version>0.9.7</version>
<packaging>jar</packaging>

<name>MGBridge</name>
Expand Down Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>vip.floatationdevice</groupId>
<artifactId>guilded4j</artifactId>
<version>0.9.6</version>
<version>0.9.15</version>
</dependency>
</dependencies>
</project>
47 changes: 32 additions & 15 deletions src/main/java/vip/floatationdevice/mgbridge/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,39 @@ static boolean loadConfig()
return false;
}
BindManager.loadBindMap();
// set socks proxy
if(!notSet(cfg.getString("socksProxy"))) // is socksProxy field set?
// set proxy
if("direct".equalsIgnoreCase(cfg.getString("proxy.type", "direct")))
; // do nothing
else if("default".equalsIgnoreCase(cfg.getString("proxy.type")))
{
String[] socksProxy = cfg.getString("socksProxy").split(":");
if("default".equalsIgnoreCase(cfg.getString("socksProxy"))) // use proxy settings in JVM arguments
try
{
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(System.getProperty("socksProxyHost"), Integer.parseInt(System.getProperty("socksProxyPort"))));
}
catch(Exception ignored) {}
else if(socksProxy.length == 2 && socksProxy[0].length() > 0 && socksProxy[1].length() > 0 && socksProxy[1].matches("^\\d+$")) // socksProxy is set and valid
try
{
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxy[0], Integer.parseInt(socksProxy[1])));
}
catch(Exception ignored) {}
boolean hasSocksProxy = System.getProperty("socksProxyHost") != null && System.getProperty("socksProxyPort") != null,
hasHttpProxy = System.getProperty("httpProxyHost") != null && System.getProperty("httpProxyPort") != null;
if(hasSocksProxy)
proxy = new Proxy(
Proxy.Type.SOCKS,
new InetSocketAddress(
System.getProperty("socksProxyHost"),
Integer.parseInt(System.getProperty("socksProxyPort"))
)
);
else if(hasHttpProxy)
proxy = new Proxy(
Proxy.Type.HTTP,
new InetSocketAddress(
System.getProperty("httpProxyHost"),
Integer.parseInt(System.getProperty("httpProxyPort"))
)
);
}
else
{
proxy = new Proxy(
Proxy.Type.valueOf(cfg.getString("proxy.type").toUpperCase()),
new InetSocketAddress(
cfg.getString("proxy.address").split(":")[0],
Integer.parseInt(cfg.getString("proxy.address").split(":")[1])
)
);
}
// set message formatter
if("disabled".equalsIgnoreCase(cfg.getString("toGuildedMessageFormat")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.eventbus.Subscribe;
import org.bukkit.Bukkit;
import vip.floatationdevice.guilded4j.G4JWebSocketClient;
import vip.floatationdevice.guilded4j.event.ChatMessageCreatedEvent;
import vip.floatationdevice.guilded4j.event.GuildedWebSocketClosedEvent;
import vip.floatationdevice.guilded4j.event.GuildedWebSocketWelcomeEvent;
Expand Down Expand Up @@ -65,17 +64,12 @@ public GuildedEventListener()
void connect()
{
log.info(translate("connecting"));
MGBridge.instance.getG4JClient().ws = new G4JWebSocketClient(token);
MGBridge.instance.getG4JClient().ws.setProxy(proxy);
MGBridge.instance.getG4JClient().ws.eventBus.register(this);
MGBridge.instance.getG4JClient().ws.connect();
MGBridge.instance.getG4JClient().registerEventListener(this).connectWebSocket();
}

void disconnect()
{
// unregister gEventListener from event bus first to prevent automatic reconnection
MGBridge.instance.getG4JClient().ws.eventBus.unregister(this);
MGBridge.instance.getG4JClient().ws.close();
MGBridge.instance.getG4JClient().unregisterEventListener(this).disconnectWebSocket();
log.info(translate("disconnected"));
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The version of the config.yml file. This is used to check if the config.yml file is up-to-date.
# Useful if you want to manually update config.yml without deleting the old one.
version: 1
version: 2

# Print the response after forwarding a message to Guilded
debug: false
Expand Down Expand Up @@ -32,9 +32,14 @@ toMinecraftMessageFormat: "[§eGuilded§r] <{PLAYER}> {MESSAGE}"
# Forward player join/quit messages
forwardJoinLeaveEvents: true

# Socks proxy address. Enter an address (e.g. "127.0.0.1:1080") to use a proxy,
# enter "default" to use the default proxy, or leave blank to not use a proxy.
socksProxy:
# Proxy configuration settings.
proxy:
# Connect to Guilded with proxy? If you don't need a proxy, set this to "direct".
# Available values are: "default", "direct", "http", "socks".
# Use "default" to automatically detect proxy settings from JVM arguments.
type: "default"
# Proxy address (e.g. "127.0.0.1:1080")
address: "127.0.0.1:1080"

# If the WebSocket connection is lost, the plugin will attempt to reconnect every this amount of seconds
reconnectDelay: 3
43 changes: 1 addition & 42 deletions src/test/java/vip/floatationdevice/mgbridge/Test.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
package vip.floatationdevice.mgbridge;

import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.net.InetSocketAddress;
import java.net.Proxy;

import static vip.floatationdevice.mgbridge.ConfigManager.toGuildedMessageFormat;
import static vip.floatationdevice.mgbridge.ConfigManager.toMinecraftMessageFormat;
import static vip.floatationdevice.mgbridge.MGBridge.notSet;

public class Test
{
public static void main(String[] args)
{
Proxy proxy = Proxy.NO_PROXY;
File cfgFile = new File("src/main/resources/config.yml");
System.out.println(cfgFile.exists());
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(cfgFile);
System.out.println(cfg.getString("socksProxy"));
if(!notSet(cfg.getString("socksProxy"))) // is socksProxy field set?
{
String[] socksProxy = cfg.getString("socksProxy").split(":");
if("default".equalsIgnoreCase(cfg.getString("socksProxy"))) // use proxy settings in JVM arguments
try
{
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(System.getProperty("socksProxyHost"), Integer.parseInt(System.getProperty("socksProxyPort"))));
}
catch(Exception ignored) {}
else if(socksProxy.length == 2) // socksProxy is set in config file
try
{
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxy[0], Integer.parseInt(socksProxy[1])));
}
catch(Exception ignored) {}
}
System.out.println("proxy: " + proxy);
toGuildedMessageFormat = cfg.getString("toGuildedMessageFormat", toGuildedMessageFormat);
toMinecraftMessageFormat = cfg.getString("toMinecraftMessageFormat", toMinecraftMessageFormat);
System.out.println("toGuildedMessageFormat: " + toGuildedMessageFormat);
System.out.println("toMinecraftMessageFormat: " + toMinecraftMessageFormat);
String mcPlayer = "MCUmbrella";
String mcMessage = "Hello world!";
System.out.println(toMinecraftMessageFormat.replace("{PLAYER}", mcPlayer).replace("{MESSAGE}", mcMessage));
String gPlayer = "GuildedUmbrella";
String gMessage = "Hello world from Guilded!";
System.out.println(toGuildedMessageFormat.replace("{PLAYER}", gPlayer).replace("{MESSAGE}", gMessage));
;
}
}

0 comments on commit ae90dd6

Please sign in to comment.