Skip to content

Commit 5d2686c

Browse files
authored
Fixed connection via domain name (SubnauticaNitrox#1272)
* Updated regex to remove domain validation * Added port to server save file and added input field * Changed logic for domain and ip validation * Moved ingamelogger to stop error if connection isnt valid * Added backwards compatibility for older servers * Seperated out the splitting of ip/port * Moved ingamelogger setup to main patcher and fixed server ip display
1 parent 7d006e9 commit 5d2686c

File tree

4 files changed

+61
-32
lines changed

4 files changed

+61
-32
lines changed

NitroxClient/MonoBehaviours/DiscordRP/DiscordRPController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public void JoinCallback(string secret)
3838
Log.Info("[Discord] Joining Server");
3939
if (SceneManager.GetActiveScene().name == "StartScreen" && MainMenuMultiplayerPanel.Main != null)
4040
{
41-
MainMenuMultiplayerPanel.Main.OpenJoinServerMenu(secret);
41+
string[] splitSecret = secret.Split(':');
42+
string ip = splitSecret[0];
43+
string port = splitSecret[1];
44+
MainMenuMultiplayerPanel.Main.OpenJoinServerMenu(ip,port);
4245
}
4346
else
4447
{

NitroxClient/MonoBehaviours/Gui/MainMenu/MainMenuMultiplayerPanel.cs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Net;
45
using System.Net.Sockets;
@@ -25,6 +26,7 @@ public class MainMenuMultiplayerPanel : MonoBehaviour
2526
public string SERVER_LIST_PATH = Path.Combine(".", "servers");
2627
private string serverHostInput;
2728
private string serverNameInput;
29+
private string serverPortInput;
2830

2931
private bool shouldFocus;
3032
private bool showingAddServer;
@@ -41,7 +43,7 @@ public void Awake()
4143

4244
if (!File.Exists(SERVER_LIST_PATH))
4345
{
44-
AddServer("local server", "127.0.0.1");
46+
AddServer("local server", "127.0.0.1", "11000");
4547
}
4648

4749
CreateButton("Add server IP", ShowAddServerWindow);
@@ -60,7 +62,7 @@ public void CreateButton(string text, UnityAction clickEvent)
6062
multiplayerButtonInst.transform.SetParent(savedGameAreaContent, false);
6163
}
6264

63-
public void CreateServerButton(string text, string joinIp)
65+
public void CreateServerButton(string text, string joinIp, string joinPort)
6466
{
6567
GameObject multiplayerButtonInst = Instantiate(multiplayerButton, savedGameAreaContent, false);
6668
multiplayerButtonInst.name = (savedGameAreaContent.childCount - 1).ToString();
@@ -73,7 +75,7 @@ public void CreateServerButton(string text, string joinIp)
7375
multiplayerButtonButton.onClick.AddListener(() =>
7476
{
7577
txt.GetComponent<Text>().color = prevTextColor; // Visual fix for black text after click (hover state still active)
76-
OpenJoinServerMenu(joinIp);
78+
OpenJoinServerMenu(joinIp, joinPort);
7779
});
7880

7981
GameObject delete = Instantiate(SavedGamesRef.GetComponent<MainMenuLoadPanel>().saveInstance.GetComponent<MainMenuLoadButton>().deleteButton);
@@ -87,11 +89,11 @@ public void CreateServerButton(string text, string joinIp)
8789
delete.transform.SetParent(multiplayerButtonInst.transform, false);
8890
}
8991

90-
public void AddServer(string name, string ip)
92+
public void AddServer(string name, string ip, string port)
9193
{
9294
using (StreamWriter sw = new StreamWriter(SERVER_LIST_PATH, true))
9395
{
94-
sw.WriteLine($"{name}|{ip}");
96+
sw.WriteLine($"{name}|{ip}|{port}");
9597
}
9698
}
9799

@@ -102,12 +104,12 @@ public void RemoveServer(int index)
102104
File.WriteAllLines(SERVER_LIST_PATH, serverLines.ToArray());
103105
}
104106

105-
public void OpenJoinServerMenu(string serverIp)
107+
public void OpenJoinServerMenu(string serverIp, string serverPort)
106108
{
107-
IPEndPoint endpoint = ResolveIp(serverIp) ?? ResolveHostName(serverIp);
109+
IPEndPoint endpoint = ResolveIPEndPoint(serverIp, serverPort);
108110
if (endpoint == null)
109111
{
110-
Log.InGame($"Unable to resolve remote address: {serverIp}");
112+
Log.InGame($"Unable to resolve remote address: {serverIp}:{serverPort}");
111113
return;
112114
}
113115

@@ -128,6 +130,7 @@ public void ShowAddServerWindow()
128130
{
129131
serverNameInput = "local";
130132
serverHostInput = "127.0.0.1";
133+
serverPortInput = "11000";
131134
showingAddServer = true;
132135
shouldFocus = true;
133136
}
@@ -158,41 +161,56 @@ private void LoadSavedServers()
158161
string[] lineData = line.Split('|');
159162
string serverName = lineData[0];
160163
string serverIp = lineData[1];
161-
CreateServerButton($"Connect to <b>{serverName}</b>\n{serverIp}", serverIp);
164+
string serverPort;
165+
if (lineData.Length == 3)
166+
{
167+
serverPort = lineData[2];
168+
}
169+
else
170+
{
171+
Match match = Regex.Match(serverIp, @"^(.*?)(?::(\d{3,5}))?$");
172+
serverIp = match.Groups[1].Value;
173+
serverPort = match.Groups[2].Success ? match.Groups[2].Value : "11000";
174+
}
175+
CreateServerButton($"Connect to <b>{serverName}</b>\n{serverIp}:{serverPort}", serverIp, serverPort);
162176
}
163177
}
164178
}
165179

