|
|
|
@ -52,7 +52,7 @@ func dtlsFunc(ctx context.Context, conn net.PacketConn, peer *net.UDPAddr) (net. |
|
|
|
return dtlsConn, nil |
|
|
|
} |
|
|
|
|
|
|
|
func oneDtlsConnection(ctx context.Context, peer *net.UDPAddr, listenConn net.PacketConn, connchan chan<- net.PacketConn, okchan chan<- struct{}, c chan<- error, sessionID []byte, streamID byte) { |
|
|
|
func oneDtlsConnection(ctx context.Context, peer *net.UDPAddr, listenConn net.PacketConn, connchan chan<- net.PacketConn, okchan chan<- struct{}, c chan<- error, sessionID []byte, streamID byte, v1 bool) { |
|
|
|
var err error = nil |
|
|
|
defer func() { c <- err }() |
|
|
|
dtlsctx, dtlscancel := context.WithCancel(ctx) |
|
|
|
@ -81,17 +81,20 @@ func oneDtlsConnection(ctx context.Context, peer *net.UDPAddr, listenConn net.Pa |
|
|
|
log.Printf("Closed DTLS connection\n") |
|
|
|
}() |
|
|
|
|
|
|
|
// Phase 1: Send Session ID + Stream ID (17 bytes)
|
|
|
|
dtlsConn.SetWriteDeadline(time.Now().Add(time.Second * 5)) |
|
|
|
idBuf := make([]byte, 17) |
|
|
|
copy(idBuf[:16], sessionID) |
|
|
|
idBuf[16] = streamID |
|
|
|
if _, err1 = dtlsConn.Write(idBuf); err1 != nil { |
|
|
|
err = fmt.Errorf("failed to send session ID: %s", err1) |
|
|
|
return |
|
|
|
// Phase 1: Send Session ID + Stream ID (17 bytes) - only for v2 protocol
|
|
|
|
if !v1 { |
|
|
|
dtlsConn.SetWriteDeadline(time.Now().Add(time.Second * 5)) |
|
|
|
idBuf := make([]byte, 17) |
|
|
|
copy(idBuf[:16], sessionID) |
|
|
|
idBuf[16] = streamID |
|
|
|
if _, err1 = dtlsConn.Write(idBuf); err1 != nil { |
|
|
|
err = fmt.Errorf("failed to send session ID: %s", err1) |
|
|
|
return |
|
|
|
} |
|
|
|
log.Printf("Established DTLS connection and sent session ID with stream %d!\n", streamID) |
|
|
|
} else { |
|
|
|
log.Printf("Established DTLS connection (v1 protocol, no session ID)!\n") |
|
|
|
} |
|
|
|
|
|
|
|
log.Printf("Established DTLS connection and sent session ID with stream %d!\n", streamID) |
|
|
|
go func() { |
|
|
|
for { |
|
|
|
select { |
|
|
|
@ -369,14 +372,14 @@ func oneTurnConnection(ctx context.Context, turnParams *turnParams, peer *net.UD |
|
|
|
conn2.SetDeadline(time.Time{}) |
|
|
|
} |
|
|
|
|
|
|
|
func oneDtlsConnectionLoop(ctx context.Context, peer *net.UDPAddr, listenConnChan <-chan net.PacketConn, connchan chan<- net.PacketConn, okchan chan<- struct{}, sessionID []byte, streamID byte) { |
|
|
|
func oneDtlsConnectionLoop(ctx context.Context, peer *net.UDPAddr, listenConnChan <-chan net.PacketConn, connchan chan<- net.PacketConn, okchan chan<- struct{}, sessionID []byte, streamID byte, v1 bool) { |
|
|
|
for { |
|
|
|
select { |
|
|
|
case <-ctx.Done(): |
|
|
|
return |
|
|
|
case listenConn := <-listenConnChan: |
|
|
|
c := make(chan error) |
|
|
|
go oneDtlsConnection(ctx, peer, listenConn, connchan, okchan, c, sessionID, streamID) |
|
|
|
go oneDtlsConnection(ctx, peer, listenConn, connchan, okchan, c, sessionID, streamID, v1) |
|
|
|
if err := <-c; err != nil { |
|
|
|
log.Printf("%s", err) |
|
|
|
} |
|
|
|
@ -432,6 +435,7 @@ func main() { //nolint:cyclop |
|
|
|
n := flag.Int("n", 0, "connections to TURN (default 4)") |
|
|
|
udp := flag.Bool("udp", false, "connect to TURN with UDP") |
|
|
|
direct := flag.Bool("no-dtls", false, "connect without obfuscation. DO NOT USE") |
|
|
|
v1 := flag.Bool("v1", false, "use v1 server protocol (no session_id and stream_id)") |
|
|
|
sessionIDFlag := flag.String("session-id", "", "override session ID (hex, 32 chars)") |
|
|
|
flag.Parse() |
|
|
|
if *peerAddr == "" { |
|
|
|
@ -525,7 +529,7 @@ func main() { //nolint:cyclop |
|
|
|
wg1.Add(1) |
|
|
|
go func() { |
|
|
|
defer wg1.Done() |
|
|
|
oneDtlsConnectionLoop(ctx, peer, listenConnChan, connchan, okchan, sessionID, 0) |
|
|
|
oneDtlsConnectionLoop(ctx, peer, listenConnChan, connchan, okchan, sessionID, 0, *v1) |
|
|
|
}() |
|
|
|
|
|
|
|
wg1.Add(1) |
|
|
|
@ -544,7 +548,7 @@ func main() { //nolint:cyclop |
|
|
|
wg1.Add(1) |
|
|
|
go func(sID byte) { |
|
|
|
defer wg1.Done() |
|
|
|
oneDtlsConnectionLoop(ctx, peer, listenConnChan, connchan, nil, sessionID, sID) |
|
|
|
oneDtlsConnectionLoop(ctx, peer, listenConnChan, connchan, nil, sessionID, sID, *v1) |
|
|
|
}(byte(streamID)) |
|
|
|
wg1.Add(1) |
|
|
|
go func() { |
|
|
|
|