From 8d15b5a4f6ba3caea47f47d30458bfef90f0859f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Sk=C3=A1la?= Date: Tue, 25 Feb 2020 22:49:13 +0100 Subject: [PATCH] Prevent discovering own address, enable ktlint for ipv8-android --- .../ipv8/android/demo/DemoApplication.kt | 3 +- ipv8-android/build.gradle | 9 +++++ .../nl/tudelft/ipv8/android/IPv8Android.kt | 3 -- .../peerdiscovery/NetworkServiceDiscovery.kt | 39 +++++++++++-------- .../ipv8/android/service/IPv8Service.kt | 2 - .../ipv8/android/service/StopIPv8Receiver.kt | 1 - .../main/java/nl/tudelft/ipv8/Community.kt | 8 ++-- ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt | 3 ++ 8 files changed, 39 insertions(+), 29 deletions(-) diff --git a/demo-android/src/main/java/nl/tudelft/ipv8/android/demo/DemoApplication.kt b/demo-android/src/main/java/nl/tudelft/ipv8/android/demo/DemoApplication.kt index d87f51af9..650a2fd6f 100644 --- a/demo-android/src/main/java/nl/tudelft/ipv8/android/demo/DemoApplication.kt +++ b/demo-android/src/main/java/nl/tudelft/ipv8/android/demo/DemoApplication.kt @@ -87,10 +87,9 @@ class DemoApplication : Application() { val driver = AndroidSqliteDriver(Database.Schema, this, "trustchain.db") val store = TrustChainSQLiteStore(Database(driver)) val randomWalk = RandomWalk.Factory() - val nsd = NetworkServiceDiscovery.Factory(getSystemService()!!) return OverlayConfiguration( TrustChainCommunity.Factory(settings, store), - listOf(randomWalk, nsd) + listOf(randomWalk) ) } diff --git a/ipv8-android/build.gradle b/ipv8-android/build.gradle index 75ac9dc77..d322667df 100644 --- a/ipv8-android/build.gradle +++ b/ipv8-android/build.gradle @@ -1,6 +1,15 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply plugin: 'org.jlleitschuh.gradle.ktlint' + +ktlint { + version = "$ktlint_version" + android = true + outputToConsole = true + ignoreFailures = true +} + android { compileSdkVersion 29 buildToolsVersion "29.0.2" diff --git a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/IPv8Android.kt b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/IPv8Android.kt index 65bb40e57..0c2f18182 100644 --- a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/IPv8Android.kt +++ b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/IPv8Android.kt @@ -6,14 +6,11 @@ import android.content.Intent import android.net.ConnectivityManager import android.os.Build import androidx.core.content.getSystemService -import androidx.lifecycle.ProcessLifecycleOwner import nl.tudelft.ipv8.IPv8 import nl.tudelft.ipv8.IPv8Configuration import nl.tudelft.ipv8.android.keyvault.AndroidCryptoProvider import nl.tudelft.ipv8.android.messaging.udp.AndroidUdpEndpoint -import nl.tudelft.ipv8.android.peerdiscovery.NetworkServiceDiscovery import nl.tudelft.ipv8.android.service.IPv8Service -import nl.tudelft.ipv8.keyvault.CryptoProvider import nl.tudelft.ipv8.keyvault.PrivateKey import nl.tudelft.ipv8.keyvault.defaultCryptoProvider import java.net.InetAddress diff --git a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/peerdiscovery/NetworkServiceDiscovery.kt b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/peerdiscovery/NetworkServiceDiscovery.kt index f54f3af4f..37393974e 100644 --- a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/peerdiscovery/NetworkServiceDiscovery.kt +++ b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/peerdiscovery/NetworkServiceDiscovery.kt @@ -8,7 +8,7 @@ import nl.tudelft.ipv8.Overlay import nl.tudelft.ipv8.messaging.Packet import nl.tudelft.ipv8.messaging.udp.UdpEndpoint import nl.tudelft.ipv8.peerdiscovery.strategy.DiscoveryStrategy -import java.net.InetAddress +import kotlin.random.Random private val logger = KotlinLogging.logger {} @@ -64,7 +64,6 @@ class NetworkServiceDiscovery( if (serviceInfo.serviceName == serviceName) { logger.debug { "Found its own service" } - return } val serviceId = getServiceId(serviceInfo.serviceName) @@ -112,22 +111,23 @@ class NetworkServiceDiscovery( val peer = overlay.myPeer val address = Address(serviceInfo.host.hostAddress, serviceInfo.port) - logger.debug { "Discovered address: $address" } - - overlay.network.discoverAddress(peer, address, overlay.serviceId) - overlay.walkTo(address) + if (overlay.myEstimatedLan != address) { + logger.debug { "Discovered address: $address" } + overlay.network.discoverAddress(peer, address, overlay.serviceId) + } else { + logger.debug { "Resolved its own IP address" } + } } } } - private fun registerService(port: Int, serviceId: String) { - val serviceInfo = NsdServiceInfo().apply { - // The name is subject to change based on conflicts - // with other services advertised on the same network. - serviceName = serviceId - serviceType = SERVICE_TYPE - setPort(port) - } + private fun registerService(port: Int, serviceName: String) { + val serviceInfo = NsdServiceInfo() + // The name is subject to change based on conflicts + // with other services advertised on the same network. + serviceInfo.serviceName = serviceName + serviceInfo.serviceType = SERVICE_TYPE + serviceInfo.port = port logger.debug { "Registering service info $serviceInfo" } @@ -152,8 +152,9 @@ class NetworkServiceDiscovery( val endpoint = overlay.endpoint if (endpoint is UdpEndpoint) { val socketPort = endpoint.getSocketPort() - logger.debug { "Registering service ${overlay.serviceId} on port $socketPort" } - registerService(socketPort, overlay.serviceId) + val serviceName = overlay.serviceId + "_" + Random.nextInt(10000) + logger.debug { "Registering service $serviceName on port $socketPort" } + registerService(socketPort, serviceName) } else { logger.error { "Overlay endpoint is not UdpEndpoint" } } @@ -161,7 +162,11 @@ class NetworkServiceDiscovery( } override fun takeStep() { - // NOOP + val addresses = overlay.getWalkableAddresses() + if (addresses.isNotEmpty()) { + val address = addresses.random() + overlay.walkTo(address) + } } override fun unload() { diff --git a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt index f8f2f6465..9552b5322 100644 --- a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt +++ b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt @@ -5,10 +5,8 @@ import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.content.Intent -import android.os.Binder import android.os.Build import android.os.IBinder -import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.content.getSystemService import androidx.lifecycle.Lifecycle diff --git a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/StopIPv8Receiver.kt b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/StopIPv8Receiver.kt index 857858451..a3064b273 100644 --- a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/StopIPv8Receiver.kt +++ b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/StopIPv8Receiver.kt @@ -4,7 +4,6 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import nl.tudelft.ipv8.android.IPv8Android -import kotlin.system.exitProcess class StopIPv8Receiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { diff --git a/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt b/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt index 8ff706472..bab29df7a 100644 --- a/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt +++ b/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt @@ -21,8 +21,8 @@ abstract class Community : Overlay { protected val prefix: ByteArray get() = ByteArray(0) + 0.toByte() + VERSION + serviceId.hexToBytes() - var myEstimatedWan: Address = Address.EMPTY - var myEstimatedLan: Address = Address.EMPTY + override var myEstimatedWan: Address = Address.EMPTY + override var myEstimatedLan: Address = Address.EMPTY private var lastBootstrap: Date? = null @@ -432,7 +432,7 @@ abstract class Community : Overlay { } companion object { - val DEFAULT_ADDRESSES = listOf( + val DEFAULT_ADDRESSES = listOf
( // Dispersy // Address("130.161.119.206", 6421), // Address("130.161.119.206", 6422), @@ -449,7 +449,7 @@ abstract class Community : Overlay { // Address("81.171.27.194", 6527), // Address("81.171.27.194", 6528) // py-ipv8 + LibNaCL - // Address("131.180.27.161", 6427), + Address("131.180.27.161", 6427), // kotlin-ipv8 Address("178.62.16.66", 8090) ) diff --git a/ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt b/ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt index 3ebf7d1e9..e7dade687 100644 --- a/ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt +++ b/ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt @@ -17,6 +17,9 @@ interface Overlay : EndpointListener { var maxPeers: Int var cryptoProvider: CryptoProvider + var myEstimatedWan: Address + var myEstimatedLan: Address + private val globalTime: ULong get() = myPeer.lamportTimestamp