Created
December 9, 2016 14:41
-
-
Save TuxCoding/99dd5bbd3b35ac3f87121f1509b8f757 to your computer and use it in GitHub Desktop.
Attach a new security manager in order to listen to File and Socket operations on the main thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.games647.lagmonitor; | |
| import com.google.common.collect.ImmutableSet; | |
| import com.google.common.collect.Sets; | |
| import java.io.FilePermission; | |
| import java.net.SocketPermission; | |
| import java.security.Permission; | |
| import java.util.Arrays; | |
| import java.util.Map.Entry; | |
| import java.util.Set; | |
| import java.util.logging.Level; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.plugin.Plugin; | |
| public class BlockingSecurityManager extends SecurityManager { | |
| private final LagMonitor plugin; | |
| private final SecurityManager delegate; | |
| private final Set<PluginViolation> violations = Sets.newConcurrentHashSet(); | |
| private final Set<String> violatedPlugins = Sets.newConcurrentHashSet(); | |
| private final Set<String> fileWhitelist = ImmutableSet.of(".jar", "session.lock"); | |
| public BlockingSecurityManager(LagMonitor plugin, SecurityManager delegate) { | |
| this.plugin = plugin; | |
| this.delegate = delegate; | |
| } | |
| public BlockingSecurityManager(LagMonitor plugin) { | |
| this(plugin, null); | |
| } | |
| public SecurityManager getOldSecurityManager() { | |
| return delegate; | |
| } | |
| @Override | |
| public void checkPermission(Permission perm, Object context) { | |
| if (delegate != null) { | |
| delegate.checkPermission(perm, context); | |
| } | |
| checkMainThreadOperation(perm); | |
| } | |
| @Override | |
| public void checkPermission(Permission perm) { | |
| if (delegate != null) { | |
| delegate.checkPermission(perm); | |
| } | |
| checkMainThreadOperation(perm); | |
| } | |
| private void checkMainThreadOperation(Permission perm) { | |
| if (Bukkit.isPrimaryThread() && isBlockingAction(perm)) { | |
| //warn the admin | |
| } | |
| } | |
| private boolean isBlockingAction(Permission permission) { | |
| String actions = permission.getActions(); | |
| if (permission instanceof FilePermission) { | |
| //which could executed by the main thread, doesn't it`? | |
| return (actions.contains("read") || actions.contains("write")) | |
| && fileWhitelist.stream().noneMatch(ignored -> permission.getName().contains(ignored)); | |
| //read write | |
| } else if (permission instanceof SocketPermission) { | |
| //on new socket connections | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment