Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
feat:split ssh-tunnel ssh server input field
Browse files Browse the repository at this point in the history
  • Loading branch information
vran-dev committed Aug 4, 2021
1 parent 6ed9bd8 commit b7973d0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.controls.JFXToggleButton;
import com.jfoenix.validation.RegexValidator;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
Expand All @@ -37,6 +36,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -75,6 +75,9 @@ public class ServerViewController {
@FXML
private JFXTextField sshServer;

@FXML
private JFXTextField sshServerPort;

@FXML
private JFXTextField sshUsername;

Expand Down Expand Up @@ -216,10 +219,11 @@ private void propertyBind(ServerConfigurationVO config) {
zkPort.textProperty().setValue(config.getZkPort() + "");
zkAlias.textProperty().setValue(config.getZkAlias());
sshServer.textProperty().setValue(config.getSshServer());
sshServerPort.textProperty().setValue(Objects.toString(config.getSshServerPort(), ""));
sshUsername.textProperty().setValue(config.getSshUsername());
sshPassword.textProperty().setValue(config.getSshPassword());
remoteServer.textProperty().setValue(config.getRemoteServer());
remoteServerPort.textProperty().setValue(config.getRemoteServerPort() + "");
remoteServerPort.textProperty().setValue(Objects.toString(config.getRemoteServerPort(), ""));
sshTunnelCheckbox.selectedProperty().setValue(config.isSshEnabled());
final String acl = String.join("\n", config.getAclList());
aclTextArea.textProperty().setValue(acl);
Expand Down Expand Up @@ -295,12 +299,11 @@ private void initValidator() {

remoteServer.setValidators(new StringNotBlankValidator());
remoteServerPort.setValidators(new PortValidator());

sshServer.setValidators(new StringNotBlankValidator());
sshServerPort.setValidators(new PortValidator());
sshUsername.setValidators(new NotNullValidator());
sshPassword.setValidators(new NotNullValidator());

var sshServerMatchPattern = new RegexValidator("should be [host:port]");
sshServerMatchPattern.setRegexPattern(".*\\:\\d+$");
sshServer.setValidators(sshServerMatchPattern);
}

private void resetValidate() {
Expand All @@ -312,12 +315,14 @@ private void resetValidate() {
sshUsername.resetValidation();
sshPassword.resetValidation();
sshServer.resetValidation();
sshServerPort.resetValidation();
}

private void sshTunnelViewPropertyBind() {
var sshTunnelEnabledProperty = sshTunnelCheckbox.selectedProperty();
var disableBinding = Bindings.createBooleanBinding(() -> !sshTunnelEnabledProperty.get(), sshTunnelEnabledProperty);
sshServer.disableProperty().bind(disableBinding);
sshServerPort.disableProperty().bind(disableBinding);
sshUsername.disableProperty().bind(disableBinding);
sshPassword.disableProperty().bind(disableBinding);
remoteServer.disableProperty().bind(disableBinding);
Expand All @@ -342,10 +347,19 @@ private void onSave() {
serverConfigVO.setZkUrl(zkHost.getText() + ":" + zkPort.getText());
// ssh-tunnel config
serverConfigVO.setRemoteServer(remoteServer.textProperty().get());
serverConfigVO.setRemoteServerPort(Integer.parseInt(remoteServerPort.textProperty().get()));
if (Strings.isNullOrEmpty(remoteServerPort.getText())) {
serverConfigVO.setRemoteServerPort(null);
} else {
serverConfigVO.setRemoteServerPort(Integer.parseInt(remoteServerPort.getText()));
}
serverConfigVO.setSshUsername(sshUsername.textProperty().get());
serverConfigVO.setSshPassword(sshPassword.textProperty().get());
serverConfigVO.setSshServer(sshServer.textProperty().get());
if (Strings.isNullOrEmpty(sshServerPort.getText())) {
serverConfigVO.setSshServerPort(null);
} else {
serverConfigVO.setSshServerPort(Integer.parseInt(sshServerPort.getText()));
}
serverConfigVO.setSshEnabled(sshTunnelCheckbox.isSelected());
// zookeeper ACL config
List<String> acls = Arrays.stream(aclTextArea.textProperty().get().split("\n"))
Expand Down Expand Up @@ -378,7 +392,8 @@ private boolean baseValidateBeforeSave() {
remoteServerPort.validate(),
sshUsername.validate(),
sshPassword.validate(),
sshServer.validate()
sshServer.validate(),
sshServerPort.validate()
).allMatch(t -> t);
} else {
baseValidate = Stream.of(zkHost.validate(), zkPort.validate(), zkAlias.validate()).allMatch(t -> t);
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/cc/cc1234/app/facade/PrettyZooFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public void saveServerConfiguration(ServerConfigurationVO serverConfigurationVO)
}

if (serverConfigurationVO.getSshServer().trim().length() > 0) {
var sshServerHostAndPort = serverConfigurationVO.getSshServer().split(":");
tunnelConfigurationBuilder.sshHost(sshServerHostAndPort[0]).sshPort(Integer.parseInt(sshServerHostAndPort[1]));
tunnelConfigurationBuilder.sshHost(serverConfigurationVO.getSshServer())
.sshPort(serverConfigurationVO.getSshServerPort());
}
tunnelConfigurationBuilder.localhost(serverConfigurationVO.getZkHost())
.localPort(serverConfigurationVO.getZkPort());
Expand Down Expand Up @@ -199,9 +199,9 @@ public void exportConfig(File file) {

public void importConfig(File configFile) {
Try.of(() -> {
Asserts.notNull(configFile, "文件不存在");
Asserts.assertTrue(configFile.isFile(), "请选择文件");
})
Asserts.notNull(configFile, "文件不存在");
Asserts.assertTrue(configFile.isFile(), "请选择文件");
})
.onFailure(e -> VToast.error(e.getMessage()))
.onSuccess(e -> configurationDomainService.importConfig(configFile));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,22 @@ public void onServerChange(ServerConfigData newValue) {
newValue.getSshTunnelConfig()
.map(sshTunnelConfig -> {
old.setSshEnabled(newValue.getSshTunnelEnabled());
if (sshTunnelConfig.getSshHost() == null) {
old.setSshServer("");
} else {
old.setSshServer(String.format("%s:%d", sshTunnelConfig.getSshHost(), sshTunnelConfig.getSshPort()));
}
if (sshTunnelConfig.getRemoteHost() == null) {
old.setRemoteServer("");
} else {
old.setRemoteServer(sshTunnelConfig.getRemoteHost());
old.setRemoteServerPort(sshTunnelConfig.getRemotePort());
}
old.setSshServer(sshTunnelConfig.getSshHost());
old.setSshServerPort(sshTunnelConfig.getSshPort());
old.setRemoteServer(sshTunnelConfig.getRemoteHost());
old.setRemoteServerPort(sshTunnelConfig.getRemotePort());
old.setSshUsername(sshTunnelConfig.getSshUsername());
old.setSshPassword(sshTunnelConfig.getPassword());
return true;
})
.orElseGet(() -> {
old.setSshEnabled(newValue.getSshTunnelEnabled());
old.setSshServer("");
old.setSshServerPort(null);
old.setSshUsername("");
old.setSshPassword("");
old.setRemoteServer("");
old.setRemoteServerPort(null);
return true;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static ServerConfigurationVO to(ServerConfiguration serverConfiguration)
if (sshTunnelConfig.getSshHost() == null) {
vo.setSshServer("");
} else {
vo.setSshServer(sshTunnelConfig.getSshHost() + ":" + sshTunnelConfig.getSshPort());
vo.setSshServer(sshTunnelConfig.getSshHost());
vo.setSshServerPort(sshTunnelConfig.getSshPort());
}
vo.setSshUsername(sshTunnelConfig.getSshUsername());
vo.setSshPassword(sshTunnelConfig.getSshPassword());
Expand Down Expand Up @@ -76,7 +77,8 @@ public static ServerConfigurationVO to(ServerConfigData serverConfig) {
if (sshTunnelConfig.getRemoteHost() == null || sshTunnelConfig.getRemotePort() == null) {
vo.setRemoteServer("");
} else {
vo.setSshServer(sshTunnelConfig.getSshHost() + ":" + sshTunnelConfig.getSshPort());
vo.setSshServer(sshTunnelConfig.getSshHost());
vo.setSshServerPort(sshTunnelConfig.getSshPort());
}
});
vo.setSshEnabled(serverConfig.getSshTunnelEnabled());
Expand Down
63 changes: 37 additions & 26 deletions app/src/main/java/cc/cc1234/app/vo/ServerConfigurationVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public class ServerConfigurationVO {

private SimpleStringProperty sshServer = new SimpleStringProperty("");

private ObjectProperty<Integer> sshServerPort = new SimpleObjectProperty<>();

private SimpleStringProperty sshUsername = new SimpleStringProperty("");

private SimpleStringProperty sshPassword = new SimpleStringProperty("");

private SimpleStringProperty remoteServer = new SimpleStringProperty("");

private SimpleIntegerProperty remoteServerPort = new SimpleIntegerProperty(0);
private ObjectProperty<Integer> remoteServerPort = new SimpleObjectProperty<>();

public void unbind() {
zkUrl.unbind();
Expand All @@ -38,6 +40,7 @@ public void unbind() {
aclList.unbind();
sshEnabled.unbind();
sshServer.unbind();
sshServerPort.unbind();
sshUsername.unbind();
sshPassword.unbind();
remoteServer.unbind();
Expand All @@ -48,26 +51,26 @@ public String getZkUrl() {
return zkUrl.get();
}

public SimpleStringProperty zkUrlProperty() {
return zkUrl;
}

public void setZkUrl(String zkUrl) {
this.zkUrl.set(zkUrl);
}

public ServerStatus getStatus() {
return status.get();
public SimpleStringProperty zkUrlProperty() {
return zkUrl;
}

public SimpleObjectProperty<ServerStatus> statusProperty() {
return status;
public ServerStatus getStatus() {
return status.get();
}

public void setStatus(ServerStatus status) {
this.status.set(status);
}

public SimpleObjectProperty<ServerStatus> statusProperty() {
return status;
}

public String getZkHost() {
return zkHost.get();
}
Expand All @@ -84,14 +87,14 @@ public int getZkPort() {
return zkPort.get();
}

public SimpleIntegerProperty zkPortProperty() {
return zkPort;
}

public void setZkPort(int zkPort) {
this.zkPort.set(zkPort);
}

public SimpleIntegerProperty zkPortProperty() {
return zkPort;
}

public ObservableList<String> getAclList() {
return aclList.get();
}
Expand Down Expand Up @@ -128,6 +131,18 @@ public SimpleStringProperty sshServerProperty() {
return sshServer;
}

public Integer getSshServerPort() {
return sshServerPort.get();
}

public void setSshServerPort(Integer sshServerPort) {
this.sshServerPort.set(sshServerPort);
}

public ObjectProperty<Integer> sshServerPortProperty() {
return sshServerPort;
}

public String getSshUsername() {
return sshUsername.get();
}
Expand Down Expand Up @@ -160,31 +175,27 @@ public void setRemoteServer(String remoteServer) {
this.remoteServer.set(remoteServer);
}

public SimpleStringProperty remoteServerProperty() {
return remoteServer;
}

public int getRemoteServerPort() {
public Integer getRemoteServerPort() {
return remoteServerPort.get();
}

public SimpleIntegerProperty remoteServerPortProperty() {
return remoteServerPort;
public void setRemoteServerPort(Integer remoteServerPort) {
this.remoteServerPort.set(remoteServerPort);
}

public void setRemoteServerPort(int remoteServerPort) {
this.remoteServerPort.set(remoteServerPort);
public ObjectProperty<Integer> remoteServerPortProperty() {
return remoteServerPort;
}

public String getZkAlias() {
return zkAlias.get();
}

public SimpleStringProperty zkAliasProperty() {
return zkAlias;
}

public void setZkAlias(String zkAlias) {
this.zkAlias.set(zkAlias);
}

public SimpleStringProperty zkAliasProperty() {
return zkAlias;
}
}
14 changes: 8 additions & 6 deletions app/src/main/resources/fxml/ServerView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
<children>
<AnchorPane prefHeight="140.0" prefWidth="200.0" style="-fx-border-color: #ddd;">
<children>
<JFXTextField fx:id="sshServer" disable="true" layoutX="15.0" layoutY="14.0" prefHeight="20.0" promptText="ssh server : port" AnchorPane.leftAnchor="30.0" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="40.0" />
<JFXTextField fx:id="sshUsername" disable="true" layoutX="15.0" layoutY="54.0" prefHeight="20.0" promptText="%server.input.ssh.username.prompt" AnchorPane.leftAnchor="30.0" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="85.0" />
<JFXPasswordField fx:id="sshPassword" disable="true" layoutX="32.0" layoutY="132.0" prefHeight="23.0" prefWidth="144.0" promptText="%server.input.ssh.password.prompt" AnchorPane.leftAnchor="31.0" AnchorPane.rightAnchor="65.0" AnchorPane.topAnchor="131.0" />
<Text layoutX="92.0" layoutY="19.0" strokeType="OUTSIDE" strokeWidth="0.0" text="SSH Server" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="10.0" />
<JFXButton fx:id="sshPasswordVisibleButton" layoutX="182.0" layoutY="132.0" prefHeight="23.0" prefWidth="34.0" styleClass="visible-button" text="visible" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="131.0" />
<JFXTextField fx:id="sshServer" disable="true" layoutX="22.0" layoutY="42.0" prefHeight="23.0" prefWidth="144.0" promptText="ssh server" AnchorPane.leftAnchor="21.0" AnchorPane.rightAnchor="110.0" AnchorPane.topAnchor="41.0" />
<JFXTextField fx:id="sshUsername" disable="true" layoutX="15.0" layoutY="54.0" prefHeight="20.0" promptText="%server.input.ssh.username.prompt" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="85.0" />
<JFXPasswordField fx:id="sshPassword" disable="true" layoutX="32.0" layoutY="132.0" prefHeight="23.0" prefWidth="144.0" promptText="%server.input.ssh.password.prompt" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="60.0" AnchorPane.topAnchor="131.0" />
<Text layoutX="92.0" layoutY="19.0" strokeType="OUTSIDE" strokeWidth="0.0" text="SSH Server" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="10.0" />
<JFXButton fx:id="sshPasswordVisibleButton" layoutX="182.0" layoutY="132.0" prefHeight="23.0" prefWidth="34.0" styleClass="visible-button" text="visible" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="131.0" />
<Label layoutX="150.0" layoutY="45.0" prefHeight="23.0" text=":" AnchorPane.rightAnchor="100.0" AnchorPane.topAnchor="41.0" />
<JFXTextField fx:id="sshServerPort" alignment="CENTER" disable="true" layoutX="166.0" layoutY="51.0" prefHeight="23.0" prefWidth="60.0" promptText="port" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="41.0" />
</children>
</AnchorPane>
<ProgressBar fx:id="sshTunnelProgressBarTo" prefHeight="18.0" prefWidth="75.0" progress="0.0" GridPane.columnIndex="1" />
Expand All @@ -49,7 +51,7 @@
<JFXTextField fx:id="remoteServer" disable="true" layoutX="33.0" layoutY="73.0" prefHeight="23.0" prefWidth="80.0" promptText="remote server" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="100.0" AnchorPane.topAnchor="72.0" />
<Text layoutX="70.0" layoutY="55.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Remote Server" AnchorPane.leftAnchor="22.0" AnchorPane.topAnchor="20.0" />
<Label layoutX="154.0" layoutY="75.0" prefHeight="23.0" text=":" AnchorPane.rightAnchor="90.0" AnchorPane.topAnchor="71.0" />
<JFXTextField fx:id="remoteServerPort" disable="true" layoutX="170.0" layoutY="72.0" prefHeight="23.0" prefWidth="70.0" promptText="port" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="71.0" />
<JFXTextField fx:id="remoteServerPort" alignment="CENTER" disable="true" layoutX="170.0" layoutY="72.0" prefHeight="23.0" prefWidth="70.0" promptText="port" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="71.0" />
</children>
</AnchorPane>
</children>
Expand Down

0 comments on commit b7973d0

Please sign in to comment.