Port NAT harvester properties to new config (#229)

* feat: Port "nat harvester" to the new config.

* ref: Fix ktlint errors.
This commit is contained in:
bgrozev 2021-05-26 18:01:12 +03:00 committed by GitHub
parent 3e0a9f97ef
commit 2586d8b2da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 30 deletions

View File

@ -41,22 +41,7 @@ public class MappingCandidateHarvesters
* The {@link Logger} used by the {@link MappingCandidateHarvesters} * The {@link Logger} used by the {@link MappingCandidateHarvesters}
* class for logging output. * class for logging output.
*/ */
private static final Logger logger private static final Logger logger = Logger.getLogger(MappingCandidateHarvesters.class.getName());
= Logger.getLogger(MappingCandidateHarvesters.class.getName());
/**
* The name of the property that specifies the local address, if any, for
* the pre-configured NAT harvester.
*/
public static final String NAT_HARVESTER_LOCAL_ADDRESS_PNAME
= "org.ice4j.ice.harvest.NAT_HARVESTER_LOCAL_ADDRESS";
/**
* The name of the property that specifies the public address, if any, for
* the pre-configured NAT harvester.
*/
public static final String NAT_HARVESTER_PUBLIC_ADDRESS_PNAME
= "org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS";
/** /**
* Whether {@link #harvesters} has been initialized. * Whether {@link #harvesters} has been initialized.
@ -106,23 +91,14 @@ public class MappingCandidateHarvesters
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
List<MappingCandidateHarvester> harvesterList = new LinkedList<>(); List<MappingCandidateHarvester> harvesterList = new LinkedList<>();
for (HarvestConfig.StaticMapping staticMapping : config.getStaticMappings())
// Pre-configured NAT harvester.
String localAddressStr
= StackProperties.getString(NAT_HARVESTER_LOCAL_ADDRESS_PNAME);
String publicAddressStr
= StackProperties.getString(NAT_HARVESTER_PUBLIC_ADDRESS_PNAME);
if (localAddressStr != null && publicAddressStr != null)
{ {
logger.info("Adding a static mapping: " + staticMapping);
// the port number is unused, 9 is for "discard" // the port number is unused, 9 is for "discard"
TransportAddress localAddress TransportAddress localAddress = new TransportAddress(staticMapping.getLocalAddress(), 9, Transport.UDP);
= new TransportAddress(localAddressStr, 9, Transport.UDP); TransportAddress publicAddress = new TransportAddress(staticMapping.getPublicAddress(), 9, Transport.UDP);
TransportAddress publicAddress
= new TransportAddress(publicAddressStr, 9, Transport.UDP);
harvesterList.add( harvesterList.add(new MappingCandidateHarvester(publicAddress, localAddress));
new MappingCandidateHarvester(publicAddress, localAddress));
} }
// AWS harvester // AWS harvester

View File

@ -16,6 +16,7 @@
package org.ice4j.ice.harvest package org.ice4j.ice.harvest
import com.typesafe.config.Config
import org.jitsi.metaconfig.config import org.jitsi.metaconfig.config
import org.jitsi.metaconfig.optionalconfig import org.jitsi.metaconfig.optionalconfig
import java.time.Duration import java.time.Duration
@ -74,6 +75,34 @@ class HarvestConfig {
} }
fun forceAwsHarvester() = enableAwsHarvester fun forceAwsHarvester() = enableAwsHarvester
private val legacyNatHarvesterLocalAddress: String? by optionalconfig {
"org.ice4j.ice.harvest.NAT_HARVESTER_LOCAL_ADDRESS".from(configSource)
}
private val legacyNatHarvesterPublicAddress: String? by optionalconfig {
"org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS".from(configSource)
}
private val staticMappingsFromNewConfig: Set<StaticMapping> by config {
"ice4j.harvest.mapping.static-mappings".from(configSource)
.convertFrom<List<Config>> { cfg ->
cfg.map { StaticMapping(it.getString("localAddress"), it.getString("publicAddress")) }.toSet()
}
}
val staticMappings: Set<StaticMapping> = let {
if (legacyNatHarvesterLocalAddress != null && legacyNatHarvesterPublicAddress != null) {
setOf(
StaticMapping(legacyNatHarvesterLocalAddress!!, legacyNatHarvesterPublicAddress!!),
*staticMappingsFromNewConfig.toTypedArray()
)
} else {
staticMappingsFromNewConfig
}
}
data class StaticMapping(val localAddress: String, val publicAddress: String)
companion object { companion object {
@JvmField @JvmField
val config = HarvestConfig() val config = HarvestConfig()

View File

@ -53,6 +53,13 @@ ice4j {
addresses = [ ] addresses = [ ]
# addresses = [ "stun1.example.com:5379", "stun2.example.com:443" ] # addresses = [ "stun1.example.com:5379", "stun2.example.com:443" ]
} }
// Pre-configured mappings
static-mappings = [
#{
# local-address = "10.10.0.123"
# public-address = "1.2.3.4"
#}
]
} }
} }
} }