commit fb9a61125ca148256d965fe875c3973a0998d7b3 Author: David Fifield david@bamsoftware.com Date: Mon Nov 26 01:44:39 2012 -0800
Proxy ORPort to WebSocket. --- websocket-transport/websocket-server.go | 49 +++++++++++++++++++++++++++++- 1 files changed, 47 insertions(+), 2 deletions(-)
diff --git a/websocket-transport/websocket-server.go b/websocket-transport/websocket-server.go index 8b62b60..fcdcd7f 100644 --- a/websocket-transport/websocket-server.go +++ b/websocket-transport/websocket-server.go @@ -4,15 +4,19 @@ import ( "encoding/base64" "errors" "fmt" + "io" "net" "net/http" "os" "os/signal" + "sync" "time" )
const defaultPort = 9901
+var ptInfo ptServerInfo + // When a connection handler starts, +1 is written to this channel; when it // ends, -1 is written. var handlerChan = make(chan int) @@ -114,8 +118,49 @@ func NewWebsocketConn(ws *websocket) websocketConn { return conn }
+func proxy(local *net.TCPConn, conn *websocketConn) { + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + _, err := io.Copy(conn, local) + if err != nil { + logDebug("error copying ORPort to WebSocket: " + err.Error()) + } + local.CloseRead() + conn.Close() + wg.Done() + }() + + go func() { + _, err := io.Copy(local, conn) + if err != nil { + logDebug("error copying WebSocket to ORPort: " + err.Error()) + } + local.CloseWrite() + conn.Close() + wg.Done() + }() + + wg.Wait() +} + func websocketHandler(ws *websocket) { - fmt.Printf("blah\n") + conn := NewWebsocketConn(ws) + + handlerChan <- 1 + defer func() { + handlerChan <- -1 + }() + + s, err := net.DialTCP("tcp", nil, ptInfo.OrAddr) + if err != nil { + logDebug("Failed to connect to ORPort: " + err.Error()) + return + } + + proxy(s, &conn) }
func startListener(addr *net.TCPAddr) (*net.TCPListener, error) { @@ -139,7 +184,7 @@ func startListener(addr *net.TCPAddr) (*net.TCPListener, error) { func main() { const ptMethodName = "websocket"
- ptInfo := ptServerSetup([]string{ptMethodName}) + ptInfo = ptServerSetup([]string{ptMethodName})
listeners := make([]*net.TCPListener, 0) for _, bindAddr := range ptInfo.BindAddrs {