Skip to content

Commit

Permalink
Allow LAN connections when NAT Loopback is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 30, 2024
1 parent dd7172a commit de4ba40
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static class MatchmakerAcceptPatches
public static MatchMakerGroupPreview MatchMakerGroupPreview { get; set; }
public static int HostExpectedNumberOfPlayers { get; set; } = 1;
public static WeatherClass[] Nodes { get; set; } = null;
public static string RemoteIp;
public static int RemotePort;
private static string groupId;
private static long timestamp;
#endregion
Expand Down
15 changes: 9 additions & 6 deletions Fika.Core/Networking/FikaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public NetManager NetClient
}
}
public NetPeer ServerConnection { get; private set; }
public string IP { get; private set; }
public int Port { get; private set; }
/*public string IP { get; private set; }
public int Port { get; private set; }*/
public bool SpawnPointsReceived { get; private set; } = false;
private readonly ManualLogSource clientLogger = BepInEx.Logging.Logger.CreateLogSource("Fika.Client");

Expand Down Expand Up @@ -94,19 +94,22 @@ public void Init()

_netClient.Start();

GetHostRequest body = new(MatchmakerAcceptPatches.GetGroupId());
/*GetHostRequest body = new(MatchmakerAcceptPatches.GetGroupId());
GetHostResponse result = FikaRequestHandler.GetHost(body);
IP = result.Ip;
Port = result.Port;
Port = result.Port;*/

if (string.IsNullOrEmpty(IP))
string ip = MatchmakerAcceptPatches.RemoteIp;
int port = MatchmakerAcceptPatches.RemotePort;

if (string.IsNullOrEmpty(ip))
{
Singleton<PreloaderUI>.Instance.ShowErrorScreen("Network Error", "Unable to connect to the raid server. IP and/or Port was empty when requesting data!");
}
else
{
ServerConnection = _netClient.Connect(IP, Port, "fika.core");
ServerConnection = _netClient.Connect(ip, port, "fika.core");
};

FikaEventDispatcher.DispatchEvent(new FikaClientCreatedEvent(this));
Expand Down
26 changes: 22 additions & 4 deletions Fika.Core/Networking/FikaPingingClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BepInEx.Logging;
using EFT.UI;
using Fika.Core.Coop.Matchmaker;
using Fika.Core.Networking.Http;
using Fika.Core.Networking.Http.Models;
using LiteNetLib;
Expand All @@ -14,6 +16,7 @@ internal class FikaPingingClient(string serverId) : INetEventListener
private readonly ManualLogSource _logger = Logger.CreateLogSource("Fika.PingingClient");
private readonly string serverId = serverId;
private IPEndPoint remoteEndPoint;
private IPEndPoint localEndPoint;
public bool Received = false;

public bool Init()
Expand All @@ -26,7 +29,12 @@ public bool Init()
GetHostRequest body = new(serverId);
GetHostResponse result = FikaRequestHandler.GetHost(body);

string ip = result.Ip;
string ip = result.Ips[0];
string localIp = null;
if (result.Ips.Length > 1)
{
localIp = result.Ips[1];
}
int port = result.Port;

if (string.IsNullOrEmpty(ip))
Expand All @@ -42,23 +50,31 @@ public bool Init()
}

remoteEndPoint = new(IPAddress.Parse(ip), port);
if (!string.IsNullOrEmpty(localIp))
{
localEndPoint = new(IPAddress.Parse(localIp), port);
}

NetClient.Start();

return true;
}

public bool PingEndPoint()
public void PingEndPoint()
{
if (Received)
{
return true;
return;
}

NetDataWriter writer = new();
writer.Put("fika.hello");

return NetClient.SendUnconnectedMessage(writer, remoteEndPoint);
NetClient.SendUnconnectedMessage(writer, remoteEndPoint);
if (localEndPoint != null)
{
NetClient.SendUnconnectedMessage(writer, localEndPoint);
}
}

public void OnConnectionRequest(ConnectionRequest request)
Expand Down Expand Up @@ -94,6 +110,8 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead
if (result == "fika.hello")
{
Received = true;
MatchmakerAcceptPatches.RemoteIp = remoteEndPoint.Address.ToString();
MatchmakerAcceptPatches.RemotePort = remoteEndPoint.Port;
}
else
{
Expand Down
19 changes: 18 additions & 1 deletion Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,24 @@ public async Task Init()
NotificationManagerClass.DisplayMessageNotification($"Server started on port {_netServer.LocalPort}.",
EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.EntryPoint);

SetHostRequest body = new(MyExternalIP, Port);
string[] Ips = [];

foreach (string ip in FikaPlugin.Instance.LocalIPs)
{
if (ip.StartsWith("192.168")) // need to add more cases here later, for now only check normal range...
{
Ips = [MyExternalIP, ip];
}
}

if (Ips.Length < 1)
{
Ips = [MyExternalIP, ""];
NotificationManagerClass.DisplayMessageNotification("Could not find a valid local IP!",
iconType: EFT.Communications.ENotificationIconType.Alert);
}

SetHostRequest body = new(Ips, Port);
FikaRequestHandler.UpdateSetHost(body);

FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this));
Expand Down
9 changes: 5 additions & 4 deletions Fika.Core/Networking/Models/GetHostResponse.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Fika.Core.Networking.Http.Models
{
[DataContract]
public struct GetHostResponse
{
[DataMember(Name = "ip")]
public string Ip;
[DataMember(Name = "ips")]
public string[] Ips;

[DataMember(Name = "port")]
public int Port;

public GetHostResponse(string ip, int port)
public GetHostResponse(string[] ips, int port)
{
Ip = ip;
Ips = ips;
Port = port;
}
}
Expand Down
9 changes: 5 additions & 4 deletions Fika.Core/Networking/Models/SetHostRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fika.Core.Coop.Components;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Fika.Core.Networking.Http.Models
Expand All @@ -9,16 +10,16 @@ public struct SetHostRequest
[DataMember(Name = "serverId")]
public string ServerId;

[DataMember(Name = "ip")]
public string Ip;
[DataMember(Name = "ips")]
public string[] Ips;

[DataMember(Name = "port")]
public int Port;

public SetHostRequest(string ip, int port)
public SetHostRequest(string[] ips, int port)
{
ServerId = CoopHandler.GetServerId();
Ip = ip;
Ips = ips;
Port = port;
}
}
Expand Down

0 comments on commit de4ba40

Please sign in to comment.