Skip to content

Commit 91ed7c1

Browse files
committed
Merge branch 'release-0.5'
2 parents 54e8731 + d2c428d commit 91ed7c1

24 files changed

+691
-25372
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.uniovi.nmapgui</groupId>
77
<artifactId>nmapGUI</artifactId>
8-
<version>0.4.1-SNAPSHOT</version>
8+
<version>0.5-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<name>NMapGUI</name>

src/main/java/com/uniovi/nmapgui/NMapLoaderWindow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import org.springframework.context.ConfigurableApplicationContext;
2626

27-
import com.uniovi.nmapgui.executor.CommandExecutor;
27+
import com.uniovi.nmapgui.executor.CommandExecutorImpl;
2828
import com.uniovi.nmapgui.model.Command;
2929

3030

@@ -41,7 +41,7 @@ public class NMapLoaderWindow extends JFrame {
4141
private JButton stop;
4242
private ConfigurableApplicationContext springContext;
4343
private JButton go;
44-
private CommandExecutor executor = new CommandExecutor(new Command("-V"));
44+
private CommandExecutorImpl executor = new CommandExecutorImpl(new Command("-V"));
4545
private boolean nmapInstalled;
4646

4747

src/main/java/com/uniovi/nmapgui/WebController.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,82 @@
1818
import org.springframework.web.bind.annotation.ResponseBody;
1919

2020
import com.uniovi.nmapgui.executor.CommandExecutor;
21+
import com.uniovi.nmapgui.executor.CommandExecutorImpl;
22+
import com.uniovi.nmapgui.executor.CommandExecutorObserver;
2123
import com.uniovi.nmapgui.model.Command;
2224
import com.uniovi.nmapgui.util.Filefinder;
2325

2426
@Controller
25-
public class WebController {
26-
private List<Command> commands;
27+
public class WebController implements CommandExecutorObserver{
28+
private List<Command> ongoingCommands = new ArrayList<Command>();
29+
private List<Command> finishedCommands = new ArrayList<Command>();
2730
private Command command;
31+
private boolean finishedCommandQueued=false;
2832

2933

3034
@GetMapping("/nmap")
3135
public String command(Model model) {
36+
3237
command = new Command();
33-
commands= new ArrayList<Command>();
3438
model.addAttribute("command", command);
35-
model.addAttribute("commands", commands);
36-
37-
39+
model.addAttribute("commands", ongoingCommands);
40+
model.addAttribute("commands", finishedCommands);
41+
finishedCommandQueued=true;
3842
return "index";
3943
}
4044

4145
@GetMapping("/nmap-exe")
4246
public String command(Model model, @RequestParam String code) {
4347
command = new Command(code);
44-
commands.add(0,command);
45-
new CommandExecutor(command).execute();
48+
ongoingCommands.add(0,command);
49+
CommandExecutor executor = new CommandExecutorImpl(command);
50+
executor.addObserver(this);
51+
executor.execute();
4652
model.addAttribute("command", command);
47-
model.addAttribute("commands", commands);
53+
model.addAttribute("commands", ongoingCommands);
4854

49-
return "fragments/contents :: output";
55+
56+
return "fragments/contents :: ongoing";
57+
}
58+
59+
@GetMapping("/nmap/removeCommand")
60+
public String removeCommand(Model model, @RequestParam int index) {
61+
finishedCommands.remove(index);
62+
model.addAttribute("command", command);
63+
model.addAttribute("finishedCommands", finishedCommands);
64+
65+
66+
return "fragments/contents :: finished";
5067
}
5168

5269

5370
@GetMapping("/nmap/update")
54-
public String updateOut(Model model, @RequestParam boolean allowDel) {
71+
public String updateOut(Model model) {
5572

5673
model.addAttribute("command", command);
57-
model.addAttribute("commands", commands);
58-
boolean notFinished=false;
59-
for(Command cmd : commands)
60-
if(notFinished=!cmd.isFinished())
61-
break;
62-
if(!notFinished && allowDel)
63-
commands=new ArrayList<>();
64-
74+
model.addAttribute("commands", ongoingCommands);
6575

66-
return "fragments/contents :: output";
76+
return "fragments/contents :: ongoing";
6777
}
68-
78+
6979
@GetMapping("/nmap/update-finished")
80+
public String updateEnded(Model model) {
81+
82+
model.addAttribute("command", command);
83+
model.addAttribute("finishedCommands", finishedCommands);
84+
finishedCommandQueued=false;
85+
return "fragments/contents :: finished";
86+
}
87+
88+
@GetMapping("/nmap/finishedQueued")
7089
public @ResponseBody Boolean updateEnd() {
71-
for(Command cmd : commands)
72-
if(!cmd.isFinished())
73-
return false;
74-
return true;
90+
return finishedCommandQueued;
7591
}
92+
@GetMapping("/nmap/stopUpdating")
93+
public @ResponseBody Boolean stopUpdating() {
94+
return ongoingCommands.isEmpty();
95+
}
96+
7697
@GetMapping("/nmap/download/{filename}")
7798
public ResponseEntity<InputStreamResource> download(@PathVariable("filename") String filename) {
7899

@@ -86,36 +107,15 @@ public ResponseEntity<InputStreamResource> download(@PathVariable("filename") St
86107
.body(resource);
87108
} catch (FileNotFoundException e) {
88109
return ResponseEntity.notFound().build();
89-
}
90-
110+
}
91111

92112
}
93113

94-
// @GetMapping("/nmap/update-finished-list")
95-
// public @ResponseBody List<Integer> updateEndList() {
96-
// List<Integer> ids = new ArrayList<Integer>();
97-
// int index=0;
98-
// for(Command cmd : commands)
99-
// {
100-
// if(!cmd.isFinished())
101-
// ids.add(++index);
102-
// else if(!cmd.isChkUpdateFlag()){
103-
// cmd.setChkUpdateFlag(true);
104-
// ids.add(++index);
105-
// }
106-
// }
107-
// return ids;
108-
// }
109-
//
110-
// @GetMapping("/nmap/updateid")
111-
// public String updateOut(Model model, @RequestParam int id) {
112-
// model.addAttribute("command", command);
113-
// model.addAttribute("commands", commands);
114-
//
115-
//
116-
// return "index :: out"+id;
117-
// }
118-
//
119-
//
114+
115+
public void finishedCommand(Command cmd){
116+
ongoingCommands.remove(cmd);
117+
finishedCommands.add(0,cmd);
118+
finishedCommandQueued = true;
119+
}
120120

121121
}
Lines changed: 5 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,9 @@
11
package com.uniovi.nmapgui.executor;
22

3-
import java.io.*;
4-
import java.text.SimpleDateFormat;
5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
import java.util.Date;
8-
import java.util.List;
9-
import java.util.regex.Matcher;
10-
import java.util.regex.Pattern;
11-
import javax.xml.bind.JAXBContext;
12-
import javax.xml.bind.Unmarshaller;
13-
14-
import com.uniovi.nmapgui.model.*;
15-
import com.uniovi.nmapgui.util.TransInfoHtml;
16-
17-
public class CommandExecutor {
18-
private Command cmd;
19-
private String tempPath = System.getProperty("java.io.tmpdir")+"/";
20-
private Thread commandThread;
21-
22-
23-
public CommandExecutor(Command command) {
24-
this();
25-
cmd=command;
26-
}
27-
public CommandExecutor(){};
28-
29-
30-
public boolean execute(){
31-
String[] command = composeCommand();
32-
33-
try {
34-
Process p = Runtime.getRuntime().exec(command);
35-
final InputStream stream = p.getInputStream();
36-
final InputStream errors = p.getErrorStream();
37-
commandThread = new Thread(new Runnable() {
38-
public void run() {
39-
BufferedReader reader = null;
40-
BufferedReader errorReader = null;
41-
42-
try {
43-
boolean firstLine=true;
44-
reader = new BufferedReader(new InputStreamReader(stream));
45-
String line = null;
46-
while ((line = reader.readLine()) != null) {
47-
line=escape(line);
48-
if (line.contains( " open "))
49-
line="<span class=\"open\">"+line+"</span>";
50-
else if (line.contains( " closed "))
51-
line="<span class=\"closed\">"+line+"</span>";
52-
else if (line.contains( " filtered "))
53-
line="<span class=\"filtered\">"+line+"</span>";
54-
String jump = "\n";
55-
if(firstLine)
56-
jump="";
57-
cmd.getOutput().setText(cmd.getOutput().getText()+jump+line);
58-
firstLine=false;
59-
60-
}
61-
errorReader = new BufferedReader(new InputStreamReader(errors));
62-
while ((line = errorReader.readLine()) != null) {
63-
line=escape(line);
64-
line="<span class=\"closed\">"+line+"</span>";
65-
String jump = "\n";
66-
if(firstLine)
67-
jump="";
68-
cmd.getOutput().setText(cmd.getOutput().getText()+jump+"<i>"+line+"</i>");
69-
firstLine=false;
70-
71-
}
72-
73-
} catch (Exception e) {
74-
e.printStackTrace();
75-
} finally {
76-
readXML();
77-
cmd.setFinished(true);
78-
if (reader != null) {
79-
try {
80-
reader.close();
81-
} catch (IOException e) {
82-
cmd.setFinished(true);
83-
}
84-
}
85-
}
86-
}
87-
});
88-
commandThread.start();
89-
} catch (IOException e) {
90-
e.printStackTrace();
91-
}
92-
return true;
93-
}
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-
}
120-
121-
private String escape(String str) {
122-
String line=str;
123-
line = line.replace("&", "&amp;");
124-
line = line.replace( "\"", "&quot;");
125-
line = line.replace( "<", "&lt;");
126-
line = line.replace( ">", "&gt;");
127-
return line;
128-
}
129-
public Command getCmd() {
130-
return cmd;
131-
}
132-
133-
134-
public void setCmd(Command cmd) {
135-
this.cmd = cmd;
136-
}
137-
138-
139-
public Thread getCommandThread() {
140-
return commandThread;
141-
}
142-
143-
144-
public String getTempPath() {
145-
return tempPath;
146-
}
147-
148-
149-
public void setTempPath(String tempPath) {
150-
this.tempPath = tempPath;
151-
}
152-
153-
public void readXML() {
154-
StringBuilder sb = new StringBuilder();
155-
try (BufferedReader br = new BufferedReader(new FileReader(tempPath))){
156-
String sCurrentLine;
157-
while ((sCurrentLine = br.readLine()) != null) {
158-
sb.append(sCurrentLine);
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);
164-
cmd.getOutput().setXml(TransInfoHtml.transformToHtml(sb.toString()));
165-
cmd.getOutput().setScan(scan);
166-
167-
} catch (Exception e) {
168-
e.printStackTrace();
169-
}
170-
}
171-
3+
public interface CommandExecutor {
4+
boolean execute();
5+
public void addObserver(CommandExecutorObserver observer) ;
6+
public void removeObserver(CommandExecutorObserver observer);
7+
public void notifyEnd();
1728

1739
}

0 commit comments

Comments
 (0)