Add HTTPS support to API (mandatory for domain name in .dev)
This commit is contained in:
parent
ee602af486
commit
eace65ad53
3 changed files with 79 additions and 24 deletions
|
@ -15,6 +15,13 @@ public class Configuration {
|
|||
private String db_database;
|
||||
private String db_password;
|
||||
|
||||
private String tcp_port;
|
||||
private String use_ssl;
|
||||
private String ssl_keystore;
|
||||
private String ssl_keystorePasswd;
|
||||
|
||||
|
||||
|
||||
private File _file;
|
||||
|
||||
public Configuration(String path){
|
||||
|
@ -73,4 +80,20 @@ public class Configuration {
|
|||
public String getDbPassword(){
|
||||
return this.db_password;
|
||||
}
|
||||
|
||||
public String getSslKeystore(){
|
||||
return this.ssl_keystore;
|
||||
}
|
||||
|
||||
public String getSslKeystorePasswd(){
|
||||
return this.ssl_keystorePasswd;
|
||||
}
|
||||
|
||||
public int getTcpPort(){
|
||||
return Integer.parseInt(this.tcp_port);
|
||||
}
|
||||
|
||||
public boolean useSsl(){
|
||||
return Boolean.parseBoolean(this.use_ssl);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
package be.jeffcheasey88.peeratcode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.password4j.Password;
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
import javax.net.ssl.SSLServerSocketFactory;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
||||
|
@ -23,19 +24,20 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
|||
import be.jeffcheasey88.peeratcode.webserver.Router;
|
||||
|
||||
public class Main {
|
||||
|
||||
// Define SSL Protocol parameters
|
||||
public static void main(String[] args) throws Exception {
|
||||
Configuration config = new Configuration("config.txt");
|
||||
config.load();
|
||||
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
Router router = new Router();
|
||||
|
||||
router.setDefault(new Response(){
|
||||
router.setDefault(new Response() {
|
||||
|
||||
@Override
|
||||
public Pattern getPattern(){return null;}
|
||||
public Pattern getPattern() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
|
@ -48,16 +50,10 @@ public class Main {
|
|||
|
||||
initRoutes(router, new DatabaseRepository(config));
|
||||
|
||||
ServerSocket server = new ServerSocket(80);
|
||||
|
||||
while(!server.isClosed()){
|
||||
Socket socket = server.accept();
|
||||
Client client = new Client(socket, router);
|
||||
client.start();
|
||||
}
|
||||
|
||||
startWebServer(config, router);
|
||||
}
|
||||
private static void initRoutes(Router router, DatabaseRepository repo){
|
||||
|
||||
private static void initRoutes(Router router, DatabaseRepository repo) {
|
||||
router.register(new ChapterElement(repo));
|
||||
router.register(new ChapterList(repo));
|
||||
router.register(new PuzzleElement(repo));
|
||||
|
@ -65,4 +61,40 @@ public class Main {
|
|||
router.register(new Login(repo));
|
||||
}
|
||||
|
||||
private static void startWebServer(Configuration config, Router router) throws IOException {
|
||||
if (config.useSsl()) {
|
||||
SSLServerSocket server = null;
|
||||
try {
|
||||
System.setProperty("javax.net.ssl.keyStore", config.getSslKeystore());
|
||||
System.setProperty("javax.net.ssl.keyStorePassword", config.getSslKeystorePasswd());
|
||||
|
||||
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
||||
server = (SSLServerSocket) ssf.createServerSocket(config.getTcpPort());
|
||||
|
||||
while (!server.isClosed()) {
|
||||
SSLSocket socket = (SSLSocket) server.accept();
|
||||
Client client = new Client(socket, router);
|
||||
client.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (server != null) {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
try (ServerSocket server = new ServerSocket(config.getTcpPort())){
|
||||
while(!server.isClosed()){
|
||||
Socket socket = server.accept();
|
||||
Client client = new Client(socket, router);
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.Arrays;
|
|||
|
||||
public class Client extends Thread{
|
||||
|
||||
private Socket socket;
|
||||
private Socket socket; // A quoi il sert ??
|
||||
private HttpReader reader;
|
||||
private HttpWriter writer;
|
||||
private Router router;
|
||||
|
|
Loading…
Add table
Reference in a new issue