mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
fix(config): improve container detection (#420)
fix(config): detect container
This commit is contained in:
parent
8457222b28
commit
765215270a
3 changed files with 80 additions and 42 deletions
|
@ -13,5 +13,14 @@ bin
|
|||
config
|
||||
test
|
||||
web/*
|
||||
!web/public
|
||||
!web/src
|
||||
!web/build*
|
||||
#!web/build.go
|
||||
!web/package.json
|
||||
!web/yarn.lock
|
||||
!web/.yarnrc.yml
|
||||
!web/.yarn/releases
|
||||
!web/.eslintrc.js
|
||||
!web/postcss.config.js
|
||||
!web/tailwind.config.js
|
||||
!web/tsconfig.json
|
||||
|
|
|
@ -43,7 +43,7 @@ func (s *service) List(ctx context.Context) ([]domain.APIKey, error) {
|
|||
}
|
||||
|
||||
func (s *service) Store(ctx context.Context, key *domain.APIKey) error {
|
||||
key.Key = generateSecureToken(16)
|
||||
key.Key = GenerateSecureToken(16)
|
||||
|
||||
if err := s.repo.Store(ctx, key); err != nil {
|
||||
return err
|
||||
|
@ -82,7 +82,7 @@ func (s *service) ValidateAPIKey(ctx context.Context, key string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func generateSecureToken(length int) string {
|
||||
func GenerateSecureToken(length int) string {
|
||||
b := make([]byte, length)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return ""
|
||||
|
|
|
@ -1,60 +1,31 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/api"
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
path := filepath.Join(configPath, configFile)
|
||||
|
||||
// check if configPath exists, if not create it
|
||||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.MkdirAll(configPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// check if config exists, if not create it
|
||||
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil { // perm 0666
|
||||
// handle failed create
|
||||
log.Printf("error creating file: %q", err)
|
||||
return err
|
||||
}
|
||||
|
||||
host := "127.0.0.1"
|
||||
if pd, _ := os.Open("/proc/1/cgroup"); pd != nil {
|
||||
defer pd.Close()
|
||||
b := make([]byte, 4096, 4096)
|
||||
pd.Read(b)
|
||||
if strings.Contains(string(b), "/docker") || strings.Contains(string(b), "/lxc") {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(`# config.toml
|
||||
var configTemplate = `# config.toml
|
||||
|
||||
# Hostname / IP
|
||||
#
|
||||
# Default: "localhost"
|
||||
#
|
||||
host = "` + host + `"
|
||||
host = "{{ .host }}"
|
||||
|
||||
# Port
|
||||
#
|
||||
|
@ -87,15 +58,74 @@ logLevel = "DEBUG"
|
|||
|
||||
# Session secret
|
||||
#
|
||||
sessionSecret = "secret-session-key"`)
|
||||
sessionSecret = "{{ .sessionSecret }}"
|
||||
`
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
cfgPath := filepath.Join(configPath, configFile)
|
||||
|
||||
// check if configPath exists, if not create it
|
||||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.MkdirAll(configPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// check if config exists, if not create it
|
||||
if _, err := os.Stat(cfgPath); errors.Is(err, os.ErrNotExist) {
|
||||
// set default host
|
||||
host := "127.0.0.1"
|
||||
|
||||
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||
// docker creates a .dockerenv file at the root
|
||||
// of the directory tree inside the container.
|
||||
// if this file exists then the viewer is running
|
||||
// from inside a container so return true
|
||||
host = "0.0.0.0"
|
||||
} else if pd, _ := os.Open("/proc/1/cgroup"); pd != nil {
|
||||
defer pd.Close()
|
||||
b := make([]byte, 4096, 4096)
|
||||
pd.Read(b)
|
||||
if strings.Contains(string(b), "/docker") || strings.Contains(string(b), "/lxc") {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(cfgPath)
|
||||
if err != nil { // perm 0666
|
||||
// handle failed create
|
||||
log.Printf("error creating file: %q", err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// generate default sessionSecret
|
||||
sessionSecret := api.GenerateSecureToken(16)
|
||||
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("config").Parse(configTemplate)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not create config template")
|
||||
}
|
||||
|
||||
tmplVars := map[string]string{
|
||||
"host": host,
|
||||
"sessionSecret": sessionSecret,
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
if err = tmpl.Execute(&buffer, &tmplVars); err != nil {
|
||||
return errors.Wrap(err, "could not write torrent url template output")
|
||||
}
|
||||
|
||||
if _, err = f.WriteString(buffer.String()); err != nil {
|
||||
log.Printf("error writing contents to file: %v %q", configPath, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Sync()
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -157,8 +187,7 @@ func (c *AppConfig) load(configPath string) {
|
|||
|
||||
// check if path and file exists
|
||||
// if not, create path and file
|
||||
err := writeConfig(configPath, "config.toml")
|
||||
if err != nil {
|
||||
if err := writeConfig(configPath, "config.toml"); err != nil {
|
||||
log.Printf("write error: %q", err)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue