Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add server ip in connection info #38723

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ type ConnectionInfo struct {
ClientIP string
ClientPort string
ServerID int
ServerIP string
ServerPort int
Duration float64
User string
Expand Down