Skip to content

Commit

Permalink
Merge branch 'release-0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cues committed Sep 29, 2017
2 parents 54e8731 + d2c428d commit 91ed7c1
Show file tree
Hide file tree
Showing 24 changed files with 691 additions and 25,372 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.uniovi.nmapgui</groupId>
<artifactId>nmapGUI</artifactId>
<version>0.4.1-SNAPSHOT</version>
<version>0.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>NMapGUI</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/uniovi/nmapgui/NMapLoaderWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import org.springframework.context.ConfigurableApplicationContext;

import com.uniovi.nmapgui.executor.CommandExecutor;
import com.uniovi.nmapgui.executor.CommandExecutorImpl;
import com.uniovi.nmapgui.model.Command;


Expand All @@ -41,7 +41,7 @@ public class NMapLoaderWindow extends JFrame {
private JButton stop;
private ConfigurableApplicationContext springContext;
private JButton go;
private CommandExecutor executor = new CommandExecutor(new Command("-V"));
private CommandExecutorImpl executor = new CommandExecutorImpl(new Command("-V"));
private boolean nmapInstalled;


Expand Down
106 changes: 53 additions & 53 deletions src/main/java/com/uniovi/nmapgui/WebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,82 @@
import org.springframework.web.bind.annotation.ResponseBody;

import com.uniovi.nmapgui.executor.CommandExecutor;
import com.uniovi.nmapgui.executor.CommandExecutorImpl;
import com.uniovi.nmapgui.executor.CommandExecutorObserver;
import com.uniovi.nmapgui.model.Command;
import com.uniovi.nmapgui.util.Filefinder;

@Controller
public class WebController {
private List<Command> commands;
public class WebController implements CommandExecutorObserver{
private List<Command> ongoingCommands = new ArrayList<Command>();
private List<Command> finishedCommands = new ArrayList<Command>();
private Command command;
private boolean finishedCommandQueued=false;


@GetMapping("/nmap")
public String command(Model model) {

command = new Command();
commands= new ArrayList<Command>();
model.addAttribute("command", command);
model.addAttribute("commands", commands);


model.addAttribute("commands", ongoingCommands);
model.addAttribute("commands", finishedCommands);
finishedCommandQueued=true;
return "index";
}

@GetMapping("/nmap-exe")
public String command(Model model, @RequestParam String code) {
command = new Command(code);
commands.add(0,command);
new CommandExecutor(command).execute();
ongoingCommands.add(0,command);
CommandExecutor executor = new CommandExecutorImpl(command);
executor.addObserver(this);
executor.execute();
model.addAttribute("command", command);
model.addAttribute("commands", commands);
model.addAttribute("commands", ongoingCommands);

return "fragments/contents :: output";

return "fragments/contents :: ongoing";
}

@GetMapping("/nmap/removeCommand")
public String removeCommand(Model model, @RequestParam int index) {
finishedCommands.remove(index);
model.addAttribute("command", command);
model.addAttribute("finishedCommands", finishedCommands);


return "fragments/contents :: finished";
}


@GetMapping("/nmap/update")
public String updateOut(Model model, @RequestParam boolean allowDel) {
public String updateOut(Model model) {

model.addAttribute("command", command);
model.addAttribute("commands", commands);
boolean notFinished=false;
for(Command cmd : commands)
if(notFinished=!cmd.isFinished())
break;
if(!notFinished && allowDel)
commands=new ArrayList<>();

model.addAttribute("commands", ongoingCommands);

return "fragments/contents :: output";
return "fragments/contents :: ongoing";
}

@GetMapping("/nmap/update-finished")
public String updateEnded(Model model) {

model.addAttribute("command", command);
model.addAttribute("finishedCommands", finishedCommands);
finishedCommandQueued=false;
return "fragments/contents :: finished";
}

@GetMapping("/nmap/finishedQueued")
public @ResponseBody Boolean updateEnd() {
for(Command cmd : commands)
if(!cmd.isFinished())
return false;
return true;
return finishedCommandQueued;
}
@GetMapping("/nmap/stopUpdating")
public @ResponseBody Boolean stopUpdating() {
return ongoingCommands.isEmpty();
}

@GetMapping("/nmap/download/{filename}")
public ResponseEntity<InputStreamResource> download(@PathVariable("filename") String filename) {

Expand All @@ -86,36 +107,15 @@ public ResponseEntity<InputStreamResource> download(@PathVariable("filename") St
.body(resource);
} catch (FileNotFoundException e) {
return ResponseEntity.notFound().build();
}

}

}

// @GetMapping("/nmap/update-finished-list")
// public @ResponseBody List<Integer> updateEndList() {
// List<Integer> ids = new ArrayList<Integer>();
// int index=0;
// for(Command cmd : commands)
// {
// if(!cmd.isFinished())
// ids.add(++index);
// else if(!cmd.isChkUpdateFlag()){
// cmd.setChkUpdateFlag(true);
// ids.add(++index);
// }
// }
// return ids;
// }
//
// @GetMapping("/nmap/updateid")
// public String updateOut(Model model, @RequestParam int id) {
// model.addAttribute("command", command);
// model.addAttribute("commands", commands);
//
//
// return "index :: out"+id;
// }
//
//

public void finishedCommand(Command cmd){
ongoingCommands.remove(cmd);
finishedCommands.add(0,cmd);
finishedCommandQueued = true;
}

}
174 changes: 5 additions & 169 deletions src/main/java/com/uniovi/nmapgui/executor/CommandExecutor.java
Original file line number Diff line number Diff line change
@@ -1,173 +1,9 @@
package com.uniovi.nmapgui.executor;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.uniovi.nmapgui.model.*;
import com.uniovi.nmapgui.util.TransInfoHtml;

public class CommandExecutor {
private Command cmd;
private String tempPath = System.getProperty("java.io.tmpdir")+"/";
private Thread commandThread;


public CommandExecutor(Command command) {
this();
cmd=command;
}
public CommandExecutor(){};


public boolean execute(){
String[] command = composeCommand();

try {
Process p = Runtime.getRuntime().exec(command);
final InputStream stream = p.getInputStream();
final InputStream errors = p.getErrorStream();
commandThread = new Thread(new Runnable() {
public void run() {
BufferedReader reader = null;
BufferedReader errorReader = null;

try {
boolean firstLine=true;
reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
while ((line = reader.readLine()) != null) {
line=escape(line);
if (line.contains( " open "))
line="<span class=\"open\">"+line+"</span>";
else if (line.contains( " closed "))
line="<span class=\"closed\">"+line+"</span>";
else if (line.contains( " filtered "))
line="<span class=\"filtered\">"+line+"</span>";
String jump = "\n";
if(firstLine)
jump="";
cmd.getOutput().setText(cmd.getOutput().getText()+jump+line);
firstLine=false;

}
errorReader = new BufferedReader(new InputStreamReader(errors));
while ((line = errorReader.readLine()) != null) {
line=escape(line);
line="<span class=\"closed\">"+line+"</span>";
String jump = "\n";
if(firstLine)
jump="";
cmd.getOutput().setText(cmd.getOutput().getText()+jump+"<i>"+line+"</i>");
firstLine=false;

}

} catch (Exception e) {
e.printStackTrace();
} finally {
readXML();
cmd.setFinished(true);
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
cmd.setFinished(true);
}
}
}
}
});
commandThread.start();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

private String[] composeCommand() {

String filename= "nmap-scan_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
.format(new Date())+ ".xml";

this.cmd.getOutput().setFilename(filename);
tempPath=tempPath + filename;
List<String> commandList = new ArrayList<String>();
commandList.add("nmap");
commandList.addAll(splitOptions());
commandList.addAll(Arrays.asList(new String[]{"-oX" , getTempPath(), "--webxml"}));

return commandList.toArray(new String[]{});

}

private List<String> splitOptions(){
List<String> options = new ArrayList<>();
//Splits string by spaces other than the ones in substring quotes
Matcher matcher = Pattern.compile("\\s*([^(\"|\')]\\S*|\".+?\"|\'.+?\')\\s*").matcher(cmd.getText());
while (matcher.find())
options.add(matcher.group(1));

return options;
}

private String escape(String str) {
String line=str;
line = line.replace("&", "&amp;");
line = line.replace( "\"", "&quot;");
line = line.replace( "<", "&lt;");
line = line.replace( ">", "&gt;");
return line;
}
public Command getCmd() {
return cmd;
}


public void setCmd(Command cmd) {
this.cmd = cmd;
}


public Thread getCommandThread() {
return commandThread;
}


public String getTempPath() {
return tempPath;
}


public void setTempPath(String tempPath) {
this.tempPath = tempPath;
}

public void readXML() {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(tempPath))){
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
sb.append(sCurrentLine);
}
JAXBContext jaxbContext = JAXBContext.newInstance(Scan.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(sb.toString());
Scan scan = (Scan) unmarshaller.unmarshal(reader);
cmd.getOutput().setXml(TransInfoHtml.transformToHtml(sb.toString()));
cmd.getOutput().setScan(scan);

} catch (Exception e) {
e.printStackTrace();
}
}

public interface CommandExecutor {
boolean execute();
public void addObserver(CommandExecutorObserver observer) ;
public void removeObserver(CommandExecutorObserver observer);
public void notifyEnd();

}

0 comments on commit 91ed7c1

Please sign in to comment.