Skip to content

Commit

Permalink
add x-forwarded-for test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasen committed Dec 6, 2015
1 parent 81086ab commit 05c043f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func handleConn(c net.Conn) {

// check if it's HTTP request
if bytes.Contains(line, []byte("HTTP")) {
hdrXxf := "X-Forwarded-For: " + ipAddrFromRemoteAddr(c.RemoteAddr().String());
hdrXff := "X-Forwarded-For: " + ipAddrFromRemoteAddr(c.RemoteAddr().String());
header = bytes.NewBuffer(line)
header.Write([]byte("\n"))
cipherAddr = []byte{}
Expand All @@ -201,7 +201,7 @@ func handleConn(c net.Conn) {
}

if bytes.HasPrefix(bytes.ToLower(line), _xxfRequestHeader) {
hdrXxf = hdrXxf + "," + string(bytes.TrimSpace(line[(len(_xxfRequestHeader) + 1):]))
hdrXff = hdrXff + ", " + string(bytes.TrimSpace(line[(len(_xxfRequestHeader) + 1):]))
continue
}

Expand All @@ -211,8 +211,8 @@ func handleConn(c net.Conn) {
c.Write([]byte{0x08})
return
}
if len(hdrXxf) == 0 {
header.Write([]byte(hdrXxf))
if len(hdrXff) > 0 {
header.Write([]byte(hdrXff))
header.Write([]byte("\n"))
}
header.Write(line)
Expand Down
8 changes: 6 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func TestMain(m *testing.M) {

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
if len(r.Header["X-Forwarded-For"]) > 0 {
w.Write([]byte(r.Header["X-Forwarded-For"][0]))
}
})
go http.ListenAndServe(string(_httpServerAddr), nil)

Expand Down Expand Up @@ -174,6 +177,7 @@ func TestHTTPServer(t *testing.T) {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://"+string(_defaultFrontdAddr), nil)
req.Header.Set(string(_cipherRequestHeader), string(cipherAddr))
req.Header.Set("X-Forwarded-For", "8.8.8.8, 8.8.4.4")
res, err := client.Do(req)
if err != nil {
panic(err)
Expand All @@ -184,7 +188,7 @@ func TestHTTPServer(t *testing.T) {
panic(err)
}

if bytes.Compare(b, []byte("OK")) != 0 {
if !bytes.HasPrefix(b, []byte("OK127.0.0.1")) {
t.Fail()
}
}
Expand Down Expand Up @@ -228,7 +232,7 @@ func TestProtocolDecrypt(*testing.T) {
testProtocol(b)
}

// TODO: test x-forwarded-for
// TODO: more test with and with out x-forwarded-for

// TODO: test decryption with extra bytes in packet and check data

Expand Down

0 comments on commit 05c043f

Please sign in to comment.