From efd5d0e410699c381551909f0c3a967222f11590 Mon Sep 17 00:00:00 2001 From: Martynas Pumputis Date: Sun, 13 May 2018 10:27:42 +0200 Subject: [PATCH] Add /client_ip to network-tester The endpoint is going to be used by --no-masq-local integration tests. --- .gitignore | 1 + test/images/network-tester/webserver.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index 9671228f..bcccc7f2 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,7 @@ test/tls/tls test/tls/*.pem test/coverage test/coverage.* +test/images/network-tester/webserver *qemu-*-static terraform.tfstate terraform.tfstate.backup diff --git a/test/images/network-tester/webserver.go b/test/images/network-tester/webserver.go index e27fd0e6..4315a2aa 100644 --- a/test/images/network-tester/webserver.go +++ b/test/images/network-tester/webserver.go @@ -13,6 +13,8 @@ // Visit /status to see pass/running/fail determination. (literally, it will // return one of those words.) // +// Visit /client_ip to see the source IP addr of the request. +// // /write is used by other network test servers to register connectivity. package main @@ -78,6 +80,20 @@ func (s *State) serveStatus(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "fail") } +// serveClientIP returns the client source IP addr. +func (s *State) serveClientIP(w http.ResponseWriter, r *http.Request) { + s.lock.Lock() + defer s.lock.Unlock() + + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + go s.Logf("Warning: unable to parse remote addr: %s", err) + w.WriteHeader(http.StatusInternalServerError) + } + + fmt.Fprintf(w, host) +} + // serveRead writes our json encoded state func (s *State) serveRead(w http.ResponseWriter, r *http.Request) { s.lock.Lock() @@ -186,6 +202,7 @@ func main() { http.HandleFunc("/read", state.serveRead) http.HandleFunc("/write", state.serveWrite) http.HandleFunc("/status", state.serveStatus) + http.HandleFunc("/client_ip", state.serveClientIP) go log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *port), nil))