pref: 获取平台网络代理配置

This commit is contained in:
octopus_yan 2024-09-08 01:39:19 +08:00
parent e00a742fff
commit ef8ae3e53d
4 changed files with 55 additions and 28 deletions

View File

@ -56,6 +56,14 @@
<version>2.0.1</version>
</dependency>
<!-- java 网络代理查找库-->
<!-- https://github.com/MarkusBernhardt/proxy-vole -->
<!-- https://mvnrepository.com/artifact/org.bidib.com.github.markusbernhardt/proxy-vole -->
<dependency>
<groupId>org.bidib.com.github.markusbernhardt</groupId>
<artifactId>proxy-vole</artifactId>
<version>1.1.5</version>
</dependency>
<!-- slf4j -->
<!-- https://slf4j.org/manual.html -->

View File

@ -1,14 +1,14 @@
package cn.octopusyan.alistgui;
import atlantafx.base.theme.PrimerLight;
import cn.octopusyan.alistgui.config.AppConstant;
import cn.octopusyan.alistgui.config.ConfigManager;
import cn.octopusyan.alistgui.config.Constants;
import cn.octopusyan.alistgui.config.Context;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.manager.ConfigManager;
import cn.octopusyan.alistgui.manager.http.HttpConfig;
import cn.octopusyan.alistgui.manager.http.HttpUtil;
import cn.octopusyan.alistgui.manager.thread.ThreadPoolManager;
import cn.octopusyan.alistgui.util.AlertUtil;
import cn.octopusyan.alistgui.util.alert.AlertUtil;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;
@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.util.Objects;
public class Application extends javafx.application.Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
@ -27,18 +28,39 @@ public class Application extends javafx.application.Application {
private static Stage primaryStage;
@Override
public void init() throws Exception {
public void init() {
logger.info("application init ...");
// 初始化客户端配置
ConfigManager.load();
// http请求工具初始化
HttpConfig httpConfig = new HttpConfig();
// 加载代理设置
if (!ProxySetup.NO_PROXY.equals(ConfigManager.proxySetup())) {
// 系统代理
if (ProxySetup.SYSTEM.equals(ConfigManager.proxySetup())) {
httpConfig.setProxySelector(ProxySelector.getDefault());
}
// 自定义代理
if (ProxySetup.MANUAL.equals(ConfigManager.proxySetup()) && ConfigManager.hasProxy()) {
InetSocketAddress unresolved = InetSocketAddress.createUnresolved(
Objects.requireNonNull(ConfigManager.proxyHost()),
ConfigManager.getProxyPort()
);
httpConfig.setProxySelector(ProxySelector.of(unresolved));
}
}
httpConfig.setConnectTimeout(10);
HttpUtil.init(httpConfig);
}
@Override
public void start(Stage primaryStage) throws IOException {
Application.primaryStage = primaryStage;
logger.info("application start ...");
Application.primaryStage = primaryStage;
// 初始化弹窗工具
AlertUtil.initOwner(primaryStage);
@ -46,24 +68,6 @@ public class Application extends javafx.application.Application {
Thread.setDefaultUncaughtExceptionHandler(this::showErrorDialog);
Thread.currentThread().setUncaughtExceptionHandler(this::showErrorDialog);
// http请求工具初始化
HttpConfig httpConfig = new HttpConfig();
if(!ProxySetup.NO_PROXY.equals(ConfigManager.proxySetup())) {
// 系统代理
if (ProxySetup.SYSTEM.equals(ConfigManager.proxySetup())) {
httpConfig.setProxySelector(ProxySelector.getDefault());
}
// 自定义代理
if (ProxySetup.MANUAL.equals(ConfigManager.proxySetup()) && ConfigManager.hasProxy()) {
InetSocketAddress unresolved = InetSocketAddress.createUnresolved(ConfigManager.proxyHost(), ConfigManager.getProxyPort());
httpConfig.setProxySelector(ProxySelector.of(unresolved));
}
}
httpConfig.setConnectTimeout(10);
HttpUtil.init(httpConfig);
// i18n
Context.setLanguage(ConfigManager.language());
@ -71,10 +75,10 @@ public class Application extends javafx.application.Application {
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
// 启动主界面
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle(String.format("%s v%s", Constants.APP_TITLE, Constants.APP_VERSION));
Scene scene = Context.initScene();
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle(String.format("%s v%s", AppConstant.APP_TITLE, AppConstant.APP_VERSION));
primaryStage.show();
logger.info("application start over ...");
@ -86,7 +90,7 @@ public class Application extends javafx.application.Application {
}
@Override
public void stop() throws Exception {
public void stop() {
logger.info("application stop ...");
// 保存应用数据
ConfigManager.save();

View File

@ -1,10 +1,11 @@
package cn.octopusyan.alistgui.manager.http;
import cn.octopusyan.alistgui.config.ConfigManager;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.model.ProxyInfo;
import cn.octopusyan.alistgui.util.JsonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.markusbernhardt.proxy.ProxySearch;
import com.github.markusbernhardt.proxy.selector.misc.BufferedProxySelector;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -28,6 +29,19 @@ public class HttpUtil {
private volatile static HttpUtil util;
private volatile HttpClient httpClient;
private final HttpConfig httpConfig;
public static final ProxySearch proxySearch = ProxySearch.getDefaultProxySearch();
static {
proxySearch.addStrategy(ProxySearch.Strategy.WIN);
proxySearch.addStrategy(ProxySearch.Strategy.OS_DEFAULT);
proxySearch.addStrategy(ProxySearch.Strategy.IE);
proxySearch.addStrategy(ProxySearch.Strategy.FIREFOX);
proxySearch.addStrategy(ProxySearch.Strategy.JAVA);
// PAC 代理查询
proxySearch.setPacCacheSettings(20, 1000 * 60 * 10, BufferedProxySelector.CacheScope.CACHE_SCOPE_HOST);
// 设置系统默认代理
ProxySelector.setDefault(proxySearch.getProxySelector());
}
private HttpUtil(HttpConfig httpConfig) {
this.httpConfig = httpConfig;
@ -69,7 +83,7 @@ public class HttpUtil {
case NO_PROXY -> clearProxy();
case SYSTEM -> httpConfig.setProxySelector(ProxySelector.getDefault());
case MANUAL -> {
InetSocketAddress unresolved = InetSocketAddress.createUnresolved(ConfigManager.proxyHost(), ConfigManager.getProxyPort());
InetSocketAddress unresolved = InetSocketAddress.createUnresolved(proxy.getHost(), Integer.parseInt(proxy.getPort()));
httpConfig.setProxySelector(ProxySelector.of(unresolved));
}
}

View File

@ -15,6 +15,7 @@ module cn.octopusyan.alistgui {
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.yaml;
requires atlantafx.base;
requires proxy.vole;
exports cn.octopusyan.alistgui;
opens cn.octopusyan.alistgui to javafx.fxml;