Skip to content
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ type Agent struct {
renominationInterval time.Duration
lastRenominationTime time.Time

// Port mapping support for container
mapPort func(candidate Candidate) int

turnClientFactory func(*turn.ClientConfig) (turnClient, error)
}

Expand Down
14 changes: 14 additions & 0 deletions agent_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,3 +960,17 @@ func WithLoggerFactory(loggerFactory logging.LoggerFactory) AgentOption {
return nil
}
}

func WithMapPortHandler(handler func(cand Candidate) int, candTyp CandidateType) AgentOption {
return func(a *Agent) error {
a.mapPort = func(candidate Candidate) int {
if candidate.Type() == candTyp {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll either need to remove the candidate type filter or make it possible to add multiple filters for different candidates types.
I think removing is better because the user filter have access to the candidate type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! Updated.

return handler(candidate)
}

return 0
}

return nil
}
}
2 changes: 1 addition & 1 deletion candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (c *candidateBase) Priority() uint32 {
}

// Equal is used to compare two candidateBases.
func (c *candidateBase) Equal(other Candidate) bool {
func (c *candidateBase) Equal(other Candidate) bool { //nolint:cyclop
if c.addr() != other.addr() {
if c.addr() == nil || other.addr() == nil {
return false
Expand Down
61 changes: 49 additions & 12 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ

continue
}
if a.mapPort != nil {
mappedPort := a.mapPort(candidateHost)
if mappedPort != 0 {
candidateHost.port = mappedPort
}
}

if err := a.addCandidate(ctx, candidateHost, connAndPort.conn); err != nil {
if closeErr := candidateHost.close(); closeErr != nil {
Expand Down Expand Up @@ -512,15 +518,22 @@ func (a *Agent) gatherCandidatesLocalUDPMux(ctx context.Context) error { //nolin
return err
}

c, err := NewCandidateHost(&hostConfig)
cand, err := NewCandidateHost(&hostConfig)
if a.mapPort != nil {
mappedPort := a.mapPort(cand)
if mappedPort != 0 {
cand.port = mappedPort
}
}

if err != nil {
closeConnAndLog(conn, a.log, "failed to create host mux candidate: %s %d: %v", candidateIP, udpAddr.Port, err)

continue
}

if err := a.addCandidate(ctx, c, conn); err != nil {
if closeErr := c.close(); closeErr != nil {
if err := a.addCandidate(ctx, cand, conn); err != nil {
if closeErr := cand.close(); closeErr != nil {
a.log.Warnf("Failed to close candidate: %v", closeErr)
}

Expand Down Expand Up @@ -623,7 +636,7 @@ func (a *Agent) gatherCandidatesSrflxMapped(ctx context.Context, networkTypes []
RelAddr: currentAddr.IP.String(),
RelPort: currentAddr.Port,
}
c, err := NewCandidateServerReflexive(&srflxConfig)
cand, err := NewCandidateServerReflexive(&srflxConfig)
if err != nil {
closeConnAndLog(currentConn, a.log, "failed to create server reflexive candidate: %s %s %d: %v",
network,
Expand All @@ -633,9 +646,15 @@ func (a *Agent) gatherCandidatesSrflxMapped(ctx context.Context, networkTypes []

continue
}
if a.mapPort != nil {
mappedPort := a.mapPort(cand)
if mappedPort != 0 {
cand.port = mappedPort
}
}

if err := a.addCandidate(ctx, c, currentConn); err != nil {
if closeErr := c.close(); closeErr != nil {
if err := a.addCandidate(ctx, cand, currentConn); err != nil {
if closeErr := cand.close(); closeErr != nil {
a.log.Warnf("Failed to close candidate: %v", closeErr)
}
a.log.Warnf("Failed to append to localCandidates and run onCandidateHdlr: %v", err)
Expand Down Expand Up @@ -712,15 +731,21 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*stun.UR
RelAddr: localAddr.IP.String(),
RelPort: localAddr.Port,
}
c, err := NewCandidateServerReflexive(&srflxConfig)
cand, err := NewCandidateServerReflexive(&srflxConfig)
if err != nil {
closeConnAndLog(conn, a.log, "failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err)

return
}
if a.mapPort != nil {
mappedPort := a.mapPort(cand)
if mappedPort != 0 {
cand.port = mappedPort
}
}

if err := a.addCandidate(ctx, c, conn); err != nil {
if closeErr := c.close(); closeErr != nil {
if err := a.addCandidate(ctx, cand, conn); err != nil {
if closeErr := cand.close(); closeErr != nil {
a.log.Warnf("Failed to close candidate: %v", closeErr)
}
a.log.Warnf("Failed to append to localCandidates and run onCandidateHdlr: %v", err)
Expand Down Expand Up @@ -805,15 +830,21 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*stun.URI, net
RelAddr: lAddr.IP.String(),
RelPort: lAddr.Port,
}
c, err := NewCandidateServerReflexive(&srflxConfig)
cand, err := NewCandidateServerReflexive(&srflxConfig)
if err != nil {
closeConnAndLog(conn, a.log, "failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err)

return
}
if a.mapPort != nil {
mappedPort := a.mapPort(cand)
if mappedPort != 0 {
cand.port = mappedPort
}
}

if err := a.addCandidate(ctx, c, conn); err != nil {
if closeErr := c.close(); closeErr != nil {
if err := a.addCandidate(ctx, cand, conn); err != nil {
if closeErr := cand.close(); closeErr != nil {
a.log.Warnf("Failed to close candidate: %v", closeErr)
}
a.log.Warnf("Failed to append to localCandidates and run onCandidateHdlr: %v", err)
Expand Down Expand Up @@ -1158,6 +1189,12 @@ func (a *Agent) createRelayCandidate(ctx context.Context, ep relayEndpoint, ip n

return err
}
if a.mapPort != nil {
mappedPort := a.mapPort(candidate)
if mappedPort != 0 {
candidate.port = mappedPort
}
}

if err := a.addCandidate(ctx, candidate, ep.conn); err != nil {
if closeErr := candidate.close(); closeErr != nil {
Expand Down
Loading
Loading