ping: Add filter to destination

This commit is contained in:
世界 2025-08-25 20:51:03 +08:00
parent d0ff7b6f6c
commit 144683d882
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 108 additions and 31 deletions

View file

@ -15,7 +15,6 @@ import (
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
"golang.org/x/net/ipv4"
@ -24,7 +23,6 @@ import (
type Conn struct {
ctx context.Context
logger logger.ContextLogger
privileged bool
conn net.Conn
destination netip.Addr
@ -32,10 +30,9 @@ type Conn struct {
closed atomic.Bool
}
func Connect(ctx context.Context, logger logger.ContextLogger, privileged bool, controlFunc control.Func, destination netip.Addr) (*Conn, error) {
func Connect(ctx context.Context, privileged bool, controlFunc control.Func, destination netip.Addr) (*Conn, error) {
c := &Conn{
ctx: ctx,
logger: logger,
privileged: privileged,
destination: destination,
}
@ -123,7 +120,6 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
TotalLength: uint16(buffer.Len()),
})
ipHdr.SetChecksum(^ipHdr.CalculateChecksum())
c.logger.TraceContext(c.ctx, "read icmpv4 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
} else {
oob := make([]byte, 1024)
buffer.Advance(header.IPv6MinimumSize)
@ -164,7 +160,6 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
SrcAddr: addr,
DstAddr: c.source.Load(),
})
c.logger.TraceContext(c.ctx, "read icmpv6 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
}
} else {
_, err := buffer.ReadOnceFrom(c.conn)
@ -192,7 +187,6 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
}
icmpHdr.SetChecksum(0)
icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr[:header.ICMPv4MinimumSize], checksum.Checksum(icmpHdr.Payload(), 0)))
c.logger.TraceContext(c.ctx, "read icmpv4 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
} else {
ipHdr := header.IPv6(buffer.Bytes())
if !ipHdr.IsValid(buffer.Len()) {
@ -209,7 +203,6 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
Src: ipHdr.SourceAddressSlice(),
Dst: ipHdr.DestinationAddressSlice(),
}))
c.logger.TraceContext(c.ctx, "read icmpv6 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
}
}
return nil
@ -254,7 +247,6 @@ func (c *Conn) WriteIP(buffer *buf.Buffer) error {
icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr[:header.ICMPv4MinimumSize], checksum.Checksum(icmpHdr.Payload(), 0)))
}
c.source.Store(M.AddrFromIP(ipHdr.SourceAddressSlice()))
c.logger.TraceContext(c.ctx, "write icmpv4 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
return common.Error(c.conn.Write(ipHdr.Payload()))
} else {
ipHdr := header.IPv6(buffer.Bytes())
@ -269,7 +261,6 @@ func (c *Conn) WriteIP(buffer *buf.Buffer) error {
}))
}
c.source.Store(M.AddrFromIP(ipHdr.SourceAddressSlice()))
c.logger.TraceContext(c.ctx, "write icmpv6 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
return common.Error(c.conn.Write(ipHdr.Payload()))
}
}
@ -282,7 +273,6 @@ func (c *Conn) WriteICMP(buffer *buf.Buffer) error {
icmpHdr.SetIdent(^icmpHdr.Ident())
icmpHdr.SetChecksum(0)
icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr[:header.ICMPv4MinimumSize], checksum.Checksum(icmpHdr.Payload(), 0)))
c.logger.TraceContext(c.ctx, "write icmpv4 echo request to ", c.destination)
} else {
icmpHdr := header.ICMPv6(buffer.Bytes())
icmpHdr.SetIdent(^icmpHdr.Ident())
@ -294,11 +284,6 @@ func (c *Conn) WriteICMP(buffer *buf.Buffer) error {
}))
}
}
if !c.destination.Is6() {
c.logger.TraceContext(c.ctx, "write icmpv4 echo request to ", c.destination)
} else {
c.logger.TraceContext(c.ctx, "write icmpv6 echo request to ", c.destination)
}
return common.Error(c.conn.Write(buffer.Bytes()))
}