Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Jonttuuu | 2027806e0c | |
Jonttuuu | d7ecefb3ff |
|
@ -3,7 +3,9 @@ package fi.flexplex.connect;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
@ -12,6 +14,8 @@ import fi.flexplex.connect.util.FileChangeListener;
|
||||||
|
|
||||||
public final class FlexConnect extends JavaPlugin {
|
public final class FlexConnect extends JavaPlugin {
|
||||||
|
|
||||||
|
private final Set<String> proxyAddresses = new HashSet<>();
|
||||||
|
|
||||||
private String token = "";
|
private String token = "";
|
||||||
private FlexPlexGraphQLApi flexPlexGraphQLApi;
|
private FlexPlexGraphQLApi flexPlexGraphQLApi;
|
||||||
|
|
||||||
|
@ -23,6 +27,10 @@ public final class FlexConnect extends JavaPlugin {
|
||||||
return this.flexPlexGraphQLApi;
|
return this.flexPlexGraphQLApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> getProxyAddresses() {
|
||||||
|
return this.proxyAddresses;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
|
||||||
|
@ -52,68 +60,25 @@ public final class FlexConnect extends JavaPlugin {
|
||||||
configsModified = true;
|
configsModified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check paper config
|
// Check spigot config
|
||||||
boolean paperConfigModified = false;
|
boolean spigotConfigModified = false;
|
||||||
final YamlConfiguration paperConfig = this.getServer().spigot().getPaperConfig();
|
final YamlConfiguration spigotConfig = this.getServer().spigot().getSpigotConfig();
|
||||||
|
|
||||||
// Legacy paper configs (1.18 or older)
|
if (!spigotConfig.getBoolean("settings.bungeecord")) {
|
||||||
if (paperConfig.contains("settings.velocity-support.enabled")) {
|
spigotConfig.set("settings.bungeecord", true);
|
||||||
if (!paperConfig.getBoolean("settings.velocity-support.enabled")) {
|
spigotConfigModified = true;
|
||||||
paperConfig.set("settings.velocity-support.enabled", true);
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (paperConfig.contains("settings.velocity-support.online-mode")) {
|
|
||||||
if (!paperConfig.getBoolean("settings.velocity-support.online-mode")) {
|
|
||||||
paperConfig.set("settings.velocity-support.online-mode", true);
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (paperConfig.contains("settings.velocity-support.secret")) {
|
|
||||||
if (!paperConfig.getString("settings.velocity-support.secret").equals("flexplex")) {
|
|
||||||
paperConfig.set("settings.velocity-support.secret", "flexplex");
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current paper configurations 1.19+
|
if (spigotConfigModified) {
|
||||||
if (paperConfig.contains("proxies.velocity.enabled")) {
|
|
||||||
if (!paperConfig.getBoolean("proxies.velocity.enabled")) {
|
|
||||||
paperConfig.set("proxies.velocity.enabled", true);
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (paperConfig.contains("proxies.velocity.online-mode")) {
|
|
||||||
if (!paperConfig.getBoolean("proxies.velocity.online-mode")) {
|
|
||||||
paperConfig.set("proxies.velocity.online-mode", true);
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (paperConfig.contains("proxies.velocity.secret")) {
|
|
||||||
if (!paperConfig.getString("proxies.velocity.secret").equals("flexplex")) {
|
|
||||||
paperConfig.set("proxies.velocity.secret", "flexplex");
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// In both configurations
|
|
||||||
if (paperConfig.contains("settings.unsupported-settings.perform-username-validation")) {
|
|
||||||
if (paperConfig.getBoolean("settings.unsupported-settings.perform-username-validation")) {
|
|
||||||
paperConfig.set("settings.unsupported-settings.perform-username-validation", false);
|
|
||||||
paperConfigModified = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (paperConfigModified) {
|
|
||||||
try {
|
try {
|
||||||
paperConfig.save("paper.yml");
|
spigotConfig.save("spigot.yml");
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
this.getLogger().warning("Automatic changes has been made to paper.yml config file.");
|
this.getLogger().warning("Automatic changes has been made to spigot.yml config file.");
|
||||||
configsModified = true;
|
configsModified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions if configs were modified
|
// Actions if configs were modified
|
||||||
if (configsModified) {
|
if (configsModified) {
|
||||||
this.getLogger().warning("Changes has been made to config files. Server will now shutdown automatically.");
|
this.getLogger().warning("Changes has been made to config files. Server will now shutdown automatically.");
|
||||||
|
@ -129,6 +94,13 @@ public final class FlexConnect extends JavaPlugin {
|
||||||
|
|
||||||
// Update whitelists for flexplex api
|
// Update whitelists for flexplex api
|
||||||
this.flexPlexGraphQLApi.updateWhitelist();
|
this.flexPlexGraphQLApi.updateWhitelist();
|
||||||
|
|
||||||
|
// Load real FlexPlex proxy addresses from FlexPlex API
|
||||||
|
this.proxyAddresses.addAll(this.flexPlexGraphQLApi.getProxyAddresses());
|
||||||
|
|
||||||
|
// Load allowed proxy addresses from config
|
||||||
|
this.proxyAddresses.addAll(this.getConfig().getStringList("allowedProxies"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package fi.flexplex.connect;
|
package fi.flexplex.connect;
|
||||||
|
|
||||||
import com.destroystokyo.paper.event.server.WhitelistToggleEvent;
|
import com.destroystokyo.paper.event.server.WhitelistToggleEvent;
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
|
|
||||||
import fi.flexplex.connect.event.AsyncWhitelistChangedEvent;
|
import fi.flexplex.connect.event.AsyncWhitelistChangedEvent;
|
||||||
|
|
||||||
|
@ -20,16 +19,14 @@ public final class FlexConnectListener implements Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
|
public void onPlayerLogin(final PlayerLoginEvent event) {
|
||||||
// Authenticate players
|
if (flexConnect.getProxyAddresses().contains(event.getRealAddress().getHostAddress())) {
|
||||||
for (final ProfileProperty property : event.getPlayerProfile().getProperties()) {
|
// Connection is coming from allowed proxy
|
||||||
if (property.getName().equals("flexplex-token") && property.getValue().equals(flexConnect.getToken())) {
|
return;
|
||||||
// Authentication success
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Authentication failed
|
|
||||||
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Access denied");
|
// Do not allow connections from other proxies
|
||||||
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Access denied");
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
@ -25,6 +26,40 @@ public final class FlexPlexGraphQLApi {
|
||||||
this.graphqlEndpoint = graphQLEndpoint;
|
this.graphqlEndpoint = graphQLEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> getProxyAddresses() {
|
||||||
|
final Set<String> output = new HashSet<>();
|
||||||
|
try {
|
||||||
|
final URL url = new URL(graphqlEndpoint);
|
||||||
|
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
conn.setConnectTimeout(10000);
|
||||||
|
conn.setReadTimeout(10000);
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
|
|
||||||
|
final DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
|
||||||
|
|
||||||
|
wr.writeBytes("{\"query\":\"query { proxyAddresses }\"}");
|
||||||
|
wr.flush();
|
||||||
|
wr.close();
|
||||||
|
|
||||||
|
final JsonObject response = (JsonObject) new JsonParser().parse(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
if (response.get("data").getAsJsonObject().has("proxyAddresses")) {
|
||||||
|
final JsonArray array = response.get("data").getAsJsonObject().get("proxyAddresses").getAsJsonArray();
|
||||||
|
for (int i = 0; i < array.size(); i++) {
|
||||||
|
output.add(array.get(i).getAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
} catch (final IOException e) {
|
||||||
|
this.flexConnect.getLogger().warning("Updating whitelist data failed, propably connection to FlexPlex API server is down.");
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
public void updateWhitelist() {
|
public void updateWhitelist() {
|
||||||
this.updateWhitelist(this.flexConnect.getServer().hasWhitelist());
|
this.updateWhitelist(this.flexConnect.getServer().hasWhitelist());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
token: "default"
|
token: "default"
|
||||||
|
|
||||||
|
allowedProxies: []
|
||||||
|
|
Loading…
Reference in New Issue