@@ -427,6 +427,11 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
427
427
if err != nil {
428
428
return err
429
429
}
430
+ // Set SSH connection environment variables, from the clients perspective.
431
+ srcAddr , srcPort := addrToSSHEnvAddr (session .RemoteAddr ())
432
+ dstAddr , dstPort := addrToSSHEnvAddr (session .LocalAddr ())
433
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("SSH_CLIENT=%s %s %s" , srcAddr , srcPort , dstPort ))
434
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("SSH_CONNECTION=%s %s %s %s" , srcAddr , srcPort , dstAddr , dstPort ))
430
435
431
436
if ssh .AgentRequested (session ) {
432
437
l , err := ssh .NewAgentListener ()
@@ -441,6 +446,8 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
441
446
sshPty , windowSize , isPty := session .Pty ()
442
447
if isPty {
443
448
cmd .Env = append (cmd .Env , fmt .Sprintf ("TERM=%s" , sshPty .Term ))
449
+
450
+ // The pty package sets `SSH_TTY` on supported platforms.
444
451
ptty , process , err := pty .Start (cmd )
445
452
if err != nil {
446
453
return xerrors .Errorf ("start command: %w" , err )
@@ -695,6 +702,19 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
695
702
}
696
703
}
697
704
705
+ // addrToSSHEnvAddr turns the address and port into space-separated values,
706
+ // works with IPv4, IPv6 and invalid addresses (such as [peer/unknown-addr]).
707
+ func addrToSSHEnvAddr (a net.Addr ) (addr string , port string ) {
708
+ addr = a .String ()
709
+ port = "0"
710
+ li := strings .LastIndex (addr , ":" )
711
+ if li != - 1 {
712
+ port = addr [li + 1 :]
713
+ addr = addr [:li ]
714
+ }
715
+ return addr , port
716
+ }
717
+
698
718
// dialResponse is written to datachannels with protocol "dial" by the agent as
699
719
// the first packet to signify whether the dial succeeded or failed.
700
720
type dialResponse struct {
0 commit comments