58 lines
No EOL
1.7 KiB
Java
58 lines
No EOL
1.7 KiB
Java
import java.io.IOException;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.Socket;
|
|
|
|
public class PortConnect implements ScanType {
|
|
private String accessString; // Like "example.com:80"
|
|
|
|
public PortConnect(String accessString) {
|
|
this.accessString = accessString;
|
|
}
|
|
|
|
public static String getName() {
|
|
return "TCP Port";
|
|
}
|
|
|
|
static boolean validateAccessString(String accessString) {
|
|
// Validate the access string format (e.g., "host:port")
|
|
String[] parts = accessString.split(":");
|
|
if (parts.length != 2) {
|
|
return false;
|
|
}
|
|
String portStr = parts[1];
|
|
try {
|
|
int port = Integer.parseInt(portStr);
|
|
if(port > 0 && port <= 65535) return true;
|
|
} catch (NumberFormatException e) {
|
|
return false;
|
|
}
|
|
String host = parts[0];
|
|
//Check if host is a valid IP address or hostname
|
|
if (host.isEmpty()) {
|
|
return false;
|
|
}
|
|
String regex = "^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$";
|
|
if (!host.matches(regex)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean execute() throws IOException, InterruptedException {
|
|
String[] parts = accessString.split(":");
|
|
String host = parts[0];
|
|
int port = Integer.parseInt(parts[1]);
|
|
|
|
try (Socket socket = new Socket()) {
|
|
socket.connect(new InetSocketAddress(host, port), 5000); // 5 seconds timeout
|
|
return true;
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getAccessString() {
|
|
return accessString;
|
|
}
|
|
} |