@@ -19,6 +19,7 @@ package collector
19
19
20
20
import (
21
21
"encoding/binary"
22
+ "net"
22
23
23
24
"github.com/sirupsen/logrus"
24
25
@@ -99,6 +100,26 @@ func (c *ConnectCollector) Start(_ *module.Manager, context *common.AccessLogCon
99
100
func (c * ConnectCollector ) Stop () {
100
101
}
101
102
103
+ func (c * ConnectCollector ) fixSocketFamilyIfNeed (event * events.SocketConnectEvent , result * ip.SocketPair ) {
104
+ if result == nil {
105
+ return
106
+ }
107
+ if parseIP := net .ParseIP (result .SrcIP ); parseIP != nil {
108
+ var actual uint32
109
+ if parseIP .To4 () != nil {
110
+ actual = unix .AF_INET
111
+ } else {
112
+ actual = unix .AF_INET6
113
+ }
114
+
115
+ if result .Family != actual {
116
+ connectLogger .Debugf ("fix the socket family from %d to %d, connection ID: %d, randomID: %d" ,
117
+ result .Family , actual , event .ConID , event .RandomID )
118
+ result .Family = actual
119
+ }
120
+ }
121
+ }
122
+
102
123
func (c * ConnectCollector ) buildSocketFromConnectEvent (event * events.SocketConnectEvent ) * ip.SocketPair {
103
124
if event .SocketFamily != unix .AF_INET && event .SocketFamily != unix .AF_INET6 && event .SocketFamily != enums .SocketFamilyUnknown {
104
125
// if not ipv4, ipv6 or unknown, ignore
@@ -122,6 +143,7 @@ func (c *ConnectCollector) buildSocketFromConnectEvent(event *events.SocketConne
122
143
connectLogger .Debugf ("found the connection from the socket, connection ID: %d, randomID: %d" ,
123
144
event .ConID , event .RandomID )
124
145
pair .Role = enums .ConnectionRole (event .Role )
146
+ c .fixSocketFamilyIfNeed (event , pair )
125
147
c .tryToUpdateSocketFromConntrack (event , pair )
126
148
return pair
127
149
}
@@ -193,12 +215,14 @@ func (c *ConnectCollector) buildSocketPair(event *events.SocketConnectEvent) *ip
193
215
return result
194
216
}
195
217
218
+ c .fixSocketFamilyIfNeed (event , result )
196
219
c .tryToUpdateSocketFromConntrack (event , result )
197
220
return result
198
221
}
199
222
200
223
func (c * ConnectCollector ) tryToUpdateSocketFromConntrack (event * events.SocketConnectEvent , socket * ip.SocketPair ) {
201
- if socket != nil && socket .IsValid () && c .connTracker != nil && ! tools .IsLocalHostAddress (socket .DestIP ) {
224
+ if socket != nil && socket .IsValid () && c .connTracker != nil && ! tools .IsLocalHostAddress (socket .DestIP ) &&
225
+ event .FuncName != enums .SocketFunctionNameAccept { // accept event don't need to update the remote address
202
226
// if no contract and socket data is valid, then trying to get the remote address from the socket
203
227
// to encase the remote address is not the real remote address
204
228
originalIP := socket .DestIP
0 commit comments