feat: 网络管理采用事件的形式

This commit is contained in:
502647092 2016-04-09 00:36:42 +08:00
parent 2270006a84
commit 774c1ae62a
4 changed files with 185 additions and 83 deletions

View File

@ -0,0 +1,83 @@
package pw.yumc.Yum.events;
import java.net.URI;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
public class PluginNetworkEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final boolean isPrimaryThread;
private final Plugin plugin;
private boolean cancel;
private URI url;
public static HandlerList getHandlerList() {
return handlers;
}
/**
* 插件联网事件
*
* @param plugin
* 插件
* @param url
* 地址
* @param isMainThread
* 是否为主线程
*/
public PluginNetworkEvent(final Plugin plugin, final URI url, final boolean isPrimaryThread) {
this.plugin = plugin;
this.url = url;
this.isPrimaryThread = isPrimaryThread;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
/**
* @return 插件
*/
public Plugin getPlugin() {
return plugin;
}
/**
* @return 网址
*/
public URI getUrl() {
return url;
}
@Override
public boolean isCancelled() {
return cancel;
}
/**
* @return 是否在主线程
*/
public boolean isPrimaryThread() {
return isPrimaryThread;
}
@Override
public void setCancelled(final boolean value) {
cancel = value;
}
/**
* @param url
* 设置新的URL地址
*/
public void setUrl(final URI url) {
this.url = url;
}
}

View File

@ -0,0 +1,34 @@
package pw.yumc.Yum.listeners;
import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.kit.ExceptionKit;
import pw.yumc.Yum.Yum;
import pw.yumc.Yum.events.PluginNetworkEvent;
import pw.yumc.Yum.managers.ConfigManager;
public class PluginNetworkListener implements Listener {
public PluginNetworkListener(final Yum yum) {
Bukkit.getPluginManager().registerEvents(this, yum);
}
public void onPluginNetworkConect(final PluginNetworkEvent e) {
final Plugin plugin = e.getPlugin();
final String urlinfo = e.getUrl().toString();
final boolean isPrimaryThread = e.isPrimaryThread();
final String str = isPrimaryThread ? "§6[§bYum §a网络管理§6] §c插件 §6%s §c尝试在主线程访问 §e%s §4可能会导致服务器卡顿或无响应!" : "§6[§bYum §a网络监控§6] §c插件 §6%s §c尝试访问 §e%s §c请注意服务器网络安全!";
if (plugin != null) {
Bukkit.getConsoleSender().sendMessage(String.format(str, plugin.getName(), urlinfo));
if (!ConfigManager.isAllowPrimaryThread() && isPrimaryThread) {
Bukkit.getConsoleSender().sendMessage("§6[§bYum §a网络管理§6] §4已阻止插件 §b" + plugin.getName() + " §4在主线程访问网络!");
ExceptionKit.throwException(new IOException("[Yum 网络防护] 已开启网络防护 不允许在主线程访问网络!"));
}
}
}
}

View File

@ -1,83 +0,0 @@
package pw.yumc.Yum.manager;
import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.kit.ExceptionKit;
import cn.citycraft.PluginHelper.kit.PluginKit;
import pw.yumc.Yum.Yum;
/**
* 网络代理处理类
*
* @since 2016年3月26日 下午4:24:09
* @author
*/
public class NetworkManager {
public NetworkManager register(final Yum plugin) {
Bukkit.getConsoleSender().sendMessage("§6[§bYum §a网络管理§6] §a注入网络管理系统 将托管服务器网络!");
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault(), plugin));
return this;
}
public void unregister() {
final ProxySelector cur = ProxySelector.getDefault();
if (cur instanceof YumProxySelector) {
ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector());
}
}
class YumProxySelector extends ProxySelector {
private final boolean debug;
private final boolean allowPrimaryThread;
private final FileConfig config;
private final ProxySelector defaultSelector;
public YumProxySelector(final ProxySelector defaultSelector, final Yum plugin) {
this.config = plugin.getConfig();
this.defaultSelector = defaultSelector;
this.debug = config.getBoolean("NetworkDebug");
this.allowPrimaryThread = config.getBoolean("AllowPrimaryThread");
}
@Override
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
defaultSelector.connectFailed(uri, sa, ioe);
}
public ProxySelector getDefaultSelector() {
return defaultSelector;
}
@Override
public List<Proxy> select(final URI uri) {
final boolean isPrimaryThread = Bukkit.isPrimaryThread();
if (debug || isPrimaryThread) {
final Plugin plugin = PluginKit.getOperatePlugin();
final String urlinfo = uri.toString();
if (!urlinfo.startsWith("socket") && !urlinfo.toLowerCase().contains("yumc") && !urlinfo.toLowerCase().contains("pom.xml") && !urlinfo.toLowerCase().contains("502647092")) {
final String str = isPrimaryThread ? "§6[§bYum §a网络管理§6] §c插件 §6%s §c尝试在主线程访问 §e%s §4可能会导致服务器卡顿或无响应!" : "§6[§bYum §a网络监控§6] §c插件 §6%s §c尝试访问 §e%s §c请注意服务器网络安全!";
if (plugin != null) {
Bukkit.getConsoleSender().sendMessage(String.format(str, plugin.getName(), urlinfo));
if (!allowPrimaryThread && isPrimaryThread) {
Bukkit.getConsoleSender().sendMessage("§6[§bYum §a网络管理§6] §4已阻止插件 §b" + plugin.getName() + " §4在主线程访问网络!");
ExceptionKit.throwException(new IOException("[Yum 网络防护] 已开启网络防护 不允许在主线程访问网络!"));
}
}
}
}
return defaultSelector.select(uri);
}
}
}

View File

@ -0,0 +1,68 @@
package pw.yumc.Yum.managers;
import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.kit.PluginKit;
import pw.yumc.Yum.Yum;
import pw.yumc.Yum.events.PluginNetworkEvent;
/**
* 网络代理处理类
*
* @since 2016年3月26日 下午4:24:09
* @author
*/
public class NetworkManager {
public NetworkManager register(final Yum plugin) {
Bukkit.getConsoleSender().sendMessage("§6[§bYum §a网络管理§6] §a注入网络管理系统 将托管服务器网络!");
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault(), plugin));
return this;
}
public void unregister() {
final ProxySelector cur = ProxySelector.getDefault();
if (cur instanceof YumProxySelector) {
ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector());
}
}
class YumProxySelector extends ProxySelector {
private final ProxySelector defaultSelector;
public YumProxySelector(final ProxySelector defaultSelector, final Yum plugin) {
this.defaultSelector = defaultSelector;
}
@Override
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
defaultSelector.connectFailed(uri, sa, ioe);
}
public ProxySelector getDefaultSelector() {
return defaultSelector;
}
@Override
public List<Proxy> select(final URI uri) {
final boolean isPrimaryThread = Bukkit.isPrimaryThread();
final Plugin plugin = PluginKit.getOperatePlugin();
final PluginNetworkEvent pne = new PluginNetworkEvent(plugin, uri, isPrimaryThread);
Bukkit.getPluginManager().callEvent(pne);
if (pne.isCancelled()) {
return null;
}
return defaultSelector.select(uri);
}
}
}