Skip to content

Commit

Permalink
feat: docker: add scheme label
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidapaulopt committed Dec 7, 2024
1 parent 0b89bb6 commit ec45770
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
16 changes: 15 additions & 1 deletion docs/content/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ weight: 200

{{% steps %}}

### 1.1.1

#### New Docker container labels

##### tsdproxy.autodetect

If TSDProxy, for any reason, can't detect the container's network you can
disable it.

##### tsdproxy.scheme

If a container uses https, use tsdproxy.scheme=https label.

### 1.1.0

#### New File Provider

TSDProxy now supports a new file provider. It's useful if you want to proxy url without Docker.
TSDProxy now supports a new file provider. It's useful if you want to proxy URL
without Docker.
Now you can use TSDProxy even without Docker.

### 1.0.0
Expand Down
10 changes: 10 additions & 0 deletions docs/content/docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,14 @@ labels:
tsdproxy.autodetect: "false"
```

### tsdproxy.scheme

Defaults to "http", set to https to enable "https" if the container is running with TLS.

```yaml
labels:
tsdproxy.enable: "true"
tsdproxy.scheme: "https"
```

{{% /steps %}}
16 changes: 9 additions & 7 deletions internal/targetproviders/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
LabelContainerAccessLog = LabelPrefix + "containeraccesslog"
LabelProxyProvider = LabelPrefix + "proxyprovider"
LabelAutoDetect = LabelPrefix + "autodetect"

LabelScheme = LabelPrefix + "scheme"
//
dialTimeout = 2 * time.Second
autoDetectTries = 5
Expand All @@ -52,6 +52,7 @@ type container struct {
defaultTargetHostname string
defaultBridgeAddress string
targetProviderName string
scheme string
autodetect bool
}

Expand All @@ -70,6 +71,7 @@ func newContainer(logger zerolog.Logger, dcontainer types.ContainerJSON, imageIn
}

c.autodetect = c.getLabelBool(LabelAutoDetect, true)
c.scheme = c.getLabelString(LabelScheme, "http")

return c
}
Expand Down Expand Up @@ -223,7 +225,7 @@ func (c *container) getProxyURL() (*url.URL, error) {
}

// validate url
return url.Parse("https://" + name)
return url.Parse(c.scheme + "://" + name)
}

// getName method returns the name of the container
Expand Down Expand Up @@ -259,7 +261,7 @@ func (c *container) getTargetURL(hostname string) (*url.URL, error) {
}
}
// auto detect failed set to defaultTargetHostname with exposed port
return url.Parse("http://" + c.defaultTargetHostname + ":" + exposedPort)
return url.Parse(c.scheme + "://" + c.defaultTargetHostname + ":" + exposedPort)
}

// tryConnectContainer method tries to connect to the container
Expand Down Expand Up @@ -310,7 +312,7 @@ func (c *container) tryInternalPort(hostname, port string) (*url.URL, error) {
if err := c.dial(network.IPAddress, port); err == nil {
c.log.Info().Str("address", network.IPAddress).
Str("port", port).Msg("Successfully connected using internal ip and internal port")
return url.Parse(fmt.Sprintf("http://%s:%s", network.IPAddress, port))
return url.Parse(c.scheme + "://" + network.IPAddress + ":" + port)
}
c.log.Debug().Str("address", network.IPAddress).
Str("port", port).Msg("Failed to connect")
Expand All @@ -320,7 +322,7 @@ func (c *container) tryInternalPort(hostname, port string) (*url.URL, error) {
if c.container.HostConfig.NetworkMode == "host" && c.defaultBridgeAddress != "" {
if err := c.dial(c.defaultBridgeAddress, port); err == nil {
c.log.Info().Str("address", c.defaultBridgeAddress).Str("port", port).Msg("Successfully connected using defaultBridgeAddress and internal port")
return url.Parse(fmt.Sprintf("http://%s:%s", c.defaultBridgeAddress, port))
return url.Parse(c.scheme + "://" + c.defaultBridgeAddress + ":" + port)
}

c.log.Debug().Str("address", c.defaultBridgeAddress).Str("port", port).Msg("Failed to connect")
Expand All @@ -334,7 +336,7 @@ func (c *container) tryExposedPort(hostname, port string) (*url.URL, error) {
for _, network := range c.container.NetworkSettings.Networks {
if err := c.dial(network.Gateway, port); err == nil {
c.log.Info().Str("address", network.Gateway).Str("port", port).Msg("Successfully connected using docker network gateway and exposed port")
return url.Parse(fmt.Sprintf("http://%s:%s", network.Gateway, port))
return url.Parse(c.scheme + "://" + network.Gateway + ":" + port)
}

c.log.Debug().Str("address", network.Gateway).Str("port", port).Msg("Failed to connect using docker network gateway and exposed port")
Expand All @@ -343,7 +345,7 @@ func (c *container) tryExposedPort(hostname, port string) (*url.URL, error) {
// try connecting to configured host and exposed port
if err := c.dial(hostname, port); err == nil {
c.log.Info().Str("address", hostname).Str("port", port).Msg("Successfully connected using configured host and exposed port")
return url.Parse(fmt.Sprintf("http://%s:%s", hostname, port))
return url.Parse(c.scheme + "://" + hostname + ":" + port)
}

c.log.Debug().Str("address", hostname).Str("port", port).Msg("Failed to connect")
Expand Down

0 comments on commit ec45770

Please sign in to comment.