Initial commit
This commit is contained in:
commit
d61c61f82a
5 changed files with 186 additions and 0 deletions
49
HTTPConnection.java
Normal file
49
HTTPConnection.java
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
public record HTTPConnection() {
|
||||
public static HttpURLConnection open(String _url) throws IOException {
|
||||
URL url;
|
||||
try {
|
||||
url = new URI(_url).toURL();
|
||||
} catch (MalformedURLException | URISyntaxException e) {
|
||||
throw new IllegalArgumentException("Invalid URL format: " + _url, e);
|
||||
}
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(5000); // 5 seconds
|
||||
connection.setReadTimeout(5000); // 5 seconds
|
||||
// Use default SSL socket factory for secure connections
|
||||
if (connection instanceof javax.net.ssl.HttpsURLConnection) {
|
||||
javax.net.ssl.SSLContext sslContext;
|
||||
try {
|
||||
sslContext = javax.net.ssl.SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, new javax.net.ssl.TrustManager[] {
|
||||
new javax.net.ssl.X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
|
||||
String authType) {
|
||||
}
|
||||
|
||||
public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
|
||||
String authType) {
|
||||
}
|
||||
}
|
||||
}, new java.security.SecureRandom());
|
||||
((javax.net.ssl.HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to create SSL context", e);
|
||||
}
|
||||
}
|
||||
if (connection instanceof javax.net.ssl.HttpsURLConnection) {
|
||||
((javax.net.ssl.HttpsURLConnection) connection).setHostnameVerifier((hostname, session) -> true);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
47
HTTPGet.java
Normal file
47
HTTPGet.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import javax.rmi.ssl.SslRMIClientSocketFactory;
|
||||
|
||||
public class HTTPGet implements ScanType {
|
||||
private String accessString; // Like "http://example.com"
|
||||
|
||||
public HTTPGet(String accessString) {
|
||||
this.accessString = accessString;
|
||||
}
|
||||
|
||||
public static String getName() {
|
||||
return "HTTP GET";
|
||||
}
|
||||
|
||||
static boolean validateAccessString(String accessString) {
|
||||
// Validate the access string format (e.g., "http://example.com")
|
||||
if (accessString == null || accessString.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
String regex = "^(http|https)://[a-zA-Z0-9.-]+(:\\d+)?(/.*)?$";
|
||||
return accessString.matches(regex);
|
||||
}
|
||||
|
||||
public boolean execute() throws IOException, InterruptedException {
|
||||
// Send real HTTP request
|
||||
HttpURLConnection connection = HTTPConnection.open(accessString);
|
||||
if (!(connection instanceof HttpURLConnection)) {
|
||||
throw new IOException("Invalid URL connection type");
|
||||
}
|
||||
connection.setRequestMethod("GET");
|
||||
connection.connect();
|
||||
int responseCode = connection.getResponseCode();
|
||||
connection.disconnect();
|
||||
return responseCode == 200; // HTTP OK
|
||||
}
|
||||
|
||||
public String getAccessString() {
|
||||
return accessString;
|
||||
}
|
||||
}
|
||||
44
HTTPHead.java
Normal file
44
HTTPHead.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
public class HTTPHead implements ScanType {
|
||||
private String accessString; // Like "http://example.com"
|
||||
|
||||
public HTTPHead(String accessString) {
|
||||
this.accessString = accessString;
|
||||
}
|
||||
|
||||
public static String getName() {
|
||||
return "HTTP HEAD";
|
||||
}
|
||||
|
||||
static boolean validateAccessString(String accessString) {
|
||||
// Validate the access string format (e.g., "http://example.com")
|
||||
if (accessString == null || accessString.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
String regex = "^(http|https)://[a-zA-Z0-9.-]+(:\\d+)?(/.*)?$";
|
||||
return accessString.matches(regex);
|
||||
}
|
||||
|
||||
public boolean execute() throws IOException, InterruptedException {
|
||||
// Send real HTTP request
|
||||
HttpURLConnection connection = HTTPConnection.open(accessString);
|
||||
if (!(connection instanceof HttpURLConnection)) {
|
||||
throw new IOException("Invalid URL connection type");
|
||||
}
|
||||
connection.setRequestMethod("HEAD");
|
||||
connection.connect();
|
||||
int responseCode = connection.getResponseCode();
|
||||
connection.disconnect();
|
||||
return responseCode == 200; // HTTP OK
|
||||
}
|
||||
|
||||
public String getAccessString() {
|
||||
return accessString;
|
||||
}
|
||||
}
|
||||
13
ScanType.java
Normal file
13
ScanType.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.io.IOException;
|
||||
|
||||
public interface ScanType {
|
||||
String accessString = null;
|
||||
static String getName() {
|
||||
return "ScanType";
|
||||
}
|
||||
static boolean validateAccessString(String accessString) {
|
||||
return accessString != null && !accessString.isEmpty();
|
||||
}
|
||||
boolean execute() throws IOException, InterruptedException;
|
||||
public abstract String getAccessString();
|
||||
}
|
||||
33
Storage.java
Normal file
33
Storage.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Storage {
|
||||
private static final String FILE_NAME = "scans.dat";
|
||||
|
||||
public static void saveScans(List<StoredScan> storedScans) throws IOException {
|
||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
|
||||
out.writeObject(storedScans);
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<StoredScan> loadScans() throws IOException, ClassNotFoundException {
|
||||
File file = new File(FILE_NAME);
|
||||
if (!file.exists()) return new ArrayList<>();
|
||||
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
|
||||
return (List<StoredScan>) in.readObject();
|
||||
}
|
||||
}
|
||||
|
||||
public static class StoredScan implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public String type;
|
||||
public String accessString;
|
||||
public String customName;
|
||||
|
||||
public StoredScan(String type, String accessString, String customName) {
|
||||
this.type = type;
|
||||
this.accessString = accessString;
|
||||
this.customName = customName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue