all PortTextFetcher descendants are now truly configurable

This commit is contained in:
Anton Keks 2013-02-01 00:40:44 +02:00
parent 254f664435
commit a4b08fee50
5 changed files with 87 additions and 41 deletions

View File

@ -128,7 +128,7 @@ text.fetchers.info=Fetcher information:
text.fetchers.info.notAvailable=Unfortunately, no additional information about this fetcher is available.
text.fetchers.preferences=Preferences of the selected fetcher
text.fetcher.portText.send=Text to send (\\n, \\r, \\t and \\xXX are permitted)
text.fetcher.portText.match=Regexp to match in the response
text.fetcher.portText.match=Regexp to match in the response, line by line
text.fetcher.portText.replace=Regexp replacement string ($0 .. $9 are substituted for matched groups)
text.about=%NAME\n\nVersion: %VERSION\nBuild date: %DATE\n\n%COPYLEFT
text.about.system=Java: %JAVA\nOS: %OS
@ -213,7 +213,7 @@ fetcher.comment.info=Shows the comments entered for the address using the IP Det
fetcher.webDetect=Web detect
fetcher.webDetect.info=Detects the web server software name and version, if possible.\n\nWorks by sending a HEAD request and reading the Server HTTP header from the response.
fetcher.httpSender=HTTP Sender
fetcher.httpSender.info=Sends the specified textual requests to the specified TCP port, retrieves the response and uses the specified regular expression to extract the needed information out.\n\nVery customizable, can be used for any textual protocol, like HTTP, SMTP, POP3, IMAP, etc. For advanced users.
fetcher.httpSender.info=Sends the specified textual requests to the specified TCP port (eg 3128, http proxy), retrieves the response and uses the specified regular expression to extract the needed information out.\n\nVery customizable, can be used for any textual protocol, like HTTP, SMTP, POP3, IMAP, etc. For advanced users.
fetcher.portText.custom=Custom
fetcher.netbios=NetBIOS Info
fetcher.netbios.info=Retrieves the NetBIOS information about Windows machines.\n\nThe response has the following format:\nDOMAIN\\USER@COMPUTER [MAC]\n\nWhere:\nDOMAIN - Windows domain or workgroup\nUSER - currently logged in user\nCOMPUTER - Windows computer name (may be different from DNS name)\nSome parts may be absent, depending on the response.\n\nNote that this won't work with machines that have firewall enabled (which are most modern installations).\nThis fetcher is provided mostly for feature-compatibility with version 2.x.

View File

