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

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

View File

@ -22,8 +22,6 @@ import pw.yumc.Yum.Yum;
public class NetworkManager {
private static boolean debug;
private static HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
private final Yum main;
public NetworkManager(final Yum plugin) {
@ -31,6 +29,7 @@ public class NetworkManager {
}
public void register() {
main.getLogger().info("注入网络代理 将托管服务器网络!");
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault()));
}
@ -42,12 +41,14 @@ public class NetworkManager {
public void unregister() {
final ProxySelector cur = ProxySelector.getDefault();
if (cur instanceof YumProxySelector) {
main.getLogger().info("恢复网络代理 使用默认网络!");
ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector());
}
}
class YumProxySelector extends ProxySelector {
private final ProxySelector defaultSelector;
private final HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
public YumProxySelector(final ProxySelector defaultSelector) {
this.defaultSelector = defaultSelector;
@ -55,16 +56,17 @@ public class NetworkManager {
@Override
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() {
return this.defaultSelector;
return defaultSelector;
}
@Override
public List<Proxy> select(final URI uri) {
if (NetworkManager.debug || Bukkit.isPrimaryThread()) {
if (debug || Bukkit.isPrimaryThread()) {
try {
final Plugin plugin = this.getRequestingPlugin();
final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath();
final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!";
@ -74,20 +76,24 @@ public class NetworkManager {
} 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() {
if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) {
pluginMap.clear();
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
pluginMap.put(plugin.getClass().getClassLoader(), plugin);
}
}
}
private Plugin getRequestingPlugin() {
if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) {
collectPlugin();
}
final StackTraceElement[] stacktrace = new Exception().getStackTrace();
for (final StackTraceElement element : stacktrace) {
try {