|
| 1 | +package ping |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/netip" |
| 5 | + |
| 6 | + "github.com/sagernet/sing-tun" |
| 7 | + "github.com/sagernet/sing-tun/internal/gtcpip/header" |
| 8 | + "github.com/sagernet/sing/common/buf" |
| 9 | +) |
| 10 | + |
| 11 | +type DestinationWriter struct { |
| 12 | + tun.DirectRouteDestination |
| 13 | + destination netip.Addr |
| 14 | +} |
| 15 | + |
| 16 | +func NewDestinationWriter(routeDestination tun.DirectRouteDestination, destination netip.Addr) *DestinationWriter { |
| 17 | + return &DestinationWriter{routeDestination, destination} |
| 18 | +} |
| 19 | + |
| 20 | +func (w *DestinationWriter) WritePacket(packet *buf.Buffer) error { |
| 21 | + var ipHdr header.Network |
| 22 | + switch header.IPVersion(packet.Bytes()) { |
| 23 | + case header.IPv4Version: |
| 24 | + ipHdr = header.IPv4(packet.Bytes()) |
| 25 | + case header.IPv6Version: |
| 26 | + ipHdr = header.IPv6(packet.Bytes()) |
| 27 | + default: |
| 28 | + return w.DirectRouteDestination.WritePacket(packet) |
| 29 | + } |
| 30 | + ipHdr.SetDestinationAddr(w.destination) |
| 31 | + if ipHdr4, isIPv4 := ipHdr.(header.IPv4); isIPv4 { |
| 32 | + ipHdr4.SetChecksum(^ipHdr4.CalculateChecksum()) |
| 33 | + } |
| 34 | + if ipHdr.TransportProtocol() == header.ICMPv6ProtocolNumber { |
| 35 | + icmpHdr := header.ICMPv6(ipHdr.Payload()) |
| 36 | + icmpHdr.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ |
| 37 | + Header: icmpHdr, |
| 38 | + Src: ipHdr.SourceAddressSlice(), |
| 39 | + Dst: ipHdr.DestinationAddressSlice(), |
| 40 | + })) |
| 41 | + } |
| 42 | + return w.DirectRouteDestination.WritePacket(packet) |
| 43 | +} |
| 44 | + |
| 45 | +type ContextDestinationWriter struct { |
| 46 | + tun.DirectRouteContext |
| 47 | + destination netip.Addr |
| 48 | +} |
| 49 | + |
| 50 | +func NewContextDestinationWriter(context tun.DirectRouteContext, destination netip.Addr) *ContextDestinationWriter { |
| 51 | + return &ContextDestinationWriter{ |
| 52 | + context, destination, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (w *ContextDestinationWriter) WritePacket(packet []byte) error { |
| 57 | + var ipHdr header.Network |
| 58 | + switch header.IPVersion(packet) { |
| 59 | + case header.IPv4Version: |
| 60 | + ipHdr = header.IPv4(packet) |
| 61 | + case header.IPv6Version: |
| 62 | + ipHdr = header.IPv6(packet) |
| 63 | + default: |
| 64 | + return w.DirectRouteContext.WritePacket(packet) |
| 65 | + } |
| 66 | + ipHdr.SetSourceAddr(w.destination) |
| 67 | + if ipHdr4, isIPv4 := ipHdr.(header.IPv4); isIPv4 { |
| 68 | + ipHdr4.SetChecksum(^ipHdr4.CalculateChecksum()) |
| 69 | + } |
| 70 | + if ipHdr.TransportProtocol() == header.ICMPv6ProtocolNumber { |
| 71 | + icmpHdr := header.ICMPv6(ipHdr.Payload()) |
| 72 | + icmpHdr.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ |
| 73 | + Header: icmpHdr, |
| 74 | + Src: ipHdr.SourceAddressSlice(), |
| 75 | + Dst: ipHdr.DestinationAddressSlice(), |
| 76 | + })) |
| 77 | + } |
| 78 | + return w.DirectRouteContext.WritePacket(packet) |
| 79 | +} |
0 commit comments