From 9b57fd8952a9452f33da0fe2582e64cc9b0fa513 Mon Sep 17 00:00:00 2001 From: blacknon Date: Wed, 14 Aug 2024 01:09:51 +0900 Subject: [PATCH] update. --- cmd/lssh/args.go | 42 ++++++++++++++++++++++++++++++++++++-- common/common.go | 13 ++++++++++++ conf/conf_struct_server.go | 16 +++++++++++++++ ssh/cmd.go | 14 +++++++++++++ ssh/run.go | 36 ++++++++++++++++++++++++++++++++ ssh/shell.go | 30 +++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 2 deletions(-) diff --git a/cmd/lssh/args.go b/cmd/lssh/args.go index f3a1983a..30d96208 100644 --- a/cmd/lssh/args.go +++ b/cmd/lssh/args.go @@ -62,8 +62,8 @@ USAGE: app.Version = "0.6.12" // TODO(blacknon): オプションの追加 - // -m ... NFSマウントで、リモートホストの特定ディレクトリをローカルにマウント可能にする (v0.7.0) - // -M ... リバースNFSマウントで、リモートホストの特定ディレクトリをローカルにマウント可能にする (v0.7.0) + // -n ... NFSマウントで、リモートホストの特定ディレクトリをローカルにマウント可能にする (v0.7.0) + // -m ... リバースNFSマウントで、リモートホストの特定ディレクトリをローカルにマウント可能にする (v0.7.0) // -f ... バックグラウンドでの接続(X11接続やport forwardingをバックグラウンドで実行する場合など)。 // 「ssh -f」と同じ。 (v0.7.0) // (https://github.com/sevlyar/go-daemon) @@ -85,6 +85,8 @@ USAGE: cli.StringFlag{Name: "D", Usage: "Dynamic port forward mode(Socks5). Specify a `port`. Only single connection works."}, cli.StringFlag{Name: "d", Usage: "HTTP Dynamic port forward mode. Specify a `port`. Only single connection works."}, cli.StringFlag{Name: "r", Usage: "HTTP Reverse Dynamic port forward mode. Specify a `port`. Only single connection works."}, + cli.StringFlag{Name: "n", Usage: "NFS Dynamic forward mode. Specify a `port:/path/to/remote`. Only single connection works."}, + cli.StringFlag{Name: "m", Usage: "NFS Reverse Dynamic forward mode. Specify a `port:/path/to/local`. Only single connection works."}, // Other bool cli.BoolFlag{Name: "w", Usage: "Displays the server header when in command execution mode."}, @@ -236,6 +238,36 @@ USAGE: } } + // Set NFS Forwarding + nfsForwarding := c.String("n") + if nfsForwarding != "" { + port, path, err := common.ParseNFSForwardPortPath(nfsForwarding) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %s\n", err) + os.Exit(1) + } + + path = common.GetFullPath(path) + + r.NFSDynamicForwardPort = port + r.NFSDynamicForwardPath = path + } + + // Set NFS Reverse Forwarding + nfsReverseForwarding := c.String("m") + if nfsReverseForwarding != "" { + port, path, err := common.ParseNFSForwardPortPath(nfsReverseForwarding) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %s\n", err) + os.Exit(1) + } + + path = common.GetFullPath(path) + + r.NFSReverseDynamicForwardPort = port + r.NFSReverseDynamicForwardPath = path + } + // if err if err != nil { fmt.Printf("Error: %s \n", err) @@ -256,6 +288,12 @@ USAGE: // HTTP Reverse Dynamic port forwarding port r.HTTPReverseDynamicPortForward = c.String("r") + // NFS Dynamic forwarding port + // r.NFSDynamicPortForward = c.String("n") + + // NFS Reverse Dynamic forwarding port + // r.NFSReverseDynamicPortForward = c.String("m") + r.Start() return nil } diff --git a/common/common.go b/common/common.go index 82d742e7..6a2eba04 100644 --- a/common/common.go +++ b/common/common.go @@ -370,6 +370,19 @@ func ParseForwardPort(value string) (local, remote string, err error) { return } +func ParseNFSForwardPortPath(value string) (port, path string, err error) { + data := strings.Split(value, ":") + if len(data) != 2 { + err = errors.New("Could not parse.") + return + } + + port = data[0] + path = data[1] + + return +} + // ParseHostPath return host and path, from host:/path/to/dir/file. func ParseHostPath(value string) (host []string, path string) { if !strings.Contains(value, ":") { diff --git a/conf/conf_struct_server.go b/conf/conf_struct_server.go index 6d86a1ef..e2458d60 100644 --- a/conf/conf_struct_server.go +++ b/conf/conf_struct_server.go @@ -93,6 +93,22 @@ type ServerConfig struct { // ex.) "11080" HTTPReverseDynamicPortForward string `toml:"http_reverse_dynamic_port_forward"` + // NFS Dynamic Forward port setting + // ex.) "12049" + NFSDynamicForwardPort string `toml:"nfs_dynamic_forward"` + + // NFS Dynamic Forward path setting + // ex.) "/path/to/remote" + NFSDynamicForwardPath string `toml:"nfs_dynamic_forward_path"` + + // NFS Reverse Dynamic Forward port setting + // ex.) "12049" + NFSReverseDynamicForwardPort string `toml:"nfs_reverse_dynamic_forward"` + + // NFS Reverse Dynamic Forward path setting + // ex.) "/path/to/local" + NFSReverseDynamicForwardPath string `toml:"nfs_reverse_dynamic_forward_path"` + // x11 forwarding setting X11 bool `toml:"x11"` diff --git a/ssh/cmd.go b/ssh/cmd.go index 102a2783..673191f3 100644 --- a/ssh/cmd.go +++ b/ssh/cmd.go @@ -161,6 +161,20 @@ func (r *Run) cmd() (err error) { go c.HTTPReverseDynamicForward("localhost", config.HTTPReverseDynamicPortForward) } + // NFS Dynamic Forward + if r.NFSDynamicForwardPort != "" && r.NFSDynamicForwardPath != "" { + config.NFSDynamicForwardPort = r.NFSDynamicForwardPort + config.NFSDynamicForwardPath = r.NFSDynamicForwardPath + go c.NFSForward("localhost", config.NFSDynamicForwardPort, config.NFSDynamicForwardPath) + } + + // NFS Reverse Dynamic Forward + if r.NFSReverseDynamicForwardPath != "" && r.NFSReverseDynamicForwardPath != "" { + config.NFSReverseDynamicForwardPort = r.NFSReverseDynamicForwardPort + config.NFSReverseDynamicForwardPath = r.NFSReverseDynamicForwardPath + go c.NFSReverseForward("localhost", config.NFSReverseDynamicForwardPort, config.NFSReverseDynamicForwardPath) + } + // if tty if r.IsTerm { c.Stdin = os.Stdin diff --git a/ssh/run.go b/ssh/run.go index 0e3c0958..6e1a8542 100644 --- a/ssh/run.go +++ b/ssh/run.go @@ -19,6 +19,8 @@ import ( "golang.org/x/crypto/ssh/terminal" ) +// TOOD(blacknon): なんかProxyのポートが表示おかしいので、修正する(v0.7.0) + // TODO(blacknon): 自動再接続機能の追加(v1.0.0) // autosshのように、接続が切れた際に自動的に再接続を試みる動作をさせたい // パラメータでの有効・無効指定が必要になる。 @@ -95,6 +97,22 @@ type Run struct { // set remotehost port num (ex. 11080). HTTPReverseDynamicPortForward string + // NFS Dynamic Forward + // set localhost port num (ex. 12049). + NFSDynamicForwardPort string + + // NFS Dynamic Forward Path + // set remotehost path (ex. /path/to/remote). + NFSDynamicForwardPath string + + // NFS Reverse Dynamic Forward + // set remotehost port num (ex. 12049). + NFSReverseDynamicForwardPort string + + // NFS Reverse Dynamic Forward Path + // set localhost path (ex. /path/to/local). + NFSReverseDynamicForwardPath string + // Exec command ExecCmd []string @@ -263,6 +281,24 @@ func (r *Run) printHTTPReverseDynamicPortForward(port string) { } } +// printNFSDynamicForward is printout forwarding. +// use ssh command run header. only use shell(). +func (r *Run) printNFSDynamicForward(port, path string) { + if port != "" { + fmt.Fprintf(os.Stderr, "NFSDynamicForward:%s:%s\n", port, path) + fmt.Fprintf(os.Stderr, " %s\n", "connect NFS.") + } +} + +// printNFSReverseDynamicForward is printout forwarding. +// use ssh command run header. only use shell(). +func (r *Run) printNFSReverseDynamicForward(port, path string) { + if port != "" { + fmt.Fprintf(os.Stderr, "NFSReverseDynamicForward:%s:%s\n", port, path) + fmt.Fprintf(os.Stderr, " %s\n", "connect NFS.") + } +} + // printProxy is printout proxy route. // use ssh command run header. only use shell(). func (r *Run) printProxy(server string) { diff --git a/ssh/shell.go b/ssh/shell.go index 6a961e6e..6cdaf5b7 100644 --- a/ssh/shell.go +++ b/ssh/shell.go @@ -55,6 +55,26 @@ func (r *Run) shell() (err error) { config.HTTPReverseDynamicPortForward = r.HTTPReverseDynamicPortForward } + // OverWrite nfs dynacmic forwarding + if r.NFSDynamicForwardPort != "" { + config.NFSDynamicForwardPort = r.NFSDynamicForwardPort + } + + // OverWrite nfs dynamic path + if r.NFSDynamicForwardPath != "" { + config.NFSDynamicForwardPath = r.NFSDynamicForwardPath + } + + // OverWrite nfs reverse dynamic forwarding + if r.NFSReverseDynamicForwardPort != "" { + config.NFSReverseDynamicForwardPort = r.NFSReverseDynamicForwardPort + } + + // OverWrite nfs reverse dynamic path + if r.NFSReverseDynamicForwardPath != "" { + config.NFSReverseDynamicForwardPath = r.NFSReverseDynamicForwardPath + } + // OverWrite local bashrc use if r.IsBashrc { config.LocalRcUse = "yes" @@ -131,6 +151,16 @@ func (r *Run) shell() (err error) { go connect.HTTPReverseDynamicForward("localhost", config.HTTPReverseDynamicPortForward) } + // NFS Dynamic Forwarding + if config.NFSDynamicForwardPort != "" && config.NFSDynamicForwardPath != "" { + go connect.NFSForward("localhost", config.NFSDynamicForwardPort, config.NFSDynamicForwardPath) + } + + // NFS Reverse Dynamic Forwarding + if config.NFSReverseDynamicForwardPort != "" && config.NFSReverseDynamicForwardPath != "" { + go connect.NFSReverseForward("localhost", config.NFSReverseDynamicForwardPort, config.NFSReverseDynamicForwardPath) + } + // switch check Not-execute flag // TODO(blacknon): Backgroundフラグを実装したら追加 switch {