mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-06 13:27:39 +03:00
Implementation read waiter for socks5 UDP and UoT
This commit is contained in:
parent
ae8098ad39
commit
aa34723225
4 changed files with 97 additions and 1 deletions
48
protocol/socks/packet_wait.go
Normal file
48
protocol/socks/packet_wait.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package socks
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
var _ N.PacketReadWaitCreator = (*AssociatePacketConn)(nil)
|
||||
|
||||
func (c *AssociatePacketConn) CreateReadWaiter() (N.PacketReadWaiter, bool) {
|
||||
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(c.NetPacketConn)
|
||||
if !isReadWaiter {
|
||||
return nil, false
|
||||
}
|
||||
return &AssociatePacketReadWaiter{c, readWaiter}, true
|
||||
}
|
||||
|
||||
var _ N.PacketReadWaiter = (*AssociatePacketReadWaiter)(nil)
|
||||
|
||||
type AssociatePacketReadWaiter struct {
|
||||
conn *AssociatePacketConn
|
||||
readWaiter N.PacketReadWaiter
|
||||
}
|
||||
|
||||
func (w *AssociatePacketReadWaiter) InitializeReadWaiter(newBuffer func() *buf.Buffer) {
|
||||
w.readWaiter.InitializeReadWaiter(newBuffer)
|
||||
}
|
||||
|
||||
func (w *AssociatePacketReadWaiter) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
|
||||
buffer, destination, err = w.readWaiter.WaitReadPacket()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if buffer.Len() < 3 {
|
||||
buffer.Release()
|
||||
return nil, M.Socksaddr{}, ErrInvalidPacket
|
||||
}
|
||||
w.conn.remoteAddr = destination
|
||||
buffer.Advance(3)
|
||||
destination, err = M.SocksaddrSerializer.ReadAddrPort(buffer)
|
||||
if err != nil {
|
||||
buffer.Release()
|
||||
return nil, M.Socksaddr{}, err
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue