Added base for remote connection and hello packet.

This commit is contained in:
2019-10-12 19:26:28 +01:00
parent 097f7b796a
commit 7f5a775941
8 changed files with 223 additions and 29 deletions

View File

@@ -42,6 +42,7 @@
<e p="MediaInfo.cs" t="Include" /> <e p="MediaInfo.cs" t="Include" />
<e p="MediaScan.cs" t="Include" /> <e p="MediaScan.cs" t="Include" />
<e p="PrintHex.cs" t="Include" /> <e p="PrintHex.cs" t="Include" />
<e p="Remote.cs" t="Include" />
<e p="Statistics.cs" t="Include" /> <e p="Statistics.cs" t="Include" />
<e p="Update.cs" t="Include" /> <e p="Update.cs" t="Include" />
<e p="Verify.cs" t="Include" /> <e p="Verify.cs" t="Include" />
@@ -591,6 +592,11 @@
<e p="ListDevices.cs" t="Include" /> <e p="ListDevices.cs" t="Include" />
<e p="Structs.cs" t="Include" /> <e p="Structs.cs" t="Include" />
</e> </e>
<e p="Remote" t="Include">
<e p="Consts.cs" t="Include" />
<e p="Enums.cs" t="Include" />
<e p="Structs.cs" t="Include" />
</e>
<e p="Windows" t="Include"> <e p="Windows" t="Include">
<e p="Command.cs" t="Include" /> <e p="Command.cs" t="Include" />
<e p="Enums.cs" t="Include" /> <e p="Enums.cs" t="Include" />

View File

@@ -53,6 +53,9 @@
<Compile Include="Linux\Structs.cs" /> <Compile Include="Linux\Structs.cs" />
<Compile Include="Linux\Enums.cs" /> <Compile Include="Linux\Enums.cs" />
<Compile Include="Enums.cs" /> <Compile Include="Enums.cs" />
<Compile Include="Remote\Consts.cs" />
<Compile Include="Remote\Enums.cs" />
<Compile Include="Remote\Structs.cs" />
<Compile Include="Windows\Extern.cs" /> <Compile Include="Windows\Extern.cs" />
<Compile Include="Windows\ListDevices.cs" /> <Compile Include="Windows\ListDevices.cs" />
<Compile Include="Windows\Structs.cs" /> <Compile Include="Windows\Structs.cs" />

View File

@@ -0,0 +1,9 @@
namespace DiscImageChef.Devices.Remote
{
public class Consts
{
public const string PacketId = "DICPACKT";
public const int PacketVersion = 1;
public const int MaxProtocol = 1;
}
}

View File

@@ -0,0 +1,7 @@
namespace DiscImageChef.Devices.Remote
{
public enum DicPacketType : byte
{
Hello = 1
}
}

View File

@@ -0,0 +1,44 @@
using System.Runtime.InteropServices;
namespace DiscImageChef.Devices.Remote
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DicPacketHeader
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string id;
public uint len;
public byte version;
public DicPacketType packetType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] spare;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DicPacketHello
{
public DicPacketHeader hdr;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string application;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string version;
public byte maxProtocol;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] spare;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string sysname;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string release;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string machine;
}
}

View File

