From c3abd2aeb2b43e2c68b71b48449a5fa0de0f2fb8 Mon Sep 17 00:00:00 2001
From: Kuat <kyessenov@users.noreply.github.com>
Date: Thu, 12 Jan 2017 14:52:56 -0800
Subject: [PATCH] Update prototype to use iptables (#42)

---
 src/envoy/prototype/README.md      | 13 +++++++++++++
 src/envoy/prototype/envoy-esp.conf | 11 +++++++++--
 test/backend/echo/echo.go          | 21 +++++++--------------
 3 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/src/envoy/prototype/README.md b/src/envoy/prototype/README.md
index 2ddbe98459c..00e73a169d6 100644
--- a/src/envoy/prototype/README.md
+++ b/src/envoy/prototype/README.md
@@ -36,6 +36,19 @@ This Proxy will use Envoy and talk to Mixer server.
   go run echo.go
 ```
 
+* Modify your iptables:
+
+```
+  sudo iptables -t nat -A OUTPUT -p tcp --dport 9090 -j REDIRECT --to-port 9092
+```
+
+Once you are done, you should remove this rule:
+
+```
+  sudo iptables -t nat -D OUTPUT -p tcp --dport 9090 -j REDIRECT --to-port 9092
+```
+
+
 * Start Envoy proxy, run
 
 ```
diff --git a/src/envoy/prototype/envoy-esp.conf b/src/envoy/prototype/envoy-esp.conf
index dff3bd67b1a..11690bed565 100644
--- a/src/envoy/prototype/envoy-esp.conf
+++ b/src/envoy/prototype/envoy-esp.conf
@@ -1,7 +1,14 @@
 {
   "listeners": [
+    {
+      "port": 9092,
+      "bind_to_port": true,
+      "use_original_dst": true,
+      "filters": []
+    },
     {
       "port": 9090,
+      "bind_to_port": false,
       "filters": [
         {
           "type": "read",
@@ -26,7 +33,7 @@
             },
             "access_log": [
               {
-                "path": "/tmp/access.envoy"
+                "path": "/dev/stdout"
               }
             ],
             "filters": [
@@ -50,7 +57,7 @@
     }
   ],
   "admin": {
-    "access_log_path": "/tmp/access.envoy",
+    "access_log_path": "/dev/stdout",
     "port": 9001
   },
   "cluster_manager": {
diff --git a/test/backend/echo/echo.go b/test/backend/echo/echo.go
index 51aaa07e353..ef13f515ff1 100644
--- a/test/backend/echo/echo.go
+++ b/test/backend/echo/echo.go
@@ -23,19 +23,22 @@ import (
 	"io/ioutil"
 	"net/http"
 	"strconv"
-	"sync"
-	"time"
 )
 
 var (
 	port = flag.Int("port", 8080, "default http port")
 
-	mu       sync.Mutex
 	requests = 0
 	data     = 0
 )
 
 func handler(w http.ResponseWriter, r *http.Request) {
+	fmt.Printf("%v %v %v %v\n", r.Method, r.URL, r.Proto, r.RemoteAddr)
+	for name, headers := range r.Header {
+		for _, h := range headers {
+			fmt.Printf("%v: %v\n", name, h)
+		}
+	}
 	body, err := ioutil.ReadAll(r.Body)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -51,24 +54,14 @@ func handler(w http.ResponseWriter, r *http.Request) {
 	w.WriteHeader(http.StatusOK)
 	w.Write(body)
 
-	mu.Lock()
 	requests++
 	data += len(body)
-	defer mu.Unlock()
+	fmt.Printf("Requests Requests: %v  Data: %v\n", requests, data)
 }
 
 func main() {
 	flag.Parse()
 
-	go func() {
-		for {
-			mu.Lock()
-			fmt.Printf("Requests Requests: %v  Data: %v\n", requests, data)
-			mu.Unlock()
-			time.Sleep(time.Second)
-		}
-	}()
-
 	fmt.Printf("Listening on port %v\n", *port)
 
 	http.HandleFunc("/", handler)