Skip to content

Commit

Permalink
improvements & auth fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixf1nity committed Nov 15, 2023
1 parent cdceb29 commit a613fef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@
<dependency>
<groupId>com.github.alexdlaird</groupId>
<artifactId>java-ngrok</artifactId>
<version>2.2.3</version>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.2.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
44 changes: 13 additions & 31 deletions src/main/java/me/infinity/ngrokcom/NgrokCommunication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.alexdlaird.ngrok.NgrokClient;
import com.github.alexdlaird.ngrok.conf.JavaNgrokConfig;
import com.github.alexdlaird.ngrok.installer.NgrokInstaller;
import com.github.alexdlaird.ngrok.installer.NgrokVersion;
import com.github.alexdlaird.ngrok.protocol.CreateTunnel;
import com.github.alexdlaird.ngrok.protocol.Proto;
import com.github.alexdlaird.ngrok.protocol.Region;
Expand All @@ -15,6 +17,8 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class NgrokCommunication extends JavaPlugin {

Expand All @@ -27,6 +31,9 @@ public final class NgrokCommunication extends JavaPlugin {

@Override
public void onEnable() {

Logger.getLogger(String.valueOf(com.github.alexdlaird.ngrok.process.NgrokProcess.class)).setLevel(Level.OFF);

this.saveDefaultConfig();
int ngrokPort = this.getServer().getPort();
this.discordModule = this.getConfig().getBoolean("DISCORD_UPDATES.ENABLED");
Expand All @@ -45,17 +52,20 @@ public void onEnable() {
.block();

if (this.client != null) this.discordModuleStatus = true;

}

final JavaNgrokConfig javaNgrokConfig = new JavaNgrokConfig.Builder()
.withAuthToken(this.getConfig().getString("NGROK_SETTINGS.AUTH_TOKEN"))
.withNgrokVersion(NgrokVersion.V3)
.withRegion(Region.valueOf(Objects.requireNonNull(this.getConfig().getString("NGROK_SETTINGS.REGION")).toUpperCase()))
.build();

this.ngrokClient = new NgrokClient.Builder()
.withNgrokInstaller(new NgrokInstaller())
.withJavaNgrokConfig(javaNgrokConfig)
.build();

this.ngrokClient.getNgrokProcess().setAuthToken(this.getConfig().getString("NGROK_SETTINGS.AUTH_TOKEN"));

final CreateTunnel createTunnel = new CreateTunnel.Builder()
.withProto(Proto.TCP)
.withAddr(ngrokPort) // Use the configured Ngrok port
Expand Down Expand Up @@ -83,44 +93,16 @@ public void onEnable() {

@Override
public void onDisable() {
saveConfigSilently();
try {
if (ngrokClient != null && publicIp != null) {
this.ngrokClient.disconnect(publicIp);
this.ngrokClient.kill();
saveConfigSilently();
}
} catch (Exception ignored) {
// Suppress any exceptions during shutdown
}

try {
if (discordModuleStatus) {
if (this.client != null) {
this.client.logout().block();
saveConfigSilently();
}
}
} catch (Exception ignored) {
// Suppress any exceptions during shutdown
}
}
// Method to save the configuration without generating warnings
private void saveConfigSilently() {
try {
// Temporarily store the original system output and error streams
// to suppress warnings during configuration save
System.setOut(null);
System.setErr(null);

// Save the default configuration
saveDefaultConfig();

// Restore the original output and error streams
System.setOut(System.out);
System.setErr(System.err);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ignored) {}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ version: '${project.version}'
main: me.infinity.ngrokcom.NgrokCommunication
api-version: 1.20
load: STARTUP
libraries:
- com.discord4j:discord4j-core:3.2.6

0 comments on commit a613fef

Please sign in to comment.