static void Main(string[] args) { var argReader = new ArgReader(args); RecoverSerialPorts(argReader, out var primarySerialPort, out var secondarySerialPort); if (string.IsNullOrWhiteSpace(secondarySerialPort)) { ProcessInterceptor(primarySerialPort, secondarySerialPort); } else { ProcessSimulator(argReader, primarySerialPort); } }
static void ProcessSimulator(ArgReader argReader, string primarySerialPort) { var regexPatterns = RecoverRegexPatterns(argReader); var cryptoHandler = new ActiveCryptoHandler(); cryptoHandler.WorkingKeyDefined += OnWorkingKeyDefined; var virtualDevice = new SerialDevice(primarySerialPort); var simulatedDevice = new RegexSimulatedDevice(cryptoHandler, regexPatterns); simulatedDevice.RegexApplied += OnRegexApplied; simulatedDevice.CorruptCommand += OnCorruptCommand; simulatedDevice.AbortRequested += OnAbort; var interceptor = new Interceptor(virtualDevice, simulatedDevice); interceptor.Request += OnRequest; interceptor.Response += OnResponse; AppendLog("Started"); Thread.Sleep(Timeout.Infinite); }
static void RecoverSerialPorts(ArgReader argReader, out string primarySerialPort, out string secondarySerialPort) { switch (argReader.SerialPorts.Length) { case 0: Console.WriteLine("Which serial port to attach to?"); primarySerialPort = Console.ReadLine(); Console.WriteLine("Which serial port to connect to?"); secondarySerialPort = Console.ReadLine(); return; case 1: primarySerialPort = argReader.SerialPorts[0]; secondarySerialPort = String.Empty; return; default: primarySerialPort = argReader.SerialPorts[0]; secondarySerialPort = argReader.SerialPorts[1]; return; } }
static Dictionary <Regex, string> RecoverRegexPatterns(ArgReader argReader) { if (argReader.RegexPatterns.Count > 0) { return(argReader.RegexPatterns); } var regexPatterns = new Dictionary <Regex, string>(); while (true) { Console.WriteLine("Input the regex or blank to stop"); var regexString = Console.ReadLine(); if (string.IsNullOrWhiteSpace(regexString)) { return(regexPatterns); } var regex = new Regex(regexString); var pattern = Console.ReadLine(); regexPatterns[regex] = pattern; } }