Skip to content

Commit

Permalink
Merge pull request #486 from MUnique/dev/improve-ip-resolve
Browse files Browse the repository at this point in the history
Improvements for the connect server ip resolve
  • Loading branch information
sven-n authored Sep 4, 2024
2 parents 6f38b88 + 1dc20fe commit 9413e67
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/ConnectServer/PacketHandler/ServerInfoRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MUnique.OpenMU.ConnectServer.PacketHandler;

using System.Net;
using Microsoft.Extensions.Logging;
using MUnique.OpenMU.Network;
using MUnique.OpenMU.Network.Packets.ConnectServer;
Expand All @@ -15,6 +16,7 @@ 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 @@ -25,6 +27,7 @@ public ServerInfoRequestHandler(IConnectServer connectServer, ILogger<ServerInfo
{
this._connectServer = connectServer;
this._logger = logger;
this._localIpAddresses = Dns.GetHostAddresses(Dns.GetHostName()).ToHashSet();
}

/// <inheritdoc/>
Expand All @@ -38,8 +41,35 @@ public async ValueTask HandlePacketAsync(Client client, Memory<byte> packet)
await client.Connection.DisconnectAsync().ConfigureAwait(false);
}

if (this._connectServer.ConnectInfos.TryGetValue(serverId, out var connectInfo))
// First we look, if we can just use the IP address which the client connected to.
// If the game server is running on the same ip as the connect server, we can use that.
// 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);
if (isGameServerOnSameMachineAsConnectServer
&& isClientConnectedOnNonRegisteredAddress) // only if we can't use the cached data
{
int WritePacket()
{
var data = client.Connection.Output.GetSpan(ConnectionInfoRef.Length)[..ConnectionInfoRef.Length];
_ = new ConnectionInfoRef(data)
{
IpAddress = localIpEndPoint!.Address.ToString(),
Port = (ushort)serverItem!.EndPoint.Port
};
return data.Length;
}

await client.Connection.SendAsync(WritePacket).ConfigureAwait(false);
}
else if (this._connectServer.ConnectInfos.TryGetValue(serverId, out var connectInfo))
{
// more optimal way, because the serialized data was cached.

int WritePacket()
{
var span = client.Connection.Output.GetSpan(connectInfo.Length)[..connectInfo.Length];
Expand Down
4 changes: 4 additions & 0 deletions src/Network/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public Connection(IDuplexPipe duplexPipe, IPipelinedDecryptor? decryptionPipe, I
this._logger = logger;
this.Source = decryptionPipe?.Reader ?? this._duplexPipe!.Input;
this._remoteEndPoint = this.SocketConnection?.Socket.RemoteEndPoint ?? new IPEndPoint(IPAddress.Any, 0);
this.LocalEndPoint = this.SocketConnection?.Socket.LocalEndPoint;
this.OutputLock = new();
}

Expand All @@ -77,6 +78,9 @@ public Connection(IDuplexPipe duplexPipe, IPipelinedDecryptor? decryptionPipe, I
/// <inheritdoc />
public EndPoint? EndPoint => this._remoteEndPoint;

/// <inheritdoc />
public EndPoint? LocalEndPoint { get; }

/// <inheritdoc />
public PipeWriter Output => this._outputWriter ??= new ExtendedPipeWriter(this._encryptionPipe?.Writer ?? this._duplexPipe!.Output, OutgoingBytesCounter);

Expand Down
5 changes: 5 additions & 0 deletions src/Network/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public interface IConnection : IDisposable
/// </summary>
EndPoint? EndPoint { get; }

/// <summary>
/// Gets the local endpoint of the connection.
/// </summary>
EndPoint? LocalEndPoint { get; }

/// <summary>
/// Gets the pipe writer to send data.
/// </summary>
Expand Down

0 comments on commit 9413e67

Please sign in to comment.