commit d61c61f82ab3ad339a56007641488dc5a65ce6c4 Author: shishcat Date: Wed May 28 09:42:09 2025 +0200 Initial commit diff --git a/HTTPConnection.java b/HTTPConnection.java new file mode 100644 index 0000000..3b57344 --- /dev/null +++ b/HTTPConnection.java @@ -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; + } +} diff --git a/HTTPGet.java b/HTTPGet.java new file mode 100644 index 0000000..b8c6e9a --- /dev/null +++ b/HTTPGet.java @@ -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; + } +} \ No newline at end of file diff --git a/HTTPHead.java b/HTTPHead.java new file mode 100644 index 0000000..fc85ad3 --- /dev/null +++ b/HTTPHead.java @@ -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; + } +} \ No newline at end of file diff --git a/ScanType.java b/ScanType.java new file mode 100644 index 0000000..59e4849 --- /dev/null +++ b/ScanType.java @@ -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(); +} diff --git a/Storage.java b/Storage.java new file mode 100644 index 0000000..8a744d1 --- /dev/null +++ b/Storage.java @@ -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 storedScans) throws IOException { + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) { + out.writeObject(storedScans); + } + } + @SuppressWarnings("unchecked") + public static List 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) 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; + } + } +}