2024-01-13 04:24:12 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-05-09 18:45:43 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace GodotTools.IdeMessaging
|
|
|
|
{
|
|
|
|
public class ClientHandshake : IHandshake
|
|
|
|
{
|
|
|
|
private static readonly string ClientHandshakeBase = $"{Peer.ClientHandshakeName},Version={Peer.ProtocolVersionMajor}.{Peer.ProtocolVersionMinor}.{Peer.ProtocolVersionRevision}";
|
|
|
|
private static readonly string ServerHandshakePattern = $@"{Regex.Escape(Peer.ServerHandshakeName)},Version=([0-9]+)\.([0-9]+)\.([0-9]+),([_a-zA-Z][_a-zA-Z0-9]{{0,63}})";
|
|
|
|
|
|
|
|
public string GetHandshakeLine(string identity) => $"{ClientHandshakeBase},{identity}";
|
|
|
|
|
2024-01-13 04:24:12 +00:00
|
|
|
public bool IsValidPeerHandshake(string handshake, [NotNullWhen(true)] out string? identity, ILogger logger)
|
2020-05-09 18:45:43 +00:00
|
|
|
{
|
|
|
|
identity = null;
|
|
|
|
|
|
|
|
var match = Regex.Match(handshake, ServerHandshakePattern);
|
|
|
|
|
|
|
|
if (!match.Success)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!uint.TryParse(match.Groups[1].Value, out uint serverMajor) || Peer.ProtocolVersionMajor != serverMajor)
|
|
|
|
{
|
|
|
|
logger.LogDebug("Incompatible major version: " + match.Groups[1].Value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uint.TryParse(match.Groups[2].Value, out uint serverMinor) || Peer.ProtocolVersionMinor < serverMinor)
|
|
|
|
{
|
|
|
|
logger.LogDebug("Incompatible minor version: " + match.Groups[2].Value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uint.TryParse(match.Groups[3].Value, out uint _)) // Revision
|
|
|
|
{
|
|
|
|
logger.LogDebug("Incompatible revision build: " + match.Groups[3].Value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
identity = match.Groups[4].Value;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|