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,48 @@ type SSHOptions struct {
|
||||
ExtraArgs []string `mapstructure:"extra_args"`
|
||||
// Options are passed as -o Key=Value.
|
||||
Options map[string]string `mapstructure:"options"`
|
||||
// Forwards are named port forwards, selected with --forward or netssh tunnel.
|
||||
Forwards map[string]Forward `mapstructure:"forwards"`
|
||||
}
|
||||
|
||||
// Forward is one port forward; exactly one of the fields is set.
|
||||
type Forward struct {
|
||||
Local string `mapstructure:"local"` // -L [bind:]port:host:hostport
|
||||
Remote string `mapstructure:"remote"` // -R [bind:]port:host:hostport
|
||||
Dynamic string `mapstructure:"dynamic"` // -D [bind:]port (SOCKS proxy)
|
||||
}
|
||||
|
||||
// Flag returns the ssh arguments for the forward, e.g. -L 8080:localhost:80.
|
||||
func (f Forward) Flag() []string {
|
||||
switch {
|
||||
case f.Local != "":
|
||||
return []string{"-L", f.Local}
|
||||
case f.Remote != "":
|
||||
return []string{"-R", f.Remote}
|
||||
case f.Dynamic != "":
|
||||
return []string{"-D", f.Dynamic}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// String renders the forward as its ssh flag, e.g. "-L 8080:localhost:80".
|
||||
func (f Forward) String() string { return strings.Join(f.Flag(), " ") }
|
||||
|
||||
func (f Forward) validate() error {
|
||||
set := 0
|
||||
for _, spec := range []string{f.Local, f.Remote, f.Dynamic} {
|
||||
if spec == "" {
|
||||
continue
|
||||
}
|
||||
set++
|
||||
if strings.HasPrefix(spec, "-") || strings.ContainsAny(spec, " \t\n\r") {
|
||||
return fmt.Errorf("invalid forward %q", spec)
|
||||
}
|
||||
}
|
||||
if set != 1 {
|
||||
return fmt.Errorf("set exactly one of local, remote, dynamic")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SSHRule applies SSH options to hosts that match every non-empty field.
|
||||
@@ -113,6 +155,14 @@ func (o SSHOptions) Validate(scope string) error {
|
||||
return fmt.Errorf("%s.options: invalid option %q", scope, key)
|
||||
}
|
||||
}
|
||||
for name, forward := range o.Forwards {
|
||||
if !instanceName.MatchString(name) {
|
||||
return fmt.Errorf("%s.forwards: name %q must contain only letters, digits, underscores, or hyphens", scope, name)
|
||||
}
|
||||
if err := forward.validate(); err != nil {
|
||||
return fmt.Errorf("%s.forwards.%s: %v", scope, name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -147,6 +197,14 @@ func (o SSHOptions) merge(override SSHOptions) SSHOptions {
|
||||
for key, value := range override.Options {
|
||||
out.Options[strings.ToLower(key)] = value
|
||||
}
|
||||
// Forwards override per name, so a rule can redefine a global "web" forward.
|
||||
out.Forwards = make(map[string]Forward, len(o.Forwards)+len(override.Forwards))
|
||||
for name, forward := range o.Forwards {
|
||||
out.Forwards[strings.ToLower(name)] = forward
|
||||
}
|
||||
for name, forward := range override.Forwards {
|
||||
out.Forwards[strings.ToLower(name)] = forward
|
||||
}
|
||||
if override.ProxyJump != "" {
|
||||
out.ProxyJump = override.ProxyJump
|
||||
}
|
||||
|
||||
@@ -88,3 +88,50 @@ func TestSSHOptionsValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardsLayering(t *testing.T) {
|
||||
cfg, err := loadYAML(t, `
|
||||
netboxes:
|
||||
- name: prod
|
||||
url: https://prod.example
|
||||
token: t
|
||||
ssh:
|
||||
forwards:
|
||||
db: {local: "5432:localhost:5432"}
|
||||
ssh:
|
||||
forwards:
|
||||
web: {local: "8080:localhost:80"}
|
||||
socks: {dynamic: "1080"}
|
||||
rules:
|
||||
- match: {tag: dmz}
|
||||
forwards:
|
||||
web: {local: "8443:localhost:443"}
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := cfg.SSHOptionsFor(SSHHost{Instance: "prod", Name: "web01", Tags: []string{"dmz"}}).Forwards
|
||||
want := map[string]Forward{"web": {Local: "8443:localhost:443"}, "socks": {Dynamic: "1080"}, "db": {Local: "5432:localhost:5432"}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
if got := cfg.SSHOptionsFor(SSHHost{Instance: "other", Name: "web01"}).Forwards; len(got) != 2 || got["web"].Local != "8080:localhost:80" {
|
||||
t.Fatalf("other instance: %+v", got)
|
||||
}
|
||||
if f := (Forward{Remote: "9000:localhost:9000"}); f.String() != "-R 9000:localhost:9000" {
|
||||
t.Fatal(f.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardsValidation(t *testing.T) {
|
||||
for _, forwards := range []string{
|
||||
`web: {local: "8080:a:80", dynamic: "1080"}`,
|
||||
`web: {local: "-oProxyCommand=x"}`,
|
||||
`web: {local: "8080 a:80"}`,
|
||||
`"bad name": {local: "8080:a:80"}`,
|
||||
} {
|
||||
if _, err := loadYAML(t, "ssh:\n forwards:\n "+forwards+"\n"); err == nil || !strings.Contains(err.Error(), "forwards") {
|
||||
t.Errorf("%s: got %v", forwards, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user