@@ -360,6 +360,16 @@ func run(ctx context.Context) error {
360360
361361 mux .Handle ("/" , relay )
362362
363+ g , ctx := errgroup .WithContext (ctx )
364+
365+ if global .S .Port == "443" {
366+ // if we don't have a domain name we'll listen only on port 80 without doing the autocert dance yet
367+ if global .Settings .Domain == "" {
368+ log .Info ().Msg ("no domain setup yet, running only on port 80 for now" )
369+ global .S .Port = "80"
370+ }
371+ }
372+
363373 server := & http.Server {
364374 Addr : global .S .Host + ":" + global .S .Port ,
365375 Handler : ipBlockMiddleware (setupCheckMiddleware (mux )),
@@ -368,33 +378,57 @@ func run(ctx context.Context) error {
368378 },
369379 }
370380
371- var listenFunc func () error
372381 if global .S .Port == "443" {
373382 manager := & autocert.Manager {
374383 Prompt : func (_ string ) bool { return true },
375384 HostPolicy : autocert .HostWhitelist (global .Settings .Domain ),
376385 Cache : autocert .DirCache ("certs" ),
377386 }
378- server .TLSConfig = manager .TLSConfig ()
379- listenFunc = func () error { return server .ListenAndServeTLS ("" , "" ) }
380- log .Info ().Msg ("running on https://" + global .S .Host + ":" + global .S .Port )
387+
388+ // HTTP server on 80 for ACME challenges and user access
389+ httpServer := & http.Server {
390+ Addr : global .S .Host + ":80" ,
391+ Handler : manager .HTTPHandler (mux ),
392+ BaseContext : func (_ net.Listener ) context.Context {
393+ return ctx
394+ },
395+ }
396+ g .Go (func () error { return httpServer .ListenAndServe () })
397+
398+ // HTTPS server on 443
399+ httpsServer := & http.Server {
400+ Addr : global .S .Host + ":443" ,
401+ Handler : ipBlockMiddleware (mux ),
402+ BaseContext : func (_ net.Listener ) context.Context {
403+ return ctx
404+ },
405+ }
406+ httpsServer .TLSConfig = manager .TLSConfig ()
407+
408+ g .Go (func () error { return httpsServer .ListenAndServeTLS ("" , "" ) })
409+ log .Info ().Msg ("running on https://" + global .S .Host + ":443 and http://" + global .S .Host + ":80" )
410+ g .Go (func () error {
411+ <- ctx .Done ()
412+ httpsServer .Shutdown (context .Background ())
413+ httpServer .Shutdown (context .Background ())
414+ return nil
415+ })
381416 } else {
382- listenFunc = server .ListenAndServe
417+ g . Go ( server .ListenAndServe )
383418 log .Info ().Msg ("running on http://" + global .S .Host + ":" + global .S .Port )
419+
420+ g .Go (func () error {
421+ <- ctx .Done ()
422+ if err := server .Shutdown (context .Background ()); err != nil {
423+ return err
424+ }
425+ if err := server .Close (); err != nil {
426+ return err
427+ }
428+ return nil
429+ })
384430 }
385431
386- g , ctx := errgroup .WithContext (ctx )
387- g .Go (listenFunc )
388- g .Go (func () error {
389- <- ctx .Done ()
390- if err := server .Shutdown (context .Background ()); err != nil {
391- return err
392- }
393- if err := server .Close (); err != nil {
394- return err
395- }
396- return nil
397- })
398432 return g .Wait ()
399433}
400434
0 commit comments