Use system URI parser to parse AaruRemote endpoints. Fixes #409.

This commit is contained in:
2021-06-22 20:23:21 +01:00
parent 0d5802bf74
commit c92a4e5a3a
4 changed files with 37 additions and 22 deletions

View File

@@ -73,17 +73,22 @@ namespace Aaru.Devices
IsRemovable = false;
_devicePath = devicePath;
if(devicePath.StartsWith("dic://", StringComparison.OrdinalIgnoreCase) ||
devicePath.StartsWith("aaru://", StringComparison.OrdinalIgnoreCase))
Uri aaruUri;
try
{
devicePath =
devicePath.Substring(devicePath.StartsWith("dic://", StringComparison.OrdinalIgnoreCase) ? 6 : 7);
aaruUri = new Uri(devicePath);
}
catch(Exception)
{
// Ignore, treat as local path below
aaruUri = new Uri("/");
}
string[] pieces = devicePath.Split('/');
string host = pieces[0];
devicePath = devicePath.Substring(host.Length);
_remote = new Remote.Remote(host);
if(aaruUri.Scheme == "dic" ||
aaruUri.Scheme == "aaru")
{
devicePath = aaruUri.AbsolutePath;
if(devicePath.StartsWith('/'))
devicePath = devicePath.Substring(1);
@@ -91,6 +96,8 @@ namespace Aaru.Devices
if(devicePath.StartsWith("dev"))
devicePath = $"/{devicePath}";
_remote = new Remote.Remote(aaruUri);
Error = !_remote.Open(devicePath, out int errno);
LastError = errno;
}

View File

@@ -92,10 +92,17 @@ namespace Aaru.Devices
try
{
if(aaruRemote.ToLowerInvariant().StartsWith("aaru://", StringComparison.OrdinalIgnoreCase))
aaruRemote = aaruRemote.Substring(7);
var aaruUri = new Uri(aaruRemote);
using var remote = new Remote.Remote(aaruRemote);
if(aaruUri.Scheme != "aaru" &&
aaruUri.Scheme != "dic")
{
AaruConsole.ErrorWriteLine("Invalid remote URI.");
return new DeviceInfo[0];
}
using var remote = new Remote.Remote(aaruUri);
isRemote = true;
serverApplication = remote.ServerApplication;

View File

@@ -51,13 +51,17 @@ namespace Aaru.Devices.Remote
readonly string _host;
readonly Socket _socket;
public Remote(string host)
public Remote(Uri uri)
{
_host = host;
if(uri.Scheme != "aaru" &&
uri.Scheme != "dic")
throw new ArgumentException("Invalid remote protocol.", nameof(uri.Scheme));
if(!IPAddress.TryParse(host, out IPAddress ipAddress))
_host = uri.DnsSafeHost;
if(!IPAddress.TryParse(_host, out IPAddress ipAddress))
{
IPHostEntry ipHostEntry = Dns.GetHostEntry(host);
IPHostEntry ipHostEntry = Dns.GetHostEntry(_host);
ipAddress = ipHostEntry.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
}
@@ -69,12 +73,12 @@ namespace Aaru.Devices.Remote
throw new SocketException(11001);
}
var ipEndPoint = new IPEndPoint(ipAddress, 6666);
var ipEndPoint = new IPEndPoint(ipAddress, uri.Port > 0 ? uri.Port : 6666);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(ipEndPoint);
AaruConsole.WriteLine("Connected to {0}", host);
AaruConsole.WriteLine("Connected to {0}", uri.Host);
byte[] hdrBuf = new byte[Marshal.SizeOf<AaruPacketHeader>()];

View File

@@ -74,10 +74,7 @@ namespace Aaru.Commands
try
{
if(host.StartsWith("aaru://", StringComparison.CurrentCultureIgnoreCase))
host = host.Substring(7);
var remote = new Remote(host);
var remote = new Remote(new Uri(host));
Statistics.AddRemote(remote.ServerApplication, remote.ServerVersion, remote.ServerOperatingSystem,
remote.ServerOperatingSystemVersion, remote.ServerArchitecture);