1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Implemented "proxyRequest" method

This commit is contained in:
Adolfo Gómez García 2017-01-20 02:19:49 +01:00
parent 1a2619c170
commit d3417a1c79

View File

@ -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 {