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"?>
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
<project version="4">
 | 
					<project version="4">
 | 
				
			||||||
 | 
					  <component name="GradleMigrationSettings" migrationVersion="1" />
 | 
				
			||||||
  <component name="GradleSettings">
 | 
					  <component name="GradleSettings">
 | 
				
			||||||
    <option name="linkedExternalProjectsSettings">
 | 
					    <option name="linkedExternalProjectsSettings">
 | 
				
			||||||
      <GradleProjectSettings>
 | 
					      <GradleProjectSettings>
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,3 @@
 | 
				
			|||||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
					 | 
				
			||||||
<project version="4">
 | 
					<project version="4">
 | 
				
			||||||
  <component name="ExternalStorageConfigurationManager" enabled="true" />
 | 
					  <component name="ExternalStorageConfigurationManager" enabled="true" />
 | 
				
			||||||
  <component name="FrameworkDetectionExcludesConfiguration">
 | 
					  <component name="FrameworkDetectionExcludesConfiguration">
 | 
				
			||||||
 | 
				
			|||||||
@ -5,20 +5,11 @@
 | 
				
			|||||||
  </component>
 | 
					  </component>
 | 
				
			||||||
  <component name="ChangeListManager">
 | 
					  <component name="ChangeListManager">
 | 
				
			||||||
    <list default="true" id="ceecbd3e-6a84-488d-ae73-a0b5b3f4010e" name="Changes" comment="">
 | 
					    <list default="true" id="ceecbd3e-6a84-488d-ae73-a0b5b3f4010e" name="Changes" comment="">
 | 
				
			||||||
      <change afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" />
 | 
					      <change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
 | 
				
			||||||
      <change 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 afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" />
 | 
					      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
 | 
				
			||||||
      <change afterPath="$PROJECT_DIR$/.idea/misc.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 afterPath="$PROJECT_DIR$/.idea/vcs.xml" 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" />
 | 
				
			||||||
      <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" />
 | 
					 | 
				
			||||||
    </list>
 | 
					    </list>
 | 
				
			||||||
    <option name="SHOW_DIALOG" value="false" />
 | 
					    <option name="SHOW_DIALOG" value="false" />
 | 
				
			||||||
    <option name="HIGHLIGHT_CONFLICTS" value="true" />
 | 
					    <option name="HIGHLIGHT_CONFLICTS" value="true" />
 | 
				
			||||||
@ -38,23 +29,7 @@
 | 
				
			|||||||
        </task>
 | 
					        </task>
 | 
				
			||||||
        <projects_view>
 | 
					        <projects_view>
 | 
				
			||||||
          <tree_state>
 | 
					          <tree_state>
 | 
				
			||||||
            <expand>
 | 
					            <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>
 | 
					 | 
				
			||||||
            <select />
 | 
					            <select />
 | 
				
			||||||
          </tree_state>
 | 
					          </tree_state>
 | 
				
			||||||
        </projects_view>
 | 
					        </projects_view>
 | 
				
			||||||
@ -74,9 +49,9 @@
 | 
				
			|||||||
  <component name="MarkdownSettingsMigration">
 | 
					  <component name="MarkdownSettingsMigration">
 | 
				
			||||||
    <option name="stateVersion" value="1" />
 | 
					    <option name="stateVersion" value="1" />
 | 
				
			||||||
  </component>
 | 
					  </component>
 | 
				
			||||||
  <component name="ProjectColorInfo"><![CDATA[{
 | 
					  <component name="ProjectColorInfo">{
 | 
				
			||||||
  "associatedIndex": 0
 | 
					  "associatedIndex": 0
 | 
				
			||||||
}]]></component>
 | 
					}</component>
 | 
				
			||||||
  <component name="ProjectId" id="2Zm8KgH0I3o9715Y3ZzBs94Smav" />
 | 
					  <component name="ProjectId" id="2Zm8KgH0I3o9715Y3ZzBs94Smav" />
 | 
				
			||||||
  <component name="ProjectLevelVcsManager" settingsEditedManually="true">
 | 
					  <component name="ProjectLevelVcsManager" settingsEditedManually="true">
 | 
				
			||||||
    <ConfirmationsSetting value="2" id="Add" />
 | 
					    <ConfirmationsSetting value="2" id="Add" />
 | 
				
			||||||
