diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java index 205154c..7237e79 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java @@ -122,6 +122,7 @@ public class JabitServerConfig { @Bean public Set admins() { + security(); return Utils.readOrCreateList( ADMIN_LIST, "# Admins can send commands to the server.\n" diff --git a/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java b/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java index eafe85c..ab98b10 100644 --- a/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java +++ b/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java @@ -27,6 +27,7 @@ import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest; import ch.dissem.bitmessage.ports.CustomCommandHandler; import ch.dissem.bitmessage.ports.ProofOfWorkEngine; import ch.dissem.bitmessage.server.repository.ServerProofOfWorkRepository; +import ch.dissem.bitmessage.utils.UnixTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,7 @@ import java.util.stream.Collectors; import static ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest.Request.CALCULATING; import static ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest.Request.COMPLETE; +import static ch.dissem.bitmessage.utils.UnixTime.DAY; /** * @author Christian Basler @@ -61,6 +63,12 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal doMissingProofOfWork(); } }, 15_000); // After 15 seconds + new Timer().schedule(new TimerTask() { + @Override + public void run() { + repo.cleanupTasks(7 * DAY); + } + }, 60_000, DAY * 1000); // First time after 1 minute, then daily } public void doMissingProofOfWork() { @@ -77,11 +85,12 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal CryptoCustomMessage cryptoMessage = CryptoCustomMessage.read(message, ProofOfWorkRequest::read); ProofOfWorkRequest request = decrypt(cryptoMessage); - if (request == null) + if (request == null) { return CustomMessage.error( "Unknown sender. Please ask the server's administrator to add you as a client. " + "For this he'll need your identity." ); + } switch (request.getRequest()) { case CALCULATE: if (!repo.hasTask(request.getInitialHash())) { diff --git a/src/main/java/ch/dissem/bitmessage/server/repository/ServerProofOfWorkRepository.java b/src/main/java/ch/dissem/bitmessage/server/repository/ServerProofOfWorkRepository.java index e37e200..c4af9b2 100644 --- a/src/main/java/ch/dissem/bitmessage/server/repository/ServerProofOfWorkRepository.java +++ b/src/main/java/ch/dissem/bitmessage/server/repository/ServerProofOfWorkRepository.java @@ -19,6 +19,7 @@ package ch.dissem.bitmessage.server.repository; import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest; import ch.dissem.bitmessage.repository.JdbcConfig; import ch.dissem.bitmessage.repository.JdbcHelper; +import ch.dissem.bitmessage.utils.UnixTime; import java.sql.Connection; import java.sql.PreparedStatement; @@ -46,10 +47,11 @@ public class ServerProofOfWorkRepository extends JdbcHelper { public void storeTask(ProofOfWorkRequest request) { try (Connection connection = config.getConnection()) { PreparedStatement ps = connection.prepareStatement( - "INSERT INTO ProofOfWorkTask (initial_hash, client, target) VALUES (?, ?, ?)"); + "INSERT INTO ProofOfWorkTask (initial_hash, client, target, timestamp) VALUES (?, ?, ?, ?)"); ps.setBytes(1, request.getInitialHash()); ps.setString(2, request.getSender().getAddress()); ps.setBytes(3, request.getData()); + ps.setLong(4, UnixTime.now()); ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); @@ -112,6 +114,17 @@ public class ServerProofOfWorkRepository extends JdbcHelper { } } + public void cleanupTasks(long ageInSeconds) { + try (Connection connection = config.getConnection()) { + PreparedStatement ps = connection.prepareStatement( + "DELETE FROM ProofOfWorkTask WHERE timestamp < ?"); + ps.setLong(1, UnixTime.now(-ageInSeconds)); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static class Task { public final byte[] initialHash; public final byte[] target; diff --git a/src/main/resources/db/migration/V1.2.1__ProofOfWorkTask.sql b/src/main/resources/db/migration/V1.2.1__ProofOfWorkTask.sql index 40d3135..0561d9d 100644 --- a/src/main/resources/db/migration/V1.2.1__ProofOfWorkTask.sql +++ b/src/main/resources/db/migration/V1.2.1__ProofOfWorkTask.sql @@ -1,7 +1,7 @@ CREATE TABLE ProofOfWorkTask ( - initial_hash BINARY(64) NOT NULL PRIMARY KEY, - client VARCHAR(40) NOT NULL, + initial_hash BINARY(64) NOT NULL PRIMARY KEY, + client VARCHAR(40) NOT NULL, target BINARY(32), nonce BINARY(8), - status VARCHAR(20), + timestamp BIGINT NOT NULL, ); \ No newline at end of file