feat: sftp, scp, and named port forwards
Add netssh sftp and netssh scp. Both resolve the host like a connection does (cache, shortcuts and rewrites, instance/host, default user and port, SSH options and rules, --jump/--no-jump, --dry-run) and get shell completion. The per-client flag tables handle the differences from ssh: the port is -P, -l is a bandwidth limit (the default user goes in as -o User=), and IPv6 addresses are bracketed in host:path. All remote scp operands must name the same host. Add named port forwards under ssh.forwards (global, per instance, per rule; a more specific layer replaces a forward of the same name), netssh tunnel HOST NAME|SPEC... (ssh -N with ExitOnForwardFailure) and netssh --forward NAME. Specs such as 8080:localhost:80 work without configuration. Forwards are editable in netssh configure and completed in the shell; the TUI opens SFTP with ctrl+o and a tunnel with ctrl+t. Host resolution moves out of runSSHWrapper into resolveTarget, shared by ssh, sftp, scp, tunnel, and the TUI. Options added by netssh are now inserted before the first operand or "--": after "--", ssh read them as the destination, and scp stops reading options at the first operand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
878701aba7
commit
b7fa71fe45
19 files changed
+1389
-141
No files matched your search
@@ -22,6 +22,7 @@ func editSSHOptions(title string, o config.SSHOptions) (config.SSHOptions, error
|
||||
addressFamily := o.AddressFamily
|
||||
extraArgs := strings.Join(o.ExtraArgs, "\n")
|
||||
options := formatOptions(o.Options)
|
||||
forwards := formatForwards(o.Forwards)
|
||||
|
||||
err := huh.NewForm(huh.NewGroup(
|
||||
huh.NewInput().
|
||||
@@ -58,13 +59,25 @@ func editSSHOptions(title string, o config.SSHOptions) (config.SSHOptions, error
|
||||
_, err := parseOptions(s)
|
||||
return err
|
||||
}),
|
||||
huh.NewText().
|
||||
Title("Port forwards").
|
||||
Description("One per line: NAME L|R|D SPEC, e.g. web L 8080:localhost:80 or socks D 1080.\n"+
|
||||
"Use with netssh tunnel HOST NAME or netssh --forward NAME HOST.").
|
||||
Lines(3).
|
||||
Value(&forwards).
|
||||
Validate(func(s string) error {
|
||||
_, err := parseForwards(s)
|
||||
return err
|
||||
}),
|
||||
).Title(title)).Run()
|
||||
if err != nil {
|
||||
return o, err
|
||||
}
|
||||
|
||||
parsed, _ := parseOptions(options)
|
||||
parsedForwards, _ := parseForwards(forwards)
|
||||
return config.SSHOptions{
|
||||
Forwards: parsedForwards,
|
||||
ProxyJump: strings.TrimSpace(proxyJump),
|
||||
HostKeyChecking: hostKeyChecking,
|
||||
Target: target,
|
||||
@@ -105,6 +118,48 @@ func parseOptions(s string) (map[string]string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// forwardKinds maps the letters used in the form to the ssh flags.
|
||||
var forwardKinds = map[string]func(spec string) config.Forward{
|
||||
"L": func(spec string) config.Forward { return config.Forward{Local: spec} },
|
||||
"R": func(spec string) config.Forward { return config.Forward{Remote: spec} },
|
||||
"D": func(spec string) config.Forward { return config.Forward{Dynamic: spec} },
|
||||
}
|
||||
|
||||
// parseForwards reads "NAME L|R|D SPEC" lines into ssh.forwards.
|
||||
func parseForwards(s string) (map[string]config.Forward, error) {
|
||||
var out map[string]config.Forward
|
||||
for _, line := range parseLines(s) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 3 || forwardKinds[strings.ToUpper(fields[1])] == nil {
|
||||
return nil, fmt.Errorf("%q: expected NAME L|R|D SPEC", line)
|
||||
}
|
||||
if out == nil {
|
||||
out = make(map[string]config.Forward)
|
||||
}
|
||||
out[strings.ToLower(fields[0])] = forwardKinds[strings.ToUpper(fields[1])](fields[2])
|
||||
}
|
||||
if err := (config.SSHOptions{Forwards: out}).Validate("ssh"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func formatForwards(forwards map[string]config.Forward) string {
|
||||
names := make([]string, 0, len(forwards))
|
||||
for name := range forwards {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
lines := make([]string, 0, len(names))
|
||||
for _, name := range names {
|
||||
flag := forwards[name].Flag()
|
||||
if len(flag) == 2 {
|
||||
lines = append(lines, name+" "+strings.TrimPrefix(flag[0], "-")+" "+flag[1])
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func formatOptions(options map[string]string) string {
|
||||
keys := make([]string, 0, len(options))
|
||||
for key := range options {
|
||||
|
||||
@@ -70,3 +70,23 @@ func TestRuleValidation(t *testing.T) {
|
||||
t.Fatalf("valid match rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseForwardsRoundtrip(t *testing.T) {
|
||||
text := "Web L 8080:localhost:80\n\nsocks d 1080\nback R 9000:localhost:9000\n"
|
||||
got, err := parseForwards(text)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]config.Forward{"web": {Local: "8080:localhost:80"}, "socks": {Dynamic: "1080"}, "back": {Remote: "9000:localhost:9000"}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
if formatForwards(got) != "back R 9000:localhost:9000\nsocks D 1080\nweb L 8080:localhost:80" {
|
||||
t.Fatalf("format: %q", formatForwards(got))
|
||||
}
|
||||
for _, bad := range []string{"web 8080:localhost:80", "web X 8080", "web L -oFoo", "bad/name L 1:a:2"} {
|
||||
if _, err := parseForwards(bad); err == nil {
|
||||
t.Errorf("%q accepted", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func runMenu(cfg *config.Config) error {
|
||||
Description("Choose a section to edit, then save.").
|
||||
Options(
|
||||
huh.NewOption("SSH defaults — user, port", "defaults"),
|
||||
huh.NewOption("SSH options — jump host, host keys, target, -o", "ssh"),
|
||||
huh.NewOption("SSH options — jump host, host keys, target, -o, forwards", "ssh"),
|
||||
huh.NewOption(fmt.Sprintf("SSH rules — per tag, instance, kind, name (%d)", len(cfg.SSH.Rules)), "rules"),
|
||||
huh.NewOption("Resolver & cache", "resolver"),
|
||||
huh.NewOption("Shortcuts & command alias", "shortcuts"),
|
||||
@@ -452,7 +452,7 @@ func save(cfg config.Config) error {
|
||||
}
|
||||
|
||||
func sshOptionsEmpty(o config.SSHOptions) bool {
|
||||
return o.ProxyJump == "" && o.HostKeyChecking == "" && o.Target == "" && o.AddressFamily == "" && len(o.ExtraArgs) == 0 && len(o.Options) == 0
|
||||
return o.ProxyJump == "" && o.HostKeyChecking == "" && o.Target == "" && o.AddressFamily == "" && len(o.ExtraArgs) == 0 && len(o.Options) == 0 && len(o.Forwards) == 0
|
||||
}
|
||||
|
||||
// writeSSHOptions keeps settings the wizard does not ask for when it rewrites the config.
|
||||
@@ -479,4 +479,20 @@ func writeSSHOptions(b *strings.Builder, indent string, o config.SSHOptions) {
|
||||
fmt.Fprintf(b, "%s %s: %q\n", indent, key, o.Options[key])
|
||||
}
|
||||
}
|
||||
if len(o.Forwards) > 0 {
|
||||
names := make([]string, 0, len(o.Forwards))
|
||||
for name := range o.Forwards {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
fmt.Fprintf(b, "%sforwards:\n", indent)
|
||||
for _, name := range names {
|
||||
f := o.Forwards[name]
|
||||
for _, field := range [][2]string{{"local", f.Local}, {"remote", f.Remote}, {"dynamic", f.Dynamic}} {
|
||||
if field[1] != "" {
|
||||
fmt.Fprintf(b, "%s %s:\n%s %s: %q\n", indent, name, indent, field[0], field[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -542,3 +542,29 @@ func TestSaveShortcutRewritesRoundtrip(t *testing.T) {
|
||||
t.Fatalf("got %+v", loaded.Shortcuts.Rewrites)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveForwardsRoundtrip(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
global := map[string]config.Forward{"web": {Local: "8080:localhost:80"}, "socks": {Dynamic: "1080"}}
|
||||
cfg := config.Config{
|
||||
NetBoxes: []config.NetBoxConfig{{Name: "prod", URL: "https://prod.example", Token: "t", SSH: config.SSHOptions{Forwards: map[string]config.Forward{"db": {Local: "5432:localhost:5432"}}}}},
|
||||
Resolver: config.ResolverConfig{Strategies: []string{"primary_ip"}},
|
||||
SSH: config.SSHConfig{
|
||||
SSHOptions: config.SSHOptions{Forwards: global},
|
||||
Rules: []config.SSHRule{{Match: config.SSHMatch{Tag: "dmz"}, SSHOptions: config.SSHOptions{Forwards: map[string]config.Forward{"back": {Remote: "9000:localhost:9000"}}}}},
|
||||
},
|
||||
}
|
||||
if err := save(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := config.Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(loaded.SSH.Forwards, global) ||
|
||||
!reflect.DeepEqual(loaded.NetBoxes[0].SSH.Forwards, cfg.NetBoxes[0].SSH.Forwards) ||
|
||||
!reflect.DeepEqual(loaded.SSH.Rules[0].Forwards, cfg.SSH.Rules[0].Forwards) {
|
||||
t.Fatalf("forwards lost: %+v / %+v / %+v", loaded.SSH.Forwards, loaded.NetBoxes[0].SSH.Forwards, loaded.SSH.Rules)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user