Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sshd: Add Terminal.Env() #309

Merged
merged 2 commits into from
Apr 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions sshd/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ func (c sshConn) Name() string {
return c.User()
}

// EnvVar is an environment variable key-value pair
type EnvVar struct {
Key string
Value string
}

func (v EnvVar) String() string {
return v.Key + "=" + v.Value
}

// Env is a wrapper type around []EnvVar with some helper methods
type Env []EnvVar

// Get returns the latest value for a given key, or empty string if not found
func (e Env) Get(key string) string {
for i := len(e) - 1; i >= 0; i-- {
if e[i].Key == key {
return e[i].Value
}
}
return ""
}

// Terminal extends ssh/terminal to include a close method
type Terminal struct {
terminal.Terminal
Expand All @@ -63,6 +86,9 @@ type Terminal struct {

done chan struct{}
closeOnce sync.Once

mu sync.Mutex
env []EnvVar
}

// Make new terminal from a session channel
Expand Down Expand Up @@ -161,10 +187,27 @@ func (t *Terminal) listen(requests <-chan *ssh.Request) {
err := t.SetSize(width, height)
ok = err == nil
}
case "env":
var v EnvVar
if err := ssh.Unmarshal(req.Payload, &v); err != nil {
shazow marked this conversation as resolved.
Show resolved Hide resolved
t.mu.Lock()
t.env = append(t.env, v)
t.mu.Unlock()
ok = true
}
}

if req.WantReply {
req.Reply(ok, nil)
}
}
}

// Env returns a list of environment key-values that have been set. They are
// returned in the order that they have been set, there is no deduplication or
// other pre-processing applied.
func (t *Terminal) Env() Env {
t.mu.Lock()
defer t.mu.Unlock()
return Env(t.env)
}