Commit 904af9cb authored by David Fifield's avatar David Fifield
Browse files

Let copyLoop exit when either direction finishes.

Formerly we waiting until *both* directions finished. What this meant in
practice is that when the remote connection ended, copyLoop would become
useless but would continue blocking its caller until something else
finally closed the socks connection.
parent ee2fb42d
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"io" "io"
"log" "log"
"net" "net"
"sync"
"time" "time"
) )
...@@ -41,20 +40,19 @@ func Handler(socks net.Conn, snowflakes SnowflakeCollector) error { ...@@ -41,20 +40,19 @@ func Handler(socks net.Conn, snowflakes SnowflakeCollector) error {
// Exchanges bytes between two ReadWriters. // Exchanges bytes between two ReadWriters.
// (In this case, between a SOCKS and WebRTC connection.) // (In this case, between a SOCKS and WebRTC connection.)
func copyLoop(socks, webRTC io.ReadWriter) { func copyLoop(socks, webRTC io.ReadWriter) {
var wg sync.WaitGroup done := make(chan struct{}, 2)
wg.Add(2)
go func() { go func() {
if _, err := io.Copy(socks, webRTC); err != nil { if _, err := io.Copy(socks, webRTC); err != nil {
log.Printf("copying WebRTC to SOCKS resulted in error: %v", err) log.Printf("copying WebRTC to SOCKS resulted in error: %v", err)
} }
wg.Done() done <- struct{}{}
}() }()
go func() { go func() {
if _, err := io.Copy(webRTC, socks); err != nil { if _, err := io.Copy(webRTC, socks); err != nil {
log.Printf("copying SOCKS to WebRTC resulted in error: %v", err) log.Printf("copying SOCKS to WebRTC resulted in error: %v", err)
} }
wg.Done() done <- struct{}{}
}() }()
wg.Wait() <-done
log.Println("copy loop ended") log.Println("copy loop ended")
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment