mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-22 07:12:19 +00:00
Fixed signature serializing/deserializing and fixed null-checking when publishUsername
This commit is contained in:
parent
99409e1a13
commit
93a2271be6
@ -5,32 +5,31 @@ import com.sleepycat.bind.EntryBinding
|
|||||||
import com.sleepycat.je.DatabaseEntry
|
import com.sleepycat.je.DatabaseEntry
|
||||||
import io.netty.buffer.Unpooled
|
import io.netty.buffer.Unpooled
|
||||||
import net.tomp2p.connection.SignatureFactory
|
import net.tomp2p.connection.SignatureFactory
|
||||||
import net.tomp2p.storage.AlternativeCompositeByteBuf
|
|
||||||
import net.tomp2p.storage.Data
|
import net.tomp2p.storage.Data
|
||||||
import org.mapdb.DataInput2
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.security.InvalidKeyException
|
import java.security.InvalidKeyException
|
||||||
import java.security.SignatureException
|
import java.security.SignatureException
|
||||||
|
|
||||||
class DataSerializerEx(private val signatureFactory: SignatureFactory) : EntryBinding<Data>, Serializable {
|
|
||||||
private val LOG_TAG = "DataSerializerEx"
|
class DataSerializer(private val signatureFactory: SignatureFactory) : EntryBinding<Data>, Serializable {
|
||||||
|
private val LOG_TAG = "DataSerializer"
|
||||||
|
|
||||||
override fun entryToObject(databaseEntry: DatabaseEntry): Data? {
|
override fun entryToObject(databaseEntry: DatabaseEntry): Data? {
|
||||||
if (databaseEntry.data == null) {
|
if (databaseEntry.data == null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val inputStream = ByteArrayInputStream(databaseEntry.data)
|
val dataInput = DataInputStream(ByteArrayInputStream(databaseEntry.data))
|
||||||
var buf = Unpooled.buffer()
|
var buf = Unpooled.buffer()
|
||||||
var data: Data? = null
|
var data: Data? = null
|
||||||
while (data == null) {
|
while (data == null) {
|
||||||
buf.writeByte(inputStream.read())
|
buf.writeByte(dataInput.read())
|
||||||
data = Data.decodeHeader(buf, signatureFactory)
|
data = Data.decodeHeader(buf, signatureFactory)
|
||||||
}
|
}
|
||||||
val len = data.length()
|
val len = data.length()
|
||||||
var me = ByteArray(len)
|
var me = ByteArray(len)
|
||||||
try {
|
try {
|
||||||
inputStream.read(me)
|
dataInput.read(me)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
@ -40,21 +39,23 @@ class DataSerializerEx(private val signatureFactory: SignatureFactory) : EntryBi
|
|||||||
if (!retVal) {
|
if (!retVal) {
|
||||||
Log.e(LOG_TAG, "# ERROR: Data could not be deserialized!")
|
Log.e(LOG_TAG, "# ERROR: Data could not be deserialized!")
|
||||||
}
|
}
|
||||||
val dataInput = DataInputStream(inputStream)
|
if (data.isSigned) {
|
||||||
me = ByteArray(signatureFactory.signatureSize())
|
me = ByteArray(signatureFactory.signatureSize())
|
||||||
dataInput.readFully(me)
|
dataInput.readFully(me)
|
||||||
buf = Unpooled.wrappedBuffer(me)
|
buf = Unpooled.wrappedBuffer(me)
|
||||||
|
}
|
||||||
retVal = data.decodeDone(buf, signatureFactory);
|
retVal = data.decodeDone(buf, signatureFactory);
|
||||||
if(!retVal) {
|
if(!retVal) {
|
||||||
throw IOException("signature could not be read")
|
throw IOException("Signature could not be read!")
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun objectToEntry(data: Data, databaseEntry: DatabaseEntry) {
|
override fun objectToEntry(data: Data, databaseEntry: DatabaseEntry) {
|
||||||
val out = ByteArrayOutputStream()
|
val baos = ByteArrayOutputStream()
|
||||||
val acb = AlternativeCompositeByteBuf.compBuffer(AlternativeCompositeByteBuf.UNPOOLED_HEAP)
|
val out = DataOutputStream(baos)
|
||||||
try {
|
val acb = Unpooled.buffer()
|
||||||
|
// store data to disk
|
||||||
// header first
|
// header first
|
||||||
data.encodeHeader(acb, signatureFactory)
|
data.encodeHeader(acb, signatureFactory)
|
||||||
writeData(out, acb.nioBuffers())
|
writeData(out, acb.nioBuffers())
|
||||||
@ -62,15 +63,18 @@ class DataSerializerEx(private val signatureFactory: SignatureFactory) : EntryBi
|
|||||||
// next data - no need to copy to another buffer, just take the data
|
// next data - no need to copy to another buffer, just take the data
|
||||||
// from memory
|
// from memory
|
||||||
writeData(out, data.toByteBuffers())
|
writeData(out, data.toByteBuffers())
|
||||||
} catch (e: SignatureException) {
|
// rest
|
||||||
e.printStackTrace()
|
try {
|
||||||
|
data.encodeDone(acb, signatureFactory)
|
||||||
|
writeData(out, acb.nioBuffers())
|
||||||
} catch (e: InvalidKeyException) {
|
} catch (e: InvalidKeyException) {
|
||||||
e.printStackTrace()
|
throw IOException(e)
|
||||||
} catch (e: IOException) {
|
} catch (e: SignatureException) {
|
||||||
e.printStackTrace()
|
throw IOException(e)
|
||||||
}
|
}
|
||||||
|
out.flush()
|
||||||
databaseEntry.data = out.toByteArray()
|
databaseEntry.data = baos.toByteArray()
|
||||||
|
out.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
|
@ -9,6 +9,8 @@ import com.google.gson.JsonObject;
|
|||||||
|
|
||||||
import net.tomp2p.connection.Bindings;
|
import net.tomp2p.connection.Bindings;
|
||||||
import net.tomp2p.connection.ChannelClientConfiguration;
|
import net.tomp2p.connection.ChannelClientConfiguration;
|
||||||
|
import net.tomp2p.connection.ChannelServerConfiguration;
|
||||||
|
import net.tomp2p.connection.Ports;
|
||||||
import net.tomp2p.connection.RSASignatureFactory;
|
import net.tomp2p.connection.RSASignatureFactory;
|
||||||
import net.tomp2p.dht.PeerBuilderDHT;
|
import net.tomp2p.dht.PeerBuilderDHT;
|
||||||
import net.tomp2p.dht.PeerDHT;
|
import net.tomp2p.dht.PeerDHT;
|
||||||
@ -21,9 +23,7 @@ import net.tomp2p.p2p.PeerBuilder;
|
|||||||
import net.tomp2p.peers.Number160;
|
import net.tomp2p.peers.Number160;
|
||||||
import net.tomp2p.peers.PeerAddress;
|
import net.tomp2p.peers.PeerAddress;
|
||||||
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
||||||
import net.tomp2p.replication.AutoReplication;
|
|
||||||
import net.tomp2p.replication.IndirectReplication;
|
import net.tomp2p.replication.IndirectReplication;
|
||||||
import net.tomp2p.replication.Replication;
|
|
||||||
import net.tomp2p.storage.Data;
|
import net.tomp2p.storage.Data;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -89,6 +89,7 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
|
|||||||
new PeerBuilder(peerID)
|
new PeerBuilder(peerID)
|
||||||
.ports(7243)
|
.ports(7243)
|
||||||
.channelClientConfiguration(createChannelClientConfig())
|
.channelClientConfiguration(createChannelClientConfig())
|
||||||
|
.channelServerConfiguration(createChannelServerConfig())
|
||||||
.start()
|
.start()
|
||||||
)
|
)
|
||||||
.storage(storageBerkeleyDB)
|
.storage(storageBerkeleyDB)
|
||||||
@ -257,7 +258,7 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
Log.i(LOG_TAG, P2PUtils.put(AppHelper.getPeerID() + "_profile", null, serializedUserProfile) ? "# Profile successfully published!" : "# Profile publishing failed!");
|
Log.i(LOG_TAG, P2PUtils.put(AppHelper.getPeerID() + "_profile", null, serializedUserProfile, mainKeyPair) ? "# Profile successfully published!" : "# Profile publishing failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ChannelClientConfiguration createChannelClientConfig() {
|
private ChannelClientConfiguration createChannelClientConfig() {
|
||||||
@ -273,4 +274,17 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
|
|||||||
channelClientConfiguration.byteBufPool(false);
|
channelClientConfiguration.byteBufPool(false);
|
||||||
return channelClientConfiguration;
|
return channelClientConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ChannelServerConfiguration createChannelServerConfig() {
|
||||||
|
ChannelServerConfiguration channelServerConfiguration = new ChannelServerConfiguration();
|
||||||
|
channelServerConfiguration.bindings(new Bindings());
|
||||||
|
//these two values may be overwritten in the peer builder
|
||||||
|
channelServerConfiguration.ports(new Ports(Ports.DEFAULT_PORT, Ports.DEFAULT_PORT));
|
||||||
|
channelServerConfiguration.portsForwarding(new Ports(Ports.DEFAULT_PORT, Ports.DEFAULT_PORT));
|
||||||
|
channelServerConfiguration.behindFirewall(false);
|
||||||
|
channelServerConfiguration.pipelineFilter(new PeerBuilder.DefaultPipelineFilter());
|
||||||
|
channelServerConfiguration.signatureFactory(new RSASignatureFactory());
|
||||||
|
channelServerConfiguration.byteBufPool(false);
|
||||||
|
return channelServerConfiguration;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,13 @@ class SettingsLogic : CoreContracts.ISettingsLogic {
|
|||||||
|
|
||||||
fun publishUsername(oldUsername: String?, username: String?) {
|
fun publishUsername(oldUsername: String?, username: String?) {
|
||||||
val mainKeyPair = keyPairManager.openMainKeyPair()
|
val mainKeyPair = keyPairManager.openMainKeyPair()
|
||||||
if(oldUsername != null || !oldUsername.equals("")) {
|
oldUsername?.let {
|
||||||
|
if(!oldUsername.equals("")) {
|
||||||
P2PUtils.remove(oldUsername, null, mainKeyPair)
|
P2PUtils.remove(oldUsername, null, mainKeyPair)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (username.equals("") || username == null) {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
username?.let {
|
||||||
var data: Data? = null
|
var data: Data? = null
|
||||||
try {
|
try {
|
||||||
data = Data(AppHelper.getPeerID())
|
data = Data(AppHelper.getPeerID())
|
||||||
@ -41,6 +41,9 @@ class SettingsLogic : CoreContracts.ISettingsLogic {
|
|||||||
|
|
||||||
val isSuccess = P2PUtils.put(username, null, data, mainKeyPair)
|
val isSuccess = P2PUtils.put(username, null, data, mainKeyPair)
|
||||||
Log.i(LOG_TAG, if (isSuccess) "Username $username is published!" else "Username $username isn't published!")
|
Log.i(LOG_TAG, if (isSuccess) "Username $username is published!" else "Username $username isn't published!")
|
||||||
|
} ?: run {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user