From e092c7e9891c0544615fe3cfe2603a87ef5e9a48 Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 26 May 2021 00:17:45 -0500 Subject: [PATCH] allow docker rules to use external destinations --- app/discovery/provider/docker.go | 12 ++++++++++-- app/discovery/provider/docker_test.go | 28 ++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index 66726c1..ff03845 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -118,7 +118,11 @@ func (d *Docker) parseContainerInfo(c containerInfo) (res []discovery.URLMapper) } if v, ok := d.labelN(c.Labels, n, "dest"); ok { enabled, explicit = true, true - destURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) + if strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://") { + destURL = v // proxy to http:// and https:// destinations as-is + } else { + destURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) + } } if v, ok := d.labelN(c.Labels, n, "server"); ok { enabled = true @@ -126,7 +130,11 @@ func (d *Docker) parseContainerInfo(c containerInfo) (res []discovery.URLMapper) } if v, ok := d.labelN(c.Labels, n, "ping"); ok { enabled = true - pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) + if strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://") { + pingURL = v // if ping is fulle url with http:// or https:// use it as-is + } else { + pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) + } } if v, ok := d.labelN(c.Labels, n, "assets"); ok { diff --git a/app/discovery/provider/docker_test.go b/app/discovery/provider/docker_test.go index 43eed82..bc4ec8f 100644 --- a/app/discovery/provider/docker_test.go +++ b/app/discovery/provider/docker_test.go @@ -32,6 +32,11 @@ func TestDocker_List(t *testing.T) { Labels: map[string]string{"reproxy.route": "^/api/123/(.*)", "reproxy.dest": "/blah/$1", "reproxy.server": "example.com", "reproxy.ping": "/ping"}, }, + { + Name: "c1", State: "running", IP: "127.0.0.21", Ports: []int{12345}, + Labels: map[string]string{"reproxy.route": "^/api/90/(.*)", "reproxy.dest": "http://example.com/blah/$1", + "reproxy.server": "example.com", "reproxy.ping": "https://example.com//ping"}, + }, { Name: "c2", State: "running", IP: "127.0.0.3", Ports: []int{12346}, Labels: map[string]string{"reproxy.enabled": "y"}, @@ -53,22 +58,27 @@ func TestDocker_List(t *testing.T) { d := Docker{DockerClient: dclient} res, err := d.List() require.NoError(t, err) - require.Equal(t, 3, len(res)) + require.Equal(t, 4, len(res)) assert.Equal(t, "^/api/123/(.*)", res[0].SrcMatch.String()) assert.Equal(t, "http://127.0.0.2:12345/blah/$1", res[0].Dst) assert.Equal(t, "example.com", res[0].Server) assert.Equal(t, "http://127.0.0.2:12345/ping", res[0].PingURL) - assert.Equal(t, "^/c2/(.*)", res[1].SrcMatch.String()) - assert.Equal(t, "http://127.0.0.3:12346/$1", res[1].Dst) - assert.Equal(t, "http://127.0.0.3:12346/ping", res[1].PingURL) - assert.Equal(t, "*", res[1].Server) + assert.Equal(t, "^/api/90/(.*)", res[1].SrcMatch.String()) + assert.Equal(t, "http://example.com/blah/$1", res[1].Dst) + assert.Equal(t, "https://example.com//ping", res[1].PingURL) + assert.Equal(t, "example.com", res[1].Server) - assert.Equal(t, "^/a/(.*)", res[2].SrcMatch.String()) - assert.Equal(t, "http://127.0.0.2:12348/a/$1", res[2].Dst) - assert.Equal(t, "http://127.0.0.2:12348/ping", res[2].PingURL) - assert.Equal(t, "example.com", res[2].Server) + assert.Equal(t, "^/c2/(.*)", res[2].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.3:12346/$1", res[2].Dst) + assert.Equal(t, "http://127.0.0.3:12346/ping", res[2].PingURL) + assert.Equal(t, "*", res[2].Server) + + assert.Equal(t, "^/a/(.*)", res[3].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.2:12348/a/$1", res[3].Dst) + assert.Equal(t, "http://127.0.0.2:12348/ping", res[3].PingURL) + assert.Equal(t, "example.com", res[3].Server) } func TestDocker_ListMulti(t *testing.T) {