forked from Steven/NginxInstallerBackend
105 lines
3.7 KiB
Java
105 lines
3.7 KiB
Java
package tech.nevets.ngxinstaller;
|
|
|
|
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;
|
|
|
|
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(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);
|
|
res.type("raw");
|
|
return ts.getText();
|
|
});
|
|
|
|
|
|
Timer timer = new Timer();
|
|
TimerTask tt = new GCTask();
|
|
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() {
|
|
TextStorage.garbageCollect();
|
|
}
|
|
}
|
|
}
|