@ -91,8 +66,11 @@
 | 
				
			|||||||
    "Gradle.NginxInstallerBackend [shadowJar].executor": "Run",
 | 
					    "Gradle.NginxInstallerBackend [shadowJar].executor": "Run",
 | 
				
			||||||
    "RunOnceActivity.OpenProjectViewOnStart": "true",
 | 
					    "RunOnceActivity.OpenProjectViewOnStart": "true",
 | 
				
			||||||
    "RunOnceActivity.ShowReadmeOnStart": "true",
 | 
					    "RunOnceActivity.ShowReadmeOnStart": "true",
 | 
				
			||||||
 | 
					    "WebServerToolWindowFactoryState": "false",
 | 
				
			||||||
 | 
					    "git-widget-placeholder": "master",
 | 
				
			||||||
    "ignore.virus.scanning.warn.message": "true",
 | 
					    "ignore.virus.scanning.warn.message": "true",
 | 
				
			||||||
    "kotlin-language-version-configured": "true",
 | 
					    "kotlin-language-version-configured": "true",
 | 
				
			||||||
 | 
					    "last_opened_file_path": "F:/Projects/Web/NginxInstallerBackend",
 | 
				
			||||||
    "node.js.detected.package.eslint": "true",
 | 
					    "node.js.detected.package.eslint": "true",
 | 
				
			||||||
    "node.js.detected.package.tslint": "true",
 | 
					    "node.js.detected.package.tslint": "true",
 | 
				
			||||||
    "node.js.selected.package.eslint": "(autodetect)",
 | 
					    "node.js.selected.package.eslint": "(autodetect)",
 | 
				
			||||||
@ -101,7 +79,7 @@
 | 
				
			|||||||
    "vue.rearranger.settings.migration": "true"
 | 
					    "vue.rearranger.settings.migration": "true"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}]]></component>
 | 
					}]]></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">
 | 
					    <configuration name="Server" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
 | 
				
			||||||
      <option name="MAIN_CLASS_NAME" value="tech.nevets.ngxinstaller.Server" />
 | 
					      <option name="MAIN_CLASS_NAME" value="tech.nevets.ngxinstaller.Server" />
 | 
				
			||||||
      <module name="NginxInstallerBackend.main" />
 | 
					      <module name="NginxInstallerBackend.main" />
 | 
				
			||||||
@ -160,6 +138,7 @@
 | 
				
			|||||||
      <option name="presentableId" value="Default" />
 | 
					      <option name="presentableId" value="Default" />
 | 
				
			||||||
      <updated>1703015833777</updated>
 | 
					      <updated>1703015833777</updated>
 | 
				
			||||||
      <workItem from="1703015834937" duration="5516000" />
 | 
					      <workItem from="1703015834937" duration="5516000" />
 | 
				
			||||||
 | 
					      <workItem from="1703185574324" duration="1576000" />
 | 
				
			||||||
    </task>
 | 
					    </task>
 | 
				
			||||||
    <servers />
 | 
					    <servers />
 | 
				
			||||||
  </component>
 | 
					  </component>
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,11 @@ import com.google.gson.Gson;
 | 
				
			|||||||
import com.google.gson.GsonBuilder;
 | 
					import com.google.gson.GsonBuilder;
 | 
				
			||||||
import com.google.gson.JsonObject;
 | 
					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.Timer;
 | 
				
			||||||
import java.util.TimerTask;
 | 
					import java.util.TimerTask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -11,17 +16,62 @@ import static spark.Spark.*;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class Server {
 | 
					public class Server {
 | 
				
			||||||
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
 | 
					    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
 | 
				
			||||||
 | 
					    public static String cachedNginxSite = "";
 | 
				
			||||||
    public static void main(String[] args) {
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            cachedNginxSite = getHTML("https://nginx.org/download/");
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            throw new RuntimeException(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        port(8080);
 | 
					        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) -> {
 | 
					        post("/ngx/create", (req, res) -> {
 | 
				
			||||||
            JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
 | 
					            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() + "\" }";
 | 
					            return "{ \"url\":\"https://api.nevets.tech/ngx/raw/" + ts.getId() + "\" }";
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        get("/ngx/raw/*", (req, res) -> {
 | 
					        get("/ngx/raw/*", (req, res) -> {
 | 
				
			||||||
            String id = req.splat()[0];
 | 
					            String id = req.splat()[0];
 | 
				
			||||||
            TextStorage ts = TextStorage.getPaste(id);
 | 
					            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);
 | 
					        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 {
 | 
					    static class GCTask extends TimerTask {
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
        public void run() {
 | 
					        public void run() {
 | 
				
			||||||
 | 
				
			|||||||
@ -19,6 +19,10 @@ public class TextStorage {
 | 
				
			|||||||
        this.expiration = expiration;
 | 
					        this.expiration = expiration;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getText() {
 | 
				
			||||||
 | 
					        return text;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public String getId() {
 | 
					    public String getId() {
 | 
				
			||||||
        return this.id;
 | 
					        return this.id;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user