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

Ip adress sanitization for MP Menu screen #196

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Changes from 2 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
26 changes: 26 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ private static void OnJoinGameButtonClick()
string ip = parts[0];
int port;

//remove copy and paste mistakes and update the textbox to prevent user confusion in case of invalid ip address
ip = SanitizeIpAddressString(ip);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using String.Trim() is probably better than a new function here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trim is identical to Trim(whitespace)

hostIPAdressInput.text = parts.Length == 1 ? ip : ip + ":" + parts[1];

if (!System.Net.IPAddress.TryParse(ip, out _))
{
//log, notify user and early return to prevent crash on invalid strings
InGamePopup.ShowWarning("Invalid connection string", "Note: This mod currently only accepts IPv4 Addresses.", "OK");
Mushroom marked this conversation as resolved.
Show resolved Hide resolved
Log.Info($"IP String {ip} is not a valid IPv4 String");
return;
}

if (parts.Length == 1)
{
port = Config.DefaultPort;
Expand All @@ -181,6 +193,7 @@ private static void OnJoinGameButtonClick()
return;
}


Mushroom marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Should display a loader during the connection and only open the game once the player is connected to the server.
multiplayerMenu.gameObject.SetActive(false);

Expand All @@ -195,5 +208,18 @@ private static void OnJoinGameBackButtonClick()
multiplayerMenu.gameObject.SetActive(false);
UIRoot.instance.OpenMainMenuUI();
}

private static string SanitizeIpAddressString(string ipAddr)
pGlase marked this conversation as resolved.
Show resolved Hide resolved
{
//remove simple copy&paste errors
string cleanedIpAddr = new System.Text.StringBuilder(ipAddr)
.Replace("\r", string.Empty)
.Replace("\n", string.Empty)
.Replace(" ", string.Empty)
.Replace("\t", string.Empty)
.ToString();

return cleanedIpAddr;
}
}
}