1

I am working on a reverse proxy that proxies to different remote machines based on the service name specified by the path. In this scenario I have a situation similar to this:

remote1:
-route1:
---file1
-route2:
---nestedRoute:
------file2

A request looks like:
Client ---> fqdn/service1/route2/nestedRoute --> remote1
The response is (I modify the path to asset in the reverse proxy --see below):
Client <--- fqdn/route2/nestedRoute <-- remote1

Desired response in browser:
Client <--- fqdn/service1/route2/nestedRoute <-- remote1

How can inject service1 back into the path like the above so that a subsequent request will resolve to the correct directory?

My code is more or less like this:

func proxy(serviceName string) func(res http.ResponseWriter, req *http.Request){
    cnn, err := dialer.Dial("tcp",":1250") // Reason for doing this is relative to architecture and is a must.
    if err != nil {
     res.WriteHeader(http.StatusInternalServerError)
     return
    }
    transport := &http.Transport{
        Dial: func(network, addr string) (net.Conn, error) {
            return cnn, nil
        },}
   client := &http.Client{
        Transport: transport,
    }
    hostName := hostNameMap[serviceName]
    req.URL.Host = req.Host
    req.URL.Scheme = originSvr.Scheme
    req.RequestURI = ""
    req.URL.Path = string.TrimPrefix(req.url.path,"/serviceName")

    serverResponse, err := client.Do(req)
    if err != nil {
        res.WriteHeader(http.StatusInternalServerError)
        fmt.Fprint(res, req)
        return
    }

    res.WriteHeader(serverResponse.StatusCode)
    io.Copy(res, serverResponse.Body)
}

func main() {
    http.handle("/",rootHandler)
    http.Handle("/service1", http.HandlerFunc(proxy("service1")))
    http.Handle("/service2", handler)
    http.ListenAndServe(":8080", nil)
}

I am currently thinking of reading the body into a buffer and modifying all links to routes, though that seems rather brittle. Alternatively, I was thinking of making an iframe for each service and serving the contents of the reverse proxy from root within in each iframe.

Otherwise, I am not sure how to handle redirects with how I currently have things setup.

solonly
  • 9
  • 2

1 Answers1

-1

In this scenario I have control of the apps on the remote machines. More specifically, I am running a wsgi app and I am able to specify a prefix path that is removed and prepended whenever the site is accessed. Following the instructions here resolved my issue: https://dlukes.github.io/flask-wsgi-url-prefix.html

solonly
  • 9
  • 2