Improve bind packet conn

This commit is contained in:
世界 2023-04-08 14:48:33 +08:00
parent df54c89b04
commit 3bedba1e1e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 79 additions and 63 deletions

79
common/bufio/bind.go Normal file
View file

@ -0,0 +1,79 @@
package bufio
import (
"net"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
type BindPacketConn struct {
N.NetPacketConn
Addr net.Addr
}
func NewBindPacketConn(conn net.PacketConn, addr net.Addr) *BindPacketConn {
return &BindPacketConn{
NewPacketConn(conn),
addr,
}
}
func (c *BindPacketConn) Read(b []byte) (n int, err error) {
n, _, err = c.ReadFrom(b)
return
}
func (c *BindPacketConn) Write(b []byte) (n int, err error) {
return c.WriteTo(b, c.Addr)
}
func (c *BindPacketConn) RemoteAddr() net.Addr {
return c.Addr
}
func (c *BindPacketConn) Upstream() any {
return c.NetPacketConn
}
type UnbindPacketConn struct {
N.ExtendedConn
Addr M.Socksaddr
}
func NewUnbindPacketConn(conn net.Conn) *UnbindPacketConn {
return &UnbindPacketConn{
NewExtendedConn(conn),
M.SocksaddrFromNet(conn.RemoteAddr()),
}
}
func (c *UnbindPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
n, err = c.ExtendedConn.Read(p)
if err == nil {
addr = c.Addr.UDPAddr()
}
return
}
func (c *UnbindPacketConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
return c.ExtendedConn.Write(p)
}
func (c *UnbindPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
err = c.ExtendedConn.ReadBuffer(buffer)
if err != nil {
return
}
destination = c.Addr
return
}
func (c *UnbindPacketConn) WritePacket(buffer *buf.Buffer, _ M.Socksaddr) error {
return c.ExtendedConn.WriteBuffer(buffer)
}
func (c *UnbindPacketConn) Upstream() any {
return c.ExtendedConn
}

View file

@ -70,69 +70,6 @@ func (w *ExtendedPacketConn) Upstream() any {
return w.PacketConn
}
type BindPacketConn struct {
net.PacketConn
Addr net.Addr
}
func (c *BindPacketConn) Read(b []byte) (n int, err error) {
n, _, err = c.ReadFrom(b)
return
}
func (c *BindPacketConn) Write(b []byte) (n int, err error) {
return c.WriteTo(b, c.Addr)
}
func (c *BindPacketConn) RemoteAddr() net.Addr {
return c.Addr
}
func (c *BindPacketConn) Upstream() any {
return c.PacketConn
}
type UnbindPacketConn struct {
N.ExtendedConn
Addr M.Socksaddr
}
func (c *UnbindPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
n, err = c.ExtendedConn.Read(p)
if err == nil {
addr = c.Addr.UDPAddr()
}
return
}
func (c *UnbindPacketConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
return c.ExtendedConn.Write(p)
}
func (c *UnbindPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
err = c.ExtendedConn.ReadBuffer(buffer)
if err != nil {
return
}
destination = c.Addr
return
}
func (c *UnbindPacketConn) WritePacket(buffer *buf.Buffer, _ M.Socksaddr) error {
return c.ExtendedConn.WriteBuffer(buffer)
}
func (c *UnbindPacketConn) Upstream() any {
return c.ExtendedConn
}
func NewUnbindPacketConn(conn net.Conn) *UnbindPacketConn {
return &UnbindPacketConn{
NewExtendedConn(conn),
M.SocksaddrFromNet(conn.RemoteAddr()),
}
}
type ExtendedReaderWrapper struct {
io.Reader
}