Made changes including POST-to-GET CORS proxy in order to get NGINX site HTML and disabled CORS for the API.
This commit is contained in:
@@ -4,6 +4,11 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@@ -11,17 +16,62 @@ import static spark.Spark.*;
|
||||
|
||||
public class Server {
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
public static String cachedNginxSite = "";
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
cachedNginxSite = getHTML("https://nginx.org/download/");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
port(8080);
|
||||
options("/*",
|
||||
(request, response) -> {
|
||||
|
||||
String accessControlRequestHeaders = request
|
||||
.headers("Access-Control-Request-Headers");
|
||||
if (accessControlRequestHeaders != null) {
|
||||
response.header("Access-Control-Allow-Headers",
|
||||
accessControlRequestHeaders);
|
||||
}
|
||||
|
||||
String accessControlRequestMethod = request
|
||||
.headers("Access-Control-Request-Method");
|
||||
if (accessControlRequestMethod != null) {
|
||||
response.header("Access-Control-Allow-Methods",
|
||||
accessControlRequestMethod);
|
||||
}
|
||||
|
||||
return "OK";
|
||||
});
|
||||
|
||||
before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
|
||||
post("/ngx/dhv", (req, res) -> {
|
||||
System.out.println(req.body());
|
||||
JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
|
||||
String version = jsonObject.get("version").getAsString();
|
||||
System.out.println(version);
|
||||
try {
|
||||
if (cachedNginxSite.contains(version + ".tar.gz")) {
|
||||
System.out.println("true");
|
||||
return "{ \"url\":\"true\" }";
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
System.out.println("false");
|
||||
return "{ \"url\":\"false\" }";
|
||||
}
|
||||
System.out.println("false");
|
||||
return "{ \"url\":\"false\" }";
|
||||
});
|
||||
post("/ngx/create", (req, res) -> {
|
||||
JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
|
||||
TextStorage ts = TextStorage.createPaste(jsonObject.get("content").getAsString());
|
||||
TextStorage ts = TextStorage.createPaste(java.net.URLDecoder.decode(jsonObject.get("content").getAsString(), StandardCharsets.UTF_8));
|
||||
return "{ \"url\":\"https://api.nevets.tech/ngx/raw/" + ts.getId() + "\" }";
|
||||
});
|
||||
get("/ngx/raw/*", (req, res) -> {
|
||||
String id = req.splat()[0];
|
||||
TextStorage ts = TextStorage.getPaste(id);
|
||||
return GSON.toJson(ts);
|
||||
res.type("raw");
|
||||
return ts.getText();
|
||||
});
|
||||
|
||||
|
||||
@@ -30,6 +80,21 @@ public class Server {
|
||||
timer.schedule(tt, 0, 60000);
|
||||
}
|
||||
|
||||
public static String getHTML(String urlToRead) throws Exception {
|
||||
StringBuilder result = new StringBuilder();
|
||||
URL url = new URL(urlToRead);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()))) {
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
result.append(line);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
static class GCTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -19,6 +19,10 @@ public class TextStorage {
|
||||
this.expiration = expiration;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user