From 3a55627ba0c8620e8658011e79bd0d9038446aa4 Mon Sep 17 00:00:00 2001 From: bgrozev Date: Fri, 25 Sep 2020 11:39:13 -0500 Subject: [PATCH] ref: Port StunMappingHarvester config. (#215) --- .../harvest/MappingCandidateHarvesters.java | 38 ++++--------------- .../org/ice4j/ice/harvest/HarvestConfig.kt | 7 ++++ src/main/resources/reference.conf | 8 ++++ .../ice4j/ice/harvest/HarvestConfigTest.kt | 17 ++++++++- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvesters.java b/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvesters.java index 8da9e96..30bebfc 100644 --- a/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvesters.java +++ b/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvesters.java @@ -16,6 +16,7 @@ package org.ice4j.ice.harvest; import org.ice4j.*; +import org.jetbrains.annotations.*; import java.net.*; import java.util.*; @@ -66,16 +67,6 @@ public class MappingCandidateHarvesters public static final String FORCE_AWS_HARVESTER_PNAME = "org.ice4j.ice.harvest.FORCE_AWS_HARVESTER"; - /** - * The name of the property which contains the addresses of the STUN servers - * to use for the STUN mapping harvester. The property should contain a - * comma-separated list of addresses (pairs of IP address and port, - * separated by a colon). Example: - * {@code stun1.example.com:12345,stun2.example.com:23456} - */ - public static final String STUN_MAPPING_HARVESTER_ADDRESSES_PNAME - = "org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES"; - /** * Whether {@link #harvesters} has been initialized. */ @@ -162,26 +153,20 @@ public class MappingCandidateHarvesters } // STUN harvesters - String stunServers - = StackProperties.getString(STUN_MAPPING_HARVESTER_ADDRESSES_PNAME); - if (stunServers != null && !stunServers.isEmpty()) + List stunServers = HarvestConfig.config.stunMappingCandidateHarvesterAddresses(); + if (!stunServers.isEmpty()) { - // Create STUN harvesters (and wait for all of their discovery to - // finish). - List stunHarvesters - = createStunHarvesters(stunServers.split(",")); + // Create STUN harvesters (and wait for all of their discovery to finish). + List stunHarvesters = createStunHarvesters(stunServers); - // We have STUN servers configured, so flag the failure if none of - // them were able to discover an address. + // We have STUN servers configured, so flag failure if none of them were able to discover an address. stunDiscoveryFailed = stunHarvesters.isEmpty(); harvesterList.addAll(stunHarvesters); } harvesterList = prune(harvesterList); - harvesters - = harvesterList.toArray( - new MappingCandidateHarvester[harvesterList.size()]); + harvesters = harvesterList.toArray(new MappingCandidateHarvester[harvesterList.size()]); for (MappingCandidateHarvester harvester : harvesters) { @@ -248,17 +233,10 @@ public class MappingCandidateHarvesters * pairs). * @return the list of those who were successful in discovering an address. */ - private static List createStunHarvesters( - String[] stunServers) + private static List createStunHarvesters(@NotNull List stunServers) { List stunHarvesters = new LinkedList<>(); - if (stunServers == null || stunServers.length == 0) - { - logger.severe("No STUN servers configured."); - return stunHarvesters; - } - List> tasks = new LinkedList<>(); // Create a StunMappingCandidateHarvester for each local:remote address diff --git a/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt index fccb47e..ed37b97 100644 --- a/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt +++ b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt @@ -56,6 +56,13 @@ class HarvestConfig { } fun timeout() = timeout + val stunMappingCandidateHarvesterAddresses: List by config { + "org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES".from(configSource) + .convertFrom { it.split(",") } + "ice4j.harvest.mapping.stun.addresses".from(configSource) + } + fun stunMappingCandidateHarvesterAddresses() = stunMappingCandidateHarvesterAddresses + companion object { @JvmField val config = HarvestConfig() diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 2c94fca..3f216dc 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -41,5 +41,13 @@ ice4j { // Whether to allocate ephemeral ports for local candidates. use-dynamic-ports = true } + + // Configuration for the set of "mapping" harvesters. + mapping { + stun { + addresses = [ ] + # addresses = [ "stun1.example.com:5379", "stun2.example.com:443" ] + } + } } } diff --git a/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt b/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt index e69d18b..36e04bc 100644 --- a/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt +++ b/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt @@ -29,6 +29,7 @@ class HarvestConfigTest : ConfigTest() { config.useIpv6 shouldBe true config.useLinkLocalAddresses shouldBe true config.udpReceiveBufferSize shouldBe null + config.stunMappingCandidateHarvesterAddresses shouldBe emptyList() } context("Setting via legacy config (system properties)") { withLegacyConfig(legacyConfig) { @@ -37,6 +38,7 @@ class HarvestConfigTest : ConfigTest() { config.useIpv6 shouldBe false config.useLinkLocalAddresses shouldBe false config.udpReceiveBufferSize shouldBe 555 + config.stunMappingCandidateHarvesterAddresses shouldBe listOf("stun1.legacy:555", "stun2.legacy") } } context("Setting via new config") { @@ -46,6 +48,7 @@ class HarvestConfigTest : ConfigTest() { config.useIpv6 shouldBe false config.useLinkLocalAddresses shouldBe false config.udpReceiveBufferSize shouldBe 666 + config.stunMappingCandidateHarvesterAddresses shouldBe listOf("stun1.new:666", "stun2.new") } } context("Legacy config must take precedence") { @@ -56,6 +59,7 @@ class HarvestConfigTest : ConfigTest() { config.useIpv6 shouldBe false config.useLinkLocalAddresses shouldBe false config.udpReceiveBufferSize shouldBe 555 + config.stunMappingCandidateHarvesterAddresses shouldBe listOf("stun1.legacy:555", "stun2.legacy") } } } @@ -67,7 +71,8 @@ private val legacyConfig = mapOf( "org.ice4j.ice.harvest.USE_DYNAMIC_HOST_HARVESTER" to "false", "org.ice4j.ipv6.DISABLED" to "true", "org.ice4j.ice.harvest.DISABLE_LINK_LOCAL_ADDRESSES" to "true", - "org.ice4j.ice.harvest.AbstractUdpListener.SO_RCVBUF" to "555" + "org.ice4j.ice.harvest.AbstractUdpListener.SO_RCVBUF" to "555", + "org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES" to "stun1.legacy:555,stun2.legacy" ) // New config which overrides the default @@ -81,6 +86,11 @@ private val newConfigNonDefault = """ receive-buffer-size = 666 use-dynamic-ports = false } + mapping { + stun { + addresses = [ "stun1.new:666", "stun2.new" ] + } + } } } """.trimIndent() @@ -96,6 +106,11 @@ private val newConfigDefault = """ receive-buffer-size = 666 use-dynamic-ports = true } + mapping { + stun { + addresses = [ "stun1.new:666", "stun2.new" ] + } + } } } """.trimIndent()