You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to block the handler whenever a reverse port forwarding was requested. The tunnel should exist as long as the user stops the SSH session by entering the Ctrl+C sequence. There is a Signals function that I would expect to do this, but nothing arrives at the channel in my testing code:
funchandler(s ssh.Session) {
// ...ifrequestedRevPortForwarding { // set in the reversePortForwardingHandler using ctx.SetValue()io.WriteString(s, "[*] Forwarding initialized!\n")
io.WriteString(s, "[*] Stop with Ctrl+C")
signals:=make(chan ssh.Signal, 10)
s.Signals(signals)
gofunc() {
for {
fmt.Println(<-signals)
}
}()
}
<-s.Context().Done() // block// ...
}
With this example the user can only exit the session by closing the terminal he's in. There must be a way to handle this with signals. I know that I can use terminals to get like a "quit" command, but I want to use the Ctrl+C approach here.
Any idea why this is not working or an alternative to solve this?
The text was updated successfully, but these errors were encountered:
I have also been playing around with this project and I believe that signals are not yet fully implemented.
I managed to work around this by using the "golang.org/x/term" package to handle the session Read/Writer which in fact will handle CTRL+C correctly.
Example:
Handler: ssh.Handler(func(s ssh.Session) {
io.WriteString(s, "Shell is ready.\nPress CTRL+C to terminate session.\n")
shell := term.NewTerminal(s, "")
shell.SetPrompt(string("> " + string(shell.Escape.Reset)))
for {
line, err := shell.ReadLine()
if err == io.EOF {
return
}
if err != nil {
io.WriteString(s, fmt.Sprintln(err))
continue
}
if line == "" {
continue
}
io.WriteString(s, line)
}
}),
I found an easier solution (that also supports Ctrl+D):
constKeyCtrlC=3// ASCII ETX -> Ctrl+CconstKeyCtrlD=4// ASCII EOT -> Ctrl+D// ...funcHandleSession(session ssh.Session) {
// Hello there// ...for {
// we read one byte at oncec:= []byte{0}
_, err=session.Read(c)
iferr!=nil {
// error handling ...break
}
// we check if this byte is the ETX or EOT// control character & end the loopifc[0] ==KeyCtrlC||c[0] ==KeyCtrlD { break }
}
// ...// Bye
}
// ...
Actually that's how they do it in golang.org/x/term ^^
EDIT: You could also pass ssh.NoPty() to ssh.ListenAndServe() so that no pseudo-terminal is allocated for a client. Then the SSH client itself handles Ctrl+C and Ctrl+D.
I'm trying to block the handler whenever a reverse port forwarding was requested. The tunnel should exist as long as the user stops the SSH session by entering the Ctrl+C sequence. There is a Signals function that I would expect to do this, but nothing arrives at the channel in my testing code:
With this example the user can only exit the session by closing the terminal he's in. There must be a way to handle this with signals. I know that I can use terminals to get like a "quit" command, but I want to use the Ctrl+C approach here.
Any idea why this is not working or an alternative to solve this?
The text was updated successfully, but these errors were encountered: