66import java .util .Arrays ;
77import java .util .Date ;
88import java .util .List ;
9-
9+ import java .util .regex .Matcher ;
10+ import java .util .regex .Pattern ;
11+ import javax .xml .bind .JAXBContext ;
12+ import javax .xml .bind .Unmarshaller ;
1013
1114import com .uniovi .nmapgui .model .*;
1215import com .uniovi .nmapgui .util .TransInfoHtml ;
@@ -18,23 +21,17 @@ public class CommandExecutor {
1821
1922
2023 public CommandExecutor (Command command ) {
24+ this ();
2125 cmd =command ;
2226 }
2327 public CommandExecutor (){};
2428
2529
2630 public boolean execute (){
27- String filename = "nmap-scan_" + new SimpleDateFormat ("yyyy-MM-dd_HH-mm-ss" )
28- .format (new Date ())+ ".xml" ;
29-
30- this .cmd .getOutput ().setFilename (filename );
31- tempPath =tempPath + filename ;
32- List <String > commandsList = new ArrayList <String >();
33- commandsList .add ("nmap" );
34- commandsList .addAll (Arrays .asList (cmd .getText ().split (" " )));
35- commandsList .addAll (Arrays .asList (new String []{"-oX" , getTempPath (), "--webxml" }));
31+ String [] command = composeCommand ();
32+
3633 try {
37- Process p = Runtime .getRuntime ().exec (commandsList . toArray ( new String []{}) );
34+ Process p = Runtime .getRuntime ().exec (command );
3835 final InputStream stream = p .getInputStream ();
3936 final InputStream errors = p .getErrorStream ();
4037 commandThread = new Thread (new Runnable () {
@@ -43,9 +40,9 @@ public void run() {
4340 BufferedReader errorReader = null ;
4441
4542 try {
43+ boolean firstLine =true ;
4644 reader = new BufferedReader (new InputStreamReader (stream ));
4745 String line = null ;
48- cmd .getOutput ().setText ("<pre></pre>" );
4946 while ((line = reader .readLine ()) != null ) {
5047 line =escape (line );
5148 if (line .contains ( " open " ))
@@ -54,13 +51,23 @@ else if (line.contains( " closed "))
5451 line ="<span class=\" closed\" >" +line +"</span>" ;
5552 else if (line .contains ( " filtered " ))
5653 line ="<span class=\" filtered\" >" +line +"</span>" ;
57- cmd .getOutput ().setText (cmd .getOutput ().getText ().replaceAll ("</pre>" , "\n " )+line +"</pre>" );
54+ String jump = "\n " ;
55+ if (firstLine )
56+ jump ="" ;
57+ cmd .getOutput ().setText (cmd .getOutput ().getText ()+jump +line );
58+ firstLine =false ;
59+
5860 }
5961 errorReader = new BufferedReader (new InputStreamReader (errors ));
6062 while ((line = errorReader .readLine ()) != null ) {
6163 line =escape (line );
6264 line ="<span class=\" closed\" >" +line +"</span>" ;
63- cmd .getOutput ().setText (cmd .getOutput ().getText ().replaceAll ("</pre>" , "\n " )+"<i>" +line +"</i></pre>" );
65+ String jump = "\n " ;
66+ if (firstLine )
67+ jump ="" ;
68+ cmd .getOutput ().setText (cmd .getOutput ().getText ()+jump +"<i>" +line +"</i>" );
69+ firstLine =false ;
70+
6471 }
6572
6673 } catch (Exception e ) {
@@ -84,6 +91,32 @@ else if (line.contains( " filtered "))
8491 }
8592 return true ;
8693 }
94+
95+ private String [] composeCommand () {
96+
97+ String filename = "nmap-scan_" + new SimpleDateFormat ("yyyy-MM-dd_HH-mm-ss" )
98+ .format (new Date ())+ ".xml" ;
99+
100+ this .cmd .getOutput ().setFilename (filename );
101+ tempPath =tempPath + filename ;
102+ List <String > commandList = new ArrayList <String >();
103+ commandList .add ("nmap" );
104+ commandList .addAll (splitOptions ());
105+ commandList .addAll (Arrays .asList (new String []{"-oX" , getTempPath (), "--webxml" }));
106+
107+ return commandList .toArray (new String []{});
108+
109+ }
110+
111+ private List <String > splitOptions (){
112+ List <String > options = new ArrayList <>();
113+ //Splits string by spaces other than the ones in substring quotes
114+ Matcher matcher = Pattern .compile ("\\ s*([^(\" |\' )]\\ S*|\" .+?\" |\' .+?\' )\\ s*" ).matcher (cmd .getText ());
115+ while (matcher .find ())
116+ options .add (matcher .group (1 ));
117+
118+ return options ;
119+ }
87120
88121 private String escape (String str ) {
89122 String line =str ;
@@ -123,8 +156,13 @@ public void readXML() {
123156 String sCurrentLine ;
124157 while ((sCurrentLine = br .readLine ()) != null ) {
125158 sb .append (sCurrentLine );
126- }
159+ }
160+ JAXBContext jaxbContext = JAXBContext .newInstance (Scan .class );
161+ Unmarshaller unmarshaller = jaxbContext .createUnmarshaller ();
162+ StringReader reader = new StringReader (sb .toString ());
163+ Scan scan = (Scan ) unmarshaller .unmarshal (reader );
127164 cmd .getOutput ().setXml (TransInfoHtml .transformToHtml (sb .toString ()));
165+ cmd .getOutput ().setScan (scan );
128166
129167 } catch (Exception e ) {
130168 e .printStackTrace ();
0 commit comments