├── Reverse_proxy ├── images ├── request-analysis.png └── server-running.png ├── LICENSE ├── main.go ├── handler.go └── README.md /Reverse_proxy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ymedialabs/ReverseProxy/HEAD/Reverse_proxy -------------------------------------------------------------------------------- /images/request-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ymedialabs/ReverseProxy/HEAD/images/request-analysis.png -------------------------------------------------------------------------------- /images/server-running.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ymedialabs/ReverseProxy/HEAD/images/server-running.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ymedialabs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "net/http/httputil" 9 | "net/url" 10 | ) 11 | 12 | type Prox struct { 13 | target *url.URL 14 | proxy *httputil.ReverseProxy 15 | } 16 | 17 | func NewProxy(target string) *Prox { 18 | url, _ := url.Parse(target) 19 | 20 | return &Prox{target: url, proxy: httputil.NewSingleHostReverseProxy(url)} 21 | } 22 | 23 | func (p *Prox) handle(w http.ResponseWriter, r *http.Request) { 24 | w.Header().Set("X-GoProxy", "GoProxy") 25 | p.proxy.Transport = &myTransport{} 26 | 27 | p.proxy.ServeHTTP(w, r) 28 | 29 | } 30 | 31 | var port *string 32 | var redirecturl *string 33 | 34 | func main() { 35 | const ( 36 | defaultPort = ":9090" 37 | defaultPortUsage = "default server port, ':9090'" 38 | defaultTarget = "http://127.0.0.1:8080" 39 | defaultTargetUsage = "default redirect url, 'http://127.0.0.1:8080'" 40 | ) 41 | 42 | // flags 43 | port = flag.String("port", defaultPort, defaultPortUsage) 44 | redirecturl = flag.String("url", defaultTarget, defaultTargetUsage) 45 | 46 | flag.Parse() 47 | 48 | fmt.Println("server will run on :", *port) 49 | fmt.Println("redirecting to :", *redirecturl) 50 | 51 | // proxy 52 | proxy := NewProxy(*redirecturl) 53 | 54 | http.HandleFunc("/proxyServer", ProxyServer) 55 | 56 | // server redirection 57 | http.HandleFunc("/", proxy.handle) 58 | log.Fatal(http.ListenAndServe(":"+*port, nil)) 59 | } 60 | 61 | func ProxyServer(w http.ResponseWriter, r *http.Request) { 62 | w.Write([]byte("Reverse proxy Server Running. Accepting at port:" + *port + " Redirecting to :" + *redirecturl)) 63 | 64 | } 65 | -------------------------------------------------------------------------------- /handler.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "net/http" 10 | "net/http/httputil" 11 | "time" 12 | ) 13 | 14 | type myTransport struct { 15 | // Uncomment this if you want to capture the transport 16 | // CapturedTransport http.RoundTripper 17 | } 18 | type Montioringpath struct { 19 | Path string 20 | Count int64 21 | Duration int64 22 | AverageTime int64 23 | } 24 | 25 | var globalMap = make(map[string]Montioringpath) 26 | 27 | func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) { 28 | 29 | fmt.Println("---------------------------New Request--------------------------------------------------") 30 | buf, _ := ioutil.ReadAll(request.Body) 31 | rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf)) 32 | rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf)) 33 | 34 | fmt.Println("Request body : ", rdr1) 35 | request.Body = rdr2 // OK since rdr2 implements the 36 | 37 | start := time.Now() 38 | response, err := http.DefaultTransport.RoundTrip(request) 39 | if err != nil { 40 | print("\n\ncame in error resp here", err) 41 | return nil, err //Server is not reachable. Server not working 42 | } 43 | elapsed := time.Since(start) 44 | 45 | key := request.Method + "-" + request.URL.Path //for example for POST Method with /path1 as url path key=POST-/path1 46 | 47 | if val, ok := globalMap[key]; ok { 48 | val.Count = val.Count + 1 49 | val.Duration += elapsed.Nanoseconds() 50 | val.AverageTime = val.Duration / val.Count 51 | globalMap[key] = val 52 | //do something here 53 | } else { 54 | var m Montioringpath 55 | m.Path = request.URL.Path 56 | m.Count = 1 57 | m.Duration = elapsed.Nanoseconds() 58 | m.AverageTime = m.Duration / m.Count 59 | globalMap[key] = m 60 | } 61 | b, err := json.MarshalIndent(globalMap, "", " ") 62 | if err != nil { 63 | fmt.Println("error:", err) 64 | } 65 | 66 | body, err := httputil.DumpResponse(response, true) 67 | if err != nil { 68 | print("\n\nerror in dumb response") 69 | // copying the response body did not work 70 | return nil, err 71 | } 72 | 73 | log.Println("Response Body : ", string(body)) 74 | log.Println("Response Time:", elapsed.Nanoseconds()) 75 | 76 | fmt.Println("Analysis on different Path :", string(b)) 77 | 78 | return response, err 79 | } 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Reverse Proxy In Go With Performance Analysis 4 | 5 | ## Getting Started 6 | 7 | Just Pull this go source code and run the binary file. This is built in order to analyze the HTTP traffic that is coming to your server. 8 | 9 | HTTP Traffic from outside <-> GoProxy Server <-> Main Server 10 | 11 | This goproxy server is responsible for accepting the HTTP request from outside world and route them to the actual server. The current version of this proxy server helps to analyze the server like response time, average response time for a particular path, printing request and response body for every HTTP request etc. The output is set to a terminal where you run the binary file. 12 | 13 | This Server prints request body & response body of every request. Used http.DumpResponse which helps to copy the http response in a string format. 14 | 15 | ### Prerequisites 16 | 17 | Go Version 1.6 18 | if not installed you can follow this documentation https://golang.org/doc/install 19 | 20 | ## Deployment 21 | You can run the program with the "port" and "url" sent as command line arguments. If not set by default Reverse Proxy will be running on port 9090 and it will be redirecting the request to http://localhost:8080. 22 | 23 | Make sure you run your program at 8080 port or specify port while running the program. 24 | 25 | Run Commands: 26 | 27 | If you want to host ReverseProxy on 9090 and redirect the request to the port 5000 , run following command 28 | 29 | ``` 30 | ./ReverseProxy -port=9090 -url=http://localhost:5000 31 | ``` 32 | or 33 | 34 | ``` 35 | ./ReverseProxy 36 | ``` 37 | 38 | Note: If you modify anything in the code please do build the code once so that new binary file will be generated and you run the modified binary go file to get expected results. 39 | 40 | ## Output 41 | 42 | When you run the ReverseProxy server, It will print the port where Reverse Proxy is running and where it is redirecting the request to. 43 | 44 | ![picture](images/server-running.png) 45 | 46 | Following screenshot shows the things that are captured after making one of the login POST request through reverse proxy. 47 | 48 | ![picture](images/request-analysis.png) 49 | 50 | 51 | Whenever there is a http request , we are printing request body and response body along with headers.Along with that we are measuring the time for each api. Currently we are storing the total response time, total no of api calls for a particular path. 52 | 53 | Thanks! 54 | --------------------------------------------------------------------------------