| 1 |
using System.Diagnostics.CodeAnalysis; |
| 2 |
using System.Text.RegularExpressions; |
| 3 |
|
| 4 |
namespace GodotTools.IdeMessaging |
| 5 |
{ |
| 6 |
public class ClientHandshake : IHandshake |
| 7 |
{ |
| 8 |
private static readonly string ClientHandshakeBase = $"{Peer.ClientHandshakeName},Version={Peer.ProtocolVersionMajor}.{Peer.ProtocolVersionMinor}.{Peer.ProtocolVersionRevision}"; |
| 9 |
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}})"; |
| 10 |
|
| 11 |
public string GetHandshakeLine(string identity) => $"{ClientHandshakeBase},{identity}"; |
| 12 |
|
| 13 |
public bool IsValidPeerHandshake(string handshake, [NotNullWhen(true)] out string? identity, ILogger logger) |
| 14 |
{ |
| 15 |
identity = null; |
| 16 |
|
| 17 |
var match = Regex.Match(handshake, ServerHandshakePattern); |
| 18 |
|
| 19 |
if (!match.Success) |
| 20 |
return false; |
| 21 |
|
| 22 |
if (!uint.TryParse(match.Groups[1].Value, out uint serverMajor) || Peer.ProtocolVersionMajor != serverMajor) |
| 23 |
{ |
| 24 |
logger.LogDebug("Incompatible major version: " + match.Groups[1].Value); |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
if (!uint.TryParse(match.Groups[2].Value, out uint serverMinor) || Peer.ProtocolVersionMinor < serverMinor) |
| 29 |
{ |
| 30 |
logger.LogDebug("Incompatible minor version: " + match.Groups[2].Value); |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
if (!uint.TryParse(match.Groups[3].Value, out uint _)) |
| 35 |
{ |
| 36 |
logger.LogDebug("Incompatible revision build: " + match.Groups[3].Value); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
identity = match.Groups[4].Value; |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
} |
| 45 |
} |