2015-05-19 19:16:20 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
|
|
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext;
|
|
|
|
import ch.dissem.bitmessage.entity.payload.*;
|
2015-05-29 13:17:00 +02:00
|
|
|
import ch.dissem.bitmessage.entity.valueobject.Label;
|
2015-05-23 10:27:05 +02:00
|
|
|
import ch.dissem.bitmessage.exception.DecryptionFailedException;
|
2015-05-19 19:16:20 +02:00
|
|
|
import ch.dissem.bitmessage.ports.NetworkHandler;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2015-06-12 06:57:20 +02:00
|
|
|
import java.util.Arrays;
|
2015-05-19 19:16:20 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-05-29 13:17:00 +02:00
|
|
|
import static ch.dissem.bitmessage.entity.Plaintext.Status.*;
|
2015-05-19 19:16:20 +02:00
|
|
|
import static ch.dissem.bitmessage.utils.UnixTime.DAY;
|
|
|
|
|
|
|
|
class DefaultMessageListener implements NetworkHandler.MessageListener {
|
|
|
|
private final static Logger LOG = LoggerFactory.getLogger(DefaultMessageListener.class);
|
|
|
|
private final InternalContext ctx;
|
|
|
|
private final BitmessageContext.Listener listener;
|
|
|
|
|
|
|
|
public DefaultMessageListener(InternalContext context, BitmessageContext.Listener listener) {
|
|
|
|
this.ctx = context;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-23 10:27:05 +02:00
|
|
|
public void receive(ObjectMessage object) throws IOException {
|
2015-05-19 19:16:20 +02:00
|
|
|
ObjectPayload payload = object.getPayload();
|
2015-05-22 20:51:57 +02:00
|
|
|
if (payload.getType() == null) return;
|
|
|
|
|
2015-05-19 19:16:20 +02:00
|
|
|
switch (payload.getType()) {
|
|
|
|
case GET_PUBKEY: {
|
|
|
|
receive(object, (GetPubkey) payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PUBKEY: {
|
|
|
|
receive(object, (Pubkey) payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MSG: {
|
|
|
|
receive(object, (Msg) payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BROADCAST: {
|
|
|
|
receive(object, (Broadcast) payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void receive(ObjectMessage object, GetPubkey getPubkey) {
|
2015-12-08 20:27:32 +01:00
|
|
|
BitmessageAddress identity = ctx.getAddressRepository().findIdentity(getPubkey.getRipeTag());
|
2015-05-19 19:16:20 +02:00
|
|
|
if (identity != null && identity.getPrivateKey() != null) {
|
2015-11-08 10:14:37 +01:00
|
|
|
LOG.info("Got pubkey request for identity " + identity);
|
|
|
|
// FIXME: only send pubkey if it wasn't sent in the last 28 days
|
2015-05-22 20:51:57 +02:00
|
|
|
ctx.sendPubkey(identity, object.getStream());
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 10:27:05 +02:00
|
|
|
protected void receive(ObjectMessage object, Pubkey pubkey) throws IOException {
|
2015-05-19 19:16:20 +02:00
|
|
|
BitmessageAddress address;
|
|
|
|
try {
|
|
|
|
if (pubkey instanceof V4Pubkey) {
|
|
|
|
V4Pubkey v4Pubkey = (V4Pubkey) pubkey;
|
2015-12-08 20:27:32 +01:00
|
|
|
address = ctx.getAddressRepository().findContact(v4Pubkey.getTag());
|
2015-05-19 19:16:20 +02:00
|
|
|
if (address != null) {
|
2015-06-09 22:45:24 +02:00
|
|
|
v4Pubkey.decrypt(address.getPublicDecryptionKey());
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-08 20:27:32 +01:00
|
|
|
address = ctx.getAddressRepository().findContact(pubkey.getRipe());
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
if (address != null) {
|
2016-01-21 20:32:23 +01:00
|
|
|
updatePubkey(address, pubkey);
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
2015-05-23 10:27:05 +02:00
|
|
|
} catch (DecryptionFailedException ignore) {
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:32:23 +01:00
|
|
|
private void updatePubkey(BitmessageAddress address, Pubkey pubkey){
|
|
|
|
address.setPubkey(pubkey);
|
|
|
|
LOG.info("Got pubkey for contact " + address);
|
|
|
|
ctx.getAddressRepository().save(address);
|
|
|
|
List<Plaintext> messages = ctx.getMessageRepository().findMessages(Plaintext.Status.PUBKEY_REQUESTED, address);
|
|
|
|
LOG.info("Sending " + messages.size() + " messages for contact " + address);
|
|
|
|
for (Plaintext msg : messages) {
|
|
|
|
msg.setStatus(DOING_PROOF_OF_WORK);
|
|
|
|
ctx.getMessageRepository().save(msg);
|
|
|
|
ctx.send(
|
|
|
|
msg.getFrom(),
|
|
|
|
msg.getTo(),
|
|
|
|
new Msg(msg),
|
|
|
|
+2 * DAY
|
|
|
|
);
|
|
|
|
msg.setStatus(SENT);
|
|
|
|
ctx.getMessageRepository().save(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 10:27:05 +02:00
|
|
|
protected void receive(ObjectMessage object, Msg msg) throws IOException {
|
2015-12-08 20:27:32 +01:00
|
|
|
for (BitmessageAddress identity : ctx.getAddressRepository().getIdentities()) {
|
2015-05-19 19:16:20 +02:00
|
|
|
try {
|
|
|
|
msg.decrypt(identity.getPrivateKey().getPrivateEncryptionKey());
|
|
|
|
msg.getPlaintext().setTo(identity);
|
2015-06-09 22:45:24 +02:00
|
|
|
if (!object.isSignatureValid(msg.getPlaintext().getFrom().getPubkey())) {
|
|
|
|
LOG.warn("Msg with IV " + object.getInventoryVector() + " was successfully decrypted, but signature check failed. Ignoring.");
|
|
|
|
} else {
|
|
|
|
msg.getPlaintext().setStatus(RECEIVED);
|
|
|
|
msg.getPlaintext().addLabels(ctx.getMessageRepository().getLabels(Label.Type.INBOX, Label.Type.UNREAD));
|
|
|
|
msg.getPlaintext().setInventoryVector(object.getInventoryVector());
|
|
|
|
ctx.getMessageRepository().save(msg.getPlaintext());
|
|
|
|
listener.receive(msg.getPlaintext());
|
2016-01-21 20:32:23 +01:00
|
|
|
updatePubkey(msg.getPlaintext().getFrom(), msg.getPlaintext().getFrom().getPubkey());
|
2015-06-09 22:45:24 +02:00
|
|
|
}
|
2015-05-19 19:16:20 +02:00
|
|
|
break;
|
2015-05-23 10:27:05 +02:00
|
|
|
} catch (DecryptionFailedException ignore) {
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 10:27:05 +02:00
|
|
|
protected void receive(ObjectMessage object, Broadcast broadcast) throws IOException {
|
2015-06-12 06:57:20 +02:00
|
|
|
byte[] tag = broadcast instanceof V5Broadcast ? ((V5Broadcast) broadcast).getTag() : null;
|
2015-12-08 20:27:32 +01:00
|
|
|
for (BitmessageAddress subscription : ctx.getAddressRepository().getSubscriptions(broadcast.getVersion())) {
|
2015-06-12 06:57:20 +02:00
|
|
|
if (tag != null && !Arrays.equals(tag, subscription.getTag())) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-19 19:16:20 +02:00
|
|
|
try {
|
2015-06-09 22:45:24 +02:00
|
|
|
broadcast.decrypt(subscription.getPublicDecryptionKey());
|
|
|
|
if (!object.isSignatureValid(broadcast.getPlaintext().getFrom().getPubkey())) {
|
|
|
|
LOG.warn("Broadcast with IV " + object.getInventoryVector() + " was successfully decrypted, but signature check failed. Ignoring.");
|
|
|
|
} else {
|
2015-06-12 06:57:20 +02:00
|
|
|
broadcast.getPlaintext().setStatus(RECEIVED);
|
2015-06-18 13:41:11 +02:00
|
|
|
broadcast.getPlaintext().addLabels(ctx.getMessageRepository().getLabels(Label.Type.INBOX, Label.Type.BROADCAST, Label.Type.UNREAD));
|
2015-06-09 22:45:24 +02:00
|
|
|
broadcast.getPlaintext().setInventoryVector(object.getInventoryVector());
|
2015-06-12 06:57:20 +02:00
|
|
|
ctx.getMessageRepository().save(broadcast.getPlaintext());
|
2015-06-09 22:45:24 +02:00
|
|
|
listener.receive(broadcast.getPlaintext());
|
2016-01-21 20:32:23 +01:00
|
|
|
updatePubkey(broadcast.getPlaintext().getFrom(), broadcast.getPlaintext().getFrom().getPubkey());
|
2015-06-09 22:45:24 +02:00
|
|
|
}
|
2015-05-23 10:27:05 +02:00
|
|
|
} catch (DecryptionFailedException ignore) {
|
2015-05-19 19:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|