[tor-commits] [snowflake/master] Factor out a function to extract the client IP address.

dcf at torproject.org dcf at torproject.org
Wed Oct 18 05:14:25 UTC 2017


commit 83f87120788c2c25f9c8ac1363711d5146c097d9
Author: David Fifield <david at bamsoftware.com>
Date:   Tue Oct 17 21:39:04 2017 -0700

    Factor out a function to extract the client IP address.
---
 server/server.go | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/server/server.go b/server/server.go
index 562ccf8..29e8137 100644
--- a/server/server.go
+++ b/server/server.go
@@ -128,6 +128,16 @@ func proxy(local *net.TCPConn, conn *webSocketConn) {
 	wg.Wait()
 }
 
+// Return an address string suitable to pass into pt.DialOr.
+func clientAddr(clientIPParam string) string {
+	// Check if client addr is a valid IP
+	clientIP := net.ParseIP(clientIPParam)
+	if clientIP == nil {
+		return ""
+	}
+	return clientIPParam
+}
+
 func webSocketHandler(ws *websocket.WebSocket) {
 	// Undo timeouts on HTTP request handling.
 	ws.Conn.SetDeadline(time.Time{})
@@ -139,17 +149,9 @@ func webSocketHandler(ws *websocket.WebSocket) {
 		handlerChan <- -1
 	}()
 
-	// Check if client addr is a valid IP
-	addr := ws.Request().URL.Query().Get("client_ip")
-	clientIP := net.ParseIP(addr)
-
-	if clientIP == nil {
-		// Set client addr to empty
-		addr = ""
-	}
-
 	// Pass the address of client as the remote address of incoming connection
-	or, err := pt.DialOr(&ptInfo, addr, ptMethodName)
+	clientIPParam := ws.Request().URL.Query().Get("client_ip")
+	or, err := pt.DialOr(&ptInfo, clientIPParam, ptMethodName)
 
 	if err != nil {
 		log.Printf("failed to connect to ORPort: %s", err)





More information about the tor-commits mailing list