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;
}
}
}

View File

@ -30,6 +30,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.io.IOException;
import java.net.Socket;
import javax.sql.DataSource;
@SpringBootApplication
@ -42,6 +45,30 @@ public class WebWolf {
public static void main(String[] args) {
System.setProperty("spring.config.name", "application-webwolf");
String webwolfPort = System.getenv("WEBWOLF_PORT");
String databasePort = System.getenv("WEBGOAT_HSQLPORT");
String webGoatHost = null==System.getenv("WEBGOAT_HOST")?"127.0.0.1":System.getenv("WEBGOAT_HOST");
String webWolfHost = null==System.getenv("WEBWOLF_HOST")?"127.0.0.1":System.getenv("WEBWOLF_HOST");
String fileEncoding = System.getProperty("file.encoding");
int wolfPort = webwolfPort == null?9090:Integer.parseInt(webwolfPort);
int dbPort = databasePort == null?9001:Integer.parseInt(databasePort);
if (null==fileEncoding || !fileEncoding.equals("UTF-8")) {
System.out.println("It seems the application is startd on a OS with non default UTF-8 encoding:"+fileEncoding);
System.out.println("Please add: -Dfile.encoding=UTF-8");
System.exit(-1);
}
if (!isAlreadyRunning(webGoatHost, dbPort)) {
System.out.println("It seems that the required database is not running. Please start WebGoat with the integrated or standalone database first.");
System.exit(-1);
}
if (isAlreadyRunning(webGoatHost, wolfPort)) {
System.out.println("Port "+webWolfHost+":"+wolfPort+" is in use. Use environment value WEBWOLF_PORT to set a different value.");
System.exit(-1);
}
SpringApplication.run(WebWolf.class, args);
}
@ -51,4 +78,12 @@ public class WebWolf {
driverManagerDataSource.setDriverClassName(driverClassName);
return driverManagerDataSource;
}
private static boolean isAlreadyRunning(String host, int port) {
try (var ignored = new Socket(host, port)) {
return true;
} catch (IOException e) {
return false;
}
}
}