Deprecate bufio.ReadBuffer/ReadPacket

This commit is contained in:
世界 2023-04-17 17:48:29 +08:00
parent 421056635d
commit 28dfeaa762
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 10 additions and 7 deletions

View file

@ -10,12 +10,14 @@ import (
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
) )
// Deprecated: bad usage
func ReadBuffer(reader N.ExtendedReader, buffer *buf.Buffer) (n int, err error) { func ReadBuffer(reader N.ExtendedReader, buffer *buf.Buffer) (n int, err error) {
n, err = reader.Read(buffer.FreeBytes()) n, err = reader.Read(buffer.FreeBytes())
buffer.Truncate(n) buffer.Truncate(n)
return return
} }
// Deprecated: bad usage
func ReadPacket(reader N.PacketReader, buffer *buf.Buffer) (n int, addr net.Addr, err error) { func ReadPacket(reader N.PacketReader, buffer *buf.Buffer) (n int, addr net.Addr, err error) {
startLen := buffer.Len() startLen := buffer.Len()
addr, err = reader.ReadPacket(buffer) addr, err = reader.ReadPacket(buffer)

View file

@ -1,6 +1,7 @@
package socks package socks
import ( import (
"bytes"
"net" "net"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
@ -44,19 +45,19 @@ func (c *AssociatePacketConn) RemoteAddr() net.Addr {
//warn:unsafe //warn:unsafe
func (c *AssociatePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { func (c *AssociatePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
buffer := buf.With(p) n, addr, err = c.NetPacketConn.ReadFrom(p)
n, addr, err = bufio.ReadPacket(c.NetPacketConn, buffer)
if err != nil { if err != nil {
return return
} }
c.remoteAddr = M.SocksaddrFromNet(addr) c.remoteAddr = M.SocksaddrFromNet(addr)
buffer.Advance(3) reader := bytes.NewReader(p[3:n])
destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer) destination, err := M.SocksaddrSerializer.ReadAddrPort(reader)
if err != nil { if err != nil {
return return
} }
addr = destination.UDPAddr() addr = destination.UDPAddr()
n = copy(p, buffer.Bytes()) index := 3 + int(reader.Size()) - reader.Len()
n = copy(p, p[index:n])
return return
} }
@ -89,11 +90,11 @@ func (c *AssociatePacketConn) Write(b []byte) (n int, err error) {
} }
func (c *AssociatePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) { func (c *AssociatePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
_, addr, err := bufio.ReadPacket(c.NetPacketConn, buffer) destination, err := c.NetPacketConn.ReadPacket(buffer)
if err != nil { if err != nil {
return M.Socksaddr{}, err return M.Socksaddr{}, err
} }
c.remoteAddr = M.SocksaddrFromNet(addr) c.remoteAddr = destination
buffer.Advance(3) buffer.Advance(3)
dest, err := M.SocksaddrSerializer.ReadAddrPort(buffer) dest, err := M.SocksaddrSerializer.ReadAddrPort(buffer)
return dest, err return dest, err