Skip to content

Commit

Permalink
Added option to use a bypass permission - at the cost of fully proces…
Browse files Browse the repository at this point in the history
…sing logins.

Removed .idea directory from repo
Now using MalformedURLException instead of a supertype on web requests
Why the hell was the project in CRLF when LF is the default?!

Signed-off-by: itsTyrion <[email protected]>
  • Loading branch information
itsTyrion committed Feb 21, 2021
1 parent e2bb008 commit 69e8eb7
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 155 deletions.
13 changes: 1 addition & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
### Intellij ###

# User-specific stuff
.idea/**/workspace.xml
.idea/**/dictionaries
.idea/statistic.xml
.idea/discord.xml

# Sensitive or high-churn files
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries
.idea

### Gradle ###
.gradle
Expand Down
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions .idea/AntiVPNVelocity.iml

This file was deleted.

50 changes: 0 additions & 50 deletions .idea/compiler.xml

This file was deleted.

30 changes: 0 additions & 30 deletions .idea/jarRepositories.xml

This file was deleted.

13 changes: 0 additions & 13 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/modules/AntiVPNVelocity.main.iml

This file was deleted.

2 changes: 0 additions & 2 deletions .idea/modules/BlockVPNVelocity.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'de.itsTyrion'
version '1.2'
version '1.3'

repositories {
mavenCentral()
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/de/itsTyrion/antiVPN/AntiVPN.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.connection.PreLoginEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
Expand Down Expand Up @@ -47,7 +48,10 @@ public AntiVPN(ProxyServer server, Logger logger, @DataDirectory Path folder) th

@Subscribe
public void onInit(ProxyInitializeEvent event) {
server.getEventManager().register(this, PreLoginEvent.class, Check::preLogin);
if (config.getBoolean("preLogin", true)) {
server.getEventManager().register(this, PreLoginEvent.class, Check::preLogin);
} else
server.getEventManager().register(this, LoginEvent.class, Check::onLogin);
}

/**
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/de/itsTyrion/antiVPN/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.connection.PreLoginEvent;
import lombok.val;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
* This plugin uses the iplegit.com API, which is, without payment, limited to 5000 requests per day.
* Utility class to check incoming log-in requests.
*
* @author itsTyrion
* Created on 20.01.2021
Expand All @@ -22,21 +24,37 @@ class Check {
);

static void preLogin(PreLoginEvent e) {
val ip = e.getConnection().getRemoteAddress().getAddress().getHostAddress();
try {
if (isIPBad(ip)) {
e.setResult(PreLoginEvent.PreLoginComponentResult.denied(kickMessage));
if (isBadIP(e.getConnection().getRemoteAddress().getAddress().getHostAddress(), e.getUsername())) {

e.setResult(PreLoginEvent.PreLoginComponentResult.denied(kickMessage));
}
}

static void onLogin(LoginEvent e) {
if (e.getPlayer().hasPermission(AntiVPN.getConfig().getString("bypassPermission"))) {
return;
}
if (isBadIP(e.getPlayer().getRemoteAddress().getAddress().getHostAddress(), e.getPlayer().getUsername())) {

e.setResult(ResultedEvent.ComponentResult.denied(kickMessage));
}
}

static boolean isBadIP(String address, String username) {
try {
if (queryBadIp(address)) {
if (AntiVPN.getConfig().getBoolean("logFailedAttempts", true)) {
AntiVPN.getInstance().getLogger().info(e.getUsername() + " tried to join with VPN (" + ip + ")");
AntiVPN.getInstance().getLogger().info("Blocked " + username + " from joining (" + address + ")");
}
return true;
}
} catch (IOException | JsonParserException ex) {
} catch (MalformedURLException | JsonParserException ex) {
ex.printStackTrace();
}
return false;
}

private static boolean isIPBad(String ip) throws IOException, JsonParserException {
private static boolean queryBadIp(String ip) throws MalformedURLException, JsonParserException {
val bad = IPCache.INSTANCE.isBadIP(ip);
if (bad != null)
return bad;
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"kickMessage": "&cVPNs/Proxies are not allowed",
"__comment": "Cache duration in hours",
"ipCacheDuration": 6,
"logFailedAttempts": true
"logFailedAttempts": true,
"preLogin": true,
"bypassPermission": "antivpn.bypass"
}

0 comments on commit 69e8eb7

Please sign in to comment.