cache IP address of clients

This commit is contained in:
in0finite 2022-04-03 19:01:00 +02:00
parent 46c2eb152c
commit 03ee73afee
3 changed files with 18 additions and 12 deletions

View file

@ -78,7 +78,7 @@ namespace SanAndreasUnity.Commands
{
response += player.netId;
if (NetUtils.IsServer && context.hasServerPermissions)
response += " | " + player.connectionToClient.address;
response += " | " + player.CachedIpAddress;
response += "\n";
}
}

View file

@ -41,11 +41,16 @@ namespace SanAndreasUnity.Net
public static readonly int MinPlayerNameLength = 2;
public static readonly string DefaultPlayerName = "Player";
public string DescriptionForLogging => "(netId=" + this.netId + ", addr=" + (this.connectionToClient != null ? this.connectionToClient.address : "") + ")";
public string DescriptionForLogging => $"(netId={this.netId}, addr={this.CachedIpAddress})";
private readonly SyncDictionary<string, string> m_syncDictionary = new SyncDictionary<string, string>();
public SyncedBag ExtraData { get; }
/// <summary>
/// We cache IP address of the client, because it's sometimes not available (eg. in destroy callbacks).
/// </summary>
public string CachedIpAddress { get; private set; } = string.Empty;
Player()
{
@ -86,6 +91,16 @@ namespace SanAndreasUnity.Net
F.InvokeEventExceptionSafe(onDisable, this);
// log some info about this
if (!this.isLocalPlayer)
Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging);
}
public override void OnStartServer()
{
base.OnStartServer();
this.CachedIpAddress = this.connectionToClient.address;
}
public override void OnStartClient()
@ -113,15 +128,6 @@ namespace SanAndreasUnity.Net
F.InvokeEventExceptionSafe(onStart, this);
}
public override void OnStopClient()
{
base.OnStopClient();
// log some info about this
if (!this.isLocalPlayer)
Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging);
}
void OnOwnedGameObjectChanged(GameObject oldGo, GameObject newGo)
{
Debug.LogFormat("Owned game object changed for player (net id {0})", this.netId);

View file

@ -52,7 +52,7 @@ namespace SanAndreasUnity.Stats
m_currentIndex = 0;
GUILayout.Label(p.PlayerName, GUILayout.Width(GetWidth()));
GUILayout.Label(isServer ? p.connectionToClient.address : "", GUILayout.Width(GetWidth()));
GUILayout.Label(p.CachedIpAddress, GUILayout.Width(GetWidth()));
GUILayout.Label(p.netId.ToString(), GUILayout.Width(GetWidth()));
GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "", GUILayout.Width(GetWidth()));
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "", GUILayout.Width(GetWidth()));