fix: 修复网络代理设置端口时卡顿

This commit is contained in:
octopus_yan 2024-09-13 04:24:16 +08:00
parent c93837a7a9
commit d65990791a
2 changed files with 29 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import cn.octopusyan.alistgui.Application;
import cn.octopusyan.alistgui.config.Constants;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.manager.http.HttpUtil;
import cn.octopusyan.alistgui.manager.thread.ThreadPoolManager;
import cn.octopusyan.alistgui.model.GuiConfig;
import cn.octopusyan.alistgui.model.ProxyInfo;
import cn.octopusyan.alistgui.model.UpgradeConfig;
@ -17,6 +18,7 @@ import cn.octopusyan.alistgui.model.upgrade.Gui;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import javafx.application.Platform;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -29,6 +31,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
@ -168,8 +171,6 @@ public class ConfigManager {
if (!matcher.matches()) return;
getProxyInfo().setHost(host);
checkProxy();
}
public static String proxyPort() {
@ -184,21 +185,24 @@ public class ConfigManager {
if (!NumberUtil.isNumber(port)) return;
getProxyInfo().setPort(port);
checkProxy();
}
private static void checkProxy() {
public static void checkProxy(BiConsumer<Boolean, String> consumer) {
if (!hasProxy()) return;
try {
InetSocketAddress address = NetUtil.createAddress(proxyHost(), getProxyPort());
if (NetUtil.isOpen(address, 2000)) {
HttpUtil.getInstance().proxy(proxySetup(), ConfigManager.getProxyInfo());
ThreadPoolManager.getInstance().execute(() -> {
try {
InetSocketAddress address = NetUtil.createAddress(proxyHost(), getProxyPort());
if (NetUtil.isOpen(address, 5000)) {
Platform.runLater(() -> consumer.accept(true, "success"));
} else {
Platform.runLater(() -> consumer.accept(false, "connection timed out"));
}
} catch (Exception e) {
logger.error(STR."host=\{proxyHost()},port=\{proxyPort()}", e);
Platform.runLater(() -> consumer.accept(false, e.getMessage()));
}
} catch (Exception e) {
logger.error(STR."host=\{proxyHost()},port=\{proxyPort()}", e);
}
});
}
// --------------------------------{ 语言 }------------------------------------------

View File

@ -5,6 +5,7 @@ import cn.octopusyan.alistgui.base.BaseTask;
import cn.octopusyan.alistgui.config.Context;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.manager.ConfigManager;
import cn.octopusyan.alistgui.manager.http.HttpUtil;
import cn.octopusyan.alistgui.task.ProxyCheckTask;
import cn.octopusyan.alistgui.util.alert.AlertUtil;
import javafx.application.Platform;
@ -81,7 +82,16 @@ public class SetupViewModel {
proxyTestUrl.setValue(checkUrl);
getProxyCheckTask(checkUrl).execute();
ConfigManager.checkProxy((success, msg) -> {
if (!success) {
final var tmp = Context.getLanguageBinding("proxy.test.result.failed").getValue();
AlertUtil.error(STR."\{tmp}:\{msg}").show();
return;
}
HttpUtil.getInstance().proxy(ConfigManager.proxySetup(), ConfigManager.getProxyInfo());
getProxyCheckTask(checkUrl).execute();
});
}
private static ProxyCheckTask getProxyCheckTask(String checkUrl) {
@ -105,7 +115,8 @@ public class SetupViewModel {
public void onFailed(Throwable throwable) {
Platform.runLater(progress::close);
final var tmp = Context.getLanguageBinding("proxy.test.result.failed").getValue();
AlertUtil.error(tmp + throwable.getMessage()).show();
String throwableMessage = throwable.getMessage();
AlertUtil.error(tmp + (StringUtils.isEmpty(throwableMessage) ? "" : throwableMessage)).show();
}
});
return task;