From aed0a37ae2de075083c05ce8a777fbf8e7df4821 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Fri, 28 Oct 2022 17:12:55 +0800 Subject: [PATCH 1/2] *: add server ip in connection info --- server/conn.go | 11 +++++++++++ server/server.go | 1 + sessionctx/variable/session.go | 1 + 3 files changed, 13 insertions(+) diff --git a/server/conn.go b/server/conn.go index 78852ea8f834c..b4ee925a9bccd 100644 --- a/server/conn.go +++ b/server/conn.go @@ -197,6 +197,7 @@ type clientConn struct { *TiDBContext // an interface to execute sql statements. } attrs map[string]string // attributes parsed from client handshake response, not used for now. + serverHost string // server host peerHost string // peer host peerPort string // peer port status int32 // dispatching/reading/shutdown/waitshutdown @@ -947,6 +948,7 @@ func (cc *clientConn) PeerHost(hasPassword string) (host, port string, err error host = variable.DefHostname if cc.isUnixSocket { cc.peerHost = host + cc.serverHost = host return } addr := cc.bufReadConn.RemoteAddr().String() @@ -957,6 +959,15 @@ func (cc *clientConn) PeerHost(hasPassword string) (host, port string, err error } cc.peerHost = host cc.peerPort = port + + serverAddr := cc.bufReadConn.LocalAddr().String() + serverHost, _, err := net.SplitHostPort(serverAddr) + if err != nil { + err = errAccessDenied.GenWithStackByArgs(cc.user, addr, hasPassword) + return + } + cc.serverHost = serverHost + return } diff --git a/server/server.go b/server/server.go index da79b49e114a2..9ed4b963913ec 100644 --- a/server/server.go +++ b/server/server.go @@ -625,6 +625,7 @@ func (cc *clientConn) connectInfo() *variable.ConnectionInfo { ClientIP: cc.peerHost, ClientPort: cc.peerPort, ServerID: 1, + ServerIP: cc.serverHost, ServerPort: int(cc.server.cfg.Port), User: cc.user, ServerOSLoginUser: osUser, diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index f7f875e50fb93..94e5fcddc5968 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -1440,6 +1440,7 @@ type ConnectionInfo struct { ClientIP string ClientPort string ServerID int + ServerIP string ServerPort int Duration float64 User string From 63971649e90decc4bc440c6de15f96eef58f907d Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Mon, 31 Oct 2022 13:03:38 +0800 Subject: [PATCH 2/2] add test --- server/tidb_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/tidb_test.go b/server/tidb_test.go index 6e2f3d5ef4a7a..293406fe87538 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -2836,6 +2836,8 @@ func TestExtensionConnEvent(t *testing.T) { conn2.User = "root" conn2.DB = "test" + require.Equal(t, "127.0.0.1", conn1.ClientIP) + require.Equal(t, "127.0.0.1", conn1.ServerIP) require.Empty(t, conn1.User) require.Empty(t, conn1.DB) require.Equal(t, conn2, logs.infos[1]) @@ -2874,6 +2876,8 @@ func TestExtensionConnEvent(t *testing.T) { conn2.User = "noexist" conn2.DB = "test" + require.Equal(t, "127.0.0.1", conn1.ClientIP) + require.Equal(t, "127.0.0.1", conn1.ServerIP) require.Empty(t, conn1.User) require.Empty(t, conn1.DB) require.Equal(t, conn2, logs.infos[1])