diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index 280ee75..fc5e95e 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -19,8 +19,8 @@ import ( // Docker provide watch compatible changes from containers // and maps by default from ^/api/%s/(.*) to http://%s:%d/$1, i.e. http://example.com/api/my_container/something // will be mapped to http://172.17.42.1:8080/something. Ip will be the internal ip of the container and port - exposed the one -// Alternatively labels can alter this. dpx.route sets source route, and dpx.dest sets the destination. Optional dpx.server enforces -// match by server name (hostname). +// Alternatively labels can alter this. reproxy.route sets source route, and reproxy.dest sets the destination. +// Optional reproxy.server enforces match by server name (hostname). type Docker struct { DockerClient DockerClient Excludes []string diff --git a/app/discovery/provider/file_test.go b/app/discovery/provider/file_test.go index 9e91ece..ddccad6 100644 --- a/app/discovery/provider/file_test.go +++ b/app/discovery/provider/file_test.go @@ -15,7 +15,7 @@ func TestFile_Events(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) defer cancel() - tmp, err := ioutil.TempFile(os.TempDir(), "dpx-events") + tmp, err := ioutil.TempFile(os.TempDir(), "reproxy-events") require.NoError(t, err) tmp.Close() defer os.Remove(tmp.Name()) diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index 9993c49..a11dd92 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -16,7 +16,6 @@ import ( log "github.com/go-pkgz/lgr" "github.com/go-pkgz/rest" R "github.com/go-pkgz/rest" - "github.com/go-pkgz/rest/logger" "github.com/gorilla/handlers" "github.com/pkg/errors" @@ -26,16 +25,17 @@ import ( // Http is a proxy server for both http and https type Http struct { Matcher - Address string - TimeOut time.Duration - AssetsLocation string - AssetsWebRoot string - MaxBodySize int64 - GzEnabled bool - ProxyHeaders []string - SSLConfig SSLConfig - Version string - AccessLog io.Writer + Address string + TimeOut time.Duration + AssetsLocation string + AssetsWebRoot string + MaxBodySize int64 + GzEnabled bool + ProxyHeaders []string + SSLConfig SSLConfig + Version string + AccessLog io.Writer + DisableSignature bool } // Matcher source info (server and route) to the destination url @@ -71,10 +71,9 @@ func (h *Http) Run(ctx context.Context) error { handler := R.Wrap(h.proxyHandler(), R.Recoverer(lgr.Default()), - R.AppInfo("reproxy", "umputun", h.Version), + h.signatureHandler, R.Ping, h.healthMiddleware, - logger.New(logger.Prefix("[DEBUG] PROXY")).Handler, h.accessLogHandler(h.AccessLog), R.SizeLimit(h.MaxBodySize), R.Headers(h.ProxyHeaders...), @@ -199,6 +198,15 @@ func (h *Http) gzipHandler() func(next http.Handler) http.Handler { } } +func (h *Http) signatureHandler(next http.Handler) http.Handler { + if h.DisableSignature { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + next.ServeHTTP(w, r) + }) + } + return R.AppInfo("reproxy", "umputun", h.Version)(next) +} + func (h *Http) accessLogHandler(wr io.Writer) func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { return handlers.CombinedLoggingHandler(wr, next) @@ -217,7 +225,12 @@ func (h *Http) makeHTTPServer(addr string, router http.Handler) *http.Server { func (h *Http) setXRealIP(r *http.Request) { - ip, _, err := net.SplitHostPort(r.RemoteAddr) + remoteIP := r.Header.Get("X-Forwarded-For") + if remoteIP == "" { + remoteIP = r.RemoteAddr + } + + ip, _, err := net.SplitHostPort(remoteIP) if err != nil { return } diff --git a/app/proxy/proxy_test.go b/app/proxy/proxy_test.go index 84e91b3..a1a88f3 100644 --- a/app/proxy/proxy_test.go +++ b/app/proxy/proxy_test.go @@ -21,7 +21,7 @@ import ( func TestHttp_Do(t *testing.T) { port := rand.Intn(10000) + 40000 - h := Http{TimeOut: 200 * time.Millisecond, Address: fmt.Sprintf("127.0.0.1:%d", port)} + h := Http{TimeOut: 200 * time.Millisecond, Address: fmt.Sprintf("127.0.0.1:%d", port), AccessLog: io.Discard} ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() @@ -63,7 +63,7 @@ func TestHttp_Do(t *testing.T) { body, err := io.ReadAll(resp.Body) require.NoError(t, err) assert.Equal(t, "response /567/something", string(body)) - assert.Equal(t, "dpx", resp.Header.Get("App-Name")) + assert.Equal(t, "reproxy", resp.Header.Get("App-Name")) assert.Equal(t, "v1", resp.Header.Get("h1")) } @@ -77,7 +77,7 @@ func TestHttp_Do(t *testing.T) { body, err := io.ReadAll(resp.Body) require.NoError(t, err) assert.Equal(t, "response /123/something", string(body)) - assert.Equal(t, "dpx", resp.Header.Get("App-Name")) + assert.Equal(t, "reproxy", resp.Header.Get("App-Name")) assert.Equal(t, "v1", resp.Header.Get("h1")) }