reintroduced ClientInfo because it does appear to work.

Most likely some error elsewhere caused it not to work previously
This commit is contained in:
Erik Brakkee 2024-08-24 20:34:27 +02:00
parent d1f646e7eb
commit 7e062f5777
6 changed files with 17 additions and 27 deletions

View File

@ -41,7 +41,7 @@ func (listener AgentListener) Accept() (net.Conn, error) {
conn.Close()
return nil, err
}
conn = NewLocalAddrHackConn(conn, clientId)
conn = NewLocalAddrHackConn(conn, clientId.ClientId)
return conn, nil
}

View File

@ -19,7 +19,7 @@ func (s *AgentServerTestSuite) Test_clientIdPassedAsLocalAddr() {
func() any {
connection, err := serverChannel.Session.OpenStream()
s.Nil(err)
err = SendClientInfo(connection, clientId)
err = SendClientInfo(connection, ClientInfo{ClientId: clientId})
s.Nil(err)
return nil
})

View File

@ -1,7 +1,6 @@
package comms
import (
"encoding/binary"
"fmt"
"github.com/hashicorp/yamux"
"io"
@ -227,31 +226,18 @@ func CheckProtocolVersion(role Role, channel GOBChannel) error {
// decorates the yamux Session (which is a listener) and uses this connection to exchange some
// metadata before the connection is handed back to SSH.
// Cannot use GOB for sending clientinfo since this involves mixing of buffered reads by
// GOB with ather reads. Alternatively, we could wrap the GOB message and encode its length,
// and then read the exacct number of bytes when decodeing. But since the clientInfo is just
// a string, this is easier.
func SendClientInfo(conn io.Writer, info string) error {
err := binary.Write(conn, binary.BigEndian, uint32(len(info)))
if err != nil {
return err
}
_, err = conn.Write([]byte(info))
return err
func SendClientInfo(conn io.ReadWriter, info ClientInfo) error {
channel := NewGOBChannel(conn)
return SendWithTimeout(channel, info)
}
func ReceiveClientInfo(conn io.Reader) (string, error) {
var length uint32
err := binary.Read(conn, binary.BigEndian, &length)
func ReceiveClientInfo(conn io.ReadWriter) (ClientInfo, error) {
channel := NewGOBChannel(conn)
clientInfo, err := ReceiveWithTimeout[ClientInfo](channel)
if err != nil {
return "", err
return ClientInfo{}, err
}
bytes := make([]byte, length)
_, err = io.ReadFull(conn, bytes)
if err != nil {
return "", err
}
return string(bytes), nil
return clientInfo, nil
}
// message sent on the initial connection from server to agent to confirm the registration

View File

@ -64,11 +64,11 @@ func TestAgentServerTestSuite(t *testing.T) {
func (s *AgentServerTestSuite) TestClientInfoEncodeDecode() {
for _, clientId := range []string{"abc", "djkdfadfha"} {
buf := &bytes.Buffer{}
err := SendClientInfo(buf, clientId)
err := SendClientInfo(buf, ClientInfo{ClientId: clientId})
s.Nil(err)
clientIdReceived, err := ReceiveClientInfo(buf)
s.Nil(err)
s.Equal(clientId, clientIdReceived)
s.Equal(clientId, clientIdReceived.ClientId)
}
}

View File

@ -29,6 +29,10 @@ type EnvironmentInfo struct {
Shell string
}
type ClientInfo struct {
ClientId string
}
type SessionInfo struct {
ClientId string

View File

@ -180,7 +180,7 @@ func (admin *Admin) AddClient(publicId models.RendezVousId, clientConn iowrapper
// Before using this connection for SSH we use it to send client metadata to the
// agent
err = comms.SendClientInfo(agentConn, string(client.Info.ClientId))
err = comms.SendClientInfo(agentConn, comms.ClientInfo{ClientId: string(client.Info.ClientId)})
if err != nil {
return nil, err
}