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

feat(dns): Add dns hostname resolving #46

Open
wants to merge 1 commit 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
79 changes: 73 additions & 6 deletions BeaverBuddies/Connect/ClientConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using BeaverBuddies.Util;
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;
using Timberborn.CoreUI;
using Timberborn.GameSaveRepositorySystem;
using Timberborn.GameSceneLoading;
Expand Down Expand Up @@ -55,13 +57,42 @@ public bool TryToConnect(string address)
// it's actually loaded.
SingletonManager.Reset();
Plugin.Log("Connecting client");
client = ClientEventIO.Create(address, EventIO.Config.Port, LoadMap, (error) =>
Plugin.Log("Try to resolve address: " + address);

try
{
// Parse address and port
var (hostAddress, port) = ParseAddressAndPort(address);

// Set port if provided
if (port.HasValue)
{
EventIO.Config.Port = port.Value;
}

// Resolve the address if it's a hostname
hostAddress = ResolveHostnameIfNecessary(hostAddress);

// Attempt to create the client
client = ClientEventIO.Create(hostAddress, EventIO.Config.Port, LoadMap, error =>
{
ShowError("BeaverBuddies.JoinCoopGame.ConnectionFailedMessageWithError", error);
});

if (client == null)
{
Plugin.Log("Client creation failed.");
return false;
}

EventIO.Set(client);
return true;
}
catch (Exception ex)
{
ShowError("BeaverBuddies.JoinCoopGame.ConnectionFailedMessageWithError", error);
});
if (client == null) return false;
EventIO.Set(client);
return true;
ShowError("BeaverBuddies.JoinCoopGame.ConnectionFailedMessageWithError", ex.Message);
return false;
}
}

public void ConnectOrShowFailureMessage()
Expand Down Expand Up @@ -116,5 +147,41 @@ public void UpdateSingleton()
//Plugin.Log("Updating client!");
client.Update();
}

private (string, int?) ParseAddressAndPort(string address)
{
// If address contains a port, split and parse it
if (address.Contains(":"))
{
var tokens = address.Split(':');
if (tokens.Length == 2 && int.TryParse(tokens[1], out int port))
{
return (tokens[0], port);
}
else
{
throw new FormatException("Invalid address format. Could not parse port.");
}
}
return (address, null);
}

private string ResolveHostnameIfNecessary(string address)
{
// If it's not an IP address, resolve the hostname
if (!IPAddress.TryParse(address, out _))
{
IPHostEntry hostEntry = Dns.GetHostEntry(address);
if (hostEntry.AddressList.Length > 0)
{
return hostEntry.AddressList[0].ToString();
}
else
{
throw new Exception("Hostname could not be resolved to an IP address.");
}
}
return address; // If already an IP, return as-is
}
}
}
18 changes: 17 additions & 1 deletion BeaverBuddies/Connect/ClientConnectionUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using Timberborn.CoreUI;
using Timberborn.Localization;
Expand Down Expand Up @@ -64,6 +66,12 @@ public void AddJoinButton(VisualElement __result)

private void ShowBox()
{
FieldInfo characterLimitField = typeof(InputBoxShower).GetField("CharacterLimit", BindingFlags.Static | BindingFlags.NonPublic);
if (characterLimitField != null)
{
characterLimitField.SetValue(null, 100);
}

ILoc _loc = _inputBoxShower._loc;
var builder = _inputBoxShower.Create()
.SetLocalizedMessage(_loc.T("BeaverBuddies.JoinCoopGame.EnterIp"))
Expand All @@ -73,7 +81,15 @@ private void ShowBox()
_configIOService.SaveConfigToFile();
_clientConnectionService.ConnectOrShowFailureMessage(ip);
});
builder._input.value = EventIO.Config.ClientConnectionAddress;

FieldInfo inputFieldInfo = typeof(InputBoxShower.Builder).GetField("_input", BindingFlags.Instance | BindingFlags.NonPublic);
TextField inputField = inputFieldInfo?.GetValue(builder) as TextField;
if (inputField != null)
{
inputField.maxLength = 100;
inputField.value = EventIO.Config.ClientConnectionAddress;
}

builder.Show();
}
}
Expand Down