diff --git a/build.gradle b/build.gradle index 79965bd..90fb8e3 100644 --- a/build.gradle +++ b/build.gradle @@ -11,12 +11,25 @@ repositories { mavenCentral() } +jar { + manifest { + attributes 'Main-Class': 'com.wasteinformationserver.main' + } + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } +} + sourceSets { main.java.srcDirs = ['src/java'] main.resources.srcDirs = ['src/resources'] } dependencies { - compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.0.2' + compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.2.2' compile group: 'mysql',name:'mysql-connector-java',version: '8.0.18' } + +task run (type: JavaExec){ + description = "Secure algorythm testing" + main = 'com.wasteinformationserver.main' + classpath = sourceSets.main.runtimeClasspath +} \ No newline at end of file diff --git a/src/java/com/wasteinformationserver/db/MySQLConnector.java b/src/java/com/wasteinformationserver/db/MySQLConnector.java index 8a84cbb..0053679 100755 --- a/src/java/com/wasteinformationserver/db/MySQLConnector.java +++ b/src/java/com/wasteinformationserver/db/MySQLConnector.java @@ -8,7 +8,7 @@ public class MySQLConnector extends Database { static { try { - Class.forName("com.mysql.jdbc.Driver").newInstance(); + Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/java/com/wasteinformationserver/main.java b/src/java/com/wasteinformationserver/main.java index be98985..d673047 100644 --- a/src/java/com/wasteinformationserver/main.java +++ b/src/java/com/wasteinformationserver/main.java @@ -28,7 +28,8 @@ public class main { Log.message("initial login to db"); new Thread(() -> { try { - JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); + JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306); + //JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); } catch (IOException e) { //e.printStackTrace(); Log.error("no connection to db"); diff --git a/src/java/com/wasteinformationserver/website/Webserver.java b/src/java/com/wasteinformationserver/website/Webserver.java index 3006404..49369e3 100644 --- a/src/java/com/wasteinformationserver/website/Webserver.java +++ b/src/java/com/wasteinformationserver/website/Webserver.java @@ -23,12 +23,13 @@ public class Webserver { e.printStackTrace(); } + server.createContext("/", new MainPage()); server.createContext("/senddata/loginget", new LoginRequest()); - server.createContext("/senddata/registerpost",new RegisterRequest()); - server.createContext("/senddata/checkloginstate",new CheckLoginState()); - server.createContext("/senddata/wastedata",new DataRequest()); + server.createContext("/senddata/registerpost", new RegisterRequest()); + server.createContext("/senddata/checkloginstate", new CheckLoginState()); + server.createContext("/senddata/wastedata", new DataRequest()); server.createContext("/senddata/admindata", new AdminRequests()); server.createContext("/senddata/newdate", new NewDateRequest()); @@ -36,5 +37,63 @@ public class Webserver { server.start(); Log.info("Server available at http://127.0.0.1:8000 now"); + /* + + try { + server = HttpsServer.create(new InetSocketAddress(8000), 0); + + // initialise the HTTPS server + SSLContext sslContext = SSLContext.getInstance("TLS"); + + // initialise the keystore + char[] password = "password".toCharArray(); + KeyStore ks = KeyStore.getInstance("JKS"); + FileInputStream fis = new FileInputStream("testkey.jks"); + ks.load(fis, password); + + // setup the key manager factory + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, password); + + // setup the trust manager factory + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + + // setup the HTTPS context and parameters + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { + public void configure(HttpsParameters params) { + try { + // initialise the SSL context + SSLContext context = getSSLContext(); + SSLEngine engine = context.createSSLEngine(); + params.setNeedClientAuth(false); + params.setCipherSuites(engine.getEnabledCipherSuites()); + params.setProtocols(engine.getEnabledProtocols()); + + // Set the SSL parameters + SSLParameters sslParameters = context.getSupportedSSLParameters(); + params.setSSLParameters(sslParameters); + + } catch (Exception ex) { + System.out.println("Failed to create HTTPS port"); + } + } + }); + } catch (IOException e) { + e.printStackTrace(); + } catch (CertificateException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (UnrecoverableKeyException e) { + e.printStackTrace(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } + */ + } } diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java index ebeb75a..128f1d1 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java @@ -15,7 +15,7 @@ public class LoginState { private String email; private int permission; - boolean loggedin = false; + boolean loggedin = true; public void logIn(){ loggedin=true; diff --git a/src/resources/wwwroot/index.html b/src/resources/wwwroot/index.html index 969a759..9d58a73 100644 --- a/src/resources/wwwroot/index.html +++ b/src/resources/wwwroot/index.html @@ -26,6 +26,8 @@ + +
diff --git a/src/resources/wwwroot/js/index.js b/src/resources/wwwroot/js/index.js index 6cd1a42..4364878 100644 --- a/src/resources/wwwroot/js/index.js +++ b/src/resources/wwwroot/js/index.js @@ -25,4 +25,20 @@ $(document).ready(function () { } }, 'json'); }); + + + //register pwa + async function registerSW() { + console.log("registering service worker!"); + if ('serviceWorker' in navigator) { + try { + await navigator.serviceWorker.register('/sw.js'); + } catch (e) { + console.log(`SW registration failed`); + } + } + } + + registerSW(); }); + diff --git a/src/resources/wwwroot/manifest.json b/src/resources/wwwroot/manifest.json new file mode 100644 index 0000000..84a6219 --- /dev/null +++ b/src/resources/wwwroot/manifest.json @@ -0,0 +1,15 @@ +{ + "name": "Waste Information Server", + "short_name": "WasteInformationServer", + "start_url": "/", + "display": "standalone", + "background_color": "#fff", + "description": "A web app to manage waste pickups", + "icons": [ + { + "src": "wasteicon.png", + "sizes": "512x512", + "type": "image/png" + } + ] +} \ No newline at end of file diff --git a/src/resources/wwwroot/sw.js b/src/resources/wwwroot/sw.js new file mode 100644 index 0000000..3a3a004 --- /dev/null +++ b/src/resources/wwwroot/sw.js @@ -0,0 +1,59 @@ +const cacheName = 'static-v2'; +const staticAssets = [ + '/', + '/index.html', + '/css/index.css', + '/js/index.js', + '/manifest.json', + '/lib/bootstrap.min.css', + '/lib/bootstrap.min.js', + '/lib/jquery.min.js', + '/lib/popper.min.js', + '/rsc/login2.jpg', + '/wasteicon.png' +]; + +self.addEventListener('install', async e => { + console.log('V1 installing…'); + + e.waitUntil( + caches.open('static-v2').then(cache => cache.addAll(staticAssets)) + ); + + // const cache = await caches.open(cacheName); + // await cache.addAll(staticAssets); + // return self.skipWaiting(); +}); + +self.addEventListener('activate', e => { + self.clients.claim(); +}); + +self.addEventListener('fetch', async e => { + const req = e.request; + const url = new URL(req.url); + + if (url.origin === location.origin) { + e.respondWith(cacheFirst(req)); + } else { + e.respondWith(networkAndCache(req)); + } +}); + +async function cacheFirst(req) { + const cache = await caches.open(cacheName); + const cached = await cache.match(req); + return cached || fetch(req); +} + +async function networkAndCache(req) { + const cache = await caches.open(cacheName); + try { + const fresh = await fetch(req); + await cache.put(req, fresh.clone()); + return fresh; + } catch (e) { + const cached = await cache.match(req); + return cached; + } +} \ No newline at end of file