Skip to content

Commit

Permalink
add -plain flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Dec 31, 2023
1 parent 2df893f commit add1103
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/tootik/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (
addr = flag.String("addr", ":8443", "HTTPS listening address")
blockListPath = flag.String("blocklist", "", "Blocklist CSV")
closed = flag.Bool("closed", false, "Disable new user registration")
plain = flag.Bool("plain", false, "Use HTTP instead of HTTPS")
version = flag.Bool("version", false, "Print version and exit")
)

Expand Down Expand Up @@ -134,7 +135,7 @@ func main() {

wg.Add(1)
go func() {
if err := fed.ListenAndServe(ctx, db, resolver, nobody, log, *addr, *cert, *key); err != nil {
if err := fed.ListenAndServe(ctx, db, resolver, nobody, log, *addr, *cert, *key, *plain); err != nil {
log.Error("HTTPS listener has failed", "error", err)
}
cancel()
Expand Down
29 changes: 19 additions & 10 deletions fed/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func robots(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Disallow: /\n"))
}

func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *ap.Actor, log *slog.Logger, addr, cert, key string) error {
func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *ap.Actor, log *slog.Logger, addr, cert, key string, plain bool) error {
mux := http.NewServeMux()
mux.HandleFunc("/robots.txt", robots)
mux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -92,18 +92,22 @@ func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *
defer w.Close()

certDir := filepath.Dir(cert)
if err := w.Add(certDir); err != nil {
return err
}
certAbsPath := filepath.Join(certDir, filepath.Base(cert))

keyDir := filepath.Dir(key)
if keyDir != certDir {
if err := w.Add(keyDir); err != nil {
keyAbsPath := filepath.Join(keyDir, filepath.Base(key))

if !plain {
if err := w.Add(certDir); err != nil {
return err
}

if keyDir != certDir {
if err := w.Add(keyDir); err != nil {
return err
}
}
}
keyAbsPath := filepath.Join(keyDir, filepath.Base(key))

for ctx.Err() == nil {
var wg sync.WaitGroup
Expand Down Expand Up @@ -148,7 +152,7 @@ func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *
}

if (event.Has(fsnotify.Write) || event.Has(fsnotify.Create)) && (event.Name == certAbsPath || event.Name == keyAbsPath) {
log.Info("Stopping HTTPS server: file has changed", "name", event.Name)
log.Info("Stopping server: file has changed", "name", event.Name)
timer.Reset(certReloadDelay)
}

Expand All @@ -161,8 +165,13 @@ func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *
}
}()

log.Info("Starting HTTPS server")
err := server.ListenAndServeTLS(cert, key)
log.Info("Starting server")
var err error
if plain {
err = server.ListenAndServe()
} else {
err = server.ListenAndServeTLS(cert, key)
}

stopServer()
wg.Wait()
Expand Down

0 comments on commit add1103

Please sign in to comment.