diff options
| author | Shulhan <ms@kilabit.info> | 2023-07-24 12:59:19 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-07-26 23:02:19 +0700 |
| commit | 2326f9db830c48ba37c1f5bbabb2369b3d3a3c2c (patch) | |
| tree | 6eecacfe87730c549e10b4b1d8cb9cf29c41fba0 /lib/ssh/client.go | |
| parent | 722092757fa0ca6a3ca2047dae13aff812b09aa6 (diff) | |
| download | pakakeh.go-2326f9db830c48ba37c1f5bbabb2369b3d3a3c2c.tar.xz | |
ssh/config: refactoring, simplify the Section fields
Instead of storing each Section value in separate field, store them
inside a map, Field.
This reduce the size of Section and simplify adding or getting the
key that we are not supported but maybe usable by user in the future.
This changes introduce several new methods as replacement of field:
* CASignatureAlgorithms: a method that return list of signature
algorithms that Section set or the default
* CanonicalDomains: a method that return CanonicalDomains set in Section
* CanonicalizePermittedCNames: return the permitted CNAMEs set in Section,
from KeyCanonicalizePermittedCNames.
* CertificateFile: return list of certificate file
* Environments: return system and/or custom environment that will be
passed to remote machine.
The key and value is derived from "SendEnv" and "SetEnv".
* FieldBool: return field value as boolean
* FieldInt: return the field value as int
* Hostname: return the Hostname in this Section
* IdentityAgent: return the path to SSH agent socket to be used
* Port: return the remote machine port
* User: return the remote user name
* Set: set the Field using key and value
Diffstat (limited to 'lib/ssh/client.go')
| -rw-r--r-- | lib/ssh/client.go | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/lib/ssh/client.go b/lib/ssh/client.go index ea624066..829a54ec 100644 --- a/lib/ssh/client.go +++ b/lib/ssh/client.go @@ -16,11 +16,13 @@ import ( "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" + libos "github.com/shuLhan/share/lib/os" "github.com/shuLhan/share/lib/ssh/config" ) // Client for SSH connection. type Client struct { + sysEnvs map[string]string *ssh.Client cfg *config.Section @@ -39,9 +41,9 @@ func NewClientFromConfig(cfg *config.Section) (cl *Client, err error) { var ( logp = `NewClient` - remoteAddr = fmt.Sprintf(`%s:%s`, cfg.Hostname, cfg.Port) + remoteAddr = fmt.Sprintf(`%s:%s`, cfg.Hostname(), cfg.Port()) sshConfig = &ssh.ClientConfig{ - User: cfg.User, + User: cfg.User(), HostKeyCallback: ssh.InsecureIgnoreHostKey(), } @@ -49,12 +51,13 @@ func NewClientFromConfig(cfg *config.Section) (cl *Client, err error) { ) cl = &Client{ - cfg: cfg, - stdout: os.Stdout, - stderr: os.Stderr, + sysEnvs: libos.Environments(), + cfg: cfg, + stdout: os.Stdout, + stderr: os.Stderr, } - sshAgentSockPath := cfg.GetIdentityAgent() + sshAgentSockPath := cfg.IdentityAgent() if len(sshAgentSockPath) > 0 { sshAgentSock, err := net.Dial("unix", sshAgentSockPath) if err != nil { @@ -120,7 +123,7 @@ func (cl *Client) Execute(cmd string) (err error) { sess.Stdout = cl.stdout sess.Stderr = cl.stderr - for k, v := range cl.cfg.Environments { + for k, v := range cl.cfg.Environments(cl.sysEnvs) { err = sess.Setenv(k, v) if err != nil { log.Printf("Execute: Setenv %q=%q:%s\n", k, v, err.Error()) @@ -151,9 +154,9 @@ func (cl *Client) ScpGet(remote, local string) (err error) { return fmt.Errorf("%s: empty local file", logp) } - remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User, cl.cfg.Hostname, remote) + remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User(), cl.cfg.Hostname(), remote) - args := []string{"-r", "-P", cl.cfg.Port} + args := []string{"-r", "-P", cl.cfg.Port()} if len(cl.cfg.PrivateKeyFile) > 0 { args = append(args, "-i") args = append(args, cl.cfg.PrivateKeyFile) @@ -189,9 +192,9 @@ func (cl *Client) ScpPut(local, remote string) (err error) { return fmt.Errorf("%s: empty remote file", logp) } - remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User, cl.cfg.Hostname, remote) + remote = fmt.Sprintf("%s@%s:%s", cl.cfg.User(), cl.cfg.Hostname(), remote) - args := []string{"-r", "-P", cl.cfg.Port} + args := []string{"-r", "-P", cl.cfg.Port()} if len(cl.cfg.PrivateKeyFile) > 0 { args = append(args, "-i") args = append(args, cl.cfg.PrivateKeyFile) @@ -225,5 +228,5 @@ func (cl *Client) SetSessionOutputError(stdout, stderr io.Writer) { } func (cl *Client) String() string { - return cl.cfg.User + "@" + cl.cfg.Hostname + ":" + cl.cfg.Port + return cl.cfg.User() + "@" + cl.cfg.Hostname() + ":" + cl.cfg.Port() } |
