* Check if port is valid before setting it
* Validate user input
* Change proxyPort to int
This commit is contained in:
yushijinhun 2018-12-16 15:49:57 +08:00
parent 176968d883
commit 9f060be3fd
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
5 changed files with 21 additions and 13 deletions

View File

@ -102,7 +102,7 @@ public final class Config implements Cloneable, Observable {
private StringProperty proxyHost = new SimpleStringProperty(); private StringProperty proxyHost = new SimpleStringProperty();
@SerializedName("proxyPort") @SerializedName("proxyPort")
private StringProperty proxyPort = new SimpleStringProperty(); private IntegerProperty proxyPort = new SimpleIntegerProperty();
@SerializedName("proxyUserName") @SerializedName("proxyUserName")
private StringProperty proxyUser = new SimpleStringProperty(); private StringProperty proxyUser = new SimpleStringProperty();
@ -293,15 +293,15 @@ public final class Config implements Cloneable, Observable {
return proxyHost; return proxyHost;
} }
public String getProxyPort() { public int getProxyPort() {
return proxyPort.get(); return proxyPort.get();
} }
public void setProxyPort(String proxyPort) { public void setProxyPort(int proxyPort) {
this.proxyPort.set(proxyPort); this.proxyPort.set(proxyPort);
} }
public StringProperty proxyPortProperty() { public IntegerProperty proxyPortProperty() {
return proxyPort; return proxyPort;
} }

View File

@ -20,7 +20,6 @@ package org.jackhuang.hmcl.setting;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding; import javafx.beans.binding.ObjectBinding;
import javafx.beans.value.ObservableObjectValue; import javafx.beans.value.ObservableObjectValue;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import java.net.Authenticator; import java.net.Authenticator;
@ -30,6 +29,7 @@ import java.net.Proxy;
import java.net.Proxy.Type; import java.net.Proxy.Type;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.util.Logging.LOG;
public final class ProxyManager { public final class ProxyManager {
private ProxyManager() { private ProxyManager() {
@ -49,10 +49,14 @@ public final class ProxyManager {
proxyProperty = Bindings.createObjectBinding( proxyProperty = Bindings.createObjectBinding(
() -> { () -> {
String host = config().getProxyHost(); String host = config().getProxyHost();
Integer port = Lang.toIntOrNull(config().getProxyPort()); int port = config().getProxyPort();
if (!config().hasProxy() || StringUtils.isBlank(host) || port == null || config().getProxyType() == Proxy.Type.DIRECT) { if (!config().hasProxy() || StringUtils.isBlank(host) || config().getProxyType() == Proxy.Type.DIRECT) {
return Proxy.NO_PROXY; return Proxy.NO_PROXY;
} else { } else {
if (port < 0 || port > 0xFFFF) {
LOG.warning("Illegal proxy port: " + port);
return Proxy.NO_PROXY;
}
return new Proxy(config().getProxyType(), new InetSocketAddress(host, port)); return new Proxy(config().getProxyType(), new InetSocketAddress(host, port));
} }
}, },

View File

@ -96,7 +96,11 @@ public final class SettingsPage extends SettingsView implements DecoratorPage {
// ==== Proxy ==== // ==== Proxy ====
txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty()); txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty());
txtProxyPort.textProperty().bindBidirectional(config().proxyPortProperty()); txtProxyPort.textProperty().bindBidirectional(config().proxyPortProperty(),
SafeStringConverter.fromInteger()
.restrict(it -> it >= 0 && it <= 0xFFFF)
.fallbackTo(0)
.asPredicate(Validator.addTo(txtProxyPort)));
txtProxyUsername.textProperty().bindBidirectional(config().proxyUserProperty()); txtProxyUsername.textProperty().bindBidirectional(config().proxyUserProperty());
txtProxyPassword.textProperty().bindBidirectional(config().proxyPassProperty()); txtProxyPassword.textProperty().bindBidirectional(config().proxyPassProperty());

View File

@ -43,7 +43,7 @@ public class LaunchOptions implements Serializable {
private String serverIp; private String serverIp;
private String wrapper; private String wrapper;
private String proxyHost; private String proxyHost;
private String proxyPort; private int proxyPort;
private String proxyUser; private String proxyUser;
private String proxyPass; private String proxyPass;
private boolean noGeneratedJVMArgs; private boolean noGeneratedJVMArgs;
@ -160,7 +160,7 @@ public class LaunchOptions implements Serializable {
/** /**
* the port of the proxy address. * the port of the proxy address.
*/ */
public String getProxyPort() { public int getProxyPort() {
return proxyPort; return proxyPort;
} }
@ -275,7 +275,7 @@ public class LaunchOptions implements Serializable {
return this; return this;
} }
public Builder setProxyPort(String proxyPort) { public Builder setProxyPort(int proxyPort) {
options.proxyPort = proxyPort; options.proxyPort = proxyPort;
return this; return this;
} }

View File

@ -162,11 +162,11 @@ public class DefaultLauncher extends Launcher {
if (options.isFullscreen()) if (options.isFullscreen())
res.add("--fullscreen"); res.add("--fullscreen");
if (StringUtils.isNotBlank(options.getProxyHost()) && StringUtils.isNotBlank(options.getProxyPort())) { if (StringUtils.isNotBlank(options.getProxyHost())) {
res.add("--proxyHost"); res.add("--proxyHost");
res.add(options.getProxyHost()); res.add(options.getProxyHost());
res.add("--proxyPort"); res.add("--proxyPort");
res.add(options.getProxyPort()); res.add(String.valueOf(options.getProxyPort()));
if (StringUtils.isNotBlank(options.getProxyUser()) && StringUtils.isNotBlank(options.getProxyPass())) { if (StringUtils.isNotBlank(options.getProxyUser()) && StringUtils.isNotBlank(options.getProxyPass())) {
res.add("--proxyUser"); res.add("--proxyUser");
res.add(options.getProxyUser()); res.add(options.getProxyUser());