diff --git a/Aaru.Devices/Device/Constructor.cs b/Aaru.Devices/Device/Constructor.cs index 95aa489dc..2b854a4f3 100644 --- a/Aaru.Devices/Device/Constructor.cs +++ b/Aaru.Devices/Device/Constructor.cs @@ -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; } diff --git a/Aaru.Devices/Device/List.cs b/Aaru.Devices/Device/List.cs index 20de748d4..3f7cb45f7 100644 --- a/Aaru.Devices/Device/List.cs +++ b/Aaru.Devices/Device/List.cs @@ -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; diff --git a/Aaru.Devices/Remote/Remote.cs b/Aaru.Devices/Remote/Remote.cs index 2be9f39a4..dc8322cce 100644 --- a/Aaru.Devices/Remote/Remote.cs +++ b/Aaru.Devices/Remote/Remote.cs @@ -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()]; diff --git a/Aaru/Commands/Remote.cs b/Aaru/Commands/Remote.cs index caf20a70a..7fa942228 100644 --- a/Aaru/Commands/Remote.cs +++ b/Aaru/Commands/Remote.cs @@ -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);