Add /client_ip to network-tester

The endpoint is going to be used by --no-masq-local integration tests.
This commit is contained in:
Martynas Pumputis committed 2018-06-04 14:08:24 +02:00
1 parent 6dbe95a0b5
commit efd5d0e410
2 files changed
+18

No files matched your search

+1
View File
@@ -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
+17
View File
@@ -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))