-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/charmbracelet/ssh" | ||
"github.com/charmbracelet/wish" | ||
"github.com/charmbracelet/wish/logging" | ||
) | ||
|
||
const ( | ||
host = "localhost" | ||
port = 23234 | ||
) | ||
|
||
// example usage: ssh -N -R 23236:localhost:23235 -p 23234 localhost | ||
func main() { | ||
forwardHandler := &ssh.ForwardedTCPHandler{} | ||
s, err := wish.NewServer( | ||
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)), | ||
wish.WithHostKeyPath(".ssh/term_info_ed25519"), | ||
func(s *ssh.Server) error { | ||
s.ReversePortForwardingCallback = func(_ ssh.Context, bindHost string, bindPort uint32) bool { | ||
log.Info("reverse port forwarding allowed", "host", bindHost, "port", bindPort) | ||
return true | ||
} | ||
s.RequestHandlers = map[string]ssh.RequestHandler{ | ||
"tcpip-forward": forwardHandler.HandleSSHRequest, | ||
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest, | ||
} | ||
return nil | ||
}, | ||
wish.WithMiddleware( | ||
func(h ssh.Handler) ssh.Handler { | ||
return func(s ssh.Session) { | ||
wish.Println(s, "remote port forwarding available") | ||
h(s) | ||
} | ||
}, | ||
logging.Middleware(), | ||
), | ||
) | ||
if err != nil { | ||
log.Error("could not start server", "error", err) | ||
} | ||
|
||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
log.Info("Starting SSH server", "host", host, "port", port) | ||
go func() { | ||
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) { | ||
log.Error("could not start server", "error", err) | ||
done <- nil | ||
} | ||
}() | ||
|
||
<-done | ||
log.Info("Stopping SSH server") | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer func() { cancel() }() | ||
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) { | ||
log.Error("could not stop server", "error", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters