mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-06-03 10:22:21 +00:00
Completed new antivpn plugin
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.bungee.util.ConfigDefault;
|
||||
|
||||
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);
|
||||
private final ConfigDefault<Boolean> cacheResultsDefault = new ConfigDefault<>(true,
|
||||
"cachedResults", BungeePlugin.pluginInstance);
|
||||
|
||||
private String license, kickMessage;
|
||||
private boolean cacheResults;
|
||||
|
||||
@Override
|
||||
public String getLicense() {
|
||||
return license;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cachedResults() {
|
||||
return cacheResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKickString() {
|
||||
return kickMessage;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
license = licenseDefault.get();
|
||||
kickMessage = kickStringDefault.get();
|
||||
cacheResults = cacheResultsDefault.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.api.VPNExecutor;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.event.PostLoginEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.api.scheduler.ScheduledTask;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BungeeListener extends VPNExecutor implements Listener {
|
||||
|
||||
private ScheduledTask cacheResetTask;
|
||||
|
||||
@Override
|
||||
public void registerListeners() {
|
||||
BungeePlugin.pluginInstance.getProxy().getPluginManager()
|
||||
.registerListener(BungeePlugin.pluginInstance, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runCacheReset() {
|
||||
cacheResetTask = BungeePlugin.pluginInstance.getProxy().getScheduler().schedule(BungeePlugin.pluginInstance,
|
||||
this::resetCache, 20, 20, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
if(cacheResetTask != null) {
|
||||
cacheResetTask.cancel();
|
||||
cacheResetTask = null;
|
||||
}
|
||||
threadExecutor.shutdown();
|
||||
BungeePlugin.pluginInstance.getProxy().getPluginManager().unregisterListener(this);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onListener(final PostLoginEvent event) {
|
||||
checkIp(event.getPlayer().getAddress().getAddress().getHostAddress(),
|
||||
AntiVPN.getInstance().getConfig().cachedResults(), result -> {
|
||||
if(result.isSuccess() && result.isProxy()) {
|
||||
event.getPlayer().disconnect(TextComponent.fromLegacyText(ChatColor
|
||||
.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getConfig().getKickString())));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.bungee.util.Config;
|
||||
import lombok.Getter;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
public class BungeePlugin extends Plugin {
|
||||
|
||||
public static BungeePlugin pluginInstance;
|
||||
|
||||
@Getter
|
||||
private Config config;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
pluginInstance = this;
|
||||
|
||||
//Setting up config
|
||||
config = new Config();
|
||||
|
||||
//Loading plugin
|
||||
AntiVPN.start(new BungeeConfig(), new BungeeListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
AntiVPN.getInstance().stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
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!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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