2023-02-26 22:26:09 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2023-03-25 20:15:00 +00:00
|
|
|
"github.com/azukaar/cosmos-server/src/utils"
|
2023-02-26 22:26:09 +00:00
|
|
|
// "io/ioutil"
|
|
|
|
// "io"
|
|
|
|
// "os"
|
|
|
|
// "golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewProxy takes target host and creates a reverse proxy
|
|
|
|
func NewProxy(targetHost string) (*httputil.ReverseProxy, error) {
|
|
|
|
url, err := url.Parse(targetHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(url)
|
|
|
|
|
|
|
|
// upgrade the request to websocket
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
2023-03-10 20:59:56 +00:00
|
|
|
utils.Debug("Response from backend: " + resp.Status)
|
|
|
|
utils.Debug("URL was " + resp.Request.URL.String())
|
2023-02-26 22:26:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyRequestHandler handles the http request using proxy
|
|
|
|
func ProxyRequestHandler(proxy *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
proxy.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RouteTo(destination string) *httputil.ReverseProxy /*func(http.ResponseWriter, *http.Request)*/ {
|
|
|
|
// initialize a reverse proxy and pass the actual backend server url here
|
|
|
|
proxy, err := NewProxy(destination)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a handler function which uses the reverse proxy
|
|
|
|
return proxy //ProxyRequestHandler(proxy)
|
|
|
|
}
|