Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gluetun auth support #25

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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