修复 client端抓包,client的 IP显示不准确的 bug
This commit is contained in:
parent
b74b4fed20
commit
87a877750f
|
@ -165,7 +165,7 @@ func (nc *networkCard) parseTCPPackage(packet gopacket.Packet, authHeader *pp.He
|
||||||
}
|
}
|
||||||
|
|
||||||
// deal mysql server response
|
// deal mysql server response
|
||||||
err = readToServerPackage(clientIP, clientPort, &srcIP, srcPort, &dstIP, tcpPkt, nc.receiver)
|
err = readToServerPackage(clientIP, clientPort, &dstIP, tcpPkt, nc.receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ func (nc *networkCard) parseTCPPackage(packet gopacket.Packet, authHeader *pp.He
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFromServerPackage(
|
func readFromServerPackage(
|
||||||
srcIP *string, srcPort int, tcpPkt *layers.TCP) (err error) {
|
clientIP *string, clientPort int, tcpPkt *layers.TCP) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("read Mysql package send from mysql server to client failed <-- %s", err.Error())
|
log.Error("read Mysql package send from mysql server to client failed <-- %s", err.Error())
|
||||||
|
@ -190,7 +190,7 @@ func readFromServerPackage(
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if tcpPkt.FIN {
|
if tcpPkt.FIN {
|
||||||
sessionKey := spliceSessionKey(srcIP, srcPort)
|
sessionKey := spliceSessionKey(clientIP, clientPort)
|
||||||
session := sessionPool[*sessionKey]
|
session := sessionPool[*sessionKey]
|
||||||
if session != nil {
|
if session != nil {
|
||||||
session.Close()
|
session.Close()
|
||||||
|
@ -204,7 +204,7 @@ func readFromServerPackage(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionKey := spliceSessionKey(srcIP, srcPort)
|
sessionKey := spliceSessionKey(clientIP, clientPort)
|
||||||
session := sessionPool[*sessionKey]
|
session := sessionPool[*sessionKey]
|
||||||
if session != nil {
|
if session != nil {
|
||||||
pkt := model.NewTCPPacket(tcpPayload, int64(tcpPkt.Ack), false)
|
pkt := model.NewTCPPacket(tcpPayload, int64(tcpPkt.Ack), false)
|
||||||
|
@ -215,7 +215,7 @@ func readFromServerPackage(
|
||||||
}
|
}
|
||||||
|
|
||||||
func readToServerPackage(
|
func readToServerPackage(
|
||||||
clientIP *string, clientPort int, srcIP *string, srcPort int, destIP *string, tcpPkt *layers.TCP,
|
clientIP *string, clientPort int, destIP *string, tcpPkt *layers.TCP,
|
||||||
receiver chan model.QueryPiece) (err error) {
|
receiver chan model.QueryPiece) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -225,7 +225,7 @@ func readToServerPackage(
|
||||||
|
|
||||||
// when client try close connection remove session from session pool
|
// when client try close connection remove session from session pool
|
||||||
if tcpPkt.FIN {
|
if tcpPkt.FIN {
|
||||||
sessionKey := spliceSessionKey(srcIP, srcPort)
|
sessionKey := spliceSessionKey(clientIP, clientPort)
|
||||||
session := sessionPool[*sessionKey]
|
session := sessionPool[*sessionKey]
|
||||||
if session != nil {
|
if session != nil {
|
||||||
session.Close()
|
session.Close()
|
||||||
|
@ -240,10 +240,10 @@ func readToServerPackage(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionKey := spliceSessionKey(srcIP, srcPort)
|
sessionKey := spliceSessionKey(clientIP, clientPort)
|
||||||
session := sessionPool[*sessionKey]
|
session := sessionPool[*sessionKey]
|
||||||
if session == nil {
|
if session == nil {
|
||||||
session = sd.NewSession(sessionKey, clientIP, clientPort, srcIP, srcPort, destIP, snifferPort, receiver)
|
session = sd.NewSession(sessionKey, clientIP, clientPort, destIP, snifferPort, receiver)
|
||||||
sessionPool[*sessionKey] = session
|
sessionPool[*sessionKey] = session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,13 @@ import (
|
||||||
"github.com/zr-hebo/sniffer-agent/session-dealer/mysql"
|
"github.com/zr-hebo/sniffer-agent/session-dealer/mysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSession(sessionKey, clientIP *string, clientPort int, srcIP *string, srcPort int, serverIP *string, serverPort int,
|
func NewSession(sessionKey, clientIP *string, clientPort int, serverIP *string, serverPort int,
|
||||||
receiver chan model.QueryPiece) (session ConnSession) {
|
receiver chan model.QueryPiece) (session ConnSession) {
|
||||||
switch serviceType {
|
switch serviceType {
|
||||||
case ServiceTypeMysql:
|
case ServiceTypeMysql:
|
||||||
session = mysql.NewMysqlSession(sessionKey, clientIP, clientPort, srcIP, srcPort, serverIP, serverPort, receiver)
|
session = mysql.NewMysqlSession(sessionKey, clientIP, clientPort, serverIP, serverPort, receiver)
|
||||||
default:
|
default:
|
||||||
session = mysql.NewMysqlSession(sessionKey, clientIP, clientPort, srcIP, srcPort, serverIP, serverPort, receiver)
|
session = mysql.NewMysqlSession(sessionKey, clientIP, clientPort, serverIP, serverPort, receiver)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,6 @@ type MysqlSession struct {
|
||||||
visitDB *string
|
visitDB *string
|
||||||
clientIP *string
|
clientIP *string
|
||||||
clientPort int
|
clientPort int
|
||||||
srcIP *string
|
|
||||||
srcPort int
|
|
||||||
serverIP *string
|
serverIP *string
|
||||||
serverPort int
|
serverPort int
|
||||||
stmtBeginTimeNano int64
|
stmtBeginTimeNano int64
|
||||||
|
@ -48,14 +46,12 @@ type prepareInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMysqlSession(
|
func NewMysqlSession(
|
||||||
sessionKey, clientIP *string, clientPort int, srcIP *string, srcPort int, serverIP *string, serverPort int,
|
sessionKey, clientIP *string, clientPort int, serverIP *string, serverPort int,
|
||||||
receiver chan model.QueryPiece) (ms *MysqlSession) {
|
receiver chan model.QueryPiece) (ms *MysqlSession) {
|
||||||
ms = &MysqlSession{
|
ms = &MysqlSession{
|
||||||
connectionID: sessionKey,
|
connectionID: sessionKey,
|
||||||
clientIP: clientIP,
|
clientIP: clientIP,
|
||||||
clientPort: clientPort,
|
clientPort: clientPort,
|
||||||
srcIP: srcIP,
|
|
||||||
srcPort: srcPort,
|
|
||||||
serverIP: serverIP,
|
serverIP: serverIP,
|
||||||
serverPort: serverPort,
|
serverPort: serverPort,
|
||||||
stmtBeginTimeNano: time.Now().UnixNano(),
|
stmtBeginTimeNano: time.Now().UnixNano(),
|
||||||
|
@ -323,11 +319,11 @@ func (ms *MysqlSession) GenerateQueryPiece() (qp model.QueryPiece) {
|
||||||
return mqp
|
return mqp
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterQueryPieceBySQL(mqp *model.PooledMysqlQueryPiece, querySQL []byte) (*model.PooledMysqlQueryPiece) {
|
func filterQueryPieceBySQL(mqp *model.PooledMysqlQueryPiece, querySQL []byte) *model.PooledMysqlQueryPiece {
|
||||||
if mqp == nil || querySQL == nil {
|
if mqp == nil || querySQL == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
} else if (uselessSQLPattern.Match(querySQL)) {
|
} else if uselessSQLPattern.Match(querySQL) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,10 +337,6 @@ func filterQueryPieceBySQL(mqp *model.PooledMysqlQueryPiece, querySQL []byte) (*
|
||||||
func (ms *MysqlSession) composeQueryPiece() (mqp *model.PooledMysqlQueryPiece) {
|
func (ms *MysqlSession) composeQueryPiece() (mqp *model.PooledMysqlQueryPiece) {
|
||||||
clientIP := ms.clientIP
|
clientIP := ms.clientIP
|
||||||
clientPort := ms.clientPort
|
clientPort := ms.clientPort
|
||||||
if clientIP == nil {
|
|
||||||
clientIP = ms.srcIP
|
|
||||||
clientPort = ms.serverPort
|
|
||||||
}
|
|
||||||
return model.NewPooledMysqlQueryPiece(
|
return model.NewPooledMysqlQueryPiece(
|
||||||
ms.connectionID, clientIP, ms.visitUser, ms.visitDB, ms.serverIP,
|
ms.connectionID, clientIP, ms.visitUser, ms.visitDB, ms.serverIP,
|
||||||
clientPort, ms.serverPort, communicator.GetMysqlCapturePacketRate(), ms.stmtBeginTimeNano)
|
clientPort, ms.serverPort, communicator.GetMysqlCapturePacketRate(), ms.stmtBeginTimeNano)
|
||||||
|
|
Loading…
Reference in New Issue