Skip to content

Commit

Permalink
Move URL type to pion/stun.URI
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed May 10, 2023
1 parent 6c82312 commit ca9de18
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 377 deletions.
2 changes: 1 addition & 1 deletion agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Agent struct {

selectedPair atomic.Value // *CandidatePair

urls []*URL
urls []*stun.URI
networkTypes []NetworkType

buf *packetio.Buffer
Expand Down
3 changes: 2 additions & 1 deletion agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/transport/v2"
"golang.org/x/net/proxy"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func defaultCandidateTypes() []CandidateType {
// AgentConfig collects the arguments to ice.Agent construction into
// a single structure, for future-proofness of the interface
type AgentConfig struct {
Urls []*URL
Urls []*stun.URI

// PortMin and PortMax are optional. Leave them 0 for the default UDP port allocation strategy.
PortMin uint16
Expand Down
16 changes: 8 additions & 8 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ func TestConnectivityLite(t *testing.T) {
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()

stunServerURL := &URL{
stunServerURL := &stun.URI{
Scheme: SchemeTypeSTUN,
Host: "1.2.3.4",
Port: 3478,
Proto: ProtoTypeUDP,
Proto: stun.ProtoTypeUDP,
}

natType := &vnet.NATType{
Expand All @@ -320,7 +320,7 @@ func TestConnectivityLite(t *testing.T) {
bNotifier, bConnected := onConnected()

cfg0 := &AgentConfig{
Urls: []*URL{stunServerURL},
Urls: []*stun.URI{stunServerURL},
NetworkTypes: supportedNetworkTypes(),
MulticastDNSMode: MulticastDNSModeDisabled,
Net: v.net0,
Expand All @@ -331,7 +331,7 @@ func TestConnectivityLite(t *testing.T) {
require.NoError(t, aAgent.OnConnectionStateChange(aNotifier))

cfg1 := &AgentConfig{
Urls: []*URL{},
Urls: []*stun.URI{},
Lite: true,
CandidateTypes: []CandidateType{CandidateTypeHost},
NetworkTypes: supportedNetworkTypes(),
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestConnectionStateCallback(t *testing.T) {
KeepaliveInterval := time.Duration(0)

cfg := &AgentConfig{
Urls: []*URL{},
Urls: []*stun.URI{},
NetworkTypes: supportedNetworkTypes(),
DisconnectedTimeout: &disconnectedDuration,
FailedTimeout: &failedDuration,
Expand Down Expand Up @@ -1330,7 +1330,7 @@ func TestCloseInConnectionStateCallback(t *testing.T) {
CheckInterval := 500 * time.Millisecond

cfg := &AgentConfig{
Urls: []*URL{},
Urls: []*stun.URI{},
NetworkTypes: supportedNetworkTypes(),
DisconnectedTimeout: &disconnectedDuration,
FailedTimeout: &failedDuration,
Expand Down Expand Up @@ -1382,7 +1382,7 @@ func TestRunTaskInConnectionStateCallback(t *testing.T) {
CheckInterval := 50 * time.Millisecond

cfg := &AgentConfig{
Urls: []*URL{},
Urls: []*stun.URI{},
NetworkTypes: supportedNetworkTypes(),
DisconnectedTimeout: &oneSecond,
FailedTimeout: &oneSecond,
Expand Down Expand Up @@ -1427,7 +1427,7 @@ func TestRunTaskInSelectedCandidatePairChangeCallback(t *testing.T) {
CheckInterval := 50 * time.Millisecond

cfg := &AgentConfig{
Urls: []*URL{},
Urls: []*stun.URI{},
NetworkTypes: supportedNetworkTypes(),
DisconnectedTimeout: &oneSecond,
FailedTimeout: &oneSecond,
Expand Down
7 changes: 4 additions & 3 deletions candidate_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/pion/stun"
"github.com/pion/transport/v2/test"
"github.com/pion/turn/v2"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,14 +48,14 @@ func TestRelayOnlyConnection(t *testing.T) {

cfg := &AgentConfig{
NetworkTypes: supportedNetworkTypes(),
Urls: []*URL{
Urls: []*stun.URI{
{
Scheme: SchemeTypeTURN,
Scheme: stun.SchemeTypeTURN,
Host: "127.0.0.1",
Username: "username",
Password: "password",
Port: serverPort,
Proto: ProtoTypeUDP,
Proto: stun.ProtoTypeUDP,
},
},
CandidateTypes: []CandidateType{CandidateTypeRelay},
Expand Down
3 changes: 2 additions & 1 deletion candidate_server_reflexive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/pion/stun"
"github.com/pion/transport/v2/test"
"github.com/pion/turn/v2"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestServerReflexiveOnlyConnection(t *testing.T) {

cfg := &AgentConfig{
NetworkTypes: []NetworkType{NetworkTypeUDP4},
Urls: []*URL{
Urls: []*stun.URI{
{
Scheme: SchemeTypeSTUN,
Host: "127.0.0.1",
Expand Down
30 changes: 15 additions & 15 deletions connectivity_vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func connectWithVNet(aAgent, bAgent *Agent) (*Conn, *Conn) {
}

type agentTestConfig struct {
urls []*URL
urls []*stun.URI
nat1To1IPCandidateType CandidateType
}

Expand Down Expand Up @@ -305,20 +305,20 @@ func TestConnectivityVNet(t *testing.T) {
report := test.CheckRoutines(t)
defer report()

stunServerURL := &URL{
Scheme: SchemeTypeSTUN,
stunServerURL := &stun.URI{
Scheme: stun.SchemeTypeSTUN,
Host: vnetSTUNServerIP,
Port: vnetSTUNServerPort,
Proto: ProtoTypeUDP,
Proto: stun.ProtoTypeUDP,
}

turnServerURL := &URL{
Scheme: SchemeTypeTURN,
turnServerURL := &stun.URI{
Scheme: stun.SchemeTypeTURN,
Host: vnetSTUNServerIP,
Port: vnetSTUNServerPort,
Username: "user",
Password: "pass",
Proto: ProtoTypeUDP,
Proto: stun.ProtoTypeUDP,
}

t.Run("Full-cone NATs on both ends", func(t *testing.T) {
Expand All @@ -339,12 +339,12 @@ func TestConnectivityVNet(t *testing.T) {

log.Debug("Connecting...")
a0TestConfig := &agentTestConfig{
urls: []*URL{
urls: []*stun.URI{
stunServerURL,
},
}
a1TestConfig := &agentTestConfig{
urls: []*URL{
urls: []*stun.URI{
stunServerURL,
},
}
Expand Down Expand Up @@ -376,13 +376,13 @@ func TestConnectivityVNet(t *testing.T) {

log.Debug("Connecting...")
a0TestConfig := &agentTestConfig{
urls: []*URL{
urls: []*stun.URI{
stunServerURL,
turnServerURL,
},
}
a1TestConfig := &agentTestConfig{
urls: []*URL{
urls: []*stun.URI{
stunServerURL,
},
}
Expand Down Expand Up @@ -416,11 +416,11 @@ func TestConnectivityVNet(t *testing.T) {

log.Debug("Connecting...")
a0TestConfig := &agentTestConfig{
urls: []*URL{},
urls: []*stun.URI{},
nat1To1IPCandidateType: CandidateTypeHost, // Use 1:1 NAT IP as a host candidate
}
a1TestConfig := &agentTestConfig{
urls: []*URL{},
urls: []*stun.URI{},
}
ca, cb := pipeWithVNet(v, a0TestConfig, a1TestConfig)

Expand Down Expand Up @@ -452,11 +452,11 @@ func TestConnectivityVNet(t *testing.T) {

log.Debug("Connecting...")
a0TestConfig := &agentTestConfig{
urls: []*URL{},
urls: []*stun.URI{},
nat1To1IPCandidateType: CandidateTypeServerReflexive, // Use 1:1 NAT IP as a srflx candidate
}
a1TestConfig := &agentTestConfig{
urls: []*URL{},
urls: []*stun.URI{},
}
ca, cb := pipeWithVNet(v, a0TestConfig, a1TestConfig)

Expand Down
2 changes: 0 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ var (
errReadingStreamingPacket = errors.New("error reading streaming packet")
errWriting = errors.New("error writing to")
errClosingConnection = errors.New("error closing connection")
errMissingProtocolScheme = errors.New("missing protocol scheme")
errTooManyColonsAddr = errors.New("too many colons in address")
errRead = errors.New("unexpected error trying to read")
errUnknownRole = errors.New("unknown role")
errICEWriteSTUNMessage = errors.New("the ICE conn can't write STUN messages")
Expand Down
31 changes: 16 additions & 15 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pion/ice/v2/internal/fakenet"
stunx "github.com/pion/ice/v2/internal/stun"
"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/turn/v2"
)

Expand Down Expand Up @@ -376,7 +377,7 @@ func (a *Agent) gatherCandidatesSrflxMapped(ctx context.Context, networkTypes []
}
}

func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*URL, networkTypes []NetworkType) { //nolint:gocognit
func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*stun.URI, networkTypes []NetworkType) { //nolint:gocognit
var wg sync.WaitGroup
defer wg.Wait()

Expand All @@ -393,7 +394,7 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*URL, ne
continue
}
wg.Add(1)
go func(url URL, network string, localAddr *net.UDPAddr) {
go func(url stun.URI, network string, localAddr *net.UDPAddr) {
defer wg.Done()

hostPort := fmt.Sprintf("%s:%d", url.Host, url.Port)
Expand Down Expand Up @@ -444,7 +445,7 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*URL, ne
}
}

func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkTypes []NetworkType) { //nolint:gocognit
func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*stun.URI, networkTypes []NetworkType) { //nolint:gocognit
var wg sync.WaitGroup
defer wg.Wait()

Expand All @@ -455,7 +456,7 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT

for i := range urls {
wg.Add(1)
go func(url URL, network string) {
go func(url stun.URI, network string) {
defer wg.Done()

hostPort := fmt.Sprintf("%s:%d", url.Host, url.Port)
Expand Down Expand Up @@ -518,14 +519,14 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT
}
}

func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //nolint:gocognit
func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { //nolint:gocognit
var wg sync.WaitGroup
defer wg.Wait()

network := NetworkTypeUDP4.String()
for i := range urls {
switch {
case urls[i].Scheme != SchemeTypeTURN && urls[i].Scheme != SchemeTypeTURNS:
case urls[i].Scheme != stun.SchemeTypeTURN && urls[i].Scheme != stun.SchemeTypeTURNS:
continue
case urls[i].Username == "":
a.log.Errorf("Failed to gather relay candidates: %v", ErrUsernameEmpty)
Expand All @@ -536,7 +537,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
}

wg.Add(1)
go func(url URL) {
go func(url stun.URI) {
defer wg.Done()
turnServerAddr := fmt.Sprintf("%s:%d", url.Host, url.Port)
var (
Expand All @@ -548,7 +549,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
)

switch {
case url.Proto == ProtoTypeUDP && url.Scheme == SchemeTypeTURN:
case url.Proto == stun.ProtoTypeUDP && url.Scheme == stun.SchemeTypeTURN:
if locConn, err = a.net.ListenPacket(network, "0.0.0.0:0"); err != nil {
a.log.Warnf("Failed to listen %s: %v", network, err)
return
Expand All @@ -557,8 +558,8 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
relAddr = locConn.LocalAddr().(*net.UDPAddr).IP.String() //nolint:forcetypeassert
relPort = locConn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
relayProtocol = udp
case a.proxyDialer != nil && url.Proto == ProtoTypeTCP &&
(url.Scheme == SchemeTypeTURN || url.Scheme == SchemeTypeTURNS):
case a.proxyDialer != nil && url.Proto == stun.ProtoTypeTCP &&
(url.Scheme == stun.SchemeTypeTURN || url.Scheme == stun.SchemeTypeTURNS):
conn, connectErr := a.proxyDialer.Dial(NetworkTypeTCP4.String(), turnServerAddr)
if connectErr != nil {
a.log.Warnf("Failed to dial TCP address %s via proxy dialer: %v", turnServerAddr, connectErr)
Expand All @@ -567,14 +568,14 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli

relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
if url.Scheme == SchemeTypeTURN {
if url.Scheme == stun.SchemeTypeTURN {
relayProtocol = tcp
} else if url.Scheme == SchemeTypeTURNS {
} else if url.Scheme == stun.SchemeTypeTURNS {
relayProtocol = "tls"
}
locConn = turn.NewSTUNConn(conn)

case url.Proto == ProtoTypeTCP && url.Scheme == SchemeTypeTURN:
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURN:
tcpAddr, connectErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
if connectErr != nil {
a.log.Warnf("Failed to resolve TCP address %s: %v", turnServerAddr, connectErr)
Expand All @@ -591,7 +592,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
relayProtocol = tcp
locConn = turn.NewSTUNConn(conn)
case url.Proto == ProtoTypeUDP && url.Scheme == SchemeTypeTURNS:
case url.Proto == stun.ProtoTypeUDP && url.Scheme == stun.SchemeTypeTURNS:
udpAddr, connectErr := a.net.ResolveUDPAddr(network, turnServerAddr)
if connectErr != nil {
a.log.Warnf("Failed to resolve UDP address %s: %v", turnServerAddr, connectErr)
Expand All @@ -617,7 +618,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
relPort = conn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
relayProtocol = "dtls"
locConn = &fakenet.PacketConn{Conn: conn}
case url.Proto == ProtoTypeTCP && url.Scheme == SchemeTypeTURNS:
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURNS:
tcpAddr, resolvErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
if resolvErr != nil {
a.log.Warnf("Failed to resolve relay address %s: %v", turnServerAddr, resolvErr)
Expand Down
Loading

0 comments on commit ca9de18

Please sign in to comment.