add interface xnet.SrcAddr/DstAddr
This commit is contained in:
+27
-15
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user