Fixed Jabit Server, 'status' request should now work

This commit is contained in:
Christian Basler 2017-05-10 07:39:09 +02:00
parent a1541dd51b
commit 21b0e64130
7 changed files with 70 additions and 58 deletions

View File

@ -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")

View File

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

View File

@ -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) {

View File

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

View File

@ -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<String> 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);
}
}

View File

@ -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<BitmessageAddress> admins;
@ -40,6 +44,9 @@ public class ServerListener implements BitmessageContext.Listener {
private final Collection<String> shortlist;
private final Collection<String> blacklist;
private BitmessageContext ctx;
private BitmessageAddress identity;
public ServerListener(Collection<BitmessageAddress> admins,
Collection<BitmessageAddress> clients,
Collection<String> 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<BitmessageAddress> 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<BitmessageAddress> 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<String> list, String command, String data) {
switch (command) {
switch (command.toLowerCase()) {
case "set":
list.clear();
case "add":

View File

@ -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