forked from Steven/NginxInstallerBackend
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:
parent
a604b1e3ec
commit
e52bf78803
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
@ -1,4 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
|
@ -5,20 +5,11 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="ceecbd3e-6a84-488d-ae73-a0b5b3f4010e" name="Changes" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.jar" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.properties" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/gradlew" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/gradlew.bat" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/settings.gradle" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/Server.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/TextStorage.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/Server.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/Server.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/TextStorage.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/tech/nevets/ngxinstaller/TextStorage.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -38,23 +29,7 @@
|
||||
</task>
|
||||
<projects_view>
|
||||
<tree_state>
|
||||
<expand>
|
||||
<path>
|
||||
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
|
||||
<item name="NginxInstallerBackend" type="f1a62948:ProjectNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
|
||||
<item name="NginxInstallerBackend" type="f1a62948:ProjectNode" />
|
||||
<item name="Tasks" type="e4a08cd1:TasksNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
|
||||
<item name="NginxInstallerBackend" type="f1a62948:ProjectNode" />
|
||||
<item name="Tasks" type="e4a08cd1:TasksNode" />
|
||||
<item name="shadow" type="c8890929:TasksNode$1" />
|
||||
</path>
|
||||
</expand>
|
||||
<expand />
|
||||
<select />
|
||||
</tree_state>
|
||||
</projects_view>
|
||||
@ -74,9 +49,9 @@
|
||||
<component name="MarkdownSettingsMigration">
|
||||
<option name="stateVersion" value="1" />
|
||||
</component>
|
||||
<component name="ProjectColorInfo"><![CDATA[{
|
||||
"associatedIndex": 0
|
||||
}]]></component>
|
||||
<component name="ProjectColorInfo">{
|
||||
"associatedIndex": 0
|
||||
}</component>
|
||||
<component name="ProjectId" id="2Zm8KgH0I3o9715Y3ZzBs94Smav" />
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
|
||||
<ConfirmationsSetting value="2" id="Add" />
|
||||
@ -91,8 +66,11 @@
|
||||
"Gradle.NginxInstallerBackend [shadowJar].executor": "Run",
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"WebServerToolWindowFactoryState": "false",
|
||||
"git-widget-placeholder": "master",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"last_opened_file_path": "F:/Projects/Web/NginxInstallerBackend",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.detected.package.tslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
@ -101,7 +79,7 @@
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="RunManager" selected="Gradle.NginxInstallerBackend [shadowJar]">
|
||||
<component name="RunManager" selected="Application.Server">
|
||||
<configuration name="Server" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||
<option name="MAIN_CLASS_NAME" value="tech.nevets.ngxinstaller.Server" />
|
||||
<module name="NginxInstallerBackend.main" />
|
||||
@ -160,6 +138,7 @@
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1703015833777</updated>
|
||||
<workItem from="1703015834937" duration="5516000" />
|
||||
<workItem from="1703185574324" duration="1576000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user