Skip to content

Commit

Permalink
Support transport.Net for encrypted connections
Browse files Browse the repository at this point in the history
Until now, TLS and DTLS backed STUN clients
did not use a custom transport.Net.
  • Loading branch information
stv0g committed May 10, 2023
1 parent 1b14963 commit e25856d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,33 @@ func DialURI(uri *URI, cfg *DialConfig) (*Client, error) {
}

if conn, err = nw.Dial(network, addr); err != nil {
return nil, fmt.Errorf("failed to listen: %w", err)
return nil, fmt.Errorf("failed to dial: %w", err)
}

case uri.Scheme == SchemeTypeTURNS && uri.Proto == ProtoTypeUDP:
var udpAddr *net.UDPAddr

if udpAddr, err = nw.ResolveUDPAddr("udp", addr); err != nil {
return nil, fmt.Errorf("failed to resolve address '%s': %w", addr, err)
}
dtlsCfg := cfg.DTLSConfig // Copy
dtlsCfg.ServerName = uri.Host

var dtlsCfg dtls.Config
if cfg != nil {
dtlsCfg = cfg.DTLSConfig
udpConn, err := nw.Dial("udp", addr)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
}

dtlsCfg.ServerName = uri.Host

if conn, err = dtls.Dial("udp", udpAddr, &dtlsCfg); err != nil {
if conn, err = dtls.Client(udpConn, &dtlsCfg); err != nil {
return nil, fmt.Errorf("failed to connect to '%s': %w", addr, err)
}

case (uri.Scheme == SchemeTypeTURNS || uri.Scheme == SchemeTypeSTUNS) && uri.Proto == ProtoTypeTCP:
var tlsCfg tls.Config
if cfg != nil {
tlsCfg = cfg.TLSConfig //nolint:govet
}
tlsCfg := cfg.TLSConfig //nolint:govet
tlsCfg.ServerName = uri.Host

if conn, err = tls.Dial("tcp", addr, &tlsCfg); err != nil {
return nil, fmt.Errorf("failed to connect to '%s': %w", addr, err)
tcpConn, err := nw.Dial("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
}

conn = tls.Client(tcpConn, &tlsCfg)

default:
return nil, ErrUnsupportedURI
}
Expand Down

0 comments on commit e25856d

Please sign in to comment.