From 21b0e64130bd8c314188bbbc87ecea7baef015f0 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 10 May 2017 07:39:09 +0200 Subject: [PATCH] Fixed Jabit Server, 'status' request should now work --- build.gradle | 4 +- .../dissem/bitmessage/server/CleanupJob.java | 44 --------------- .../server/JabitServerApplication.java | 2 +- .../bitmessage/server/JabitServerConfig.java | 4 +- .../server/JabitServerController.java | 15 ++++- .../bitmessage/server/ServerListener.java | 56 +++++++++++++++++-- src/test/resources/application.properties | 3 +- 7 files changed, 70 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/ch/dissem/bitmessage/server/CleanupJob.java diff --git a/build.gradle b/build.gradle index ff01ea1..e7eb4ff 100644 --- a/build.gradle +++ b/build.gradle @@ -44,10 +44,10 @@ dependencies { compile "ch.dissem.jabit:jabit-core:$jabitVersion" compile "ch.dissem.jabit:jabit-networking:$jabitVersion" compile "ch.dissem.jabit:jabit-repositories:$jabitVersion" - compile "ch.dissem.jabit:jabit-cryptography-spongy:$jabitVersion" + compile "ch.dissem.jabit:jabit-cryptography-bouncy:$jabitVersion" compile "ch.dissem.jabit:jabit-extensions:$jabitVersion" - compile 'com.h2database:h2:1.4.187' + compile 'com.h2database:h2:1.4.194' providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") testCompile("org.springframework.boot:spring-boot-starter-test") diff --git a/src/main/java/ch/dissem/bitmessage/server/CleanupJob.java b/src/main/java/ch/dissem/bitmessage/server/CleanupJob.java deleted file mode 100644 index 2294d42..0000000 --- a/src/main/java/ch/dissem/bitmessage/server/CleanupJob.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2015 Christian Basler - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.dissem.bitmessage.server; - -import ch.dissem.bitmessage.BitmessageContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.TimerTask; - -/** - * @author Christian Basler - */ -public class CleanupJob extends TimerTask { - private static final Logger LOG = LoggerFactory.getLogger(CleanupJob.class); - private final BitmessageContext ctx; - - public CleanupJob(BitmessageContext ctx) { - this.ctx = ctx; - } - - @Override - public void run() { - try { - ctx.cleanup(); - } catch (Throwable t) { - LOG.error("Problem while cleaning inventory", t); - } - } -} diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java index 83338ac..740d90f 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java @@ -23,7 +23,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 -@ComponentScan(basePackageClasses = JabitServerController.class) +@ComponentScan(basePackageClasses = JabitServerApplication.class) public class JabitServerApplication { public static void main(String[] args) { diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java index 1ac3866..3d256d5 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java @@ -17,7 +17,7 @@ package ch.dissem.bitmessage.server; import ch.dissem.bitmessage.BitmessageContext; -import ch.dissem.bitmessage.cryptography.sc.SpongyCryptography; +import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.payload.Pubkey; import ch.dissem.bitmessage.networking.nio.NioNetworkHandler; @@ -96,7 +96,7 @@ public class JabitServerConfig { @Bean public Cryptography cryptography() { - Cryptography cryptography = new SpongyCryptography(); + Cryptography cryptography = new BouncyCryptography(); Singleton.initialize(cryptography); // needed for admins and clients return cryptography; } diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerController.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerController.java index 6607955..3812cc4 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerController.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerController.java @@ -22,6 +22,7 @@ import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.server.entities.Broadcasts; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -32,7 +33,6 @@ import javax.annotation.Resource; import javax.inject.Inject; import java.util.List; import java.util.Set; -import java.util.Timer; import static ch.dissem.bitmessage.server.Converter.broadcasts; import static ch.dissem.bitmessage.server.Converter.message; @@ -60,6 +60,8 @@ public class JabitServerController { @Resource private Set blacklist; @Inject + private BitmessageAddress identity; + @Inject private BitmessageContext ctx; @RequestMapping(value = "status", method = GET, produces = "application/json") @@ -104,9 +106,18 @@ public class JabitServerController { return broadcasts(broadcaster, messages); } + @Scheduled(cron = "0 0 0 * * *") + public void broadcastStatus() { + ctx.broadcast(identity, "Status", ctx.status().toString()); + } + + @Scheduled(cron = "0 0 2 * * *") + public void cleanup() { + ctx.cleanup(); + } + @PostConstruct public void setUp() { ctx.startup(); - new Timer().scheduleAtFixedRate(new CleanupJob(ctx), 1 * HOUR, 24 * HOUR); } } diff --git a/src/main/java/ch/dissem/bitmessage/server/ServerListener.java b/src/main/java/ch/dissem/bitmessage/server/ServerListener.java index 228c6bc..4959965 100644 --- a/src/main/java/ch/dissem/bitmessage/server/ServerListener.java +++ b/src/main/java/ch/dissem/bitmessage/server/ServerListener.java @@ -19,18 +19,22 @@ package ch.dissem.bitmessage.server; import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; +import ch.dissem.bitmessage.entity.valueobject.extended.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; +import java.util.List; import java.util.Scanner; +import static ch.dissem.bitmessage.entity.Plaintext.Encoding.EXTENDED; +import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.server.Constants.*; /** * @author Christian Basler */ -public class ServerListener implements BitmessageContext.Listener { +public class ServerListener implements BitmessageContext.Listener.WithContext { private final static Logger LOG = LoggerFactory.getLogger(ServerListener.class); private final Collection admins; @@ -40,6 +44,9 @@ public class ServerListener implements BitmessageContext.Listener { private final Collection shortlist; private final Collection blacklist; + private BitmessageContext ctx; + private BitmessageAddress identity; + public ServerListener(Collection admins, Collection clients, Collection whitelist, @@ -52,13 +59,50 @@ public class ServerListener implements BitmessageContext.Listener { this.blacklist = blacklist; } + @Override + public void setContext(BitmessageContext ctx) { + this.ctx = ctx; + } + + private BitmessageAddress getIdentity() { + if (identity == null) { + List identities = ctx.addresses().getIdentities(); + if (!identities.isEmpty()) { + identity = identities.get(0); + } + } + return identity; + } + @Override public void receive(Plaintext message) { if (admins.contains(message.getFrom())) { String[] command = message.getSubject().trim().toLowerCase().split("\\s+"); String data = message.getText(); - if (command.length == 2) { - switch (command[1]) { + if (command.length == 1) { + switch (command[0].toLowerCase()) { + case "status": + Plaintext.Builder response = new Plaintext.Builder(MSG); + response.from(getIdentity()); + response.to(message.getFrom()); + if (message.getEncoding() == EXTENDED) { + response.message( + new Message.Builder() + .subject("RE: status") + .body(ctx.status().toString()) + .addParent(message) + .build() + ); + } else { + response.message("RE: status", ctx.status().toString()); + } + ctx.send(response.build()); + break; + default: + LOG.info("ignoring unknown command " + message.getSubject()); + } + } else if (command.length == 2) { + switch (command[1].toLowerCase()) { case "client": case "clients": updateUserList(CLIENT_LIST, clients, command[0], data); @@ -79,14 +123,14 @@ public class ServerListener implements BitmessageContext.Listener { updateList(BLACKLIST, blacklist, command[0], data); break; default: - LOG.trace("ignoring unknown command " + message.getSubject()); + LOG.info("ignoring unknown command " + message.getSubject()); } } } } private void updateUserList(String file, Collection list, String command, String data) { - switch (command) { + switch (command.toLowerCase()) { case "set": list.clear(); case "add": @@ -111,7 +155,7 @@ public class ServerListener implements BitmessageContext.Listener { } private void updateList(String file, Collection list, String command, String data) { - switch (command) { + switch (command.toLowerCase()) { case "set": list.clear(); case "add": diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 9366013..fb4aa02 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,6 +1,7 @@ logging.file=jabit.log logging.level.*=DEBUG -logging.level.ch.dissem.*=WARN +logging.level.ch.dissem.*=DEBUG bitmessage.port=18444 +database.url=jdbc:h2:mem:jabit;DB_CLOSE_DELAY=10 bitmessage.connection.ttl.hours=12 bitmessage.connection.limit=100