fix(NetworkManager.java): 修复网络代理检测部分...

1.配置文件添加新选项
2.调整代理注入时间
3.更新版本号
This commit is contained in:
502647092 2016-03-28 23:53:10 +08:00
parent e3126b62ed
commit d46dfdf13d
4 changed files with 43 additions and 24 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>Yum</artifactId> <artifactId>Yum</artifactId>
<version>2.0.3</version> <version>2.1</version>
<name>Yum</name> <name>Yum</name>
<description>Minecraft 服务器插件管理系统</description> <description>Minecraft 服务器插件管理系统</description>
<build> <build>

View File

@ -24,6 +24,11 @@ public class Yum extends JavaPlugin {
public FileConfig config; public FileConfig config;
NetworkManager netmgr; NetworkManager netmgr;
@Override
public FileConfig getConfig() {
return config;
}
@Override @Override
public void onDisable() { public void onDisable() {
netmgr.unregister(); netmgr.unregister();
@ -35,8 +40,6 @@ public class Yum extends JavaPlugin {
new YumCommand(this); new YumCommand(this);
new FileCommand(this); new FileCommand(this);
new VersionChecker(this); new VersionChecker(this);
netmgr = new NetworkManager(this);
netmgr.setDebug(true).register();
YumAPI.updaterepo(Bukkit.getConsoleSender()); YumAPI.updaterepo(Bukkit.getConsoleSender());
YumAPI.updatecheck(Bukkit.getConsoleSender()); YumAPI.updatecheck(Bukkit.getConsoleSender());
} }
@ -46,5 +49,7 @@ public class Yum extends JavaPlugin {
config = new FileConfig(this); config = new FileConfig(this);
// 初始化更新列 // 初始化更新列
UpdatePlugin.getUpdateList(); UpdatePlugin.getUpdateList();
// 启用网络注入
netmgr = new NetworkManager().register(this);
} }
} }

View File

@ -11,6 +11,7 @@ import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.config.FileConfig;
import pw.yumc.Yum.Yum; import pw.yumc.Yum.Yum;
/** /**
@ -20,38 +21,42 @@ import pw.yumc.Yum.Yum;
* @author * @author
*/ */
public class NetworkManager { public class NetworkManager {
private static boolean debug; public static void throwException(final Throwable exception) {
NetworkManager.<RuntimeException> throwException0(exception);
private final Yum main;
public NetworkManager(final Yum plugin) {
this.main = plugin;
} }
public void register() { @SuppressWarnings("unchecked")
main.getLogger().info("注入网络代理 将托管服务器网络!"); private static <T extends Throwable> void throwException0(final Throwable exception) throws T {
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault())); throw (T) exception;
} }
public NetworkManager setDebug(final boolean debug) { public NetworkManager register(final Yum plugin) {
NetworkManager.debug = debug; plugin.getLogger().info("注入网络代理 将托管服务器网络!");
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault(), plugin));
return this; return this;
} }
public void unregister() { public void unregister() {
final ProxySelector cur = ProxySelector.getDefault(); final ProxySelector cur = ProxySelector.getDefault();
if (cur instanceof YumProxySelector) { if (cur instanceof YumProxySelector) {
main.getLogger().info("恢复网络代理 使用默认网络!");
ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector()); ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector());
} }
} }
class YumProxySelector extends ProxySelector { class YumProxySelector extends ProxySelector {
private final boolean debug;
private final boolean allowPrimaryThread;
private final Yum main;
private final FileConfig config;
private final ProxySelector defaultSelector; private final ProxySelector defaultSelector;
private final HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>(); private final HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
public YumProxySelector(final ProxySelector defaultSelector) { public YumProxySelector(final ProxySelector defaultSelector, final Yum plugin) {
this.main = plugin;
this.config = plugin.getConfig();
this.defaultSelector = defaultSelector; this.defaultSelector = defaultSelector;
this.debug = config.getBoolean("NetworkDebug");
this.allowPrimaryThread = config.getBoolean("AllowPrimaryThread");
} }
@Override @Override
@ -66,18 +71,19 @@ public class NetworkManager {
@Override @Override
public List<Proxy> select(final URI uri) { public List<Proxy> select(final URI uri) {
if (debug || Bukkit.isPrimaryThread()) { if (debug || Bukkit.isPrimaryThread()) {
try { final Plugin plugin = this.getRequestingPlugin();
final Plugin plugin = this.getRequestingPlugin(); final String urlinfo = uri.toString();
final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath(); if (!urlinfo.startsWith("socket") && !urlinfo.toLowerCase().contains("yumc") && !urlinfo.toLowerCase().contains("pom.xml")) {
final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!"; final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!";
if (plugin == null) { if (plugin == null) {
main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo)); main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo));
Thread.dumpStack(); Thread.dumpStack();
} else if (!plugin.getName().equalsIgnoreCase("Yum")) { } else if (!plugin.getName().equalsIgnoreCase("Yum")) {
main.getLogger().warning(String.format(str, plugin.getName(), urlinfo)); main.getLogger().warning(String.format(str, plugin.getName(), urlinfo));
if (!allowPrimaryThread) {
throwException(new IOException("[NetManager] 已阻止插件 " + plugin.getName() + " 在主线程访问网络!"));
}
} }
} catch (final Exception e) {
e.printStackTrace();
} }
} }
return defaultSelector.select(uri); return defaultSelector.select(uri);
@ -99,7 +105,10 @@ public class NetworkManager {
try { try {
final ClassLoader loader = Class.forName(element.getClassName(), false, getClass().getClassLoader()).getClassLoader(); final ClassLoader loader = Class.forName(element.getClassName(), false, getClass().getClassLoader()).getClassLoader();
if (pluginMap.containsKey(loader)) { if (pluginMap.containsKey(loader)) {
return pluginMap.get(loader); final Plugin p = pluginMap.get(loader);
if (element.getClassName().contains("pw.yumc.Yum.utils.") || !p.getName().equalsIgnoreCase("Yum")) {
return p;
}
} }
} catch (final ClassNotFoundException ex) { } catch (final ClassNotFoundException ex) {
} }

View File

@ -1,4 +1,4 @@
Version: 2.0 Version: 2.1
#是否只允许控制台执行插件命令 #是否只允许控制台执行插件命令
onlyCommandConsole: false onlyCommandConsole: false
#是否只允许控制台执行插件文件命令 #是否只允许控制台执行插件文件命令
@ -13,3 +13,8 @@ ignorelist:
- 'Vault' - 'Vault'
- 'iConomy' - 'iConomy'
- 'GroupManager' - 'GroupManager'
- 'PermissionsEx'
#网络调试模式
NetworkDebug: false
#是否允许插件主线程访问网络
AllowPrimaryThread: false