diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index 1ea0f69..5bd21f0 100755 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -310,7 +310,7 @@ func main() { defer wsConn.Close() shell := chooseShell(shells) - _, err = comms.AgentInitialization(wsConn, comms.NewAgentInfo(shell)) + _, err = comms.AgentInitialization(wsConn, comms.NewEnvironmentInfo(shell)) if err != nil { log.Printf("ERROR: %v", err) os.Exit(1) diff --git a/cmd/agent/sshauthorizedkeys.go b/cmd/agent/sshauthorizedkeys.go index a433d8d..a518e11 100644 --- a/cmd/agent/sshauthorizedkeys.go +++ b/cmd/agent/sshauthorizedkeys.go @@ -170,7 +170,6 @@ func (key *AuthorizedPublicKeys) authorize(ctx ssh.Context, userProvidedKey ssh. log.Printf("No valid public keys were found, login is impossible") // keep agent running, a user may still be logged in and could change the // authorizedk eys file - // TODO: if no users are logged in, the agent should exit. } for _, key := range keys { if publicKeyHandler(ctx, userProvidedKey, key) { diff --git a/cmd/converge/prometheus.go b/cmd/converge/prometheus.go index 3aa16b1..3870913 100644 --- a/cmd/converge/prometheus.go +++ b/cmd/converge/prometheus.go @@ -61,11 +61,11 @@ func agentLabels(agent models.Agent) prometheus.Labels { return prometheus.Labels{ "guid": agent.Guid, "id": agent.PublicId, - "username": agent.AgentInfo.Username, - "hostname": agent.AgentInfo.Hostname, - "pwd": agent.AgentInfo.Pwd, - "os": agent.AgentInfo.OS, - "shell": agent.AgentInfo.Shell, + "username": agent.EnvironmentInfo.Username, + "hostname": agent.EnvironmentInfo.Hostname, + "pwd": agent.EnvironmentInfo.Pwd, + "os": agent.EnvironmentInfo.OS, + "shell": agent.EnvironmentInfo.Shell, } } diff --git a/cmd/templaterender/render.go b/cmd/templaterender/render.go index c2018a8..9538c25 100644 --- a/cmd/templaterender/render.go +++ b/cmd/templaterender/render.go @@ -84,7 +84,7 @@ func main() { Guid: strconv.Itoa(rand.Int()), PublicId: "id", StartTime: time.Now().In(japan), - AgentInfo: comms.AgentInfo{ + EnvironmentInfo: comms.EnvironmentInfo{ Username: "ci", Hostname: "container123", Pwd: "/home/ci", diff --git a/cmd/wsproxy/wsproxy.go b/cmd/wsproxy/wsproxy.go index 9b91c99..d2ed03b 100644 --- a/cmd/wsproxy/wsproxy.go +++ b/cmd/wsproxy/wsproxy.go @@ -137,6 +137,11 @@ func main() { log.Printf("Error reported by server: %v", clientConnectionInfo.Message) os.Exit(1) } + err = comms.SendWithTimeout(channel, comms.NewEnvironmentInfo(os.Getenv("SHELL"))) + if err != nil { + log.Printf("Could not send environment info to server") + os.Exit(1) + } } dataConnection := wsConn diff --git a/pkg/comms/agentserver.go b/pkg/comms/agentserver.go index 95b38a4..03c7c40 100644 --- a/pkg/comms/agentserver.go +++ b/pkg/comms/agentserver.go @@ -90,7 +90,7 @@ func NewCommChannel(role Role, wsConn io.ReadWriteCloser) (CommChannel, error) { // Sending an event to the other side func ListenForAgentEvents(channel GOBChannel, - agentInfo func(agent AgentInfo), + agentInfo func(agent EnvironmentInfo), sessionInfo func(session SessionInfo), expiryTimeUpdate func(session ExpiryTimeUpdate)) { for { @@ -103,7 +103,7 @@ func ListenForAgentEvents(channel GOBChannel, } switch v := result.Value.(type) { - case AgentInfo: + case EnvironmentInfo: agentInfo(v) case SessionInfo: @@ -142,7 +142,7 @@ func ListenForServerEvents(channel CommChannel) { } } -func AgentInitialization(conn io.ReadWriter, agentInto AgentInfo) (ServerInfo, error) { +func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerInfo, error) { channel := NewGOBChannel(conn) err := CheckProtocolVersion(Agent, channel) @@ -160,18 +160,18 @@ func AgentInitialization(conn io.ReadWriter, agentInto AgentInfo) (ServerInfo, e return serverInfo, err } -func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (AgentInfo, error) { +func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (EnvironmentInfo, error) { channel := NewGOBChannel(conn) err := CheckProtocolVersion(ConvergeServer, channel) - agentInfo, err := ReceiveWithTimeout[AgentInfo](channel) + agentInfo, err := ReceiveWithTimeout[EnvironmentInfo](channel) if err != nil { - return AgentInfo{}, err + return EnvironmentInfo{}, err } log.Println("Agent info received: ", agentInfo) err = SendWithTimeout(channel, serverInfo) if err != nil { - return AgentInfo{}, nil + return EnvironmentInfo{}, nil } return agentInfo, err } diff --git a/pkg/comms/events.go b/pkg/comms/events.go index de75660..dedb71c 100644 --- a/pkg/comms/events.go +++ b/pkg/comms/events.go @@ -8,7 +8,7 @@ import ( "time" ) -const PROTOCOL_VERSION = 2 +const PROTOCOL_VERSION = 3 func init() { RegisterEventsWithGob() @@ -16,7 +16,7 @@ func init() { // Agent to server events -type AgentInfo struct { +type EnvironmentInfo struct { Username string Hostname string Pwd string @@ -67,11 +67,11 @@ type ConvergeMessage struct { Value interface{} } -func NewAgentInfo(shell string) AgentInfo { +func NewEnvironmentInfo(shell string) EnvironmentInfo { username, _ := user.Current() host, _ := os.Hostname() pwd, _ := os.Getwd() - return AgentInfo{ + return EnvironmentInfo{ Username: username.Username, Hostname: host, Pwd: pwd, @@ -93,7 +93,7 @@ func NewExpiryTimeUpdate(expiryTime time.Time) ExpiryTimeUpdate { func RegisterEventsWithGob() { // Agent to ConvergeServer - gob.Register(AgentInfo{}) + gob.Register(EnvironmentInfo{}) gob.Register(SessionInfo{}) gob.Register(ExpiryTimeUpdate{}) gob.Register(HeartBeat{}) diff --git a/pkg/models/agent.go b/pkg/models/agent.go index e27954c..2db66da 100644 --- a/pkg/models/agent.go +++ b/pkg/models/agent.go @@ -10,6 +10,6 @@ type Agent struct { PublicId string StartTime time.Time - AgentInfo comms.AgentInfo - ExpiryTime time.Time + EnvironmentInfo comms.EnvironmentInfo + ExpiryTime time.Time } diff --git a/pkg/models/client.go b/pkg/models/client.go index 14756a6..3b3bb68 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -1,13 +1,15 @@ package models import ( + "converge/pkg/comms" "time" ) type Client struct { - Guid string - PublicId string - ClientId string - StartTime time.Time - SessionType string + Guid string + PublicId string + ClientId string + StartTime time.Time + SessionType string + EnvironmentInfo comms.EnvironmentInfo } diff --git a/pkg/server/converge/admin.go b/pkg/server/converge/admin.go index 2d23580..e1bd6d5 100644 --- a/pkg/server/converge/admin.go +++ b/pkg/server/converge/admin.go @@ -31,13 +31,13 @@ type ClientConnection struct { client iowrappers2.ReadWriteAddrCloser } -func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.AgentInfo) *AgentConnection { +func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.EnvironmentInfo) *AgentConnection { return &AgentConnection{ Agent: models.Agent{ - Guid: strconv.Itoa(rand.Int()), - PublicId: publicId, - StartTime: time.Now(), - AgentInfo: agentInfo, + Guid: strconv.Itoa(rand.Int()), + PublicId: publicId, + StartTime: time.Now(), + EnvironmentInfo: agentInfo, }, commChannel: commChannel, } @@ -101,9 +101,9 @@ func (admin *Admin) logStatus() { lines = append(lines, fmt.Sprintf(format, agent.PublicId, agent.StartTime.Format(time.DateTime), agent.ExpiryTime.Format(time.DateTime), - agent.AgentInfo.Username, - agent.AgentInfo.Hostname, - agent.AgentInfo.OS)) + agent.EnvironmentInfo.Username, + agent.EnvironmentInfo.Hostname, + agent.EnvironmentInfo.OS)) } lines = append(lines, "") format = "%-10s %-20s %-20s %-20s %-20s" @@ -145,7 +145,7 @@ func (admin *Admin) getFreeId(publicId string) (string, error) { return "", fmt.Errorf("Could not allocate agent id based on requested public id '%s'", publicId) } -func (admin *Admin) addAgent(publicId string, agentInfo comms.AgentInfo, conn io.ReadWriteCloser) (*AgentConnection, error) { +func (admin *Admin) addAgent(publicId string, agentInfo comms.EnvironmentInfo, conn io.ReadWriteCloser) (*AgentConnection, error) { admin.mutex.Lock() defer admin.mutex.Unlock() @@ -287,8 +287,8 @@ func (admin *Admin) Register(publicId string, conn io.ReadWriteCloser) error { go func() { comms.ListenForAgentEvents(agent.commChannel.SideChannel, - func(info comms.AgentInfo) { - agent.AgentInfo = info + func(info comms.EnvironmentInfo) { + agent.EnvironmentInfo = info admin.logStatus() }, func(session comms.SessionInfo) { @@ -356,6 +356,11 @@ func (admin *Admin) Connect(wsProxyMode bool, publicId string, conn iowrappers2. if err != nil { return fmt.Errorf("Error sending connection info to client: %v", err) } + clientEnvironment, err := comms.ReceiveWithTimeout[comms.EnvironmentInfo](channel) + if err != nil { + return fmt.Errorf("Error receiving environment info from client: %v", err) + } + client.EnvironmentInfo = clientEnvironment } iowrappers2.SynchronizeStreams("client -- agent", client.client, client.agent) diff --git a/pkg/server/templates/sessions.templ b/pkg/server/templates/sessions.templ index 9686ac0..0129637 100644 --- a/pkg/server/templates/sessions.templ +++ b/pkg/server/templates/sessions.templ @@ -47,10 +47,10 @@ templ State(state *models.State, location *time.Location) { {agent.PublicId} {agent.StartTime.In(location).Format(time.DateTime)} {agent.ExpiryTime.In(location).Format(time.DateTime)} - {agent.AgentInfo.Username} - {agent.AgentInfo.Hostname} - {agent.AgentInfo.OS} - {agent.AgentInfo.Shell} + {agent.EnvironmentInfo.Username} + {agent.EnvironmentInfo.Hostname} + {agent.EnvironmentInfo.OS} + {agent.EnvironmentInfo.Shell} } @@ -70,6 +70,9 @@ templ State(state *models.State, location *time.Location) { start time session type rendez-vous id + username + host + os for _, client := range state.Clients { @@ -78,6 +81,9 @@ templ State(state *models.State, location *time.Location) { {client.StartTime.In(location).Format(time.DateTime)} {client.SessionType} {client.PublicId} + {client.EnvironmentInfo.Username} + {client.EnvironmentInfo.Hostname} + {client.EnvironmentInfo.OS} } diff --git a/pkg/server/templates/usage.templ b/pkg/server/templates/usage.templ index 8958010..c22fa53 100644 --- a/pkg/server/templates/usage.templ +++ b/pkg/server/templates/usage.templ @@ -178,7 +178,7 @@ templ Usage(access models.ConvergeAccess) { - +