summaryrefslogtreecommitdiff
path: root/internal/sshenv/sshenv.go
blob: 1ec177c0a19adf07a32a2b5691d895ba661dcba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package sshenv

import (
	"os"
	"strings"
)

const (
	// GitProtocolEnv defines the ENV name holding the git protocol used
	GitProtocolEnv = "GIT_PROTOCOL"
	// SSHConnectionEnv defines the ENV holding the SSH connection
	SSHConnectionEnv = "SSH_CONNECTION"
	// SSHOriginalCommandEnv defines the ENV containing the original SSH command
	SSHOriginalCommandEnv = "SSH_ORIGINAL_COMMAND"
)

type Env struct {
	GitProtocolVersion string
	IsSSHConnection    bool
	OriginalCommand    string
	RemoteAddr         string
}

func NewFromEnv() Env {
	isSSHConnection := false
	if ok := os.Getenv(SSHConnectionEnv); ok != "" {
		isSSHConnection = true
	}

	return Env{
		GitProtocolVersion: os.Getenv(GitProtocolEnv),
		IsSSHConnection:    isSSHConnection,
		RemoteAddr:         remoteAddrFromEnv(),
		OriginalCommand:    os.Getenv(SSHOriginalCommandEnv),
	}
}

// remoteAddrFromEnv returns the connection address from ENV string
func remoteAddrFromEnv() string {
	address := os.Getenv(SSHConnectionEnv)

	if address != "" {
		return strings.Fields(address)[0]
	}
	return ""
}