Support mapping a single port in MappingCandidateHarvester. (#235)
* test: Add unit test for MappingCandidateHarvester. * ref: Move MappingCandidateHarvester to kotlin. * feat: Add the option to match the configured port. * feat: Support statically mapping a specific port. * feat: Add a name to MappingCandidateHarvester. * doc: Add more docs to reference.conf.
This commit is contained in:
parent
311a495b21
commit
076281c76b
@ -98,9 +98,10 @@ public class DefaultNominator
|
||||
if (ev.getSource() instanceof CandidatePair)
|
||||
{
|
||||
// STUN Usage for Consent Freshness is of no concern here.
|
||||
if (IceMediaStream.PROPERTY_PAIR_CONSENT_FRESHNESS_CHANGED.equals(
|
||||
propertyName))
|
||||
if (IceMediaStream.PROPERTY_PAIR_CONSENT_FRESHNESS_CHANGED.equals(propertyName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CandidatePair validPair = (CandidatePair) ev.getSource();
|
||||
Component parentComponent = validPair.getParentComponent();
|
||||
|
||||
@ -83,7 +83,7 @@ public class AwsCandidateHarvester
|
||||
*/
|
||||
public AwsCandidateHarvester()
|
||||
{
|
||||
super();
|
||||
super("aws");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,179 +0,0 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
*
|
||||
* Copyright @ 2015 Atlassian Pty Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.ice4j.ice.harvest;
|
||||
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.ice.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* Uses a list of addresses as a predefined static mask in order to generate
|
||||
* {@link TransportAddress}es. This harvester is meant for use in situations
|
||||
* where servers are deployed behind a NAT or in a DMZ with static port mapping.
|
||||
* <p>
|
||||
* Every time the {@link #harvest(Component)} method is called, the mapping
|
||||
* harvester will return a list of candidates that provide masked alternatives
|
||||
* for every host candidate in the component. Kind of like a STUN server.
|
||||
* <p>
|
||||
* Example: You run this on a server with address 192.168.0.1, that is behind
|
||||
* a NAT with public IP: 93.184.216.119. You allocate a host candidate
|
||||
* 192.168.0.1/UDP/5000. This harvester is going to then generate an address
|
||||
* 93.184.216.119/UDP/5000
|
||||
* <p>
|
||||
* This harvester is instant and does not introduce any harvesting latency.
|
||||
*
|
||||
* @author Emil Ivov
|
||||
*/
|
||||
public class MappingCandidateHarvester
|
||||
extends AbstractCandidateHarvester
|
||||
{
|
||||
/**
|
||||
* The <tt>Logger</tt> used by the <tt>StunCandidateHarvester</tt> class and
|
||||
* its instances for logging output.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(StunCandidateHarvester.class.getName());
|
||||
|
||||
/**
|
||||
* The addresses that we will use as a mask
|
||||
*/
|
||||
protected TransportAddress mask;
|
||||
|
||||
/**
|
||||
* The addresses that we will be masking
|
||||
*/
|
||||
protected TransportAddress face;
|
||||
|
||||
/**
|
||||
* Creates a mapping harvester with the specified <tt>mask</tt>
|
||||
*
|
||||
* @param mask the <tt>TransportAddress</tt>es that would be used as a mask.
|
||||
* @param face the <tt>TransportAddress</tt>es that we will be masking.
|
||||
*/
|
||||
public MappingCandidateHarvester(TransportAddress mask,
|
||||
TransportAddress face)
|
||||
{
|
||||
this.mask = Objects.requireNonNull(mask);
|
||||
this.face = Objects.requireNonNull(face);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a {@link MappingCandidateHarvester} instance without
|
||||
* specified addresses (only useful in subclasses which override
|
||||
* {@link #getMask()} and {@link #getFace()}).
|
||||
*/
|
||||
protected MappingCandidateHarvester()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps all candidates to this harvester's mask and adds them to
|
||||
* <tt>component</tt>.
|
||||
*
|
||||
* @param component the {@link Component} that we'd like to map candidates
|
||||
* to.
|
||||
* @return the <tt>LocalCandidate</tt>s gathered by this
|
||||
* <tt>CandidateHarvester</tt> or <tt>null</tt> if no mask is specified.
|
||||
*/
|
||||
@Override
|
||||
public Collection<LocalCandidate> harvest(Component component)
|
||||
{
|
||||
TransportAddress mask = getMask();
|
||||
TransportAddress face = getFace();
|
||||
if (face == null || mask == null)
|
||||
{
|
||||
logger.warning("Harvester not configured: face=" + face
|
||||
+ ", mask=" + mask);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Report the LocalCandidates gathered by this CandidateHarvester so
|
||||
// that the harvest is sure to be considered successful.
|
||||
Collection<LocalCandidate> candidates = new HashSet<>();
|
||||
|
||||
for (Candidate<?> cand : component.getLocalCandidates())
|
||||
{
|
||||
if (!(cand instanceof HostCandidate)
|
||||
|| !cand.getTransportAddress().getHostAddress()
|
||||
.equals(face.getHostAddress())
|
||||
|| cand.getTransport() != face.getTransport())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
HostCandidate hostCandidate = (HostCandidate) cand;
|
||||
TransportAddress mappedAddress = new TransportAddress(
|
||||
mask.getHostAddress(),
|
||||
hostCandidate.getHostAddress().getPort(),
|
||||
hostCandidate.getHostAddress().getTransport());
|
||||
|
||||
ServerReflexiveCandidate mappedCandidate
|
||||
= new ServerReflexiveCandidate(
|
||||
mappedAddress,
|
||||
hostCandidate,
|
||||
hostCandidate.getStunServerAddress(),
|
||||
CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE);
|
||||
if (hostCandidate.isSSL())
|
||||
mappedCandidate.setSSL(true);
|
||||
|
||||
//try to add the candidate to the component and then
|
||||
//only add it to the harvest not redundant
|
||||
if( !candidates.contains(mappedCandidate)
|
||||
&& component.addLocalCandidate(mappedCandidate))
|
||||
{
|
||||
candidates.add(mappedCandidate);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public (mask) address, or null.
|
||||
* @return the public (mask) address, or null.
|
||||
*/
|
||||
public TransportAddress getMask()
|
||||
{
|
||||
return mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the local (face) address, or null.
|
||||
* @return the local (face) address, or null.
|
||||
*/
|
||||
public TransportAddress getFace()
|
||||
{
|
||||
return face;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
TransportAddress face = getFace();
|
||||
TransportAddress mask = getMask();
|
||||
return
|
||||
this.getClass().getName()
|
||||
+ ", face=" + (face == null ? "null" : face.getAddress())
|
||||
+ ", mask=" + (mask == null ? "null" : mask.getAddress());
|
||||
}
|
||||
}
|
||||
@ -72,6 +72,21 @@ public class MappingCandidateHarvesters
|
||||
return harvesters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the (first) mapping harvester which matches a given public address, or {@code null} if none match it.
|
||||
*/
|
||||
public static MappingCandidateHarvester findHarvesterForAddress(TransportAddress publicAddress)
|
||||
{
|
||||
for (MappingCandidateHarvester harvester : harvesters)
|
||||
{
|
||||
if (harvester.publicAddressMatches(publicAddress))
|
||||
{
|
||||
return harvester;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes {@link #harvesters}.
|
||||
* First it reads the configuration and instantiates harvesters accordingly,
|
||||
@ -94,11 +109,20 @@ public class MappingCandidateHarvesters
|
||||
for (HarvestConfig.StaticMapping staticMapping : config.getStaticMappings())
|
||||
{
|
||||
logger.info("Adding a static mapping: " + staticMapping);
|
||||
// the port number is unused, 9 is for "discard"
|
||||
TransportAddress localAddress = new TransportAddress(staticMapping.getLocalAddress(), 9, Transport.UDP);
|
||||
TransportAddress publicAddress = new TransportAddress(staticMapping.getPublicAddress(), 9, Transport.UDP);
|
||||
// If the configuration has no port, then the port value is not used in any way. We put 9 (for "discard")
|
||||
// as a filler.
|
||||
int localPort = staticMapping.getLocalPort() != null ? staticMapping.getLocalPort() : 9;
|
||||
int publicPort = staticMapping.getPublicPort() != null ? staticMapping.getPublicPort() : 9;
|
||||
TransportAddress localAddress
|
||||
= new TransportAddress(staticMapping.getLocalAddress(), localPort, Transport.UDP);
|
||||
TransportAddress publicAddress
|
||||
= new TransportAddress(staticMapping.getPublicAddress(), publicPort, Transport.UDP);
|
||||
|
||||
harvesterList.add(new MappingCandidateHarvester(publicAddress, localAddress));
|
||||
harvesterList.add(new StaticMappingCandidateHarvester(
|
||||
publicAddress,
|
||||
localAddress,
|
||||
staticMapping.getName(),
|
||||
staticMapping.getLocalPort() != null));
|
||||
}
|
||||
|
||||
// AWS harvester
|
||||
|
||||
@ -20,13 +20,13 @@ package org.ice4j.ice.harvest;
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stunclient.*;
|
||||
import org.jetbrains.annotations.*;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* A {@link MappingCandidateHarvester} which uses a STUN servers to discover
|
||||
* its public IP address.
|
||||
* A {@link MappingCandidateHarvester} which uses a STUN servers to discover its public IP address.
|
||||
*
|
||||
* @author Damian Minkov
|
||||
* @author Boris Grozev
|
||||
@ -38,13 +38,24 @@ public class StunMappingCandidateHarvester
|
||||
* The <tt>Logger</tt> used by the <tt>StunMappingCandidateHarvester</tt>
|
||||
* class and its instances for logging output.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(StunMappingCandidateHarvester.class.getName());
|
||||
private static final Logger logger = Logger.getLogger(StunMappingCandidateHarvester.class.getName());
|
||||
|
||||
/**
|
||||
* The list of servers we will use to discover our public address.
|
||||
*/
|
||||
private TransportAddress stunServerAddress;
|
||||
private final TransportAddress stunServerAddress;
|
||||
|
||||
/**
|
||||
* The local address this {@link StunMappingCandidateHarvester} was initialized with, and which will be used to
|
||||
* communicate to STUN servers.
|
||||
*/
|
||||
@NotNull
|
||||
private TransportAddress localAddress;
|
||||
|
||||
/**
|
||||
* The public address discovered through STUN, if any.
|
||||
*/
|
||||
private TransportAddress publicAddress = null;
|
||||
|
||||
/**
|
||||
* Initializes a new {@link StunMappingCandidateHarvester} instance with
|
||||
@ -55,10 +66,11 @@ public class StunMappingCandidateHarvester
|
||||
* @param stunServerAddress The address of the STUN server.
|
||||
*/
|
||||
public StunMappingCandidateHarvester(
|
||||
TransportAddress localAddress,
|
||||
@NotNull TransportAddress localAddress,
|
||||
TransportAddress stunServerAddress)
|
||||
{
|
||||
face = localAddress;
|
||||
super("stun");
|
||||
this.localAddress = localAddress;
|
||||
this.stunServerAddress = stunServerAddress;
|
||||
}
|
||||
|
||||
@ -75,29 +87,48 @@ public class StunMappingCandidateHarvester
|
||||
= new SimpleAddressDetector(stunServerAddress);
|
||||
sad.start();
|
||||
|
||||
IceSocketWrapper localSocket
|
||||
= new IceUdpSocketWrapper(new DatagramSocket(face));
|
||||
IceSocketWrapper localSocket = new IceUdpSocketWrapper(new DatagramSocket(localAddress));
|
||||
|
||||
mask = sad.getMappingFor(localSocket);
|
||||
publicAddress = sad.getMappingFor(localSocket);
|
||||
|
||||
/* Record bound address as face. */
|
||||
SocketAddress boundAddr = localSocket.getLocalSocketAddress();
|
||||
if (boundAddr instanceof InetSocketAddress) {
|
||||
face = new TransportAddress((InetSocketAddress)boundAddr, face.getTransport());
|
||||
if (boundAddr instanceof InetSocketAddress)
|
||||
{
|
||||
localAddress = new TransportAddress((InetSocketAddress)boundAddr, localAddress.getTransport());
|
||||
}
|
||||
|
||||
if (mask != null)
|
||||
if (publicAddress != null)
|
||||
{
|
||||
logger.info("Discovered public address " + mask
|
||||
logger.info("Discovered public address " + publicAddress
|
||||
+ " from STUN server " + stunServerAddress
|
||||
+ " using local address " + face);
|
||||
+ " using local address " + localSocket);
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
//whatever happens, we just log
|
||||
logger.log(Level.INFO, "We failed to obtain addresses "
|
||||
+ "for the following reason: ", exc);
|
||||
logger.log(Level.INFO, "We failed to obtain addresses for the following reason: ", exc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public TransportAddress getFace()
|
||||
{
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public TransportAddress getMask()
|
||||
{
|
||||
return publicAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
package org.ice4j.ice.harvest
|
||||
|
||||
import com.typesafe.config.Config
|
||||
import org.jitsi.metaconfig.ConfigException
|
||||
import org.jitsi.metaconfig.config
|
||||
import org.jitsi.metaconfig.optionalconfig
|
||||
import java.time.Duration
|
||||
@ -86,7 +87,22 @@ class HarvestConfig {
|
||||
private val staticMappingsFromNewConfig: Set<StaticMapping> by config {
|
||||
"ice4j.harvest.mapping.static-mappings".from(configSource)
|
||||
.convertFrom<List<Config>> { cfg ->
|
||||
cfg.map { StaticMapping(it.getString("local-address"), it.getString("public-address")) }.toSet()
|
||||
cfg.map {
|
||||
val localPort = if (it.hasPath("local-port")) it.getInt("local-port") else null
|
||||
val publicPort = if (it.hasPath("public-port")) it.getInt("public-port") else null
|
||||
if ((localPort == null || publicPort == null) && localPort != publicPort) {
|
||||
throw ConfigException.UnableToRetrieve.ConditionNotMet(
|
||||
"Inconsistent value for local-port and public-port, both must be present or both missing."
|
||||
)
|
||||
}
|
||||
StaticMapping(
|
||||
localAddress = it.getString("local-address"),
|
||||
publicAddress = it.getString("public-address"),
|
||||
localPort = localPort,
|
||||
publicPort = publicPort,
|
||||
name = if (it.hasPath("name")) it.getString("name") else null
|
||||
)
|
||||
}.toSet()
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,7 +117,13 @@ class HarvestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
data class StaticMapping(val localAddress: String, val publicAddress: String)
|
||||
data class StaticMapping(
|
||||
val localAddress: String,
|
||||
val publicAddress: String,
|
||||
val localPort: Int? = null,
|
||||
val publicPort: Int? = null,
|
||||
val name: String? = null
|
||||
)
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
|
||||
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
*
|
||||
* Copyright @ 2015 Atlassian Pty Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.ice4j.ice.harvest
|
||||
|
||||
import org.ice4j.TransportAddress
|
||||
import org.ice4j.ice.CandidateExtendedType
|
||||
import org.ice4j.ice.Component
|
||||
import org.ice4j.ice.HostCandidate
|
||||
import org.ice4j.ice.LocalCandidate
|
||||
import org.ice4j.ice.ServerReflexiveCandidate
|
||||
|
||||
/**
|
||||
* A [CandidateHarvester] which maps existing local [HostCandidate]s to new candidates.
|
||||
*/
|
||||
abstract class MappingCandidateHarvester @JvmOverloads constructor(
|
||||
val name: String,
|
||||
/**
|
||||
* Whether this harvester should match the port of the public address.
|
||||
*
|
||||
* When [matchPort] is enabled, mapping candidates will be added only when the local host candidate's address and
|
||||
* port match the public (mask) address, and the public (mask) address's port will be used.
|
||||
*
|
||||
* When [matchPort] is disabled, mapping candidates will be added whenever the local host candidate's inet address
|
||||
* matches the public (mask) address, and the host candidate port will be preserved.
|
||||
*/
|
||||
val matchPort: Boolean = false
|
||||
) : AbstractCandidateHarvester() {
|
||||
abstract val face: TransportAddress?
|
||||
abstract val mask: TransportAddress?
|
||||
|
||||
/**
|
||||
* Checks whether the given [address] matches the public address of this harvester.
|
||||
* only compares the inet address (since by default the port is not matched in [harvest]), but other implementations
|
||||
* may chose to also compare the port.
|
||||
*/
|
||||
fun publicAddressMatches(address: TransportAddress): Boolean {
|
||||
val mask = this.mask
|
||||
return if (mask == null) {
|
||||
false
|
||||
} else {
|
||||
mask.address == address.address && (!matchPort || mask.port == address.port)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for existing [HostCandidate]s in [component] which match our local address ([face]) and creates
|
||||
* associated [ServerReflexiveCandidate]s substituting [mask] for the address.
|
||||
*
|
||||
* @param component the [Component] that we'd like to harvest candidates for.
|
||||
* @return the [LocalCandidate]s created and added to [component].
|
||||
*/
|
||||
override fun harvest(component: Component): Collection<LocalCandidate> {
|
||||
val localAddress = face ?: return emptyList()
|
||||
val publicAddress = mask ?: return emptyList()
|
||||
|
||||
// Report the LocalCandidates gathered by this CandidateHarvester so
|
||||
// that the harvest is sure to be considered successful.
|
||||
val candidates = mutableSetOf<LocalCandidate>()
|
||||
component.localCandidates
|
||||
.filter {
|
||||
it is HostCandidate &&
|
||||
it.transportAddress.hostAddress == localAddress.hostAddress &&
|
||||
it.transport == localAddress.transport &&
|
||||
(!matchPort || it.transportAddress.port == localAddress.port)
|
||||
}.forEach { hostCandidate ->
|
||||
hostCandidate as HostCandidate
|
||||
|
||||
val mappedAddress = TransportAddress(
|
||||
publicAddress.hostAddress,
|
||||
if (matchPort) publicAddress.port else hostCandidate.hostAddress.port,
|
||||
hostCandidate.hostAddress.transport
|
||||
)
|
||||
val mappedCandidate = ServerReflexiveCandidate(
|
||||
mappedAddress,
|
||||
hostCandidate,
|
||||
hostCandidate.stunServerAddress,
|
||||
CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE
|
||||
)
|
||||
if (hostCandidate.isSSL) mappedCandidate.isSSL = true
|
||||
|
||||
// Try to add the candidate to the component and only then add it to the harvest.
|
||||
if (!candidates.contains(mappedCandidate) && component.addLocalCandidate(mappedCandidate)) {
|
||||
candidates.add(mappedCandidate)
|
||||
}
|
||||
}
|
||||
|
||||
return candidates
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package org.ice4j.ice.harvest
|
||||
|
||||
import org.ice4j.TransportAddress
|
||||
|
||||
/**
|
||||
* Uses a predefined static mask in order to generate [TransportAddress]es. This harvester is meant for use in
|
||||
* situations where servers are deployed behind a NAT or in a DMZ with static port mapping.
|
||||
*
|
||||
* Every time the [.harvest] method is called, the mapping harvester will return a list of candidates that provide
|
||||
* masked alternatives for every host candidate in the component. Kind of like a STUN server.
|
||||
*
|
||||
* Example: You run this on a server with address 192.168.0.1, that is behind a NAT with public IP: 93.184.216.119.
|
||||
* You allocate a host candidate 192.168.0.1/UDP/5000. This harvester is going to then generate an address
|
||||
* 93.184.216.119/UDP/5000.
|
||||
*
|
||||
* This harvester is instant and does not introduce any harvesting latency.
|
||||
*
|
||||
* @author Emil Ivov
|
||||
*/
|
||||
class StaticMappingCandidateHarvester @JvmOverloads constructor(
|
||||
/** The public address (aka mask) */
|
||||
override val mask: TransportAddress,
|
||||
/** The local address (aka face) */
|
||||
override val face: TransportAddress,
|
||||
name: String? = null,
|
||||
matchPort: Boolean = false
|
||||
) : MappingCandidateHarvester(name ?: "static_mapping", matchPort) {
|
||||
override fun toString() = "${javaClass.name}(face=$face, mask=$mask)"
|
||||
}
|
||||
@ -56,8 +56,23 @@ ice4j {
|
||||
// Pre-configured mappings
|
||||
static-mappings = [
|
||||
#{
|
||||
# // This results in a candidate for 1.2.3.4:20000 being added whenever a host candidate for
|
||||
# // 10.10.0.123:10000 exists.
|
||||
# local-address = "10.10.0.123"
|
||||
# public-address = "1.2.3.4"
|
||||
# // The ports are optional, but need to either both be provided or both not provided.
|
||||
# local-port = 10000
|
||||
# public-port = 20000
|
||||
# // Optional name
|
||||
# name = "my-mapping-for-a-specific-port"
|
||||
#},
|
||||
#{
|
||||
# // This results in a candidate for 1.2.3.4:PORT being added whenever a host candidate for 10.10.0.123
|
||||
# // exists, where the PORT is the port of the existing host candidate.
|
||||
# local-address = "10.10.0.123"
|
||||
# public-address = "1.2.3.4"
|
||||
# // Optional name
|
||||
# name = "my-mapping-for-all-ports"
|
||||
#}
|
||||
]
|
||||
}
|
||||
|
||||
@ -58,13 +58,10 @@ public class MappingTest
|
||||
|
||||
try
|
||||
{
|
||||
localAddress
|
||||
= new TransportAddress(localAddressStr, 9, Transport.UDP);
|
||||
publicAddress
|
||||
= new TransportAddress(publicAddressStr, 9, Transport.UDP);
|
||||
localAddress = new TransportAddress(localAddressStr, 9, Transport.UDP);
|
||||
publicAddress = new TransportAddress(publicAddressStr, 9, Transport.UDP);
|
||||
|
||||
logger.info("Will append a NAT harvester for " +
|
||||
localAddress + "=>" + publicAddress);
|
||||
logger.info("Will append a NAT harvester for " + localAddress + "=>" + publicAddress);
|
||||
|
||||
}
|
||||
catch(Exception exc)
|
||||
@ -75,28 +72,21 @@ public class MappingTest
|
||||
return;
|
||||
}
|
||||
|
||||
MappingCandidateHarvester natHarvester
|
||||
= new MappingCandidateHarvester(publicAddress, localAddress);
|
||||
MappingCandidateHarvester natHarvester = new StaticMappingCandidateHarvester(publicAddress, localAddress);
|
||||
|
||||
List<CandidateHarvester> harvesters = new ArrayList<>();
|
||||
harvesters.add(natHarvester);
|
||||
|
||||
Agent localAgent = createAgent(2020, false, harvesters);
|
||||
localAgent.setNominationStrategy(
|
||||
NominationStrategy.NOMINATE_HIGHEST_PRIO);
|
||||
localAgent.setNominationStrategy(NominationStrategy.NOMINATE_HIGHEST_PRIO);
|
||||
|
||||
String localSDP = SdpUtils.createSDPDescription(localAgent);
|
||||
|
||||
//wait a bit so that the logger can stop dumping stuff:
|
||||
Thread.sleep(500);
|
||||
|
||||
logger.info("=================== feed the following"
|
||||
+" to the remote agent ===================");
|
||||
|
||||
|
||||
logger.info("=================== feed the following to the remote agent ===================");
|
||||
logger.info("\n" + localSDP);
|
||||
|
||||
logger.info("======================================"
|
||||
+ "========================================\n");
|
||||
logger.info("==============================================================================\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
package org.ice4j.ice.harvest
|
||||
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.matchers.shouldBe
|
||||
import org.ice4j.ConfigTest
|
||||
import org.ice4j.ice.harvest.HarvestConfig.Companion.config
|
||||
@ -63,6 +64,73 @@ class HarvestConfigTest : ConfigTest() {
|
||||
}
|
||||
}
|
||||
}
|
||||
context("Static mappings") {
|
||||
context("With all fields present") {
|
||||
withNewConfig(
|
||||
"""
|
||||
ice4j.harvest.mapping.static-mappings = [
|
||||
{
|
||||
local-address = "10.0.0.1"
|
||||
local-port = 10000
|
||||
public-address = "192.168.255.255"
|
||||
public-port = 33333
|
||||
name = "my-mapping"
|
||||
}
|
||||
]
|
||||
""".trimIndent()
|
||||
) {
|
||||
HarvestConfig().staticMappings shouldBe listOf(
|
||||
HarvestConfig.StaticMapping(
|
||||
localAddress = "10.0.0.1",
|
||||
localPort = 10000,
|
||||
publicAddress = "192.168.255.255",
|
||||
publicPort = 33333,
|
||||
name = "my-mapping"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
context("With optional fields missing") {
|
||||
withNewConfig(
|
||||
"""
|
||||
ice4j.harvest.mapping.static-mappings = [
|
||||
{
|
||||
local-address = "10.0.0.1"
|
||||
public-address = "192.168.255.255"
|
||||
}
|
||||
]
|
||||
""".trimIndent()
|
||||
) {
|
||||
HarvestConfig().staticMappings shouldBe listOf(
|
||||
HarvestConfig.StaticMapping(
|
||||
localAddress = "10.0.0.1",
|
||||
localPort = null,
|
||||
publicAddress = "192.168.255.255",
|
||||
publicPort = null,
|
||||
name = null
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
context("With inconsistent local-port and public-port") {
|
||||
withNewConfig(
|
||||
"""
|
||||
ice4j.harvest.mapping.static-mappings = [
|
||||
{
|
||||
local-address = "10.0.0.1"
|
||||
public-address = "192.168.255.255"
|
||||
local-port = 10000
|
||||
//public-port = 33333
|
||||
}
|
||||
]
|
||||
""".trimIndent()
|
||||
) {
|
||||
shouldThrow<Throwable> {
|
||||
HarvestConfig().staticMappings
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright @ 2020 - present 8x8, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import io.kotest.core.spec.IsolationMode
|
||||
import io.kotest.core.spec.style.ShouldSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.ice4j.Transport
|
||||
import org.ice4j.TransportAddress
|
||||
import org.ice4j.ice.Component
|
||||
import org.ice4j.ice.HostCandidate
|
||||
import org.ice4j.ice.ServerReflexiveCandidate
|
||||
import org.ice4j.ice.harvest.StaticMappingCandidateHarvester
|
||||
import org.jitsi.utils.logging2.createLogger
|
||||
|
||||
class StaticMappingCandidateHarvesterTest : ShouldSpec() {
|
||||
override fun isolationMode(): IsolationMode = IsolationMode.InstancePerLeaf
|
||||
|
||||
val component = mockk<Component>()
|
||||
|
||||
init {
|
||||
val hostCandidate1 = createHostCandidate("10.0.0.1", 10000)
|
||||
val hostCandidate2 = createHostCandidate("10.0.0.111", 10000)
|
||||
val hostCandidate3 = createHostCandidate("10.0.0.1", 11111)
|
||||
val srflxCandidate1 = mockk<ServerReflexiveCandidate>().apply {
|
||||
every { hostAddress } returns ta("10.10.0.1", 22222)
|
||||
}
|
||||
val srflxCandidate2 = mockk<ServerReflexiveCandidate>().apply {
|
||||
every { hostAddress } returns ta("192.168.0.1", 1234)
|
||||
}
|
||||
|
||||
component.apply {
|
||||
every { logger } returns createLogger()
|
||||
every { componentID } returns Component.RTP
|
||||
every { localCandidates } returns listOf(
|
||||
hostCandidate1, hostCandidate2, hostCandidate3, srflxCandidate1, srflxCandidate2
|
||||
)
|
||||
every { addLocalCandidate(any()) } returns true
|
||||
}
|
||||
|
||||
val publicHostname = "192.168.255.255"
|
||||
|
||||
context("Harvesting without matching port") {
|
||||
val harvester = StaticMappingCandidateHarvester(
|
||||
face = ta("10.0.0.1", 10000),
|
||||
mask = ta(publicHostname, 20000),
|
||||
matchPort = false
|
||||
)
|
||||
|
||||
context("publicAddressMatches") {
|
||||
harvester.publicAddressMatches(ta(publicHostname, 1234)) shouldBe true
|
||||
harvester.publicAddressMatches(ta(publicHostname, 10000)) shouldBe true
|
||||
harvester.publicAddressMatches(ta(publicHostname, 20000)) shouldBe true
|
||||
harvester.publicAddressMatches(ta("192.168.1.1", 20000)) shouldBe false
|
||||
}
|
||||
|
||||
val candidatesAdded = harvester.harvest(component)
|
||||
should("Add a candidate corresponding to hostCandidate1") {
|
||||
val addedCandidate = candidatesAdded.find { it.base == hostCandidate1 }!!
|
||||
addedCandidate.transportAddress shouldBe ta(publicHostname, hostCandidate1.transportAddress.port)
|
||||
}
|
||||
should("Not add a candidate corresponding to hostCandidate2 (hostname does not match)") {
|
||||
candidatesAdded.find { it.base == hostCandidate2 } shouldBe null
|
||||
}
|
||||
should("Add a candidate corresponding to hostCandidate3") {
|
||||
val addedCandidate = candidatesAdded.find { it.base == hostCandidate3 }!!
|
||||
addedCandidate.transportAddress shouldBe ta(publicHostname, hostCandidate3.transportAddress.port)
|
||||
}
|
||||
should("Not add a candidate corresponding to any of the srflx candidateS (they are not HostCandidate)") {
|
||||
candidatesAdded.find { it.base == srflxCandidate1 } shouldBe null
|
||||
candidatesAdded.find { it.base == srflxCandidate2 } shouldBe null
|
||||
}
|
||||
should("Not add any other candidates") {
|
||||
candidatesAdded.size shouldBe 2
|
||||
}
|
||||
}
|
||||
context("Harvesting with matching port") {
|
||||
val publicPort = 33333
|
||||
val harvester = StaticMappingCandidateHarvester(
|
||||
face = ta("10.0.0.1", 10000),
|
||||
mask = ta(publicHostname, publicPort),
|
||||
matchPort = true
|
||||
)
|
||||
|
||||
context("publicAddressMatches") {
|
||||
harvester.publicAddressMatches(ta(publicHostname, 1234)) shouldBe false
|
||||
harvester.publicAddressMatches(ta(publicHostname, 10000)) shouldBe false
|
||||
harvester.publicAddressMatches(ta(publicHostname, 20000)) shouldBe false
|
||||
harvester.publicAddressMatches(ta(publicHostname, publicPort)) shouldBe true
|
||||
harvester.publicAddressMatches(ta("192.168.1.1", 20000)) shouldBe false
|
||||
}
|
||||
|
||||
val candidatesAdded = harvester.harvest(component)
|
||||
should("Add a candidate corresponding to hostCandidate1") {
|
||||
val addedCandidate = candidatesAdded.find { it.base == hostCandidate1 }!!
|
||||
addedCandidate.transportAddress shouldBe ta(publicHostname, publicPort)
|
||||
}
|
||||
should("Not add a candidate corresponding to hostCandidate2 (hostname does not match)") {
|
||||
candidatesAdded.find { it.base == hostCandidate2 } shouldBe null
|
||||
}
|
||||
should("Not add a candidate corresponding to hostCandidate3 (port does not match)") {
|
||||
candidatesAdded.find { it.base == hostCandidate3 } shouldBe null
|
||||
}
|
||||
should("Not add a candidate corresponding to any of the srflx candidateS (they are not HostCandidate)") {
|
||||
candidatesAdded.find { it.base == srflxCandidate1 } shouldBe null
|
||||
candidatesAdded.find { it.base == srflxCandidate2 } shouldBe null
|
||||
}
|
||||
should("Not add any other candidates") {
|
||||
candidatesAdded.size shouldBe 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createHostCandidate(hostname: String, port: Int) = mockk<HostCandidate>().apply {
|
||||
val localAddress = ta(hostname, port)
|
||||
every { hostAddress } returns localAddress
|
||||
every { transportAddress } returns localAddress
|
||||
every { transport } returns localAddress.transport
|
||||
every { stunServerAddress } returns null
|
||||
every { parentComponent } returns component
|
||||
every { isSSL } returns false
|
||||
}
|
||||
}
|
||||
|
||||
/** Create a UDP TransportAddress */
|
||||
private fun ta(hostname: String, port: Int) = TransportAddress(hostname, port, Transport.UDP)
|
||||
Loading…
x
Reference in New Issue
Block a user