Skip to content

Commit

Permalink
Merge pull request #488 from MUnique/dev/improve-ip-resolve-chatserver
Browse files Browse the repository at this point in the history
Improved ip resolving for the chat server, too
  • Loading branch information
sven-n authored Sep 7, 2024
2 parents de61e91 + c73c8c3 commit 0bf1a6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/ConnectServer/PacketHandler/ServerInfoRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class ServerInfoRequestHandler : IPacketHandler<Client>
{
private readonly IConnectServer _connectServer;
private readonly ILogger<ServerInfoRequestHandler> _logger;
private readonly HashSet<IPAddress> _localIpAddresses;

/// <summary>
/// Initializes a new instance of the <see cref="ServerInfoRequestHandler" /> class.
Expand All @@ -27,7 +26,6 @@ public ServerInfoRequestHandler(IConnectServer connectServer, ILogger<ServerInfo
{
this._connectServer = connectServer;
this._logger = logger;
this._localIpAddresses = Dns.GetHostAddresses(Dns.GetHostName()).ToHashSet();
}

/// <inheritdoc/>
Expand All @@ -46,10 +44,9 @@ public async ValueTask HandlePacketAsync(Client client, Memory<byte> packet)
// This way, we can be sure, that the client can connect to it, too.
var localIpEndPoint = client.Connection.LocalEndPoint as IPEndPoint;
var serverItem = this._connectServer.ServerList.GetItem(serverId);
var isGameServerOnSameMachineAsConnectServer = serverItem is not null
&& this._localIpAddresses.Contains(serverItem.EndPoint.Address);
var isClientConnectedOnNonRegisteredAddress = localIpEndPoint is not null
&& !object.Equals(client.Address, localIpEndPoint.Address);
var isGameServerOnSameMachineAsConnectServer = (serverItem?.EndPoint.Address).IsOnSameHost();
var isClientConnectedOnNonRegisteredAddress = !object.Equals(serverItem?.EndPoint.Address, localIpEndPoint?.Address);

if (isGameServerOnSameMachineAsConnectServer
&& isClientConnectedOnNonRegisteredAddress) // only if we can't use the cached data
{
Expand Down
12 changes: 11 additions & 1 deletion src/GameServer/RemoteView/Messenger/ChatRoomCreatedPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace MUnique.OpenMU.GameServer.RemoteView.Messenger;

using System.Net;
using System.Runtime.InteropServices;
using MUnique.OpenMU.GameLogic.Views.Messenger;
using MUnique.OpenMU.Interfaces;
using MUnique.OpenMU.Network;
using MUnique.OpenMU.Network.Packets.ServerToClient;
using MUnique.OpenMU.PlugIns;

Expand All @@ -28,8 +30,16 @@ public class ChatRoomCreatedPlugIn : IChatRoomCreatedPlugIn
/// <inheritdoc/>
public async ValueTask ChatRoomCreatedAsync(ChatServerAuthenticationInfo authenticationInfo, string friendName, bool success)
{
var hostAddress = authenticationInfo.HostAddress;
if (IPAddress.TryParse(authenticationInfo.HostAddress, out var chatServerAddress)
&& chatServerAddress.IsOnSameHost()
&& this._player.Connection?.LocalEndPoint is IPEndPoint localEndPoint)
{
hostAddress = localEndPoint.Address.ToString();
}

await this._player.Connection.SendChatRoomConnectionInfoAsync(
authenticationInfo.HostAddress,
hostAddress,
authenticationInfo.RoomId,
uint.Parse(authenticationInfo.AuthenticationToken),
friendName,
Expand Down
39 changes: 39 additions & 0 deletions src/Network/IpAddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <copyright file="IpAddressExtensions.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

namespace MUnique.OpenMU.Network;

using System.Diagnostics.CodeAnalysis;
using System.Net;

/// <summary>
/// Extensions for <see cref="IPAddress"/>.
/// </summary>
public static class IpAddressExtensions
{
private static HashSet<IPAddress>? _localIpAddresses;

/// <summary>
/// Determines whether the address in on same (this) host.
/// </summary>
/// <param name="address">The next server address.</param>
/// <returns>
/// <c>true</c> if the specified address is on same host as this machine; otherwise, <c>false</c>.
/// </returns>
public static bool IsOnSameHost([NotNullWhen(true)] this IPAddress? address)
{
if (address is null)
{
return false;
}

if (IPAddress.IsLoopback(address))
{
return true;
}

_localIpAddresses ??= Dns.GetHostAddresses(Dns.GetHostName()).ToHashSet();
return _localIpAddresses.Contains(address);
}
}

0 comments on commit 0bf1a6e

Please sign in to comment.