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

Improvements for the connect server ip resolve #486

Merged
merged 2 commits into from
Sep 4, 2024
Merged
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
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
Loading