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

1.配置文件添加新选项
2.调整代理注入时间
3.更新版本号
dev 2.1
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>
<groupId>pw.yumc</groupId>
<artifactId>Yum</artifactId>
<version>2.0.3</version>
<version>2.1</version>
<name>Yum</name>
<description>Minecraft 服务器插件管理系统</description>
<build>

View File

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

View File

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

View File

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