mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-06-19 18:10:40 +00:00
Implementing new configuration system
This commit is contained in:
@@ -1,195 +0,0 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.bungee.util.ConfigDefault;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BungeeConfig implements VPNConfig {
|
||||
private final ConfigDefault<String> licenseDefault = new ConfigDefault<>("",
|
||||
"license", BungeePlugin.pluginInstance), kickStringDefault =
|
||||
new ConfigDefault<>("Proxies are not allowed on our server",
|
||||
"kickMessage", BungeePlugin.pluginInstance),
|
||||
defaultDatabaseType = new ConfigDefault<>("H2",
|
||||
"database.type", BungeePlugin.pluginInstance),
|
||||
defaultDatabaseName = new ConfigDefault<>("kaurivpn",
|
||||
"database.database", BungeePlugin.pluginInstance),
|
||||
defaultMongoURL = new ConfigDefault<>("", "database.mongoURL", BungeePlugin.pluginInstance),
|
||||
defaultUsername = new ConfigDefault<>("root",
|
||||
"database.username", BungeePlugin.pluginInstance),
|
||||
defaultPassword = new ConfigDefault<>("password",
|
||||
"database.password", BungeePlugin.pluginInstance),
|
||||
defaultIp = new ConfigDefault<>("localhost", "database.ip", BungeePlugin.pluginInstance),
|
||||
defaultAlertMsg = new ConfigDefault<>("&8[&6KauriVPN&8] &e%player% &7has joined on a VPN/proxy" +
|
||||
" &8(&f%reason%&8) &7in location &8(&f%city%&7, &f%country%&8)", "alerts.message",
|
||||
BungeePlugin.pluginInstance);
|
||||
private final ConfigDefault<Boolean> cacheResultsDefault = new ConfigDefault<>(true,
|
||||
"cachedResults", BungeePlugin.pluginInstance),
|
||||
defaultDatabaseEnabled = new ConfigDefault<>(false, "database.enabled",
|
||||
BungeePlugin.pluginInstance), defaultCommandsEnable = new ConfigDefault<>(false,
|
||||
"commands.enabled", BungeePlugin.pluginInstance), defaultKickPlayers
|
||||
= new ConfigDefault<>(true, "kickPlayers", BungeePlugin.pluginInstance),
|
||||
defaultAlertToStaff = new ConfigDefault<>(true, "alerts.enabled",
|
||||
BungeePlugin.pluginInstance),
|
||||
defaultUseCredentials = new ConfigDefault<>(true,
|
||||
"database.useCredentials", BungeePlugin.pluginInstance),
|
||||
defaultMetrics = new ConfigDefault<>(true, "bstats", BungeePlugin.pluginInstance);
|
||||
private final ConfigDefault<Integer>
|
||||
defaultPort = new ConfigDefault<>(-1, "database.port", BungeePlugin.pluginInstance);
|
||||
private final ConfigDefault<List<String>> prefixWhitelistsDefault = new ConfigDefault<>(new ArrayList<>(),
|
||||
"prefixWhitelists", BungeePlugin.pluginInstance), defaultCommands = new ConfigDefault<>(
|
||||
Collections.singletonList("kick %player% VPNs are not allowed on our server!"), "commands.execute",
|
||||
BungeePlugin.pluginInstance),
|
||||
defBlockedCountries = new ConfigDefault<>(new ArrayList<>(), "blockedCountries",
|
||||
BungeePlugin.pluginInstance),
|
||||
defAllowedCountries = new ConfigDefault<>(new ArrayList<>(), "allowedCountries",
|
||||
BungeePlugin.pluginInstance);
|
||||
|
||||
private String license, kickMessage, databaseType, databaseName, mongoURL, username, password, ip, alertMsg;
|
||||
private List<String> prefixWhitelists, commands, allowedCountries, blockedCountries;
|
||||
private int port;
|
||||
private boolean cacheResults, useCredentials, databaseEnabled, commandsEnabled, kickPlayers, alertToStaff, metrics;
|
||||
|
||||
@Override
|
||||
public String getLicense() {
|
||||
return license;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cachedResults() {
|
||||
return cacheResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKickString() {
|
||||
return kickMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alertMessage() {
|
||||
return alertMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean alertToStaff() {
|
||||
return alertToStaff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean runCommands() {
|
||||
return commandsEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean kickPlayersOnDetect() {
|
||||
return kickPlayers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getPrefixWhitelists() {
|
||||
return prefixWhitelists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDatabaseEnabled() {
|
||||
return databaseEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useDatabaseCreds() {
|
||||
return useCredentials;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mongoDatabaseURL() {
|
||||
return mongoURL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDatabaseType() {
|
||||
return databaseType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDatabaseName() {
|
||||
return databaseName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> allowedCountries() {
|
||||
return allowedCountries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> blockedCountries() {
|
||||
return blockedCountries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
if(port == -1) {
|
||||
switch (getDatabaseType().toLowerCase()) {
|
||||
case "mongodb":
|
||||
case "mongo":
|
||||
case "mongod":
|
||||
return 27017;
|
||||
case "sql":
|
||||
case "mysql":
|
||||
return 3306;
|
||||
}
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean metrics() {
|
||||
return metrics;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
license = licenseDefault.get();
|
||||
kickMessage = kickStringDefault.get();
|
||||
cacheResults = cacheResultsDefault.get();
|
||||
prefixWhitelists = prefixWhitelistsDefault.get();
|
||||
databaseEnabled = defaultDatabaseEnabled.get();
|
||||
useCredentials = defaultUseCredentials.get();
|
||||
databaseType = defaultDatabaseType.get();
|
||||
databaseName = defaultDatabaseName.get();
|
||||
mongoURL = defaultMongoURL.get();
|
||||
username = defaultUsername.get();
|
||||
password = defaultPassword.get();
|
||||
ip = defaultIp.get();
|
||||
port = defaultPort.get();
|
||||
commandsEnabled = defaultCommandsEnable.get();
|
||||
commands = defaultCommands.get();
|
||||
kickPlayers = defaultKickPlayers.get();
|
||||
alertToStaff = defaultAlertToStaff.get();
|
||||
alertMsg = defaultAlertMsg.get();
|
||||
metrics = defaultMetrics.get();
|
||||
blockedCountries = defBlockedCountries.get();
|
||||
allowedCountries = defAllowedCountries.get();
|
||||
}
|
||||
}
|
||||
@@ -52,31 +52,31 @@ public class BungeeListener extends VPNExecutor implements Listener {
|
||||
//Or has a name that starts with a certain prefix. This is for Bedrock exempting.
|
||||
|| AntiVPN.getInstance().getExecutor().isWhitelisted(event.getPlayer().getAddress().getAddress()
|
||||
.getHostAddress())
|
||||
|| AntiVPN.getInstance().getConfig().getPrefixWhitelists().stream()
|
||||
|| AntiVPN.getInstance().getVpnConfig().getPrefixWhitelists().stream()
|
||||
.anyMatch(prefix -> event.getPlayer().getName().startsWith(prefix))) return;
|
||||
|
||||
checkIp(event.getPlayer().getAddress().getAddress().getHostAddress(),
|
||||
AntiVPN.getInstance().getConfig().cachedResults(), result -> {
|
||||
AntiVPN.getInstance().getVpnConfig().cachedResults(), result -> {
|
||||
if(result.isSuccess() && result.isProxy()) {
|
||||
if(AntiVPN.getInstance().getConfig().kickPlayersOnDetect())
|
||||
if(AntiVPN.getInstance().getVpnConfig().kickPlayersOnDetect())
|
||||
event.getPlayer().disconnect(TextComponent.fromLegacyText(ChatColor
|
||||
.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getConfig().getKickString())));
|
||||
AntiVPN.getInstance().getVpnConfig().getKickString())));
|
||||
BungeeCord.getInstance().getLogger().info(event.getPlayer().getName()
|
||||
+ " joined on a VPN/Proxy (" + result.getMethod() + ")");
|
||||
|
||||
if(AntiVPN.getInstance().getConfig().alertToStaff()) //Ensuring the user wishes to alert to staff
|
||||
if(AntiVPN.getInstance().getVpnConfig().alertToStaff()) //Ensuring the user wishes to alert to staff
|
||||
AntiVPN.getInstance().getPlayerExecutor().getOnlinePlayers().stream()
|
||||
.filter(APIPlayer::isAlertsEnabled)
|
||||
.forEach(pl -> pl.sendMessage(AntiVPN.getInstance().getConfig().alertMessage()
|
||||
.forEach(pl -> pl.sendMessage(AntiVPN.getInstance().getVpnConfig().alertMessage()
|
||||
.replace("%player%", event.getPlayer().getName())
|
||||
.replace("%reason%", result.getMethod())
|
||||
.replace("%country%", result.getCountryName())
|
||||
.replace("%city%", result.getCity())));
|
||||
|
||||
//In case the user wants to run their own commands instead of using the built in kicking
|
||||
if(AntiVPN.getInstance().getConfig().runCommands()) {
|
||||
for (String command : AntiVPN.getInstance().getConfig().commands()) {
|
||||
if(AntiVPN.getInstance().getVpnConfig().runCommands()) {
|
||||
for (String command : AntiVPN.getInstance().getVpnConfig().commands()) {
|
||||
BungeeCord.getInstance().getPluginManager()
|
||||
.dispatchCommand(BungeeCord.getInstance().getConsole(),
|
||||
ChatColor.translateAlternateColorCodes('&',
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.bungee.util.Config;
|
||||
import dev.brighten.antivpn.bungee.util.ConfigDefault;
|
||||
import dev.brighten.antivpn.command.Command;
|
||||
import lombok.Getter;
|
||||
import dev.brighten.antivpn.utils.ConfigDefault;
|
||||
import lombok.val;
|
||||
import net.md_5.bungee.BungeeCord;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
@@ -24,8 +22,6 @@ public class BungeePlugin extends Plugin {
|
||||
|
||||
public static BungeePlugin pluginInstance;
|
||||
|
||||
@Getter
|
||||
private Config config;
|
||||
private SingleLineChart vpnDetections, ipsChecked;
|
||||
|
||||
private static final BaseComponent[] noPermission = new ComponentBuilder("No permission").color(ChatColor.RED)
|
||||
@@ -37,14 +33,13 @@ public class BungeePlugin extends Plugin {
|
||||
|
||||
//Setting up config
|
||||
BungeeCord.getInstance().getLogger().info("Loading config...");
|
||||
config = new Config();
|
||||
|
||||
|
||||
//Loading plugin
|
||||
BungeeCord.getInstance().getLogger().info("Starting AntiVPN services...");
|
||||
AntiVPN.start(new BungeeConfig(), new BungeeListener(), new BungeePlayerExecutor(), getDataFolder());
|
||||
AntiVPN.start(new BungeeListener(), new BungeePlayerExecutor(), getDataFolder());
|
||||
|
||||
if(AntiVPN.getInstance().getConfig().metrics()) {
|
||||
if(AntiVPN.getInstance().getVpnConfig().metrics()) {
|
||||
BungeeCord.getInstance().getLogger().info("Starting bStats metrics...");
|
||||
Metrics metrics = new Metrics(this, 12616);
|
||||
metrics.addCustomChart(vpnDetections = new SingleLineChart("vpn_detections",
|
||||
@@ -103,7 +98,7 @@ public class BungeePlugin extends Plugin {
|
||||
|
||||
BungeeCord.getInstance().getLogger().info("Getting strings...");
|
||||
AntiVPN.getInstance().getMessageHandler().initStrings(vpnString -> new ConfigDefault<>
|
||||
(vpnString.getDefaultMessage(), "messages." + vpnString.getKey(), BungeePlugin.pluginInstance)
|
||||
(vpnString.getDefaultMessage(), "messages." + vpnString.getKey(), AntiVPN.getInstance())
|
||||
.get());
|
||||
//TODO Finish system before implementing on startup
|
||||
/*BungeeCord.getInstance().getLogger().info("Getting strings...");
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
package dev.brighten.antivpn.bungee.util;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import dev.brighten.antivpn.bungee.BungeePlugin;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Author: nitramleo (Martin)
|
||||
* Date created: 10-Aug-18
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
private File file;
|
||||
private Configuration configuration;
|
||||
|
||||
public Config() {
|
||||
this.file = new File(BungeePlugin.pluginInstance.getDataFolder(), "config.yml");
|
||||
try {
|
||||
if (!this.file.exists()) {
|
||||
if (!BungeePlugin.pluginInstance.getDataFolder().exists()) {
|
||||
BungeePlugin.pluginInstance.getDataFolder().mkdir();
|
||||
}
|
||||
this.file.createNewFile();
|
||||
try (final InputStream is = BungeePlugin.pluginInstance.getResourceAsStream("config.yml");
|
||||
final OutputStream os = new FileOutputStream(this.file)) {
|
||||
ByteStreams.copy(is, os);
|
||||
}
|
||||
}
|
||||
this.configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(this.file);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void load() {
|
||||
this.file = new File(BungeePlugin.pluginInstance.getDataFolder(), "config.yml");
|
||||
try {
|
||||
this.configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(this.file);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
ConfigurationProvider.getProvider( YamlConfiguration.class).save(this.configuration, this.file);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Configuration getConfiguration() {
|
||||
return this.configuration;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
public double getDouble(final String path) {
|
||||
if (this.configuration.get(path) != null) {
|
||||
return this.configuration.getDouble(path);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
public int getInt(final String path) {
|
||||
if (this.configuration.get(path) != null) {
|
||||
return this.configuration.getInt(path);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Object get(final String path) {
|
||||
return this.configuration.get(path);
|
||||
}
|
||||
|
||||
public void set(final String path, final Object object) {
|
||||
configuration.set(path, object);
|
||||
}
|
||||
|
||||
public boolean getBoolean(final String path) {
|
||||
return this.configuration.get(path) != null && this.configuration.getBoolean(path);
|
||||
}
|
||||
|
||||
public String getString(final String path) {
|
||||
if (this.configuration.get(path) != null) {
|
||||
return ChatColor.translateAlternateColorCodes('&', this.configuration.getString(path));
|
||||
}
|
||||
return "String at path: " + path + " not found!";
|
||||
}
|
||||
|
||||
public List<String> getStringList(final String path) {
|
||||
if (this.configuration.get(path) != null) {
|
||||
final ArrayList<String> strings = new ArrayList<String>();
|
||||
for (final String string : this.configuration.getStringList(path)) {
|
||||
strings.add(ChatColor.translateAlternateColorCodes('&', string));
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
return Arrays.asList("String List at path: " + path + " not found!");
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package dev.brighten.antivpn.bungee.util;
|
||||
|
||||
import dev.brighten.antivpn.bungee.BungeePlugin;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class ConfigDefault<A> {
|
||||
|
||||
private final A defaultValue;
|
||||
private final String path;
|
||||
private final BungeePlugin plugin;
|
||||
|
||||
public A get() {
|
||||
if(plugin.getConfig().get(path) != null)
|
||||
return (A) plugin.getConfig().get(path);
|
||||
else {
|
||||
plugin.getConfig().set(path, defaultValue);
|
||||
plugin.getConfig().save();
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public A set(A value) {
|
||||
plugin.getConfig().set(path, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user