@@ -0,0 +1,123 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Remote.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Verbs.
//
// --[ Description ] ----------------------------------------------------------
//
// Implements the 'remote' verb.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console;
using Mono.Options;
namespace DiscImageChef.Commands
{
internal class RemoteCommand : Command
{
private string host;
private bool showHelp;
public RemoteCommand() : base("remote", "Tests connection to a DiscImageChef Remote Server.")
{
Options = new OptionSet
{
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
$"{MainClass.AssemblyCopyright}",
"",
$"usage: DiscImageChef {Name} [OPTIONS] host",
"",
Help,
{"help|h|?", "Show this message and exit.", v => showHelp = v != null}
};
}
public override int Invoke(IEnumerable<string> arguments)
{
var extra = Options.Parse(arguments);
if (showHelp)
{
Options.WriteOptionDescriptions(CommandSet.Out);
return (int) ErrorNumber.HelpRequested;
}
MainClass.PrintCopyright();
if (MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
if (MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
// Statistics.AddCommand("remote");
if (extra.Count > 1)
{
DicConsole.ErrorWriteLine("Too many arguments.");
return (int) ErrorNumber.UnexpectedArgumentCount;
}
if (extra.Count == 0)
{
DicConsole.ErrorWriteLine("Missing input image.");
return (int) ErrorNumber.MissingArgument;
}
host = extra[0];
DicConsole.DebugWriteLine("Remote command", "--debug={0}", MainClass.Debug);
DicConsole.DebugWriteLine("Remote command", "--host={0}", host);
DicConsole.DebugWriteLine("Remote command", "--verbose={0}", MainClass.Verbose);
var ipHostEntry = Dns.GetHostEntry(host);
var ipAddress = ipHostEntry.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
if (ipAddress is null)
{
DicConsole.ErrorWriteLine("Host not found");
return (int) Errno.ENODEV;
}
var ipEndPoint = new IPEndPoint(ipAddress, 6666);
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(ipEndPoint);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (Exception e)
{
DicConsole.Write(e.Message);
}
return (int) ErrorNumber.NoError;
}
}
}

View File

@@ -57,6 +57,7 @@
<Compile Include="Commands\ImageInfo.cs" /> <Compile Include="Commands\ImageInfo.cs" />
<Compile Include="Commands\ListNamespaces.cs" /> <Compile Include="Commands\ListNamespaces.cs" />
<Compile Include="Commands\ListOptions.cs" /> <Compile Include="Commands\ListOptions.cs" />
<Compile Include="Commands\Remote.cs" />
<Compile Include="Commands\Update.cs" /> <Compile Include="Commands\Update.cs" />
<Compile Include="Main.cs" /> <Compile Include="Main.cs" />
<Compile Include="Commands\Formats.cs" /> <Compile Include="Commands\Formats.cs" />

View File

@@ -47,58 +47,58 @@ using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID;
namespace DiscImageChef namespace DiscImageChef
{ {
class MainClass internal class MainClass
{ {
internal static bool Verbose; internal static bool Verbose;
internal static bool Debug; internal static bool Debug;
internal static string AssemblyCopyright; internal static string AssemblyCopyright;
internal static string AssemblyTitle; internal static string AssemblyTitle;
internal static AssemblyInformationalVersionAttribute AssemblyVersion; internal static AssemblyInformationalVersionAttribute AssemblyVersion;
[STAThread] [STAThread]
public static int Main(string[] args) public static int Main(string[] args)
{ {
object[] attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); var attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
AssemblyTitle = ((AssemblyTitleAttribute)attributes[0]).Title; AssemblyTitle = ((AssemblyTitleAttribute) attributes[0]).Title;
attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
AssemblyVersion = AssemblyVersion =
Attribute.GetCustomAttribute(typeof(MainClass).Assembly, typeof(AssemblyInformationalVersionAttribute)) Attribute.GetCustomAttribute(typeof(MainClass).Assembly, typeof(AssemblyInformationalVersionAttribute))
as AssemblyInformationalVersionAttribute; as AssemblyInformationalVersionAttribute;
AssemblyCopyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright; AssemblyCopyright = ((AssemblyCopyrightAttribute) attributes[0]).Copyright;
DicConsole.WriteLineEvent += System.Console.WriteLine; DicConsole.WriteLineEvent += System.Console.WriteLine;
DicConsole.WriteEvent += System.Console.Write; DicConsole.WriteEvent += System.Console.Write;
DicConsole.ErrorWriteLineEvent += System.Console.Error.WriteLine; DicConsole.ErrorWriteLineEvent += System.Console.Error.WriteLine;
Settings.Settings.LoadSettings(); Settings.Settings.LoadSettings();
DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); var ctx = DicContext.Create(Settings.Settings.LocalDbPath);
ctx.Database.Migrate(); ctx.Database.Migrate();
ctx.SaveChanges(); ctx.SaveChanges();
bool masterDbUpdate = false; var masterDbUpdate = false;
if(!File.Exists(Settings.Settings.MasterDbPath)) if (!File.Exists(Settings.Settings.MasterDbPath))
{ {
masterDbUpdate = true; masterDbUpdate = true;
UpdateCommand.DoUpdate(true); UpdateCommand.DoUpdate(true);
} }
DicContext mctx = DicContext.Create(Settings.Settings.MasterDbPath); var mctx = DicContext.Create(Settings.Settings.MasterDbPath);
mctx.Database.Migrate(); mctx.Database.Migrate();
mctx.SaveChanges(); mctx.SaveChanges();
if((args.Length < 1 || args[0].ToLowerInvariant() != "gui") && if ((args.Length < 1 || args[0].ToLowerInvariant() != "gui") &&
Settings.Settings.Current.GdprCompliance < DicSettings.GdprLevel) Settings.Settings.Current.GdprCompliance < DicSettings.GdprLevel)
new ConfigureCommand(true, true).Invoke(args); new ConfigureCommand(true, true).Invoke(args);
Statistics.LoadStats(); Statistics.LoadStats();
if(Settings.Settings.Current.Stats != null && Settings.Settings.Current.Stats.ShareStats) if (Settings.Settings.Current.Stats != null && Settings.Settings.Current.Stats.ShareStats)
Task.Run(() => { Statistics.SubmitStats(); }); Task.Run(() => { Statistics.SubmitStats(); });
PlatformID currentPlatform = DetectOS.GetRealPlatformID(); var currentPlatform = DetectOS.GetRealPlatformID();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
CommandSet commands = new CommandSet("DiscImageChef") var commands = new CommandSet("DiscImageChef")
{ {
$"{AssemblyTitle} {AssemblyVersion?.InformationalVersion}", $"{AssemblyTitle} {AssemblyVersion?.InformationalVersion}",
$"{AssemblyCopyright}", $"{AssemblyCopyright}",
@@ -106,7 +106,7 @@ namespace DiscImageChef
"usage: DiscImageChef COMMAND [OPTIONS]", "usage: DiscImageChef COMMAND [OPTIONS]",
"", "",
"Global options:", "Global options:",
{"verbose|v", "Shows verbose output.", b => Verbose = b != null}, {"verbose|v", "Shows verbose output.", b => Verbose = b != null},
{"debug|d", "Shows debug output from plugins.", b => Debug = b != null}, {"debug|d", "Shows debug output from plugins.", b => Debug = b != null},
"", "",
"Available commands:", "Available commands:",
@@ -120,8 +120,8 @@ namespace DiscImageChef
new DecodeCommand() new DecodeCommand()
}; };
if(currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux || if (currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux ||
currentPlatform == PlatformID.Win32NT) currentPlatform == PlatformID.Win32NT)
{ {
commands.Add(new DeviceInfoCommand()); commands.Add(new DeviceInfoCommand());
commands.Add(new DeviceReportCommand()); commands.Add(new DeviceReportCommand());
@@ -133,16 +133,16 @@ namespace DiscImageChef
commands.Add(new FormatsCommand()); commands.Add(new FormatsCommand());
commands.Add(new ImageInfoCommand()); commands.Add(new ImageInfoCommand());
if(currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux || if (currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux ||
currentPlatform == PlatformID.Win32NT) commands.Add(new ListDevicesCommand()); currentPlatform == PlatformID.Win32NT) commands.Add(new ListDevicesCommand());
commands.Add(new ListEncodingsCommand()); commands.Add(new ListEncodingsCommand());
commands.Add(new ListNamespacesCommand()); commands.Add(new ListNamespacesCommand());
commands.Add(new ListOptionsCommand()); commands.Add(new ListOptionsCommand());
commands.Add(new LsCommand()); commands.Add(new LsCommand());
if(currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux || if (currentPlatform == PlatformID.FreeBSD || currentPlatform == PlatformID.Linux ||
currentPlatform == PlatformID.Win32NT) currentPlatform == PlatformID.Win32NT)
{ {
commands.Add(new MediaInfoCommand()); commands.Add(new MediaInfoCommand());
commands.Add(new MediaScanCommand()); commands.Add(new MediaScanCommand());
@@ -152,8 +152,9 @@ namespace DiscImageChef
commands.Add(new StatisticsCommand()); commands.Add(new StatisticsCommand());
commands.Add(new UpdateCommand(masterDbUpdate)); commands.Add(new UpdateCommand(masterDbUpdate));
commands.Add(new VerifyCommand()); commands.Add(new VerifyCommand());
commands.Add(new RemoteCommand());
int ret = commands.Run(args); var ret = commands.Run(args);
Statistics.SaveStats(); Statistics.SaveStats();
@@ -163,7 +164,7 @@ namespace DiscImageChef
internal static void PrintCopyright() internal static void PrintCopyright()
{ {
DicConsole.WriteLine("{0} {1}", AssemblyTitle, AssemblyVersion?.InformationalVersion); DicConsole.WriteLine("{0} {1}", AssemblyTitle, AssemblyVersion?.InformationalVersion);
DicConsole.WriteLine("{0}", AssemblyCopyright); DicConsole.WriteLine("{0}", AssemblyCopyright);
DicConsole.WriteLine(); DicConsole.WriteLine();
} }
} }