This commit is contained in:
2023-11-22 12:59:06 -06:00
parent c8907ab681
commit e1bc89c4e7
14 changed files with 482 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package tech.nevets.rickrollprot;
import spark.Request;
import spark.Response;
import spark.Route;
import java.net.URI;
import java.net.URLDecoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
public class CheckRoute implements Route {
// Route: "/api/check/:link"
@Override
public Object handle(Request req, Response res) throws Exception {
boolean isRickRoll = false;
String link = URLDecoder.decode(req.params("link"), StandardCharsets.UTF_8);
String response = makeGetRequest(link);
if (response == null) {
res.status(400);
return "Invalid Link";
}
if (response.matches("(?i)never.*?gonna.*?Give.*?You.*?Up")
|| response.matches("(?i)rick.*?astley")
|| response.matches("(?i)rick.*?roll")
|| response.contains("nggyu")) {
isRickRoll = true;
}
return "{\"isRickRoll\":" + isRickRoll + "}";
}
private static String makeGetRequest(String link) {
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(new URI(link))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.headers().firstValue("Location").isPresent()) {
return makeGetRequest(response.headers().firstValue("Location").get());
}
return response.body();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -0,0 +1,11 @@
package tech.nevets.rickrollprot;
import static spark.Spark.*;
public class Server {
public static void main(String[] args) {
port(8080);
get("/api/check/:link", new CheckRoute());
}
}