卸载插件是还原网络代理...

This commit is contained in:
502647092 2016-03-27 23:42:25 +08:00
parent 4de1213c3e
commit e3126b62ed
2 changed files with 34 additions and 20 deletions

View File

@ -12,6 +12,7 @@ import cn.citycraft.PluginHelper.utils.VersionChecker;
import pw.yumc.Yum.api.YumAPI; import pw.yumc.Yum.api.YumAPI;
import pw.yumc.Yum.commands.FileCommand; import pw.yumc.Yum.commands.FileCommand;
import pw.yumc.Yum.commands.YumCommand; import pw.yumc.Yum.commands.YumCommand;
import pw.yumc.Yum.manager.NetworkManager;
/** /**
* MC插件仓库 * MC插件仓库
@ -21,6 +22,12 @@ import pw.yumc.Yum.commands.YumCommand;
*/ */
public class Yum extends JavaPlugin { public class Yum extends JavaPlugin {
public FileConfig config; public FileConfig config;
NetworkManager netmgr;
@Override
public void onDisable() {
netmgr.unregister();
}
@Override @Override
public void onEnable() { public void onEnable() {
@ -28,7 +35,8 @@ public class Yum extends JavaPlugin {
new YumCommand(this); new YumCommand(this);
new FileCommand(this); new FileCommand(this);
new VersionChecker(this); new VersionChecker(this);
// new NetworkManager(this).setDebug(true).register(); netmgr = new NetworkManager(this);
netmgr.setDebug(true).register();
YumAPI.updaterepo(Bukkit.getConsoleSender()); YumAPI.updaterepo(Bukkit.getConsoleSender());
YumAPI.updatecheck(Bukkit.getConsoleSender()); YumAPI.updatecheck(Bukkit.getConsoleSender());
} }

View File

@ -22,8 +22,6 @@ import pw.yumc.Yum.Yum;
public class NetworkManager { public class NetworkManager {
private static boolean debug; private static boolean debug;
private static HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
private final Yum main; private final Yum main;
public NetworkManager(final Yum plugin) { public NetworkManager(final Yum plugin) {
@ -31,6 +29,7 @@ public class NetworkManager {
} }
public void register() { public void register() {
main.getLogger().info("注入网络代理 将托管服务器网络!");
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault())); ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault()));
} }
@ -42,12 +41,14 @@ public class NetworkManager {
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 ProxySelector defaultSelector; private final ProxySelector defaultSelector;
private final HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
public YumProxySelector(final ProxySelector defaultSelector) { public YumProxySelector(final ProxySelector defaultSelector) {
this.defaultSelector = defaultSelector; this.defaultSelector = defaultSelector;
@ -55,39 +56,44 @@ public class NetworkManager {
@Override @Override
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) { public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
this.defaultSelector.connectFailed(uri, sa, ioe); defaultSelector.connectFailed(uri, sa, ioe);
} }
public ProxySelector getDefaultSelector() { public ProxySelector getDefaultSelector() {
return this.defaultSelector; return defaultSelector;
} }
@Override @Override
public List<Proxy> select(final URI uri) { public List<Proxy> select(final URI uri) {
if (NetworkManager.debug || Bukkit.isPrimaryThread()) { if (debug || Bukkit.isPrimaryThread()) {
final Plugin plugin = this.getRequestingPlugin(); try {
final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath(); final Plugin plugin = this.getRequestingPlugin();
final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!"; final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath();
if (plugin == null) { final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!";
main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo)); if (plugin == null) {
Thread.dumpStack(); main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo));
} else if (!plugin.getName().equalsIgnoreCase("Yum")) { Thread.dumpStack();
main.getLogger().warning(String.format(str, plugin.getName(), urlinfo)); } else if (!plugin.getName().equalsIgnoreCase("Yum")) {
main.getLogger().warning(String.format(str, plugin.getName(), urlinfo));
}
} catch (final Exception e) {
e.printStackTrace();
} }
} }
return this.defaultSelector.select(uri); return defaultSelector.select(uri);
} }
private void collectPlugin() { private void collectPlugin() {
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) {
pluginMap.put(plugin.getClass().getClassLoader(), plugin); pluginMap.clear();
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
pluginMap.put(plugin.getClass().getClassLoader(), plugin);
}
} }
} }
private Plugin getRequestingPlugin() { private Plugin getRequestingPlugin() {
if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) { collectPlugin();
collectPlugin();
}
final StackTraceElement[] stacktrace = new Exception().getStackTrace(); final StackTraceElement[] stacktrace = new Exception().getStackTrace();
for (final StackTraceElement element : stacktrace) { for (final StackTraceElement element : stacktrace) {
try { try {