From 26e75fe2233317c023992c80e57b96941e943943 Mon Sep 17 00:00:00 2001 From: Siting Ren Date: Fri, 30 Sep 2022 23:10:41 +0800 Subject: [PATCH] Add client_os_user_name for session (#148) --- msgs/festartupmsg.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/msgs/festartupmsg.go b/msgs/festartupmsg.go index c7bf934..4110bcc 100644 --- a/msgs/festartupmsg.go +++ b/msgs/festartupmsg.go @@ -32,7 +32,10 @@ package msgs // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -import "fmt" +import ( + "fmt" + "os/user" +) // FEStartupMsg docs type FEStartupMsg struct { @@ -43,11 +46,18 @@ type FEStartupMsg struct { Database string SessionID string ClientPID int + OSUsername string } // Flatten docs func (m *FEStartupMsg) Flatten() ([]byte, byte) { + m.OSUsername = "" + currentUser, err := user.Current() + if err == nil { + m.OSUsername = currentUser.Username + } + buf := newMsgBuffer() const fixedProtocolVersion uint32 = 0x00030005 buf.appendUint32(fixedProtocolVersion) @@ -69,6 +79,7 @@ func (m *FEStartupMsg) Flatten() ([]byte, byte) { buf.appendLabeledString("client_version", m.DriverVersion) buf.appendLabeledString("client_label", m.SessionID) buf.appendLabeledString("client_pid", fmt.Sprintf("%d", m.ClientPID)) + buf.appendLabeledString("client_os_user_name", m.OSUsername) buf.appendBytes([]byte{0}) return buf.bytes(), 0 @@ -76,12 +87,13 @@ func (m *FEStartupMsg) Flatten() ([]byte, byte) { func (m *FEStartupMsg) String() string { return fmt.Sprintf( - "Startup (packet): ProtocolVersion:%08X, DriverName='%s', DriverVersion='%s', UserName='%s', Database='%s', SessionID='%s', ClientPID=%d", + "Startup (packet): ProtocolVersion:%08X, DriverName='%s', DriverVersion='%s', UserName='%s', Database='%s', SessionID='%s', ClientPID=%d, ClientOSUserName='%s'", m.ProtocolVersion, m.DriverName, m.DriverVersion, m.Username, m.Database, m.SessionID, - m.ClientPID) + m.ClientPID, + m.OSUsername) }