Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Port Validation (Sentry) #6857

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions megamek/i18n/megamek/client/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4226,6 +4226,7 @@ MegaMek.LoadGameIncorrectVersion.message=Error: MegaMek does not support version
MegaMek.HostGameAlert.title=Host a Game
MegaMek.HostDialog.title=Start Game
MegaMek.PlayerNameError=Error: enter a player name.
MegaMek.PortError=Error: enter a valid port. Port must be between {0} and {1}.
MegaMek.StartServerError=Error: could not start server at localhost:%s \n(%s)
MegaMek.ServerConnectionError=Error: could not connect to server at %s:%s
MegaMek.ConnectDialog.title=Connect To Game
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package megamek.client.ui.swing.gameConnectionDialogs;

import static megamek.codeUtilities.MathUtility.clamp;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
Expand All @@ -26,7 +28,6 @@
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.*;

import megamek.MMConstants;
Expand All @@ -37,6 +38,7 @@
import megamek.client.ui.swing.OkayAction;
import megamek.client.ui.swing.dialog.DialogButton;
import megamek.client.ui.swing.util.UIUtil;
import megamek.codeUtilities.MathUtility;
import megamek.codeUtilities.StringUtility;
import megamek.common.preference.ClientPreferences;
import megamek.common.preference.PreferenceManager;
Expand All @@ -47,8 +49,8 @@ public abstract class AbstractGameConnectionDialog extends ClientDialog implemen
private static final MMLogger logger = MMLogger.create(AbstractGameConnectionDialog.class);

/**
* We need a way to access the action map for a JComboBox editor, so that we can
* have it fire an action when wenter is pressed. This simple class allows this.
* We need a way to access the action map for a JComboBox editor, so that we can have it fire an action when wenter
* is pressed. This simple class allows this.
*/
public static class SimpleComboBoxEditor extends JTextField implements ComboBoxEditor {

Expand Down Expand Up @@ -94,7 +96,7 @@ protected AbstractGameConnectionDialog(JFrame owner, String title, boolean modal
}

protected AbstractGameConnectionDialog(JFrame owner, String title, boolean modal, String playerName,
Vector<String> playerNames) {
Vector<String> playerNames) {
super(owner, title, modal);

this.playerNames = playerNames;
Expand Down Expand Up @@ -240,16 +242,20 @@ public boolean dataValidation(String errorTitleKey) {
try {
setPlayerName(Server.validatePlayerName(playerName));
} catch (Exception ex) {
JOptionPane.showMessageDialog(getOwner(), Messages.getString("MegaMek.PlayerNameError"),
Messages.getString(errorTitleKey), JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(getOwner(),
Messages.getString("MegaMek.PlayerNameError"),
Messages.getString(errorTitleKey),
JOptionPane.ERROR_MESSAGE);
return false;
}

try {
setPort(Server.validatePort(port));
} catch (Exception ex) {
JOptionPane.showMessageDialog(getOwner(), Messages.getString("MegaMek.PortError"),
Messages.getString(errorTitleKey), JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(getOwner(),
Messages.getFormattedString("MegaMek.PortError", MMConstants.MIN_PORT, MMConstants.MAX_PORT),
Messages.getString(errorTitleKey),
JOptionPane.ERROR_MESSAGE);
return false;
}

Expand All @@ -261,12 +267,8 @@ public boolean dataValidation(String errorTitleKey) {
public void actionPerformed(ActionEvent e) {
// reached from the Okay button or pressing Enter in the text fields
setPlayerName(getPlayerNameFromUI());
try {
setPort(Integer.parseInt(getPortField().getText()));
} catch (NumberFormatException ex) {
logger.error(ex, "");
}

int port = MathUtility.parseInt(getPortField().getText(), MMConstants.DEFAULT_PORT);
setPort(clamp(port, MMConstants.MIN_PORT, MMConstants.MAX_PORT));
setConfirmed(true);
getClientPreferences().setLastPlayerName(getPlayerName());
getClientPreferences().setLastConnectPort(getPort());
Expand Down