Skip to content

Commit

Permalink
gluetun auth support (#25)
Browse files Browse the repository at this point in the history
Gluetun's routes will become private by default starting from v3.40.0. 

This PR enables setting authentication when fetching the port mapping

Reference:
https://github.com/qdm12/gluetun-wiki/blob/main/setup/advanced/control-server.md
  • Loading branch information
dsantos authored Oct 13, 2024
1 parent 791d0d7 commit 285de48
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ transmission-gluetun-port -h

## Available environment variables

| Name | Description | Default |
|------|-------------|---------|
| `TRANSMISSION_USER` | Transmission user | - |
| `TRANSMISSION_PASSWORD` | Transmission password | - |
| `GLUETUN_HOST` | Gluetun api host | `127.0.0.1` |
| `GLUETUN_PORT` | Gluetun api port | `8000` |
| `INITIAL_DELAY` | Initial delay ([format](https://pkg.go.dev/time#ParseDuration)) | `5s` |
| `CHECK_INTERVAL` | Update interval ([format](https://pkg.go.dev/time#ParseDuration)) | `1m` |
| `ERROR_INTERVAL` | Update interval in case of error ([format](https://pkg.go.dev/time#ParseDuration)) | `5s` |
| Name | Description | Default |
|-------------------------|------------------------------------------------------------------------------------|-------------|
| `TRANSMISSION_USER` | Transmission user | - |
| `TRANSMISSION_PASSWORD` | Transmission password | - |
| `GLUETUN_HOST` | Gluetun api host | `127.0.0.1` |
| `GLUETUN_PORT` | Gluetun api port | `8000` |
| `GLUETUN_AUTH_TYPE` | Gluetun auth type: `basic`, `apikey` | `none` |
| `GLUETUN_AUTH_USERNAME` | Gluetun basic auth username | - |
| `GLUETUN_AUTH_PASSWORD` | Gluetun basic auth password | - |
| `GLUETUN_AUTH_API_KEY` | Gluetun auth api key | - |
| `INITIAL_DELAY` | Initial delay ([format](https://pkg.go.dev/time#ParseDuration)) | `5s` |
| `CHECK_INTERVAL` | Update interval ([format](https://pkg.go.dev/time#ParseDuration)) | `1m` |
| `ERROR_INTERVAL` | Update interval in case of error ([format](https://pkg.go.dev/time#ParseDuration)) | `5s` |
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ var (
transmissionPassword = os.Getenv("TRANSMISSION_PASSWORD")

// Gluetun
gluetunHostname = getEnv("GLUETUN_HOSTNAME", "127.0.0.1")
gluetunPort = getEnv("GLUETUN_PORT", "8000")
gluetunHostname = getEnv("GLUETUN_HOSTNAME", "127.0.0.1")
gluetunPort = getEnv("GLUETUN_PORT", "8000")
gluetunAuthType = getEnv("GLUETUN_AUTH_TYPE", "none")
gluetunAuthUsername = os.Getenv("GLUETUN_AUTH_USERNAME")
gluetunAuthPassword = os.Getenv("GLUETUN_AUTH_PASSWORD")
gluetunAuthAPIKey = os.Getenv("GLUETUN_AUTH_API_KEY")

// Control flow
initialDelayStr = getEnv("INITIAL_DELAY", "5s")
checkIntervalStr = getEnv("CHECK_INTERVAL", "1m")
errorIntervalStr = getEnv("ERROR_INTERVAL", "5s")

gluetunAuthTypeBasic = "basic"
gluetunAuthTypeAPIKey = "apikey"
gluetunAuthTypeAPIKeyHeader = "X-API-Key"
)

func init() {
Expand Down Expand Up @@ -66,13 +74,22 @@ func main() {
log.Fatalf("failed to create transmission client: %v", err)
}

log.Printf("fetching port mapping from gluetun using auth type: %s", gluetunAuthType)

for {
portMapping := &GluetunResponse{}
resp, err := httpClient.R().
req := httpClient.R().
SetResult(portMapping).
ForceContentType("application/json").
Get(gluetunPortApi)
ForceContentType("application/json")

switch gluetunAuthType {
case gluetunAuthTypeBasic:
req.SetBasicAuth(gluetunAuthUsername, gluetunAuthPassword)
case gluetunAuthTypeAPIKey:
req.SetHeader(gluetunAuthTypeAPIKeyHeader, gluetunAuthAPIKey)
}

resp, err := req.Get(gluetunPortApi)
if err != nil || resp.IsError() {
log.Printf("failed to fetch port mapping from gluetun: %v, %d", err, resp.StatusCode())
}
Expand Down

0 comments on commit 285de48

Please sign in to comment.