Fix packet conn api

This commit is contained in:
世界 2023-04-25 13:49:43 +08:00
parent f6a6b7258c
commit ff03af056a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 100 additions and 0 deletions

View file

@ -209,6 +209,37 @@ func (c *clientPacketConn) FrontHeadroom() int {
return 2
}
func (c *clientPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
if !c.responseRead {
err = c.readResponse()
if err != nil {
return
}
c.responseRead = true
}
var length uint16
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
if err != nil {
return
}
if cap(p) < int(length) {
return 0, nil, io.ErrShortBuffer
}
n, err = io.ReadFull(c.ExtendedConn, p[:length])
return
}
func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if !c.requestWritten {
return c.writeRequest(p)
}
err = binary.Write(c.ExtendedConn, binary.BigEndian, uint16(len(p)))
if err != nil {
return
}
return c.ExtendedConn.Write(p)
}
func (c *clientPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
err = c.ReadBuffer(buffer)
return

View file

@ -2,6 +2,8 @@ package mux
import (
"encoding/binary"
"io"
"net"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
@ -122,6 +124,33 @@ func (c *serverPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksad
return c.ExtendedConn.WriteBuffer(buffer)
}
func (c *serverPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
var length uint16
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
if err != nil {
return
}
if cap(p) < int(length) {
return 0, nil, io.ErrShortBuffer
}
n, err = io.ReadFull(c.ExtendedConn, p[:length])
return
}
func (c *serverPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if !c.responseWritten {
_, err = c.ExtendedConn.Write([]byte{statusSuccess})
if err != nil {
return
}
}
err = binary.Write(c.ExtendedConn, binary.BigEndian, uint16(len(p)))
if err != nil {
return
}
return c.ExtendedConn.Write(p)
}
func (c *serverPacketConn) NeedAdditionalReadDeadline() bool {
return true
}
@ -160,6 +189,46 @@ func (c *serverPacketAddrConn) HandshakeFailure(err error) error {
return c.ExtendedConn.WriteBuffer(buffer)
}
func (c *serverPacketAddrConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
destination, err := M.SocksaddrSerializer.ReadAddrPort(c.ExtendedConn)
if err != nil {
return
}
if destination.IsFqdn() {
addr = destination
} else {
addr = destination.UDPAddr()
}
var length uint16
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
if err != nil {
return
}
if cap(p) < int(length) {
return 0, nil, io.ErrShortBuffer
}
n, err = io.ReadFull(c.ExtendedConn, p[:length])
return
}
func (c *serverPacketAddrConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if !c.responseWritten {
_, err = c.ExtendedConn.Write([]byte{statusSuccess})
if err != nil {
return
}
}
err = M.SocksaddrSerializer.WriteAddrPort(c.ExtendedConn, M.SocksaddrFromNet(addr))
if err != nil {
return
}
err = binary.Write(c.ExtendedConn, binary.BigEndian, uint16(len(p)))
if err != nil {
return
}
return c.ExtendedConn.Write(p)
}
func (c *serverPacketAddrConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
destination, err = M.SocksaddrSerializer.ReadAddrPort(c.ExtendedConn)
if err != nil {