166-
private IPEndPoint ResolveIp(string serverIp)
180+
private IPEndPoint ResolveIPEndPoint(string serverIp, string serverPort)
167181
{
168-
Match match = Regex.Match(serverIp, @"^((?:(?:[\da-f]+:??){8}|[\d\.]+|[^:\s]+)):?(\d+)?$"); // Pattern test url: https://regex101.com/r/WY4FWp/10
169-
if (!match.Success)
182+
UriHostNameType hostType = Uri.CheckHostName(serverIp);
183+
IPAddress address;
184+
switch (hostType)
170185
{
171-
return null;
186+
case UriHostNameType.IPv4:
187+
case UriHostNameType.IPv6:
188+
IPAddress.TryParse(serverIp, out address);
189+
break;
190+
case UriHostNameType.Dns:
191+
address = ResolveHostName(serverIp, serverPort);
192+
break;
193+
default:
194+
return null;
172195
}
173196

174-
IPAddress ip = IPAddress.Parse(match.Groups[1].Value);
175-
int port = match.Groups[2].Success ? int.Parse(match.Groups[2].Value) : 11000;
176-
return new IPEndPoint(ip, port);
177-
}
178-
179-
private IPEndPoint ResolveHostName(string hostname)
180-
{
181-
Match match = Regex.Match(hostname, @"^\s*([a-zA-Z\.]*)\:?(\d{2,5})?\s*$");
182-
if (!match.Success)
197+
if (address == null)
183198
{
184-
Log.ErrorSensitive("Hostname {hostname} has an invalid format", hostname);
185199
return null;
186200
}
201+
return new IPEndPoint(address, int.Parse(serverPort));
202+
}
187203

204+
private IPAddress ResolveHostName(string hostname, string serverPort)
205+
{
188206
try
189207
{
190-
IPHostEntry hostEntry = Dns.GetHostEntry(match.Groups[1].Value);
191-
return new IPEndPoint(hostEntry.AddressList[0], match.Groups[2].Success ? int.Parse(match.Groups[2].Value) : 11000);
208+
IPHostEntry hostEntry = Dns.GetHostEntry(hostname);
209+
return hostEntry.AddressList[0];
192210
}
193211
catch (SocketException ex)
194212
{
195-
Log.ErrorSensitive(ex, "Unable to resolve the address {hostname}", hostname);
213+
Log.ErrorSensitive(ex, "Unable to resolve the address {hostname}:{serverPort}", hostname, serverPort);
196214
return null;
197215
}
198216
}
@@ -201,8 +219,9 @@ private void OnAddServerButtonClicked()
201219
{
202220
serverNameInput = serverNameInput.Trim();
203221
serverHostInput = serverHostInput.Trim();
204-
AddServer(serverNameInput, serverHostInput);
205-
CreateServerButton($"Connect to <b>{serverNameInput}</b>\n{serverHostInput}", serverHostInput);
222+
serverPortInput = serverPortInput.Trim();
223+
AddServer(serverNameInput, serverHostInput, serverPortInput);
224+
CreateServerButton($"Connect to <b>{serverNameInput}</b>\n{serverHostInput}:{serverPortInput}", serverHostInput, serverPortInput);
206225
HideAddServerWindow();
207226
}
208227

@@ -270,6 +289,13 @@ private void DoAddServerWindow(int windowId)
270289
serverHostInput = GUILayout.TextField(serverHostInput, 120);
271290
}
272291

292+
using (new GUILayout.HorizontalScope())
293+
{
294+
GUILayout.Label("Port:");
295+
GUI.SetNextControlName("serverPortField");
296+
serverPortInput = GUILayout.TextField(serverPortInput);
297+
}
298+
273299
if (GUILayout.Button("Add server"))
274300
{
275301
OnAddServerButtonClicked();

NitroxClient/MonoBehaviours/Multiplayer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using NitroxModel.Packets;
2020
using NitroxModel.Packets.Processors.Abstract;
2121
using NitroxModel_Subnautica.Helper;
22-
using NitroxModel_Subnautica.Logger;
2322
using UnityEngine;
2423
using UnityEngine.SceneManagement;
2524

@@ -77,7 +76,6 @@ public static IEnumerator LoadAsync()
7776

7877
public void Awake()
7978
{
80-
Log.InGameLogger = new SubnauticaInGameLogger();
8179
Log.InGame("Multiplayer Client Loaded...");
8280

8381
multiplayerSession = NitroxServiceLocator.LocateService<IMultiplayerSession>();

NitroxPatcher/Main.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using NitroxModel.DataStructures.Util;
1212
using NitroxModel.Helper;
1313
using NitroxModel.Logger;
14+
using NitroxModel_Subnautica.Logger;
1415
using NitroxPatcher.Modules;
1516
using NitroxPatcher.Patches;
1617
using UnityEngine;
@@ -30,6 +31,7 @@ public static class Main
3031
public static void Execute()
3132
{
3233
Log.Setup(true);
34+
Log.InGameLogger = new SubnauticaInGameLogger();
3335
Log.Info($"Using Nitrox version {Assembly.GetExecutingAssembly().GetName().Version} built on {File.GetCreationTimeUtc(Assembly.GetExecutingAssembly().Location)}");
3436
try
3537
{

0 commit comments

Comments
 (0)