From 609bdf70799ebc0c84fe97d4f52a088ae5784310 Mon Sep 17 00:00:00 2001 From: starainrt Date: Sat, 11 Feb 2023 16:55:57 +0800 Subject: [PATCH] add signal control for password input --- io.go | 38 +++++++++++++++++++++++++++++++++----- signal_windows.go | 22 ++++++++++++++++++++++ singal_other.go | 26 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 signal_windows.go create mode 100644 singal_other.go diff --git a/io.go b/io.go index 62b6096..4d8845d 100644 --- a/io.go +++ b/io.go @@ -3,11 +3,11 @@ package stario import ( "bufio" "fmt" + "golang.org/x/crypto/ssh/terminal" "os" + "runtime" "strconv" "strings" - - "golang.org/x/crypto/ssh/terminal" ) type InputMsg struct { @@ -17,11 +17,19 @@ type InputMsg struct { } func Passwd(hint string, defaultVal string) InputMsg { - return passwd(hint, defaultVal, "") + return passwd(hint, defaultVal, "", false) } func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg { - return passwd(hint, defaultVal, mask) + return passwd(hint, defaultVal, mask, false) +} + +func PasswdResponseSignal(hint string, defaultVal string) InputMsg { + return passwd(hint, defaultVal, "", true) +} + +func PasswdResponseSignalWithMask(hint string, defaultVal string, mask string) InputMsg { + return passwd(hint, defaultVal, mask, true) } func MessageBoxRaw(hint string, defaultVal string) InputMsg { @@ -75,7 +83,16 @@ func messageBox(hint string, defaultVal string) InputMsg { } } -func passwd(hint string, defaultVal string, mask string) InputMsg { +func isSiganl(s rune) bool { + switch s { + case 0x03, 0x1a, 0x1c: + return true + default: + return false + } +} + +func passwd(hint string, defaultVal string, mask string, handleSignal bool) InputMsg { var ioBuf []rune if hint != "" { fmt.Print(hint) @@ -96,6 +113,17 @@ func passwd(hint string, defaultVal string, mask string) InputMsg { if err != nil { return InputMsg{msg: "", err: err} } + if handleSignal && isSiganl(b) { + if runtime.GOOS != "windows" { + terminal.Restore(fd, state) + } + if err := signal(b); err != nil { + return InputMsg{ + msg: "", + err: err, + } + } + } if b == 0x0d { strValue := strings.TrimSpace(string(ioBuf)) if len(strValue) == 0 { diff --git a/signal_windows.go b/signal_windows.go new file mode 100644 index 0000000..9f955f4 --- /dev/null +++ b/signal_windows.go @@ -0,0 +1,22 @@ +//go:build windows +// +build windows + +package stario + +import ( + "errors" +) + +func signal(sigtype rune) error { + //todo: use win32api call signal + switch sigtype { + case 0x03: + return errors.New("SIGNAL SIGINT RECIVED") + case 0x1a: + return errors.New("SIGNAL SIGSTOP RECIVED") + case 0x1c: + return errors.New("SIGNAL SIGQUIT RECIVED") + default: + return nil + } +} diff --git a/singal_other.go b/singal_other.go new file mode 100644 index 0000000..d300831 --- /dev/null +++ b/singal_other.go @@ -0,0 +1,26 @@ +//go:build !windows +// +build !windows + +package stario + +import ( + "errors" + "os" + "syscall" +) + +func signal(sigtype rune) error { + switch sigtype { + case 0x03: + syscall.Kill(os.Getpid(), syscall.SIGINT) + return errors.New("SIGNAL SIGINT RECIVED") + case 0x1a: + syscall.Kill(os.Getpid(), syscall.SIGSTOP) + return errors.New("SIGNAL SIGSTOP RECIVED") + case 0x1c: + syscall.Kill(os.Getpid(), syscall.SIGQUIT) + return errors.New("SIGNAL SIGQUIT RECIVED") + default: + return nil + } +}