Add ProxyManager

This commit is contained in:
yushijinhun 2018-07-17 21:00:05 +08:00
parent 8e0559fb3b
commit 5642c579a4
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
5 changed files with 146 additions and 43 deletions

View File

@ -24,6 +24,7 @@ import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.yggdrasil.GameProfile;
import org.jackhuang.hmcl.auth.yggdrasil.Texture;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
import org.jackhuang.hmcl.setting.ProxyManager;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Scheduler;
@ -55,7 +56,7 @@ public final class AccountHelper {
}
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) {
@ -63,7 +64,7 @@ public final class AccountHelper {
}
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) {

View File

@ -122,7 +122,7 @@ public final class Profile {
}
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) {

View 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");
}
}
}

View File

@ -33,10 +33,6 @@ import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.*;
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.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
@ -69,7 +65,7 @@ public class Settings {
firstLaunch = ConfigHolder.CONFIG.firstLaunch.get();
ConfigHolder.CONFIG.firstLaunch.set(false);
loadProxy();
ProxyManager.getProxy(); // init ProxyManager
for (Iterator<Map<Object, Object>> iterator = ConfigHolder.CONFIG.accounts.iterator(); iterator.hasNext();) {
Map<Object, Object> settings = iterator.next();
@ -82,7 +78,7 @@ public class Settings {
Account account;
try {
account = factory.fromStorage(settings, getProxy());
account = factory.fromStorage(settings, ProxyManager.getProxy());
} catch (Exception e) {
LOG.log(Level.WARNING, "Malformed account storage, removing: " + settings, e);
iterator.remove();
@ -131,38 +127,6 @@ public class Settings {
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() {
return Font.font(ConfigHolder.CONFIG.fontFamily.get(), ConfigHolder.CONFIG.fontSize.get());
}

View File

@ -42,6 +42,7 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
import org.jackhuang.hmcl.game.AccountHelper;
import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.setting.ProxyManager;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
@ -151,7 +152,7 @@ public class AddAccountPane extends StackPane {
lblCreationWarning.setText("");
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 -> {
Settings.INSTANCE.addAccount(variables.get("create_account"));
acceptPane.hideSpinner();
@ -211,7 +212,7 @@ public class AddAccountPane extends StackPane {
for (GameProfile profile : names) {
Image image;
try {
image = AccountHelper.getSkinImmediately(yggdrasilAccount, profile, 4, Settings.INSTANCE.getProxy());
image = AccountHelper.getSkinImmediately(yggdrasilAccount, profile, 4, ProxyManager.getProxy());
} catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Failed to get skin for " + profile.getName(), e);
image = null;