Skip to content

Commit 711ace2

Browse files
committed
Gracefully close SSH server, add verbose option to wush receive
1 parent aba4d2e commit 711ace2

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

cmd/wush/receive.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"tailscale.com/tsnet"
1616

1717
cslog "cdr.dev/slog"
18+
csloghuman "cdr.dev/slog/sloggers/sloghuman"
1819
"github.com/coder/coder/v2/agent/agentssh"
1920
"github.com/coder/serpent"
2021
"github.com/coder/wush/cliui"
@@ -23,14 +24,21 @@ import (
2324
)
2425

2526
func receiveCmd() *serpent.Command {
26-
var overlayType string
27+
var (
28+
overlayType string
29+
verbose bool
30+
)
2731
return &serpent.Command{
2832
Use: "receive",
2933
Aliases: []string{"host"},
3034
Long: "Runs the wush server. Allows other wush CLIs to connect to this computer.",
3135
Handler: func(inv *serpent.Invocation) error {
3236
ctx := inv.Context()
33-
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
37+
var logSink io.Writer = io.Discard
38+
if verbose {
39+
logSink = inv.Stderr
40+
}
41+
logger := slog.New(slog.NewTextHandler(logSink, nil))
3442
dm, err := tsserver.DERPMapTailscale(ctx)
3543
if err != nil {
3644
return err
@@ -77,7 +85,12 @@ func receiveCmd() *serpent.Command {
7785

7886
fmt.Println(cliui.Timestamp(time.Now()), "Wireguard is ready")
7987

80-
sshSrv, err := agentssh.NewServer(ctx, cslog.Make( /* sloghuman.Sink(os.Stderr)*/ ), prometheus.NewRegistry(), fs, nil)
88+
sshSrv, err := agentssh.NewServer(ctx,
89+
cslog.Make(csloghuman.Sink(logSink)),
90+
prometheus.NewRegistry(),
91+
fs,
92+
nil,
93+
)
8194
if err != nil {
8295
return err
8396
}
@@ -87,14 +100,33 @@ func receiveCmd() *serpent.Command {
87100
return err
88101
}
89102

90-
return sshSrv.Serve(ls)
103+
go func() {
104+
fmt.Println(cliui.Timestamp(time.Now()), "SSH server listening")
105+
err := sshSrv.Serve(ls)
106+
if err != nil {
107+
logger.Info("ssh server exited", "err", err)
108+
}
109+
}()
110+
111+
ctx, ctxCancel := inv.SignalNotifyContext(ctx, os.Interrupt)
112+
defer ctxCancel()
113+
114+
<-ctx.Done()
115+
return sshSrv.Close()
91116
},
92117
Options: []serpent.Option{
93118
{
94119
Flag: "overlay-type",
95120
Default: "derp",
96121
Value: serpent.EnumOf(&overlayType, "derp", "stun"),
97122
},
123+
{
124+
Flag: "verbose",
125+
FlagShorthand: "v",
126+
Description: "Enable verbose logging.",
127+
Default: "false",
128+
Value: serpent.BoolOf(&verbose),
129+
},
98130
},
99131
}
100132
}

cmd/wush/ssh.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ import (
2323

2424
func sshCmd() *serpent.Command {
2525
var (
26-
authID string
26+
authKey string
2727
waitP2P bool
2828
stunAddrOverride string
2929
stunAddrOverrideIP netip.Addr
3030
sshStdio bool
3131
)
3232
return &serpent.Command{
33-
Use: "wush",
33+
Use: "wush",
34+
Aliases: []string{"ssh"},
3435
Long: "Opens an SSH connection to a " + cliui.Code("wush") + " peer. " +
3536
"Use " + cliui.Code("wush receive") + " on the computer you would like to connect to.",
3637
Handler: func(inv *serpent.Invocation) error {
38+
3739
ctx := inv.Context()
3840
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
3941
logF := func(str string, args ...any) {
@@ -42,10 +44,10 @@ func sshCmd() *serpent.Command {
4244
}
4345
fmt.Fprintf(inv.Stderr, str+"\n", args...)
4446
}
45-
if authID == "" {
47+
if authKey == "" {
4648
err := huh.NewInput().
47-
Title("Enter the receiver's Auth ID:").
48-
Value(&authID).
49+
Title("Enter the receiver's Auth key:").
50+
Value(&authKey).
4951
Run()
5052
if err != nil {
5153
return fmt.Errorf("get auth id: %w", err)
@@ -67,7 +69,7 @@ func sshCmd() *serpent.Command {
6769
send := overlay.NewSendOverlay(logger, dm)
6870
send.STUNIPOverride = stunAddrOverrideIP
6971

70-
err = send.Auth.Parse(authID)
72+
err = send.Auth.Parse(authKey)
7173
if err != nil {
7274
return fmt.Errorf("parse auth key: %w", err)
7375
}
@@ -133,11 +135,11 @@ func sshCmd() *serpent.Command {
133135
},
134136
Options: []serpent.Option{
135137
{
136-
Flag: "auth-id",
137-
Env: "WUSH_AUTH_ID",
138-
Description: "The auth id returned by " + cliui.Code("wush receive") + ". If not provided, it will be asked for on startup.",
138+
Flag: "auth",
139+
Env: "WUSH_AUTH",
140+
Description: "The auth key returned by " + cliui.Code("wush receive") + ". If not provided, it will be asked for on startup.",
139141
Default: "",
140-
Value: serpent.StringOf(&authID),
142+
Value: serpent.StringOf(&authKey),
141143
},
142144
{
143145
Flag: "stun-ip-override",

0 commit comments

Comments
 (0)