Skip to content

Commit

Permalink
Modules formatting fixes and some vars fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaxar163 committed Jan 2, 2019
1 parent eec958f commit ec14316
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public class AddHashModule implements Module {
private static boolean registred = false;
public static final Version version = new Version(1, 0, 2, 1, Version.Type.LTS);
@Override
public void close() {

Expand All @@ -20,7 +21,7 @@ public String getName() {

@Override
public Version getVersion() {
return new Version(1, 0, 2, 0);
return version;
}

@Override
Expand All @@ -44,4 +45,9 @@ public void preInit(ModuleContext context1) {
public void postInit(ModuleContext context1) {

}

public static void main(String[] args)
{
System.err.println("This is module, use with GravitLauncher`s LaunchServer.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,10 @@
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class AntiDDoSModule implements Module, Reloadable, Reconfigurable {
public static Version version = new Version(1,0,0,0,Version.Type.BETA);
public static Path configfile = Paths.get("anti-ddos.json");

@Override
public void reload() throws Exception {
try(Reader reader = IOHelper.newReader(configfile)) {
config = LaunchServer.gson.fromJson(reader,Config.class);
} catch (IOException e) {
LogHelper.error(e);
}
}

@Override
public void reconfig(String action, String[] args) {
if(action.equals("clear"))
{
banIPProtector.banlist.clear();
LogHelper.info("IP BanList clean");
}
else if(action.equals("remove"))
{
banIPProtector.banlist.remove(args[0]);
LogHelper.info("IP %s unbanned",args[0]);
}
}

@Override
public void printConfigHelp() {
LogHelper.info("clean [none] - clean banlist");
LogHelper.info("remove [ip] - remove ip from banlist");
}
public static final Version version = new Version(1, 0, 1, 3, Version.Type.BETA);

public static class Config
{
Expand All @@ -58,8 +27,12 @@ public static class Config
public boolean printTryConnectionMessage = true;
public ArrayList<String> whitelist;
}
public static Config config;
public static BanIPProtector banIPProtector;

public Path configfile;
public Config config;
public BanIPProtector banIPProtector;
public LaunchServer srv;

@Override
public String getName() {
return "Gravit Anti-DDoS";
Expand All @@ -79,6 +52,8 @@ public int getPriority() {
public void init(ModuleContext context1) {
LaunchServerModuleContext context = (LaunchServerModuleContext) context1;
LogHelper.debug("Init anti-DDoS");
srv = context.launchServer;
configfile = context.launchServer.dir.resolve("anti-ddos.json");
if(IOHelper.exists(configfile))
{
try(Reader reader = IOHelper.newReader(configfile)) {
Expand All @@ -98,7 +73,7 @@ public void init(ModuleContext context1) {
LogHelper.error(e);
}
}
banIPProtector = new BanIPProtector();
banIPProtector = new BanIPProtector( this);
banIPProtector.whitelist.addAll(config.whitelist);
context.launchServer.socketHookManager.registerFatalErrorHook(banIPProtector);
context.launchServer.reloadManager.registerReloadable("antiddos",this);
Expand All @@ -116,12 +91,42 @@ public void preInit(ModuleContext context) {

}

@Override
public void reload() throws Exception {
try(Reader reader = IOHelper.newReader(configfile)) {
config = LaunchServer.gson.fromJson(reader,Config.class);
} catch (IOException e) {
LogHelper.error(e);
}
}

@Override
public void reconfig(String action, String[] args) {
if(action.equals("clear"))
{
banIPProtector.banlist.clear();
LogHelper.info("IP BanList clean");
}
else if(action.equals("remove"))
{
banIPProtector.banlist.remove(args[0]);
LogHelper.info("IP %s unbanned",args[0]);
}
}

@Override
public void printConfigHelp() {
LogHelper.info("clean [none] - clean banlist");
LogHelper.info("remove [ip] - remove ip from banlist");
}

@Override
public void close() throws Exception {

}

public static void main(String[] args)
{

System.err.println("This is module, use with GravitLauncher`s LaunchServer.");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ru.gravit.launchermodules.antiddos;

import ru.gravit.launchserver.manangers.hook.SocketHookManager;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.ServerSocketHandler;
import ru.gravit.launchserver.socket.SocketContext;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;

Expand All @@ -15,20 +13,26 @@
import java.util.concurrent.ConcurrentHashMap;

public class BanIPProtector implements SocketHookManager.SocketFatalErrorHook, ServerSocketHandler.Listener {
@Override
private AntiDDoSModule mod;

public BanIPProtector(AntiDDoSModule mod) {
this.mod = mod;
}

@Override
public boolean fatalErrorHook(Socket socket, Exception ex) {
String ip = IOHelper.getIP(socket.getRemoteSocketAddress());
if(whitelist.contains(ip)) return !AntiDDoSModule.config.disableSocketFatalErrors;
if(whitelist.contains(ip)) return !mod.config.disableSocketFatalErrors;
if(!banlist.containsKey(ip)) banlist.put(ip,new Entry(1));
else {
Entry e = banlist.get(ip);
e.fails++;
if(AntiDDoSModule.config.printBannedMessage && e.fails >= AntiDDoSModule.config.maxFails)
if(mod.config.printBannedMessage && e.fails >= mod.config.maxFails)
{
LogHelper.warning("IP %s banned",ip);
}
}
return !AntiDDoSModule.config.disableSocketFatalErrors;
return !mod.config.disableSocketFatalErrors;
}

@Override
Expand All @@ -38,10 +42,10 @@ public boolean onConnect(InetAddress address) {
else
{
Entry e = banlist.get(ip);
if(e.fails >= AntiDDoSModule.config.maxFails)
if(e.fails >= mod.config.maxFails)
{
e.tryconnection++;
if(AntiDDoSModule.config.printTryConnectionMessage)
if(mod.config.printTryConnectionMessage)
LogHelper.info("IP %s try connection #%d",ip,e.tryconnection);
return false;
}
Expand All @@ -55,7 +59,7 @@ public void onDisconnect(Exception e) {
}

@Override
public boolean onHandshake(long session, Integer type) {
public boolean onHandshake(long session, int type) {
return false;
}

Expand Down

0 comments on commit ec14316

Please sign in to comment.