[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [meek/master] Factor out code to get the original client IP address.



commit 1377d0e6ee7a21b441e718729f8b08180df1ceee
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Sun Dec 13 22:07:06 2015 -0800

    Factor out code to get the original client IP address.
    
    This is working towards #13171. Currently it's just getting
    req.RemoteAddr as before, except that it's now setting the port number
    to ":0" in all cases.
---
 meek-server/meek-server.go |   14 +++++++++++++-
 meek-server/useraddr.go    |   20 ++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/meek-server/meek-server.go b/meek-server/meek-server.go
index 5b28e79..482fa3c 100644
--- a/meek-server/meek-server.go
+++ b/meek-server/meek-server.go
@@ -122,6 +122,18 @@ func (state *State) Get(w http.ResponseWriter, req *http.Request) {
 	w.Write([]byte("Iâ??m just a happy little web server.\n"))
 }
 
+// Get a string representing the original client address, if available, as a
+// "host:port" string suitable to pass as the addr parameter to pt.DialOr. Never
+// fails: if the original client address is not available, returns "". If the
+// original client address is available, the returned port number is always 1.
+func getUseraddr(req *http.Request) string {
+	ip, err := originalClientIP(req)
+	if err != nil {
+		return ""
+	}
+	return net.JoinHostPort(ip.String(), "1")
+}
+
 // Look up a session by id, or create a new one (with its OR port connection) if
 // it doesn't already exist.
 func (state *State) GetSession(sessionID string, req *http.Request) (*Session, error) {
@@ -132,7 +144,7 @@ func (state *State) GetSession(sessionID string, req *http.Request) (*Session, e
 	if session == nil {
 		// log.Printf("unknown session id %q; creating new session", sessionID)
 
-		or, err := pt.DialOr(&ptInfo, req.RemoteAddr, ptMethodName)
+		or, err := pt.DialOr(&ptInfo, getUseraddr(req), ptMethodName)
 		if err != nil {
 			return nil, err
 		}
diff --git a/meek-server/useraddr.go b/meek-server/useraddr.go
new file mode 100644
index 0000000..6695d6d
--- /dev/null
+++ b/meek-server/useraddr.go
@@ -0,0 +1,20 @@
+package main
+
+import (
+	"fmt"
+	"net"
+	"net/http"
+)
+
+// Return the original client IP address as best as it can be determined.
+func originalClientIP(req *http.Request) (net.IP, error) {
+	host, _, err := net.SplitHostPort(req.RemoteAddr)
+	if err != nil {
+		return nil, err
+	}
+	ip := net.ParseIP(host)
+	if ip == nil {
+		return nil, fmt.Errorf("cannot parse %q as IP address")
+	}
+	return ip, nil
+}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits