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 {
|
||||
|
||||
Reference in new issue
Block a user