static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: Har2Exd <HAR file path> <EXD file path>"); Console.WriteLine("Exit codes: 1 - No args, 2 - Incorrect har path, 3 - Parsing error, 4 - Export error."); Environment.ExitCode = 1; } else { string harFilePath = args[0]; string exdFilePath = args[1]; if (!File.Exists(harFilePath)) { Console.WriteLine("Could not find har file: '{0}'", harFilePath); Environment.ExitCode = 2; } else { TrafficViewerFile tvf = new TrafficViewerFile(); try { Console.WriteLine("Importing from '{0}'...", harFilePath); ITrafficParser harParser = new HarParser(); harParser.Parse(harFilePath, tvf, ParsingOptions.GetDefaultProfile()); } catch (Exception ex) { Console.WriteLine("Parsing exception: '{0}'", ex.Message); Environment.ExitCode = 3; } //now export try { Console.WriteLine("Exporting to '{0}'...", exdFilePath); var exporter = new ManualExploreExporter(); exporter.Export(tvf, new FileStream(exdFilePath, FileMode.Create, FileAccess.ReadWrite)); } catch (Exception ex) { Console.WriteLine("Export exception: '{0}'", ex.Message); Environment.ExitCode = 4; } tvf.Close(false); Console.WriteLine("Done."); } } }
public void ExportAppscanToEXD() { ParsingOptions options = ParsingOptions.GetLegacyAppScanProfile(); ITrafficParser parser = new DefaultTrafficParser(); //test appscan import TrafficViewerFile tvFile = new TrafficViewerFile(); TempFile log = new TempFile(); log.Write(Properties.Resources.AppScanMETraffic); tvFile.StartImport(parser, log.Path, options); Assert.AreEqual(8, tvFile.RequestCount); ITrafficExporter exdExporter = new ManualExploreExporter(); TempFile temp = new TempFile(); Stream stream = temp.OpenStream(); exdExporter.Export(tvFile, stream, "newHost.com", 8080); Assert.IsTrue(stream.Length > 0); stream.Flush(); stream.Position = 0; XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; doc.Load(stream); int noOfRequests = doc.SelectNodes("//request").Count; Assert.AreEqual(8, noOfRequests); //check that the post request is properly formed XmlNode postRequest = doc.SelectSingleNode("//request[@method='POST']"); Assert.AreEqual(3, postRequest.SelectNodes("parameter").Count); Assert.AreEqual(2, postRequest.SelectNodes("cookie").Count); Assert.AreEqual(11, postRequest.SelectNodes("header").Count); stream.Close(); }
public void TestManualExploreImportExport() { //validate against existing TVF TrafficViewerFile compareTVF = GetCompareTVF(Resources.demoExploreFromTrafficImport); //export the tvf to exd ITrafficExporter exporter = new ManualExploreExporter(); TempFile temp = new TempFile(".exd"); Stream tempStream = temp.OpenStream(); exporter.Export(compareTVF, tempStream); tempStream.Close(); TrafficViewerFile importTVF = new TrafficViewerFile(); ITrafficParser parser = new ConfigurationParser(); parser.Parse(temp.Path, importTVF, new ParsingOptions()); ValidateTrafficSourcesRequestsAreSame(compareTVF, importTVF, false); }
/// <summary> /// Stops a manual explore proxy on the specified port and then save the resulting traffic file if specified /// </summary> /// <param name="port">The corresponding proxy port</param> /// <param name="fileName">Optional - The file name to use. If not provided no file will get created.</param> public void StopManualExploreProxy(int port, string fileName = null) { if (!_manualExploreProxies.ContainsKey(port)) { _logWriter.Log(TraceLevel.Error, "Can't stop proxy on port {0}. Not found.", port); throw new HttpProxyException(HttpStatusCode.NotFound, "Proxy not found on the specified port", ServiceCode.CommandProxyStopCannotFindPort); } bool saveFile = !String.IsNullOrWhiteSpace(fileName); string absolutePath = null; if (saveFile) //we need to save a file name after stopping, perform validations on the file name { if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || fileName.IndexOfAny(Path.GetInvalidPathChars()) >= 0) { //invalid file name _logWriter.Log(TraceLevel.Error, "Invalid characters in file name: {0}", fileName); throw new HttpProxyException(HttpStatusCode.BadRequest, "Invalid file name", ServiceCode.CommandProxyStopInvalidFileName); } //now generate full path and check it string fullPath = Path.Combine(_filesDirectory, String.Format("{0}.{1}", fileName, _recordingType)); absolutePath = Path.GetFullPath(fullPath); if (!fullPath.StartsWith(_filesDirectory)) { //path traversal attempt _logWriter.Log(TraceLevel.Error, "Directory traversal attempted with file name: {0}", fileName); throw new HttpProxyException(HttpStatusCode.BadRequest, "Directory traversal", ServiceCode.ProxyInternalError); } //check if the file already exists if (File.Exists(fullPath)) { _logWriter.Log(TraceLevel.Error, "File {0} already exists.", fileName); throw new HttpProxyException(HttpStatusCode.Forbidden, "File already exists", ServiceCode.CommandProxyStopFileExists); } } ManualExploreProxy proxy = _manualExploreProxies[port]; try { proxy.Stop(); //remove the proxy from the list of existing proxies _manualExploreProxies.Remove(port); } catch (Exception ex) { _logWriter.Log(TraceLevel.Error, "Internal error trying to stop a proxy: {0}", ex); throw new HttpProxyException(HttpStatusCode.InternalServerError, "Internal error trying to stop a proxy", ServiceCode.ProxyInternalError); } if (!saveFile) { _logWriter.Log(TraceLevel.Verbose, "No file name was specified. Discarding the data for proxy started on port {0}", port); //we are done here return; } //file path is fine try { TrafficViewerFile htd = (TrafficViewerFile)proxy.TrafficDataStore; if (_recordingType.Equals(Constants.HTD_STRING)) { htd.Save(absolutePath); } else if (_recordingType.Equals(Constants.EXD_STRING)) { ManualExploreExporter exporter = new ManualExploreExporter(); Stream exportStream = new FileStream(absolutePath, FileMode.CreateNew); exporter.Export(htd, exportStream); exportStream.Close(); } } catch (Exception ex) { _logWriter.Log(TraceLevel.Error, "An exception occured saving traffic file: {0}", ex); throw new HttpProxyException(HttpStatusCode.InternalServerError, "Cannot save file", ServiceCode.ProxyInternalError); } }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: Traffic2Exd <traffic file path> <EXD file path>"); Console.WriteLine("Supported import formats: .har, .txt, .htd"); Console.WriteLine("If the EXD file already exists the tool will append to it."); Console.WriteLine("Exit codes: 1 - No args, 2 - Incorrect file path, 3 - Parsing error, 4 - Export error, 5 - Unsupported Exception."); Environment.ExitCode = 1; } else { string trafficFilePath = args[0]; string exdFilePath = args[1]; if (!File.Exists(trafficFilePath)) { Console.WriteLine("Could not find har file: '{0}'", trafficFilePath); Environment.ExitCode = 2; } else { TrafficViewerFile tvf = new TrafficViewerFile(); try { if (File.Exists(exdFilePath)) { Console.WriteLine("EXD file {0} already exists. Appending to it.", exdFilePath); ConfigurationParser exdParser = new ConfigurationParser(); exdParser.Parse(exdFilePath, tvf, ParsingOptions.GetDefaultProfile()); } Console.WriteLine("Importing from '{0}'...", trafficFilePath); ITrafficParser parser = null; if (trafficFilePath.ToLower().EndsWith(".har")) { parser = new HarParser(); } else if (trafficFilePath.ToLower().EndsWith(".txt")) { parser = new DefaultTrafficParser(); } else if (trafficFilePath.ToLower().EndsWith(".htd")) { TrafficViewerFile tvf2 = new TrafficViewerFile(); tvf2.Open(trafficFilePath); int id = -1; TVRequestInfo info = null; while ((info = tvf2.GetNext(ref id)) != null) { tvf.AddRequestResponse(tvf2.LoadRequestData(info.Id), tvf2.LoadResponseData(info.Id)); } } else { Console.WriteLine("File extension is unsupported. Supported extensions/formats: .har, .txt, .htd"); Environment.ExitCode = 5; } if (parser != null) { parser.Parse(trafficFilePath, tvf, ParsingOptions.GetRawProfile()); } } catch (Exception ex) { Console.WriteLine("Parsing exception: '{0}'", ex.Message); Environment.ExitCode = 3; } //now export try { Console.WriteLine("Exporting to '{0}'...", exdFilePath); var exporter = new ManualExploreExporter(); exporter.Export(tvf, new FileStream(exdFilePath, FileMode.Create, FileAccess.ReadWrite)); } catch (Exception ex) { Console.WriteLine("Export exception: '{0}'", ex.Message); Environment.ExitCode = 4; } tvf.Close(false); Console.WriteLine("Done."); } } }