mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
Feature: Improve config for http server (#67)
* feat: improve config for http server * Feature: Support multiple action status per release (#69) * feat: move release actions to separate table * chore: update sqlite driver * fix(indexers): btn api client (#71) What: * Api key and torrentId in wrong order * Set hardcoded ID in jsonrpc request object * ParsetorrentId from url Fixes #68 * feat: show irc network status in settings list * feat: show irc channel status * chore: go mod tidy * feat: improve config for http server * feat: add context to user repo * feat: only set secure cookie if https
This commit is contained in:
parent
3475dddec7
commit
efa84fee8b
9 changed files with 74 additions and 56 deletions
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -97,18 +96,15 @@ func main() {
|
||||||
ircService = irc.NewService(ircRepo, filterService, indexerService, releaseService)
|
ircService = irc.NewService(ircRepo, filterService, indexerService, releaseService)
|
||||||
userService = user.NewService(userRepo)
|
userService = user.NewService(userRepo)
|
||||||
authService = auth.NewService(userService)
|
authService = auth.NewService(userService)
|
||||||
//announceService = announce.NewService(filterService, indexerService, releaseService)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// register event subscribers
|
// register event subscribers
|
||||||
events.NewSubscribers(bus, releaseService)
|
events.NewSubscribers(bus, releaseService)
|
||||||
|
|
||||||
addr := fmt.Sprintf("%v:%v", cfg.Host, cfg.Port)
|
|
||||||
|
|
||||||
errorChannel := make(chan error)
|
errorChannel := make(chan error)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
httpServer := http.NewServer(serverEvents, addr, cfg.BaseURL, version, commit, date, actionService, authService, downloadClientService, filterService, indexerService, ircService, releaseService)
|
httpServer := http.NewServer(cfg, serverEvents, version, commit, date, actionService, authService, downloadClientService, filterService, indexerService, ircService, releaseService)
|
||||||
errorChannel <- httpServer.Open()
|
errorChannel <- httpServer.Open()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -75,7 +76,7 @@ func main() {
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: hashed,
|
Password: hashed,
|
||||||
}
|
}
|
||||||
if err := userRepo.Store(user); err != nil {
|
if err := userRepo.Store(context.Background(), user); err != nil {
|
||||||
log.Fatalf("failed to create user: %v", err)
|
log.Fatalf("failed to create user: %v", err)
|
||||||
}
|
}
|
||||||
case "change-password":
|
case "change-password":
|
||||||
|
@ -85,7 +86,7 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := userRepo.FindByUsername(username)
|
user, err := userRepo.FindByUsername(context.Background(), username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to get user: %v", err)
|
log.Fatalf("failed to get user: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -104,7 +105,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Password = hashed
|
user.Password = hashed
|
||||||
if err := userRepo.Store(*user); err != nil {
|
if err := userRepo.Store(context.Background(), *user); err != nil {
|
||||||
log.Fatalf("failed to create user: %v", err)
|
log.Fatalf("failed to create user: %v", err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
|
@ -9,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Login(username, password string) (*domain.User, error)
|
Login(ctx context.Context, username, password string) (*domain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
|
@ -22,13 +23,13 @@ func NewService(userSvc user.Service) Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Login(username, password string) (*domain.User, error) {
|
func (s *service) Login(ctx context.Context, username, password string) (*domain.User, error) {
|
||||||
if username == "" || password == "" {
|
if username == "" || password == "" {
|
||||||
return nil, errors.New("bad credentials")
|
return nil, errors.New("bad credentials")
|
||||||
}
|
}
|
||||||
|
|
||||||
// find user
|
// find user
|
||||||
u, err := s.userSvc.FindByUsername(username)
|
u, err := s.userSvc.FindByUsername(ctx, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -16,10 +17,10 @@ func NewUserRepo(db *sql.DB) domain.UserRepo {
|
||||||
return &UserRepo{db: db}
|
return &UserRepo{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepo) FindByUsername(username string) (*domain.User, error) {
|
func (r *UserRepo) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||||
query := `SELECT id, username, password FROM users WHERE username = ?`
|
query := `SELECT id, username, password FROM users WHERE username = ?`
|
||||||
|
|
||||||
row := r.db.QueryRow(query, username)
|
row := r.db.QueryRowContext(ctx, query, username)
|
||||||
if err := row.Err(); err != nil {
|
if err := row.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -34,12 +35,12 @@ func (r *UserRepo) FindByUsername(username string) (*domain.User, error) {
|
||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepo) Store(user domain.User) error {
|
func (r *UserRepo) Store(ctx context.Context, user domain.User) error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if user.ID != 0 {
|
if user.ID != 0 {
|
||||||
update := `UPDATE users SET password = ? WHERE username = ?`
|
update := `UPDATE users SET password = ? WHERE username = ?`
|
||||||
_, err = r.db.Exec(update, user.Password, user.Username)
|
_, err = r.db.ExecContext(ctx, update, user.Password, user.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Stack().Err(err).Msg("error executing query")
|
log.Error().Stack().Err(err).Msg("error executing query")
|
||||||
return err
|
return err
|
||||||
|
@ -47,7 +48,7 @@ func (r *UserRepo) Store(user domain.User) error {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
query := `INSERT INTO users (username, password) VALUES (?, ?)`
|
query := `INSERT INTO users (username, password) VALUES (?, ?)`
|
||||||
_, err = r.db.Exec(query, user.Username, user.Password)
|
_, err = r.db.ExecContext(ctx, query, user.Username, user.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Stack().Err(err).Msg("error executing query")
|
log.Error().Stack().Err(err).Msg("error executing query")
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
type UserRepo interface {
|
type UserRepo interface {
|
||||||
FindByUsername(username string) (*User, error)
|
FindByUsername(ctx context.Context, username string) (*User, error)
|
||||||
Store(user User) error
|
Store(ctx context.Context, user User) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
|
|
@ -1,38 +1,37 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/config"
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
type authService interface {
|
type authService interface {
|
||||||
Login(username, password string) (*domain.User, error)
|
Login(ctx context.Context, username, password string) (*domain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type authHandler struct {
|
type authHandler struct {
|
||||||
encoder encoder
|
encoder encoder
|
||||||
|
config domain.Config
|
||||||
service authService
|
service authService
|
||||||
|
|
||||||
|
cookieStore *sessions.CookieStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAuthHandler(encoder encoder, service authService) *authHandler {
|
func newAuthHandler(encoder encoder, config domain.Config, cookieStore *sessions.CookieStore, service authService) *authHandler {
|
||||||
return &authHandler{
|
return &authHandler{
|
||||||
encoder: encoder,
|
encoder: encoder,
|
||||||
|
config: config,
|
||||||
service: service,
|
service: service,
|
||||||
|
cookieStore: cookieStore,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
// key will only be valid as long as it's running.
|
|
||||||
key = []byte(config.Config.SessionSecret)
|
|
||||||
store = sessions.NewCookieStore(key)
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h authHandler) Routes(r chi.Router) {
|
func (h authHandler) Routes(r chi.Router) {
|
||||||
r.Post("/login", h.login)
|
r.Post("/login", h.login)
|
||||||
r.Post("/logout", h.logout)
|
r.Post("/logout", h.logout)
|
||||||
|
@ -51,12 +50,23 @@ func (h authHandler) login(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
store.Options.Secure = true
|
h.cookieStore.Options.HttpOnly = true
|
||||||
store.Options.HttpOnly = true
|
h.cookieStore.Options.SameSite = http.SameSiteLaxMode
|
||||||
store.Options.SameSite = http.SameSiteStrictMode
|
h.cookieStore.Options.Path = h.config.BaseURL
|
||||||
session, _ := store.Get(r, "user_session")
|
|
||||||
|
|
||||||
_, err := h.service.Login(data.Username, data.Password)
|
// autobrr does not support serving on TLS / https, so this is only available behind reverse proxy
|
||||||
|
// if forwarded protocol is https then set cookie secure
|
||||||
|
// SameSite Strict can only be set with a secure cookie. So we overwrite it here if possible.
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
|
||||||
|
fwdProto := r.Header.Get("X-Forwarded-Proto")
|
||||||
|
if fwdProto == "https" {
|
||||||
|
h.cookieStore.Options.Secure = true
|
||||||
|
h.cookieStore.Options.SameSite = http.SameSiteStrictMode
|
||||||
|
}
|
||||||
|
|
||||||
|
session, _ := h.cookieStore.Get(r, "user_session")
|
||||||
|
|
||||||
|
_, err := h.service.Login(ctx, data.Username, data.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusUnauthorized)
|
h.encoder.StatusResponse(ctx, w, nil, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
@ -72,7 +82,7 @@ func (h authHandler) login(w http.ResponseWriter, r *http.Request) {
|
||||||
func (h authHandler) logout(w http.ResponseWriter, r *http.Request) {
|
func (h authHandler) logout(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
session, _ := store.Get(r, "user_session")
|
session, _ := h.cookieStore.Get(r, "user_session")
|
||||||
|
|
||||||
// Revoke users authentication
|
// Revoke users authentication
|
||||||
session.Values["authenticated"] = false
|
session.Values["authenticated"] = false
|
||||||
|
@ -83,7 +93,7 @@ func (h authHandler) logout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func (h authHandler) test(w http.ResponseWriter, r *http.Request) {
|
func (h authHandler) test(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
session, _ := store.Get(r, "user_session")
|
session, _ := h.cookieStore.Get(r, "user_session")
|
||||||
|
|
||||||
// Check if user is authenticated
|
// Check if user is authenticated
|
||||||
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
||||||
|
|
|
@ -2,10 +2,10 @@ package http
|
||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
func IsAuthenticated(next http.Handler) http.Handler {
|
func (s Server) IsAuthenticated(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// check session
|
// check session
|
||||||
session, _ := store.Get(r, "user_session")
|
session, _ := s.cookieStore.Get(r, "user_session")
|
||||||
|
|
||||||
// Check if user is authenticated
|
// Check if user is authenticated
|
||||||
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/config"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/web"
|
"github.com/autobrr/autobrr/web"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
"github.com/r3labs/sse/v2"
|
"github.com/r3labs/sse/v2"
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
)
|
)
|
||||||
|
@ -16,8 +18,8 @@ import (
|
||||||
type Server struct {
|
type Server struct {
|
||||||
sse *sse.Server
|
sse *sse.Server
|
||||||
|
|
||||||
address string
|
config domain.Config
|
||||||
baseUrl string
|
cookieStore *sessions.CookieStore
|
||||||
|
|
||||||
version string
|
version string
|
||||||
commit string
|
commit string
|
||||||
|
@ -32,15 +34,16 @@ type Server struct {
|
||||||
releaseService releaseService
|
releaseService releaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(sse *sse.Server, address string, baseUrl string, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, releaseSvc releaseService) Server {
|
func NewServer(config domain.Config, sse *sse.Server, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, releaseSvc releaseService) Server {
|
||||||
return Server{
|
return Server{
|
||||||
|
config: config,
|
||||||
sse: sse,
|
sse: sse,
|
||||||
address: address,
|
|
||||||
baseUrl: baseUrl,
|
|
||||||
version: version,
|
version: version,
|
||||||
commit: commit,
|
commit: commit,
|
||||||
date: date,
|
date: date,
|
||||||
|
|
||||||
|
cookieStore: sessions.NewCookieStore([]byte(config.SessionSecret)),
|
||||||
|
|
||||||
actionService: actionService,
|
actionService: actionService,
|
||||||
authService: authService,
|
authService: authService,
|
||||||
downloadClientService: downloadClientSvc,
|
downloadClientService: downloadClientSvc,
|
||||||
|
@ -52,7 +55,8 @@ func NewServer(sse *sse.Server, address string, baseUrl string, version string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) Open() error {
|
func (s Server) Open() error {
|
||||||
listener, err := net.Listen("tcp", s.address)
|
addr := fmt.Sprintf("%v:%v", s.config.Host, s.config.Port)
|
||||||
|
listener, err := net.Listen("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -91,10 +95,10 @@ func (s Server) Handler() http.Handler {
|
||||||
fileSystem.ServeHTTP(w, r)
|
fileSystem.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/api/auth", newAuthHandler(encoder, s.authService).Routes)
|
r.Route("/api/auth", newAuthHandler(encoder, s.config, s.cookieStore, s.authService).Routes)
|
||||||
|
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(IsAuthenticated)
|
r.Use(s.IsAuthenticated)
|
||||||
|
|
||||||
r.Route("/api", func(r chi.Router) {
|
r.Route("/api", func(r chi.Router) {
|
||||||
r.Route("/actions", newActionHandler(encoder, s.actionService).Routes)
|
r.Route("/actions", newActionHandler(encoder, s.actionService).Routes)
|
||||||
|
@ -121,17 +125,17 @@ func (s Server) Handler() http.Handler {
|
||||||
})
|
})
|
||||||
|
|
||||||
//r.HandleFunc("/*", handler.ServeHTTP)
|
//r.HandleFunc("/*", handler.ServeHTTP)
|
||||||
r.Get("/", index)
|
r.Get("/", s.index)
|
||||||
r.Get("/*", index)
|
r.Get("/*", s.index)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func index(w http.ResponseWriter, r *http.Request) {
|
func (s Server) index(w http.ResponseWriter, r *http.Request) {
|
||||||
p := web.IndexParams{
|
p := web.IndexParams{
|
||||||
Title: "Dashboard",
|
Title: "Dashboard",
|
||||||
Version: "thisistheversion",
|
Version: s.version,
|
||||||
BaseUrl: config.Config.BaseURL,
|
BaseUrl: s.config.BaseURL,
|
||||||
}
|
}
|
||||||
web.Index(w, p)
|
web.Index(w, p)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import "github.com/autobrr/autobrr/internal/domain"
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
FindByUsername(username string) (*domain.User, error)
|
FindByUsername(ctx context.Context, username string) (*domain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
|
@ -16,8 +19,8 @@ func NewService(repo domain.UserRepo) Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) FindByUsername(username string) (*domain.User, error) {
|
func (s *service) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||||
user, err := s.repo.FindByUsername(username)
|
user, err := s.repo.FindByUsername(ctx, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue