mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-02-11 16:59:54 +08:00
Add ProxyManager
This commit is contained in:
parent
8e0559fb3b
commit
5642c579a4
@ -24,6 +24,7 @@ import org.jackhuang.hmcl.auth.Account;
|
|||||||
import org.jackhuang.hmcl.auth.yggdrasil.GameProfile;
|
import org.jackhuang.hmcl.auth.yggdrasil.GameProfile;
|
||||||
import org.jackhuang.hmcl.auth.yggdrasil.Texture;
|
import org.jackhuang.hmcl.auth.yggdrasil.Texture;
|
||||||
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
|
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
|
||||||
|
import org.jackhuang.hmcl.setting.ProxyManager;
|
||||||
import org.jackhuang.hmcl.setting.Settings;
|
import org.jackhuang.hmcl.setting.Settings;
|
||||||
import org.jackhuang.hmcl.task.FileDownloadTask;
|
import org.jackhuang.hmcl.task.FileDownloadTask;
|
||||||
import org.jackhuang.hmcl.task.Scheduler;
|
import org.jackhuang.hmcl.task.Scheduler;
|
||||||
@ -55,7 +56,7 @@ public final class AccountHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Task loadSkinAsync(YggdrasilAccount account) {
|
public static Task loadSkinAsync(YggdrasilAccount account) {
|
||||||
return loadSkinAsync(account, Settings.INSTANCE.getProxy());
|
return loadSkinAsync(account, ProxyManager.getProxy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task loadSkinAsync(YggdrasilAccount account, Proxy proxy) {
|
public static Task loadSkinAsync(YggdrasilAccount account, Proxy proxy) {
|
||||||
@ -63,7 +64,7 @@ public final class AccountHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Task refreshSkinAsync(YggdrasilAccount account) {
|
public static Task refreshSkinAsync(YggdrasilAccount account) {
|
||||||
return refreshSkinAsync(account, Settings.INSTANCE.getProxy());
|
return refreshSkinAsync(account, ProxyManager.getProxy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task refreshSkinAsync(YggdrasilAccount account, Proxy proxy) {
|
public static Task refreshSkinAsync(YggdrasilAccount account, Proxy proxy) {
|
||||||
|
@ -122,7 +122,7 @@ public final class Profile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HMCLDependencyManager getDependency() {
|
public HMCLDependencyManager getDependency() {
|
||||||
return new HMCLDependencyManager(this, Settings.INSTANCE.getDownloadProvider(), Settings.INSTANCE.getProxy());
|
return new HMCLDependencyManager(this, Settings.INSTANCE.getDownloadProvider(), ProxyManager.getProxy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public VersionSetting getVersionSetting(String id) {
|
public VersionSetting getVersionSetting(String id) {
|
||||||
|
137
HMCL/src/main/java/org/jackhuang/hmcl/setting/ProxyManager.java
Normal file
137
HMCL/src/main/java/org/jackhuang/hmcl/setting/ProxyManager.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* Hello Minecraft! Launcher.
|
||||||
|
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||||
|
*/
|
||||||
|
package org.jackhuang.hmcl.setting;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Proxy.Type;
|
||||||
|
|
||||||
|
import org.jackhuang.hmcl.util.Lang;
|
||||||
|
import org.jackhuang.hmcl.util.StringUtils;
|
||||||
|
|
||||||
|
import javafx.beans.InvalidationListener;
|
||||||
|
import javafx.beans.binding.Bindings;
|
||||||
|
import javafx.beans.binding.ObjectBinding;
|
||||||
|
|
||||||
|
public final class ProxyManager {
|
||||||
|
private ProxyManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final ObjectBinding<Proxy> proxyProperty = Bindings.createObjectBinding(
|
||||||
|
() -> {
|
||||||
|
String host = ConfigHolder.CONFIG.proxyHost.get();
|
||||||
|
Integer port = Lang.toIntOrNull(ConfigHolder.CONFIG.proxyPort.get());
|
||||||
|
if (!ConfigHolder.CONFIG.hasProxy.get() || StringUtils.isBlank(host) || port == null || ConfigHolder.CONFIG.proxyType.get() == Proxy.Type.DIRECT) {
|
||||||
|
return Proxy.NO_PROXY;
|
||||||
|
} else {
|
||||||
|
return new Proxy(ConfigHolder.CONFIG.proxyType.get(), new InetSocketAddress(host, port));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ConfigHolder.CONFIG.proxyType,
|
||||||
|
ConfigHolder.CONFIG.proxyHost,
|
||||||
|
ConfigHolder.CONFIG.proxyPort,
|
||||||
|
ConfigHolder.CONFIG.hasProxy);
|
||||||
|
|
||||||
|
public static Proxy getProxy() {
|
||||||
|
return proxyProperty.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
initProxy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initProxy() {
|
||||||
|
proxyProperty.addListener(observable -> updateSystemProxy());
|
||||||
|
|
||||||
|
InvalidationListener onProxyAuthChanged = observable -> updateSystemProxyAuthentication();
|
||||||
|
ConfigHolder.CONFIG.proxyUser.addListener(onProxyAuthChanged);
|
||||||
|
ConfigHolder.CONFIG.proxyPass.addListener(onProxyAuthChanged);
|
||||||
|
ConfigHolder.CONFIG.hasProxyAuth.addListener(onProxyAuthChanged);
|
||||||
|
ConfigHolder.CONFIG.hasProxy.addListener(onProxyAuthChanged);
|
||||||
|
ConfigHolder.CONFIG.proxyType.addListener(onProxyAuthChanged);
|
||||||
|
|
||||||
|
updateSystemProxy();
|
||||||
|
updateSystemProxyAuthentication();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateSystemProxy() {
|
||||||
|
Proxy proxy = proxyProperty.get();
|
||||||
|
if (proxy.type() == Proxy.Type.DIRECT) {
|
||||||
|
System.clearProperty("http.proxyHost");
|
||||||
|
System.clearProperty("http.proxyPort");
|
||||||
|
System.clearProperty("https.proxyHost");
|
||||||
|
System.clearProperty("https.proxyPort");
|
||||||
|
System.clearProperty("socksProxyHost");
|
||||||
|
System.clearProperty("socksProxyPort");
|
||||||
|
} else {
|
||||||
|
InetSocketAddress address = (InetSocketAddress) proxy.address();
|
||||||
|
String host = address.getHostString();
|
||||||
|
String port = String.valueOf(address.getPort());
|
||||||
|
if (proxy.type() == Type.HTTP) {
|
||||||
|
System.clearProperty("socksProxyHost");
|
||||||
|
System.clearProperty("socksProxyPort");
|
||||||
|
System.setProperty("http.proxyHost", host);
|
||||||
|
System.setProperty("http.proxyPort", port);
|
||||||
|
System.setProperty("https.proxyHost", host);
|
||||||
|
System.setProperty("https.proxyPort", port);
|
||||||
|
} else if (proxy.type() == Type.SOCKS) {
|
||||||
|
System.clearProperty("http.proxyHost");
|
||||||
|
System.clearProperty("http.proxyPort");
|
||||||
|
System.clearProperty("https.proxyHost");
|
||||||
|
System.clearProperty("https.proxyPort");
|
||||||
|
System.setProperty("socksProxyHost", host);
|
||||||
|
System.setProperty("socksProxyPort", port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateSystemProxyAuthentication() {
|
||||||
|
String username = ConfigHolder.CONFIG.proxyUser.get();
|
||||||
|
String password = ConfigHolder.CONFIG.proxyPass.get();
|
||||||
|
Proxy.Type proxyType = ConfigHolder.CONFIG.proxyType.get();
|
||||||
|
if (ConfigHolder.CONFIG.hasProxy.get() &&
|
||||||
|
ConfigHolder.CONFIG.hasProxyAuth.get() &&
|
||||||
|
proxyType != Proxy.Type.DIRECT &&
|
||||||
|
username != null &&
|
||||||
|
password != null) {
|
||||||
|
if (proxyType == Proxy.Type.HTTP) {
|
||||||
|
System.setProperty("http.proxyUser", username);
|
||||||
|
System.setProperty("http.proxyPassword", password);
|
||||||
|
System.setProperty("https.proxyUser", username);
|
||||||
|
System.setProperty("https.proxyPassword", password);
|
||||||
|
System.clearProperty("java.net.socks.username");
|
||||||
|
System.clearProperty("java.net.socks.password");
|
||||||
|
} else if (proxyType == Proxy.Type.SOCKS) {
|
||||||
|
System.setProperty("java.net.socks.username", username);
|
||||||
|
System.setProperty("java.net.socks.password", password);
|
||||||
|
System.clearProperty("http.proxyUser");
|
||||||
|
System.clearProperty("http.proxyPassword");
|
||||||
|
System.clearProperty("https.proxyUser");
|
||||||
|
System.clearProperty("https.proxyPassword");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.clearProperty("http.proxyUser");
|
||||||
|
System.clearProperty("http.proxyPassword");
|
||||||
|
System.clearProperty("https.proxyUser");
|
||||||
|
System.clearProperty("https.proxyPassword");
|
||||||
|
System.clearProperty("java.net.socks.username");
|
||||||
|
System.clearProperty("java.net.socks.password");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -33,10 +33,6 @@ import org.jackhuang.hmcl.task.Schedulers;
|
|||||||
import org.jackhuang.hmcl.util.*;
|
import org.jackhuang.hmcl.util.*;
|
||||||
import org.jackhuang.hmcl.util.i18n.Locales;
|
import org.jackhuang.hmcl.util.i18n.Locales;
|
||||||
|
|
||||||
import java.net.Authenticator;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.PasswordAuthentication;
|
|
||||||
import java.net.Proxy;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -69,7 +65,7 @@ public class Settings {
|
|||||||
firstLaunch = ConfigHolder.CONFIG.firstLaunch.get();
|
firstLaunch = ConfigHolder.CONFIG.firstLaunch.get();
|
||||||
ConfigHolder.CONFIG.firstLaunch.set(false);
|
ConfigHolder.CONFIG.firstLaunch.set(false);
|
||||||
|
|
||||||
loadProxy();
|
ProxyManager.getProxy(); // init ProxyManager
|
||||||
|
|
||||||
for (Iterator<Map<Object, Object>> iterator = ConfigHolder.CONFIG.accounts.iterator(); iterator.hasNext();) {
|
for (Iterator<Map<Object, Object>> iterator = ConfigHolder.CONFIG.accounts.iterator(); iterator.hasNext();) {
|
||||||
Map<Object, Object> settings = iterator.next();
|
Map<Object, Object> settings = iterator.next();
|
||||||
@ -82,7 +78,7 @@ public class Settings {
|
|||||||
|
|
||||||
Account account;
|
Account account;
|
||||||
try {
|
try {
|
||||||
account = factory.fromStorage(settings, getProxy());
|
account = factory.fromStorage(settings, ProxyManager.getProxy());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.log(Level.WARNING, "Malformed account storage, removing: " + settings, e);
|
LOG.log(Level.WARNING, "Malformed account storage, removing: " + settings, e);
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
@ -131,38 +127,6 @@ public class Settings {
|
|||||||
ConfigHolder.CONFIG.localization.set(Locales.getNameByLocale(locale));
|
ConfigHolder.CONFIG.localization.set(Locales.getNameByLocale(locale));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Proxy proxy = Proxy.NO_PROXY;
|
|
||||||
|
|
||||||
public Proxy getProxy() {
|
|
||||||
return proxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadProxy() {
|
|
||||||
String host = ConfigHolder.CONFIG.proxyHost.get();
|
|
||||||
Integer port = Lang.toIntOrNull(ConfigHolder.CONFIG.proxyPort.get());
|
|
||||||
if (!ConfigHolder.CONFIG.hasProxy.get() || StringUtils.isBlank(host) || port == null || ConfigHolder.CONFIG.proxyType.get() == Proxy.Type.DIRECT)
|
|
||||||
proxy = Proxy.NO_PROXY;
|
|
||||||
else {
|
|
||||||
System.setProperty("http.proxyHost", ConfigHolder.CONFIG.proxyHost.get());
|
|
||||||
System.setProperty("http.proxyPort", ConfigHolder.CONFIG.proxyPort.get());
|
|
||||||
proxy = new Proxy(ConfigHolder.CONFIG.proxyType.get(), new InetSocketAddress(host, port));
|
|
||||||
|
|
||||||
String user = ConfigHolder.CONFIG.proxyUser.get();
|
|
||||||
String pass = ConfigHolder.CONFIG.proxyPass.get();
|
|
||||||
if (ConfigHolder.CONFIG.hasProxyAuth.get() && StringUtils.isNotBlank(user) && StringUtils.isNotBlank(pass)) {
|
|
||||||
System.setProperty("http.proxyUser", user);
|
|
||||||
System.setProperty("http.proxyPassword", pass);
|
|
||||||
|
|
||||||
Authenticator.setDefault(new Authenticator() {
|
|
||||||
@Override
|
|
||||||
public PasswordAuthentication getPasswordAuthentication() {
|
|
||||||
return new PasswordAuthentication(user, pass.toCharArray());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Font getFont() {
|
public Font getFont() {
|
||||||
return Font.font(ConfigHolder.CONFIG.fontFamily.get(), ConfigHolder.CONFIG.fontSize.get());
|
return Font.font(ConfigHolder.CONFIG.fontFamily.get(), ConfigHolder.CONFIG.fontSize.get());
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
|
|||||||
import org.jackhuang.hmcl.game.AccountHelper;
|
import org.jackhuang.hmcl.game.AccountHelper;
|
||||||
import org.jackhuang.hmcl.setting.Accounts;
|
import org.jackhuang.hmcl.setting.Accounts;
|
||||||
import org.jackhuang.hmcl.setting.ConfigHolder;
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
|
import org.jackhuang.hmcl.setting.ProxyManager;
|
||||||
import org.jackhuang.hmcl.setting.Settings;
|
import org.jackhuang.hmcl.setting.Settings;
|
||||||
import org.jackhuang.hmcl.task.Schedulers;
|
import org.jackhuang.hmcl.task.Schedulers;
|
||||||
import org.jackhuang.hmcl.task.Task;
|
import org.jackhuang.hmcl.task.Task;
|
||||||
@ -151,7 +152,7 @@ public class AddAccountPane extends StackPane {
|
|||||||
lblCreationWarning.setText("");
|
lblCreationWarning.setText("");
|
||||||
setDisable(true);
|
setDisable(true);
|
||||||
|
|
||||||
Task.ofResult("create_account", () -> factory.create(new Selector(), username, password, addtionalData, Settings.INSTANCE.getProxy()))
|
Task.ofResult("create_account", () -> factory.create(new Selector(), username, password, addtionalData, ProxyManager.getProxy()))
|
||||||
.finalized(Schedulers.javafx(), variables -> {
|
.finalized(Schedulers.javafx(), variables -> {
|
||||||
Settings.INSTANCE.addAccount(variables.get("create_account"));
|
Settings.INSTANCE.addAccount(variables.get("create_account"));
|
||||||
acceptPane.hideSpinner();
|
acceptPane.hideSpinner();
|
||||||
@ -211,7 +212,7 @@ public class AddAccountPane extends StackPane {
|
|||||||
for (GameProfile profile : names) {
|
for (GameProfile profile : names) {
|
||||||
Image image;
|
Image image;
|
||||||
try {
|
try {
|
||||||
image = AccountHelper.getSkinImmediately(yggdrasilAccount, profile, 4, Settings.INSTANCE.getProxy());
|
image = AccountHelper.getSkinImmediately(yggdrasilAccount, profile, 4, ProxyManager.getProxy());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Failed to get skin for " + profile.getName(), e);
|
Logging.LOG.log(Level.WARNING, "Failed to get skin for " + profile.getName(), e);
|
||||||
image = null;
|
image = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user