forked from forerunnergames/energy-shot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cs
73 lines (60 loc) · 2.93 KB
/
Tools.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Text.RegularExpressions;
using Godot;
namespace energyshot;
public static partial class Tools
{
// @formatter:off
[GeneratedRegex (@"^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$")]
private static partial Regex ValidIPv4Regex();
[GeneratedRegex (@"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}(([0-9]{1,3}\.){3,3}[0-9]{1,3})|([0-9a-fA-F]{1,4}:){1,4}:(([0-9]{1,3}\.){3,3}[0-9]{1,3}))$")]
private static partial Regex ValidIPv6Regex();
[GeneratedRegex (@"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z]{2,})+$")] private static partial Regex ValidHostnameRegex();
public static bool IsValidServerAddress (string address) => IsValidIPv4 (address) || IsValidIPv6 (address) || IsValidHostname (address);
// @formatter:on
public static (bool success, string address, string error) FindServerAddress (int port)
{
var uPnp = new Upnp();
var discoverResult = (Upnp.UpnpResult)uPnp.Discover();
if (discoverResult != Upnp.UpnpResult.Success)
{
var error = $"UPNP discovery failed, error [{discoverResult}]";
GD.Print (error);
return (false, string.Empty, error);
}
if (uPnp.GetGateway() == null || !uPnp.GetGateway().IsValidGateway())
{
const string error = "UPNP invalid gateway";
GD.Print (error);
return (false, string.Empty, error);
}
var removeMapResult = (Upnp.UpnpResult)uPnp.DeletePortMapping (port);
GD.Print (removeMapResult == Upnp.UpnpResult.Success
? $"UPNP successfully removed existing port mapping on port [{port}]"
: $"No existing port mapping to remove on port [{port}], or an error occurred: [{removeMapResult}]");
var mapResult = (Upnp.UpnpResult)uPnp.AddPortMapping (port);
if (mapResult != Upnp.UpnpResult.Success)
{
var error = $"UPNP port mapping failed, error [{mapResult}]";
GD.Print (error);
return (false, string.Empty, error);
}
var address = uPnp.QueryExternalAddress();
GD.Print ($"UPNP setup successfully, server address [{address}]");
return (true, address, string.Empty);
}
private static bool IsValidIPv4 (string address)
{
var regex = ValidIPv4Regex();
return regex.IsMatch (address);
}
private static bool IsValidIPv6 (string address)
{
var regex = ValidIPv6Regex();
return regex.IsMatch (address);
}
private static bool IsValidHostname (string address)
{
var regex = ValidHostnameRegex();
return regex.IsMatch (address);
}
}