@ -5,10 +5,12 @@
*/
package net.azib.ipscan.fetchers;
import java.util.MissingResourceException;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import java.util.MissingResourceException;
import java.util.prefs.Preferences;
/**
* Convenience base class for built-in fetchers
*
@ -33,6 +35,10 @@ public abstract class AbstractFetcher implements Fetcher {
}
}
public Preferences getPreferences() {
return Config.getConfig().getPreferences().node(getId().replace("fetcher.", ""));
}
public Class<? extends FetcherPrefs> getPreferencesClass() {
// no preferences by default
return null;

View File

@ -16,8 +16,7 @@ import net.azib.ipscan.config.ScannerConfig;
public class HTTPSenderFetcher extends PortTextFetcher {
public HTTPSenderFetcher(ScannerConfig scannerConfig) {
super(scannerConfig, 3128, "GET http://www.urbandplayground.com/?f=vote&band_id=150 HTTP/1.0\r\n\r\n",
"\">([^>]+?VOT[^<]+?)</");
super(scannerConfig, 3128, "GET http://www.google.com HTTP/1.0\r\n\r\n", "Location: (https?.*)$");
}
public String getId() {

View File

@ -5,14 +5,16 @@
*/
package net.azib.ipscan.fetchers;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.ScanningResult.ResultType;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.gui.fetchers.PortTextFetcherPrefs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.*;
import java.util.Collections;
import java.util.Iterator;
import java.util.logging.Level;
@ -20,12 +22,6 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.ScanningResult.ResultType;
import net.azib.ipscan.gui.fetchers.PortTextFetcherPrefs;
/**
* PortTextFetcher - generic configurable fetcher to read some particular information from a port.
*
@ -39,12 +35,14 @@ public abstract class PortTextFetcher extends AbstractFetcher {
private int defaultPort;
protected String textToSend;
protected Pattern matchingRegexp;
protected int extractGroup;
public PortTextFetcher(ScannerConfig scannerConfig, int defaultPort, String textToSend, String matchingRegexp) {
this.scannerConfig = scannerConfig;
this.defaultPort = defaultPort;
this.textToSend = textToSend;
this.matchingRegexp = Pattern.compile(matchingRegexp);
this.textToSend = getPreferences().get("textToSend", textToSend);
this.matchingRegexp = Pattern.compile(getPreferences().get("matchingRegexp", matchingRegexp));
this.extractGroup = getPreferences().getInt("extractGroup", 1);
}
public Object scan(ScanningSubject subject) {
@ -68,7 +66,7 @@ public abstract class PortTextFetcher extends AbstractFetcher {
// mark that additional info is available
subject.setResultType(ResultType.WITH_PORTS);
// return the required contents
return matcher.group(1);
return matcher.group(extractGroup);
}
}
}
@ -114,4 +112,12 @@ public abstract class PortTextFetcher extends AbstractFetcher {
public void setMatchingRegexp(Pattern matchingRegexp) {
this.matchingRegexp = matchingRegexp;
}
public int getExtractGroup() {
return extractGroup;
}
public void setExtractGroup(int extractGroup) {
this.extractGroup = extractGroup;
}
}

View File

@ -11,14 +11,16 @@ import net.azib.ipscan.fetchers.FetcherPrefs;
import net.azib.ipscan.fetchers.PortTextFetcher;
import net.azib.ipscan.gui.AbstractModalDialog;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.*;
import java.util.prefs.Preferences;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
/**
* PortTextFetcherPrefs
@ -28,7 +30,10 @@ import org.eclipse.swt.widgets.Text;
public class PortTextFetcherPrefs extends AbstractModalDialog implements FetcherPrefs {
private PortTextFetcher fetcher;
private Text textToSend;
private Text matchingRegexp;
private Text extractGroup;
public PortTextFetcherPrefs() {
}
@ -53,30 +58,60 @@ public class PortTextFetcherPrefs extends AbstractModalDialog implements Fetcher
Label sendLabel = new Label(shell, SWT.NONE);
sendLabel.setText(Labels.getLabel("text.fetcher.portText.send"));
sendLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, null, new FormAttachment(predefinedCombo, 0, SWT.BOTTOM)));
Text sendText = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
sendText.setText(stringToText(fetcher.getTextToSend()));
sendText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), new FormAttachment(sendLabel), null));
textToSend = new Text(shell, SWT.BORDER);
textToSend.setText(stringToText(fetcher.getTextToSend()));
textToSend.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), new FormAttachment(sendLabel), null));
Label matchLabel = new Label(shell, SWT.NONE);
matchLabel.setText(Labels.getLabel("text.fetcher.portText.match"));
matchLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(sendText), null));
Text matchText = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
matchText.setText(fetcher.getMatchingRegexp().pattern());
matchText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(sendText, 0, SWT.RIGHT), new FormAttachment(matchLabel), null));
matchLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(textToSend), null));
matchingRegexp = new Text(shell, SWT.BORDER);
matchingRegexp.setText(fetcher.getMatchingRegexp().pattern());
matchingRegexp.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(textToSend, 0, SWT.RIGHT), new FormAttachment(matchLabel), null));
Label replaceLabel = new Label(shell, SWT.NONE);
replaceLabel.setText(Labels.getLabel("text.fetcher.portText.replace"));
replaceLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(matchText), null));
Text replaceText = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
replaceText.setText("$1");
replaceText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(sendText, 0, SWT.RIGHT), new FormAttachment(replaceLabel), null));
replaceLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(matchingRegexp), null));
extractGroup = new Text(shell, SWT.BORDER);
extractGroup.setText("$1");
extractGroup.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(textToSend, 0, SWT.RIGHT), new FormAttachment(replaceLabel), null));
Button okButton = new Button(shell, SWT.NONE);
okButton.setText(Labels.getLabel("button.OK"));
Button cancelButton = new Button(shell, SWT.NONE);
cancelButton.setText(Labels.getLabel("button.cancel"));
positionButtonsInFormLayout(okButton, cancelButton, extractGroup);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
savePreferences();
shell.close();
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
shell.pack();
}
/**
* @return
*/
void savePreferences() {
Preferences prefs = fetcher.getPreferences();
fetcher.setTextToSend(textToSend.getText());
prefs.put("textToSend", fetcher.getTextToSend());
fetcher.setMatchingRegexp(Pattern.compile(matchingRegexp.getText()));
prefs.put("matchingRegexp", fetcher.getMatchingRegexp().pattern());
fetcher.setExtractGroup(parseInt(extractGroup.getText().replace("$", "")));
prefs.putInt("extractGroup", fetcher.getExtractGroup());
}
private String stringToText(String s) {
StringBuilder t = new StringBuilder();
for (char c : s.toCharArray()) {