mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-30 22:34:15 -05:00
cmd: hash-password: Fix broken terminal state on SIGINT (#3416)
* caddyauth: Fix hash-password broken terminal state on SIGINT * caddycmd: Move TrapSignals calls to only subcommands that run long
This commit is contained in:
parent
1dc4ec2d77
commit
bb67e19d7b
5 changed files with 26 additions and 9 deletions
|
@ -148,6 +148,8 @@ func cmdStart(fl Flags) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdRun(fl Flags) (int, error) {
|
func cmdRun(fl Flags) (int, error) {
|
||||||
|
caddy.TrapSignals()
|
||||||
|
|
||||||
runCmdConfigFlag := fl.String("config")
|
runCmdConfigFlag := fl.String("config")
|
||||||
runCmdConfigAdapterFlag := fl.String("adapter")
|
runCmdConfigAdapterFlag := fl.String("adapter")
|
||||||
runCmdResumeFlag := fl.Bool("resume")
|
runCmdResumeFlag := fl.Bool("resume")
|
||||||
|
|
|
@ -51,8 +51,6 @@ func init() {
|
||||||
// Main implements the main function of the caddy command.
|
// Main implements the main function of the caddy command.
|
||||||
// Call this if Caddy is to be the main() if your program.
|
// Call this if Caddy is to be the main() if your program.
|
||||||
func Main() {
|
func Main() {
|
||||||
caddy.TrapSignals()
|
|
||||||
|
|
||||||
switch len(os.Args) {
|
switch len(os.Args) {
|
||||||
case 0:
|
case 0:
|
||||||
fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n")
|
fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n")
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
||||||
|
@ -67,17 +68,29 @@ func cmdHashPassword(fs caddycmd.Flags) (int, error) {
|
||||||
salt := []byte(fs.String("salt"))
|
salt := []byte(fs.String("salt"))
|
||||||
|
|
||||||
if len(plaintext) == 0 {
|
if len(plaintext) == 0 {
|
||||||
if terminal.IsTerminal(int(os.Stdin.Fd())) {
|
fd := int(os.Stdin.Fd())
|
||||||
fmt.Print("Enter password: ")
|
if terminal.IsTerminal(fd) {
|
||||||
plaintext, err = terminal.ReadPassword(int(os.Stdin.Fd()))
|
// ensure the terminal state is restored on SIGINT
|
||||||
fmt.Println()
|
state, _ := terminal.GetState(fd)
|
||||||
|
c := make(chan os.Signal)
|
||||||
|
signal.Notify(c, os.Interrupt)
|
||||||
|
go func() {
|
||||||
|
<-c
|
||||||
|
_ = terminal.Restore(fd, state)
|
||||||
|
os.Exit(caddy.ExitCodeFailedStartup)
|
||||||
|
}()
|
||||||
|
defer signal.Stop(c)
|
||||||
|
|
||||||
|
fmt.Fprint(os.Stderr, "Enter password: ")
|
||||||
|
plaintext, err = terminal.ReadPassword(fd)
|
||||||
|
fmt.Fprintln(os.Stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, err
|
return caddy.ExitCodeFailedStartup, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print("Confirm password: ")
|
fmt.Fprint(os.Stderr, "Confirm password: ")
|
||||||
confirmation, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
confirmation, err := terminal.ReadPassword(fd)
|
||||||
fmt.Println()
|
fmt.Fprintln(os.Stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, err
|
return caddy.ExitCodeFailedStartup, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,8 @@ respond with a file listing.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdFileServer(fs caddycmd.Flags) (int, error) {
|
func cmdFileServer(fs caddycmd.Flags) (int, error) {
|
||||||
|
caddy.TrapSignals()
|
||||||
|
|
||||||
domain := fs.String("domain")
|
domain := fs.String("domain")
|
||||||
root := fs.String("root")
|
root := fs.String("root")
|
||||||
listen := fs.String("listen")
|
listen := fs.String("listen")
|
||||||
|
|
|
@ -66,6 +66,8 @@ default, all incoming headers are passed through unmodified.)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
|
caddy.TrapSignals()
|
||||||
|
|
||||||
from := fs.String("from")
|
from := fs.String("from")
|
||||||
to := fs.String("to")
|
to := fs.String("to")
|
||||||
changeHost := fs.Bool("change-host-header")
|
changeHost := fs.Bool("change-host-header")
|
||||||
|
|
Loading…
Reference in a new issue