Skip to content

Commit

Permalink
Merge pull request #24 from mysteriumnetwork/feature/e2e-test
Browse files Browse the repository at this point in the history
Gracefully handle HTTP requests with close header
  • Loading branch information
Waldz authored Nov 18, 2022
2 parents 7b25396 + 42b313e commit 1852acd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang:1.18-alpine

# Install packages
RUN apk add --update --no-cache bash git gcc musl-dev make iptables bind-tools
RUN apk add --update --no-cache bash git gcc musl-dev make iptables bind-tools curl

# Install application
WORKDIR /go/src/github.com/mysteriumnetwork/openvpn-forwarder
Expand Down
29 changes: 28 additions & 1 deletion e2e/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,37 @@ func TestHTTP(t *testing.T) {
assert.NotEqualf(t, originalIP, currentIP, "Request proxying failed: %s -> %s", originalIP, currentIP)
}

func TestHTTPWithCloseHeader(t *testing.T) {
// given
originalIP, err := checkIP("http://api.ipify.org/?format=text")
t.Log("Original IP:", originalIP)
assert.NoError(t, err)

forwarderIP, err := getForwarderIP()
assert.NoError(t, err)

// when
redirectRule := []string{"OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", "DNAT", "--to-destination", forwarderIP + ":8443"}
err = ipTablesAppend(redirectRule...)
defer ipTablesDelete(redirectRule...)
assert.NoError(t, err)

currentIP, err := sh.Output(
"curl", "-s",
"--http1.1",
"-H", "Connection: close",
"http://api.ipify.org/?format=text",
)
t.Log("Current IP:", currentIP)
assert.NoError(t, err)

assert.NotEqualf(t, originalIP, currentIP, "Request proxying failed: %s -> %s", originalIP, currentIP)
}

func getForwarderIP() (string, error) {
return sh.Output("dig", "forwarder", "+short")
}

func checkIP(apiURL string) (string, error) {
return sh.Output("wget", "-q", "-O", "-", apiURL)
return sh.Output("curl", "-s", apiURL)
}
5 changes: 2 additions & 3 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package proxy

import (
"bufio"
"crypto/tls"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"

Expand Down Expand Up @@ -94,8 +94,7 @@ func (s *proxyServer) handler(l net.Listener, f func(c net.Conn)) {
}

func (s *proxyServer) serveHTTP(c net.Conn) {
sc := httputil.NewServerConn(c, nil)
req, err := sc.Read()
req, err := http.ReadRequest(bufio.NewReader(c))
if err != nil {
log.Printf("Failed to read HTTP request: %v", err)
return
Expand Down

0 comments on commit 1852acd

Please sign in to comment.