Skip to content

Commit

Permalink
Add logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jul 25, 2021
1 parent 370800f commit 563e435
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
23 changes: 16 additions & 7 deletions src/main/java/cc/l89669/nonupdate/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cc.l89669.nonupdate;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;

import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;

public class Configuration {

private final ForgeConfigSpec spec;
private final EnumValue<LoggingLevel> loggingLevel;
private final ForgeConfigSpec.BooleanValue defaultBlocking;
private final ConfigValue<List<? extends String>> blacklist;

Expand All @@ -17,27 +20,33 @@ public Configuration() {
final ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
builder.comment("General settings for the mod.");
builder.push("general");
builder.comment("Use blacklist as whitelist?");
builder.comment("Logging level: MUTE, BLOCKED, DETAILED, VERBOSE", "MUTE: do not output anything", "BLOCKED: output blocked info", "DETAILED: output detailed blocked info", "VERBOSE: output every attemption, blocked and permitted");
this.loggingLevel = builder.defineEnum("loggingLevel", LoggingLevel.BLOCKED);
builder.comment("Default block all address and use blacklist as whitelist?");
this.defaultBlocking = builder.define("defaultBlocking", false);
builder.comment("Blocking/Allowing sites");
this.blacklist = builder.defineList("blacklist", new ArrayList<String>(), line -> true);
builder.comment("Blocked/Allowed domain/ip address");
this.blacklist = builder.defineList("blacklist", Lists.newArrayList("raw.githubusercontent.com", "yumc.pw"), line -> true);
this.spec = builder.build();
}

public ForgeConfigSpec getSpec() {
return this.spec;
}

public LoggingLevel loggingLevel() {
return this.loggingLevel.get();
}

public boolean defaultBlocking() {
return this.defaultBlocking.get().booleanValue();
}

public boolean checkURL(String url) {
public boolean checkBlockURL(String url) {
for (String line : blacklist.get()) {
if (url.contains(line.toLowerCase()))
return true;
return this.defaultBlocking() ^ true;
}
return false;
return this.defaultBlocking() ^ false;
}

}
20 changes: 20 additions & 0 deletions src/main/java/cc/l89669/nonupdate/LoggingLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cc.l89669.nonupdate;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum LoggingLevel {

MUTE(0),
BLOCKED(1),
DETAILED(2),
VERBOSE(3);

private final @Getter int weight;

public boolean contains(LoggingLevel level) {
return this.weight >= level.weight;
}

}
19 changes: 13 additions & 6 deletions src/main/java/cc/l89669/nonupdate/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ public class NetworkManager extends SecurityManager {
public void checkPermission(final Permission perm) {
if (perm instanceof SocketPermission) {
SocketPermission socketperm = (SocketPermission) perm;
NonUpdate.LOGGER.info("[NonUpdate] Attemption: " + socketperm.getActions() + " " + socketperm.getName());
LoggingLevel logLevel = NonUpdate.configuration.loggingLevel();
if (logLevel.contains(LoggingLevel.VERBOSE))
NonUpdate.LOGGER.info("[NonUpdate] Attemption: " + socketperm.getActions() + " " + socketperm.getName());
if (socketperm.getName().indexOf(':') == -1 && socketperm.getName().indexOf('.') == -1)
return; // sockets, for some special uses
if (socketperm.getName().startsWith("localhost:") || socketperm.getName().startsWith("127.0.0.1:"))
return; // local host, never lags
if (socketperm.getActions().contains("listen") || socketperm.getActions().contains("accept"))
return; // server is trying to communicate with players! never block them!
if (socketperm.getActions().contains("connect") || socketperm.getActions().contains("resolve")) {
boolean defaultBlocking = NonUpdate.configuration.defaultBlocking();
boolean blocking = NonUpdate.configuration.checkURL(socketperm.getName().toLowerCase());
if (defaultBlocking)
blocking = !blocking;
NonUpdate.LOGGER.info("[NonUpdate] Status: blocking={}, defaultBlocking={}", blocking, defaultBlocking);
boolean blocking = NonUpdate.configuration.checkBlockURL(socketperm.getName().toLowerCase());

if (logLevel.contains(LoggingLevel.VERBOSE)) {
NonUpdate.LOGGER.info("[NonUpdate] Status: blocking={}, defaultBlocking={}", blocking, NonUpdate.configuration.defaultBlocking());
} else if (blocking && logLevel.contains(LoggingLevel.DETAILED)) {
NonUpdate.LOGGER.info("[NonUpdate] Attemption: " + socketperm.getActions() + " " + socketperm.getName());
NonUpdate.LOGGER.info("[NonUpdate] Status: blocking={}, defaultBlocking={}", blocking, NonUpdate.configuration.defaultBlocking());
} else if (blocking && logLevel.contains(LoggingLevel.BLOCKED)) {
NonUpdate.LOGGER.info("[NonUpdate] Blocked: {}", socketperm.getName());
}
if (blocking)
throw new MalformedURLException("Socket connection blocked");
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cc/l89669/nonupdate/NonUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
import com.electronwill.nightconfig.toml.TomlFormat;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
Expand All @@ -21,13 +20,14 @@ public class NonUpdate {

public NonUpdate() {
configuration = new Configuration();
// Hacky, load as early as possible
configuration.getSpec().setConfig(TomlFormat.instance().createParser().parse(new File("config", "nonupdate-common.toml"), FileNotFoundAction.READ_NOTHING));
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, configuration.getSpec());

// Register the setup method for modloading
// FMLJavaModLoadingContext.get().getModEventBus().addListener(this::init);

// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
// MinecraftForge.EVENT_BUS.register(this);

System.setSecurityManager(new NetworkManager());
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ description='''
This mod disables network connections to certain address to prevent client freezing
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.examplemod]] #optional
[[dependencies.nonupdate]] #optional
# the modid of the dependency
modId="forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
Expand All @@ -47,12 +47,12 @@ This mod disables network connections to certain address to prevent client freez
# An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory
ordering="NONE"
# Side this dependency is applied on - BOTH, CLIENT or SERVER
side="CLIENT"
side="BOTH"
# Here's another dependency
[[dependencies.examplemod]]
[[dependencies.nonupdate]]
modId="minecraft"
mandatory=true
# This version range declares a minimum of the current minecraft version up to but not including the next major version
versionRange="[1.16.5,1.17)"
ordering="NONE"
side="CLIENT"
side="BOTH"

0 comments on commit 563e435

Please sign in to comment.