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 Jan 22, 2023
1 parent 3749155 commit 976e12c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
32 changes: 14 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -65,6 +66,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit 976e12c

Please sign in to comment.