[tor-commits] [websocket/master] More closely match the style of the dummy programs.

dcf at torproject.org dcf at torproject.org
Fri Dec 13 07:14:21 UTC 2013


commit bed953fa7d36e913259de52ec3e52ce29bc4d1a7
Author: David Fifield <david at bamsoftware.com>
Date:   Thu Dec 12 22:20:55 2013 -0800

    More closely match the style of the dummy programs.
---
 README                               |    2 +-
 websocket-client/websocket-client.go |   37 +++++++++++++++++++---------------
 websocket-server/websocket-server.go |   16 ++++++++-------
 3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/README b/README
index ea06122..f978662 100644
--- a/README
+++ b/README
@@ -21,5 +21,5 @@ Git master as of 15 August, 2013 works.
 Add configuration like the following to the relay's torrc. You can
 change the --port option; make sure that port is open in the firewall.
 
-        ExtORPort 5555
+        ExtORPort 6669
         ServerTransportPlugin websocket exec /usr/local/bin/websocket-server --port 9901
diff --git a/websocket-client/websocket-client.go b/websocket-client/websocket-client.go
index 547d217..09181db 100644
--- a/websocket-client/websocket-client.go
+++ b/websocket-client/websocket-client.go
@@ -1,8 +1,9 @@
 // Tor websocket client transport plugin.
 //
-// Usage:
-// ClientTransportPlugin websocket exec ./websocket-client
-
+// Usage in torrc:
+// 	UseBridges 1
+// 	Bridge websocket X.X.X.X:YYYY
+// 	ClientTransportPlugin websocket exec ./websocket-client
 package main
 
 import (
@@ -15,6 +16,7 @@ import (
 	"os"
 	"os/signal"
 	"sync"
+	"syscall"
 	"time"
 )
 
@@ -145,6 +147,7 @@ func handleConnection(conn *pt.SocksConn) error {
 	}
 
 	proxy(conn.Conn.(*net.TCPConn), ws)
+
 	return nil
 }
 
@@ -223,32 +226,34 @@ func main() {
 	pt.CmethodsDone()
 
 	var numHandlers int = 0
+	var sig os.Signal
+	sigChan := make(chan os.Signal, 1)
+	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
 
-	signalChan := make(chan os.Signal, 1)
-	signal.Notify(signalChan, os.Interrupt)
-	var sigint bool = false
-	for !sigint {
+	// wait for first signal
+	sig = nil
+	for sig == nil {
 		select {
 		case n := <-handlerChan:
 			numHandlers += n
-		case <-signalChan:
-			Log("SIGINT")
-			sigint = true
+		case sig = <-sigChan:
 		}
 	}
-
 	for _, ln := range listeners {
 		ln.Close()
 	}
 
-	sigint = false
-	for numHandlers != 0 && !sigint {
+	if sig == syscall.SIGTERM {
+		return
+	}
+
+	// wait for second signal or no more handlers
+	sig = nil
+	for sig == nil && numHandlers != 0 {
 		select {
 		case n := <-handlerChan:
 			numHandlers += n
-		case <-signalChan:
-			Log("SIGINT")
-			sigint = true
+		case sig = <-sigChan:
 		}
 	}
 }
diff --git a/websocket-server/websocket-server.go b/websocket-server/websocket-server.go
index 806dd51..28bcf35 100644
--- a/websocket-server/websocket-server.go
+++ b/websocket-server/websocket-server.go
@@ -1,8 +1,8 @@
 // Tor websocket server transport plugin.
 //
-// Usage:
-// ServerTransportPlugin websocket exec ./websocket-server --port 9901
-
+// Usage in torrc:
+// 	ExtORPort 6669
+// 	ServerTransportPlugin websocket exec ./websocket-server --port 9901
 package main
 
 import (
@@ -141,7 +141,6 @@ func newWebSocketConn(ws *websocket.WebSocket) webSocketConn {
 // Copy from WebSocket to socket and vice versa.
 func proxy(local *net.TCPConn, conn *webSocketConn) {
 	var wg sync.WaitGroup
-
 	wg.Add(2)
 
 	go func() {
@@ -153,7 +152,6 @@ func proxy(local *net.TCPConn, conn *webSocketConn) {
 		conn.Close()
 		wg.Done()
 	}()
-
 	go func() {
 		_, err := io.Copy(local, conn)
 		if err != nil {
@@ -171,19 +169,21 @@ func webSocketHandler(ws *websocket.WebSocket) {
 	// Undo timeouts on HTTP request handling.
 	ws.Conn.SetDeadline(time.Time{})
 	conn := newWebSocketConn(ws)
+	defer conn.Close()
 
 	handlerChan <- 1
 	defer func() {
 		handlerChan <- -1
 	}()
 
-	s, err := pt.DialOr(&ptInfo, ws.Conn.RemoteAddr(), ptMethodName)
+	or, err := pt.DialOr(&ptInfo, ws.Conn.RemoteAddr(), ptMethodName)
 	if err != nil {
 		log("Failed to connect to ORPort: " + err.Error())
 		return
 	}
+	defer or.Close()
 
-	proxy(s, &conn)
+	proxy(or, &conn)
 }
 
 func startListener(addr *net.TCPAddr) (*net.TCPListener, error) {
@@ -258,6 +258,7 @@ func main() {
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
 
+	// wait for first signal
 	sig = nil
 	for sig == nil {
 		select {
@@ -276,6 +277,7 @@ func main() {
 		return
 	}
 
+	// wait for second signal or no more handlers
 	sig = nil
 	for sig == nil && numHandlers != 0 {
 		select {





More information about the tor-commits mailing list