added start checks with hints on port usage

This commit is contained in:
René Zubcevic
2020-11-22 15:17:31 +01:00
committed by Nanne Baars
parent 74cca6d185
commit 090263b279
2 changed files with 64 additions and 0 deletions

View File

@ -26,6 +26,10 @@
package org.owasp.webgoat;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.Socket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ -46,6 +50,31 @@ public class StartWebGoat extends SpringBootServletInitializer {
public static void main(String[] args) {
log.info("Starting WebGoat with args: {}", StringUtils.arrayToCommaDelimitedString(args));
System.setProperty("spring.config.name", "application-webgoat");
String webgoatPort = System.getenv("WEBGOAT_PORT");
String databasePort = System.getenv("WEBGOAT_HSQLPORT");
String webGoatHost = null==System.getenv("WEBGOAT_HOST")?"127.0.0.1":System.getenv("WEBGOAT_HOST");
int goatPort = webgoatPort == null?8080:Integer.parseInt(webgoatPort);
int dbPort = databasePort == null?9001:Integer.parseInt(databasePort);
if (isAlreadyRunning(webGoatHost, goatPort)) {
log.error("Port {}:{} is already in use", webGoatHost, goatPort);
System.out.println("Port "+webGoatHost+":"+goatPort+" is in use. Use environment value WEBGOAT_PORT to set a different value.");
System.exit(-1);
}
if (isAlreadyRunning(webGoatHost, dbPort)) {
log.error("Port {}:{} is already in use", webGoatHost, goatPort);
System.out.println("Port "+webGoatHost+":"+goatPort+" is in use. Use environment value WEBGOAT_HSQLPORT to set a different value.");
System.exit(-1);
}
SpringApplication.run(StartWebGoat.class, args);
}
private static boolean isAlreadyRunning(String host, int port) {
try (var ignored = new Socket(host, port)) {
return true;
} catch (IOException e) {
return false;
}
}
}