add interface xnet.SrcAddr/DstAddr

This commit is contained in:
ginuerzh
2025-08-03 10:15:53 +08:00
parent 79203e407c
commit f71351f5ef
42 changed files with 518 additions and 295 deletions
+27 -15
View File
@@ -206,26 +206,38 @@ func (s *defaultService) Serve() error {
s.setState(StateReady)
}
clientAddr := conn.RemoteAddr().String()
if ca, ok := conn.(xnet.ClientAddr); ok {
if addr := ca.ClientAddr(); addr != nil {
clientAddr = addr.String()
}
}
clientIP := clientAddr
if h, _, _ := net.SplitHostPort(clientAddr); h != "" {
clientIP = h
}
sid := xid.New().String()
ctx := ctxvalue.ContextWithSid(ctx, ctxvalue.Sid(sid))
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(clientAddr))
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: clientIP})
log := s.options.logger.WithFields(map[string]any{
"sid": sid,
})
srcAddr := conn.RemoteAddr()
if a, ok := conn.(xnet.SrcAddr); ok {
if addr := a.SrcAddr(); addr != nil {
srcAddr = addr
}
}
ctx = ctxvalue.ContextWithSrcAddr(ctx, srcAddr)
clientAddr := srcAddr.String()
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(clientAddr))
dstAddr := conn.LocalAddr()
if a, ok := conn.(xnet.DstAddr); ok {
if addr := a.DstAddr(); addr != nil {
dstAddr = addr
}
}
ctx = ctxvalue.ContextWithDstAddr(ctx, dstAddr)
clientIP := clientAddr
if h, _, _ := net.SplitHostPort(clientIP); h != "" {
clientIP = h
}
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: clientIP})
for _, rec := range s.options.recorders {
if rec.Record == recorder.RecorderServiceClientAddress {
if err := rec.Recorder.Record(ctx, []byte(clientIP)); err != nil {
@@ -235,9 +247,9 @@ func (s *defaultService) Serve() error {
}
}
if s.options.admission != nil &&
!s.options.admission.Admit(ctx, clientAddr) {
!s.options.admission.Admit(ctx, srcAddr.String()) {
conn.Close()
log.Debugf("admission: %s is denied", clientAddr)
log.Debugf("admission: %s is denied", srcAddr)
continue
}