ref: Port StunMappingHarvester config. (#215)

This commit is contained in:
bgrozev 2020-09-25 11:39:13 -05:00 committed by GitHub
parent 29ae8061ad
commit 3a55627ba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 31 deletions

View File

@ -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<String> stunServers = HarvestConfig.config.stunMappingCandidateHarvesterAddresses();
if (!stunServers.isEmpty())
{
// Create STUN harvesters (and wait for all of their discovery to
// finish).
List<StunMappingCandidateHarvester> stunHarvesters
= createStunHarvesters(stunServers.split(","));
// Create STUN harvesters (and wait for all of their discovery to finish).
List<StunMappingCandidateHarvester> 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<StunMappingCandidateHarvester> createStunHarvesters(
String[] stunServers)
private static List<StunMappingCandidateHarvester> createStunHarvesters(@NotNull List<String> stunServers)
{
List<StunMappingCandidateHarvester> stunHarvesters = new LinkedList<>();
if (stunServers == null || stunServers.length == 0)
{
logger.severe("No STUN servers configured.");
return stunHarvesters;
}
List<Callable<StunMappingCandidateHarvester>> tasks = new LinkedList<>();
// Create a StunMappingCandidateHarvester for each local:remote address

View File

@ -56,6 +56,13 @@ class HarvestConfig {
}
fun timeout() = timeout
val stunMappingCandidateHarvesterAddresses: List<String> by config {
"org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES".from(configSource)
.convertFrom<String> { it.split(",") }
"ice4j.harvest.mapping.stun.addresses".from(configSource)
}
fun stunMappingCandidateHarvesterAddresses() = stunMappingCandidateHarvesterAddresses
companion object {
@JvmField
val config = HarvestConfig()

View File

@ -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" ]
}
}
}
}

View File

@ -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()