|
| 1 | +package ping |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/netip" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/sagernet/sing-tun/internal/gtcpip/checksum" |
| 11 | + "github.com/sagernet/sing-tun/internal/gtcpip/header" |
| 12 | + "github.com/sagernet/sing/common/atomic" |
| 13 | + "github.com/sagernet/sing/common/buf" |
| 14 | + "github.com/sagernet/sing/common/control" |
| 15 | + M "github.com/sagernet/sing/common/metadata" |
| 16 | +) |
| 17 | + |
| 18 | +type UnprivilegedConn struct { |
| 19 | + ctx context.Context |
| 20 | + cancel context.CancelFunc |
| 21 | + controlFunc control.Func |
| 22 | + destination netip.Addr |
| 23 | + receiveChan chan *unprivilegedResponse |
| 24 | + readDeadline atomic.TypedValue[time.Time] |
| 25 | + writeDeadline atomic.TypedValue[time.Time] |
| 26 | +} |
| 27 | + |
| 28 | +type unprivilegedResponse struct { |
| 29 | + Buffer *buf.Buffer |
| 30 | + Cmsg *buf.Buffer |
| 31 | + Addr netip.Addr |
| 32 | +} |
| 33 | + |
| 34 | +func newUnprivilegedConn(ctx context.Context, controlFunc control.Func, destination netip.Addr) (net.Conn, error) { |
| 35 | + conn, err := connect(false, controlFunc, destination) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + conn.Close() |
| 40 | + ctx, cancel := context.WithCancel(ctx) |
| 41 | + return &UnprivilegedConn{ |
| 42 | + ctx: ctx, |
| 43 | + cancel: cancel, |
| 44 | + controlFunc: controlFunc, |
| 45 | + destination: destination, |
| 46 | + receiveChan: make(chan *unprivilegedResponse), |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (c *UnprivilegedConn) Read(b []byte) (n int, err error) { |
| 51 | + select { |
| 52 | + case packet := <-c.receiveChan: |
| 53 | + n = copy(b, packet.Buffer.Bytes()) |
| 54 | + packet.Buffer.Release() |
| 55 | + packet.Cmsg.Release() |
| 56 | + return |
| 57 | + case <-c.ctx.Done(): |
| 58 | + return 0, os.ErrClosed |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (c *UnprivilegedConn) ReadMsg(b []byte, oob []byte) (n, oobn int, addr netip.Addr, err error) { |
| 63 | + select { |
| 64 | + case packet := <-c.receiveChan: |
| 65 | + n = copy(b, packet.Buffer.Bytes()) |
| 66 | + oobn = copy(oob, packet.Cmsg.Bytes()) |
| 67 | + addr = packet.Addr |
| 68 | + packet.Buffer.Release() |
| 69 | + packet.Cmsg.Release() |
| 70 | + return |
| 71 | + case <-c.ctx.Done(): |
| 72 | + return 0, 0, netip.Addr{}, os.ErrClosed |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (c *UnprivilegedConn) Write(b []byte) (n int, err error) { |
| 77 | + conn, err := connect(false, c.controlFunc, c.destination) |
| 78 | + if err != nil { |
| 79 | + return |
| 80 | + } |
| 81 | + var identifier uint16 |
| 82 | + if !c.destination.Is6() { |
| 83 | + icmpHdr := header.ICMPv4(b) |
| 84 | + identifier = icmpHdr.Ident() |
| 85 | + } else { |
| 86 | + icmpHdr := header.ICMPv6(b) |
| 87 | + identifier = icmpHdr.Ident() |
| 88 | + } |
| 89 | + if readDeadline := c.readDeadline.Load(); !readDeadline.IsZero() { |
| 90 | + conn.SetReadDeadline(readDeadline) |
| 91 | + } |
| 92 | + if writeDeadline := c.writeDeadline.Load(); !writeDeadline.IsZero() { |
| 93 | + conn.SetWriteDeadline(writeDeadline) |
| 94 | + } |
| 95 | + n, err = conn.Write(b) |
| 96 | + if err != nil { |
| 97 | + conn.Close() |
| 98 | + return |
| 99 | + } |
| 100 | + go c.fetchResponse(conn, identifier) |
| 101 | + return |
| 102 | +} |
| 103 | + |
| 104 | +func (c *UnprivilegedConn) fetchResponse(conn net.Conn, identifier uint16) { |
| 105 | + done := make(chan struct{}) |
| 106 | + defer close(done) |
| 107 | + go func() { |
| 108 | + select { |
| 109 | + case <-c.ctx.Done(): |
| 110 | + case <-done: |
| 111 | + } |
| 112 | + conn.Close() |
| 113 | + }() |
| 114 | + buffer := buf.NewPacket() |
| 115 | + cmsgBuffer := buf.NewSize(1024) |
| 116 | + n, oobN, _, addr, err := conn.(*net.UDPConn).ReadMsgUDPAddrPort(buffer.FreeBytes(), cmsgBuffer.FreeBytes()) |
| 117 | + if err != nil { |
| 118 | + buffer.Release() |
| 119 | + cmsgBuffer.Release() |
| 120 | + return |
| 121 | + } |
| 122 | + buffer.Truncate(n) |
| 123 | + cmsgBuffer.Truncate(oobN) |
| 124 | + if !c.destination.Is6() { |
| 125 | + icmpHdr := header.ICMPv4(buffer.Bytes()) |
| 126 | + icmpHdr.SetIdent(identifier) |
| 127 | + icmpHdr.SetChecksum(0) |
| 128 | + icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr[:header.ICMPv4MinimumSize], checksum.Checksum(icmpHdr.Payload(), 0))) |
| 129 | + } else { |
| 130 | + icmpHdr := header.ICMPv6(buffer.Bytes()) |
| 131 | + icmpHdr.SetIdent(identifier) |
| 132 | + // offload checksum here since we don't have source address here |
| 133 | + } |
| 134 | + select { |
| 135 | + case c.receiveChan <- &unprivilegedResponse{ |
| 136 | + Buffer: buffer, |
| 137 | + Cmsg: cmsgBuffer, |
| 138 | + Addr: addr.Addr(), |
| 139 | + }: |
| 140 | + case <-c.ctx.Done(): |
| 141 | + buffer.Release() |
| 142 | + cmsgBuffer.Release() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func (c *UnprivilegedConn) Close() error { |
| 147 | + c.cancel() |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func (c *UnprivilegedConn) LocalAddr() net.Addr { |
| 152 | + return M.Socksaddr{} |
| 153 | +} |
| 154 | + |
| 155 | +func (c *UnprivilegedConn) RemoteAddr() net.Addr { |
| 156 | + return M.SocksaddrFrom(c.destination, 0).UDPAddr() |
| 157 | +} |
| 158 | + |
| 159 | +func (c *UnprivilegedConn) SetDeadline(t time.Time) error { |
| 160 | + c.readDeadline.Store(t) |
| 161 | + c.writeDeadline.Store(t) |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func (c *UnprivilegedConn) SetReadDeadline(t time.Time) error { |
| 166 | + c.readDeadline.Store(t) |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func (c *UnprivilegedConn) SetWriteDeadline(t time.Time) error { |
| 171 | + c.writeDeadline.Store(t) |
| 172 | + return nil |
| 173 | +} |
0 commit comments