Browse Source

Add IPv6 support

pull/14/head
cacggghp 4 months ago
committed by GitHub
parent
commit
ceee08d44e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 23
      client/main.go

23
client/main.go

@ -612,7 +612,7 @@ func oneTurnConnection(ctx context.Context, turnParams *turnParams, peer *net.UD
ctx1, cancel := context.WithTimeout(ctx, 5*time.Second) ctx1, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel() defer cancel()
if turnParams.udp { if turnParams.udp {
conn, err2 := net.DialUDP("udp4", nil, turnServerUdpAddr) // nolint: noctx conn, err2 := net.DialUDP("udp", nil, turnServerUdpAddr) // nolint: noctx
if err2 != nil { if err2 != nil {
err = fmt.Errorf("failed to connect to TURN server: %s", err2) err = fmt.Errorf("failed to connect to TURN server: %s", err2)
return return
@ -625,7 +625,7 @@ func oneTurnConnection(ctx context.Context, turnParams *turnParams, peer *net.UD
}() }()
turnConn = &connectedUDPConn{conn} turnConn = &connectedUDPConn{conn}
} else { } else {
conn, err2 := d.DialContext(ctx1, "tcp4", turnServerAddr) // nolint: noctx conn, err2 := d.DialContext(ctx1, "tcp", turnServerAddr) // nolint: noctx
if err2 != nil { if err2 != nil {
err = fmt.Errorf("failed to connect to TURN server: %s", err2) err = fmt.Errorf("failed to connect to TURN server: %s", err2)
return return
@ -638,15 +638,22 @@ func oneTurnConnection(ctx context.Context, turnParams *turnParams, peer *net.UD
}() }()
turnConn = turn.NewSTUNConn(conn) turnConn = turn.NewSTUNConn(conn)
} }
var addrFamily turn.RequestedAddressFamily
if peer.IP.To4() != nil {
addrFamily = turn.RequestedAddressFamilyIPv4
} else {
addrFamily = turn.RequestedAddressFamilyIPv6
}
// Start a new TURN Client and wrap our net.Conn in a STUNConn // Start a new TURN Client and wrap our net.Conn in a STUNConn
// This allows us to simulate datagram based communication over a net.Conn // This allows us to simulate datagram based communication over a net.Conn
cfg = &turn.ClientConfig{ cfg = &turn.ClientConfig{
STUNServerAddr: turnServerAddr, STUNServerAddr: turnServerAddr,
TURNServerAddr: turnServerAddr, TURNServerAddr: turnServerAddr,
Conn: turnConn, Conn: turnConn,
Username: user, Username: user,
Password: pass, Password: pass,
LoggerFactory: logging.NewDefaultLoggerFactory(), RequestedAddressFamily: addrFamily,
LoggerFactory: logging.NewDefaultLoggerFactory(),
} }
client, err1 := turn.NewClient(cfg) client, err1 := turn.NewClient(cfg)

Loading…
Cancel
Save