From d3417a1c79809e4100df570135af2fd0d87863cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 20 Jan 2017 02:19:49 +0100 Subject: [PATCH] Implemented "proxyRequest" method --- udsProxy/src/uds/udsProxy/udsProxy.go | 88 ++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/udsProxy/src/uds/udsProxy/udsProxy.go b/udsProxy/src/uds/udsProxy/udsProxy.go index 6580cd8c0..9cb278231 100644 --- a/udsProxy/src/uds/udsProxy/udsProxy.go +++ b/udsProxy/src/uds/udsProxy/udsProxy.go @@ -1,7 +1,11 @@ package main import ( + "bytes" + "encoding/json" + "errors" "fmt" + "io/ioutil" "log" "net" "net/http" @@ -17,18 +21,27 @@ const configFilename = "/etc/udsproxy.cfg" var config struct { Server string // Server Type, "http" or "https" Port string // Server port - Broker string // Broker address - UseSSL bool // If use https for connecting with broker: Warning, certificate must be valid on Broker SSLCertificateFile string // Certificate file SSLCertificateKeyFile string // Certificate key + Broker string // Broker address + UseSSL bool // If use https for connecting with broker: Warning, certificate must be valid on Broker +} + +func validOrigin(w http.ResponseWriter, r *http.Request) error { + if strings.Split(r.RemoteAddr, ":")[0] != config.Broker { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "Access denied") + return errors.New("Invalid Origin") + } + return nil } // Test service func testService(w http.ResponseWriter, r *http.Request) { - if strings.Split(r.RemoteAddr, ":")[0] != config.Broker { - w.WriteHeader(http.StatusForbidden) - fmt.Fprintf(w, "Access denied") + if validOrigin(w, r) != nil { + return } + r.ParseForm() ip, port, timeOutStr := r.FormValue("ip"), r.FormValue("port"), r.FormValue("timeout") if ip == "" || port == "" { @@ -55,6 +68,61 @@ func testService(w http.ResponseWriter, r *http.Request) { } } +func proxyRequest(w http.ResponseWriter, r *http.Request) { + if validOrigin(w, r) != nil { + return + } + log.Print("Proxy Request from ", r.RemoteAddr) + // Content is always a POST, and we have json on request body + // If recovered json contains "data", then we must produce a POST to service + // url to request is on "url" json variable + var body struct { + Data json.RawMessage + URL string + } + + dec := json.NewDecoder(r.Body) + + if err := dec.Decode(&body); err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Error in Json: %s", err) + log.Fatal(err) + return + } + + var method string + if body.Data != nil { + log.Print("POSTING request to ", body.URL) + method = "POST" + } else { + log.Print("GETTING request from ", body.URL) + method = "GET" + } + + req, err := http.NewRequest(method, body.URL, bytes.NewBuffer(body.Data)) + + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Error building request: %s", err) + return + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{ + Timeout: time.Duration(5) * time.Second, + } + resp, err := client.Do(req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Error in POST: %s", err) + return + } + defer resp.Body.Close() + w.WriteHeader(resp.StatusCode) + b, _ := ioutil.ReadAll(resp.Body) + w.Write(b) +} + func actor(w http.ResponseWriter, r *http.Request) { // Get Request as received and forward it to UDS, withouch changing anything... // We will net params, body, etc... @@ -83,9 +151,13 @@ func main() { // Read config cfg.MapTo(&config) - fmt.Println("Broker address: ", config.Broker, ", Server type & port: ", config.Server, config.Port) - http.HandleFunc("/actor", actor) // set router - http.HandleFunc("/testService", testService) + log.Printf("Broker address: %s", config.Broker) + log.Printf("Server type: %s", config.Server) + log.Printf("Server port: %s", config.Port) + + http.HandleFunc("/actor", actor) // set router for "actor" requests + http.HandleFunc("/testService", testService) // test service + http.HandleFunc("/proxyRequest", proxyRequest) // Proxy request from broker to service if config.Server == "https" { err = http.ListenAndServeTLS(":"+config.Port, config.SSLCertificateFile, config.SSLCertificateKeyFile, nil) // set listen port } else {