Add EXP_UDPConn interface

This commit is contained in:
世界 2024-04-15 21:07:24 +08:00
parent eec2fc325a
commit 3c614f299f
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 89 additions and 25 deletions

View file

@ -18,10 +18,25 @@ type bindPacketConn struct {
addr net.Addr addr net.Addr
} }
type bindUDPConn struct {
bindPacketConn
N.EXP_UDPConn
}
func NewBindPacketConn(conn net.PacketConn, addr net.Addr) BindPacketConn { func NewBindPacketConn(conn net.PacketConn, addr net.Addr) BindPacketConn {
return &bindPacketConn{ if udpConn, isUDPConn := conn.(N.EXP_UDPConn); isUDPConn {
NewPacketConn(conn), return &bindUDPConn{
addr, bindPacketConn{
NewPacketConn(conn),
addr,
},
udpConn,
}
} else {
return &bindPacketConn{
NewPacketConn(conn),
addr,
}
} }
} }
@ -61,16 +76,36 @@ type UnbindPacketConn struct {
} }
func NewUnbindPacketConn(conn net.Conn) N.NetPacketConn { func NewUnbindPacketConn(conn net.Conn) N.NetPacketConn {
return &UnbindPacketConn{ if udpConn, isUDPConn := conn.(N.EXP_UDPConn); isUDPConn {
NewExtendedConn(conn), return &UnbindUDPConn{
M.SocksaddrFromNet(conn.RemoteAddr()), UnbindPacketConn{
NewExtendedConn(conn),
M.SocksaddrFromNet(conn.RemoteAddr()),
},
udpConn,
}
} else {
return &UnbindPacketConn{
NewExtendedConn(conn),
M.SocksaddrFromNet(conn.RemoteAddr()),
}
} }
} }
func NewUnbindPacketConnWithAddr(conn net.Conn, addr M.Socksaddr) N.NetPacketConn { func NewUnbindPacketConnWithAddr(conn net.Conn, addr M.Socksaddr) N.NetPacketConn {
return &UnbindPacketConn{ if udpConn, isUDPConn := conn.(N.EXP_UDPConn); isUDPConn {
NewExtendedConn(conn), return &UnbindUDPConn{
addr, UnbindPacketConn{
NewExtendedConn(conn),
addr,
},
udpConn,
}
} else {
return &UnbindPacketConn{
NewExtendedConn(conn),
addr,
}
} }
} }
@ -110,3 +145,22 @@ func (c *UnbindPacketConn) CreateReadWaiter() (N.PacketReadWaiter, bool) {
func (c *UnbindPacketConn) Upstream() any { func (c *UnbindPacketConn) Upstream() any {
return c.ExtendedConn return c.ExtendedConn
} }
var _ N.EXP_UDPConn = (*UnbindUDPConn)(nil)
type UnbindUDPConn struct {
UnbindPacketConn
N.EXP_UDPConn
}
func (c *UnbindUDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error) {
n, oobn, flags, addr, err = c.EXP_UDPConn.ReadMsgUDP(b, oob)
if err == nil {
addr = c.addr.UDPAddr()
}
return
}
func (c *UnbindUDPConn) WriteMsgUDP(b, oob []byte, _ *net.UDPAddr) (n, oobn int, err error) {
return c.EXP_UDPConn.WriteMsgUDP(b, oob, c.addr.UDPAddr())
}

View file

@ -148,40 +148,40 @@ type ExtendedConnWrapper struct {
writer N.ExtendedWriter writer N.ExtendedWriter
} }
func (w *ExtendedConnWrapper) ReadBuffer(buffer *buf.Buffer) error { func (c *ExtendedConnWrapper) ReadBuffer(buffer *buf.Buffer) error {
return w.reader.ReadBuffer(buffer) return c.reader.ReadBuffer(buffer)
} }
func (w *ExtendedConnWrapper) WriteBuffer(buffer *buf.Buffer) error { func (c *ExtendedConnWrapper) WriteBuffer(buffer *buf.Buffer) error {
return w.writer.WriteBuffer(buffer) return c.writer.WriteBuffer(buffer)
} }
func (w *ExtendedConnWrapper) ReadFrom(r io.Reader) (n int64, err error) { func (c *ExtendedConnWrapper) ReadFrom(r io.Reader) (n int64, err error) {
return Copy(w.writer, r) return Copy(c.writer, r)
} }
func (r *ExtendedConnWrapper) WriteTo(w io.Writer) (n int64, err error) { func (c *ExtendedConnWrapper) WriteTo(w io.Writer) (n int64, err error) {
return Copy(w, r.reader) return Copy(w, c.reader)
} }
func (w *ExtendedConnWrapper) UpstreamReader() any { func (c *ExtendedConnWrapper) UpstreamReader() any {
return w.reader return c.reader
} }
func (w *ExtendedConnWrapper) ReaderReplaceable() bool { func (c *ExtendedConnWrapper) ReaderReplaceable() bool {
return true return true
} }
func (w *ExtendedConnWrapper) UpstreamWriter() any { func (c *ExtendedConnWrapper) UpstreamWriter() any {
return w.writer return c.writer
} }
func (w *ExtendedConnWrapper) WriterReplaceable() bool { func (c *ExtendedConnWrapper) WriterReplaceable() bool {
return true return true
} }
func (w *ExtendedConnWrapper) Upstream() any { func (c *ExtendedConnWrapper) Upstream() any {
return w.Conn return c.Conn
} }
func NewExtendedConn(conn net.Conn) N.ExtendedConn { func NewExtendedConn(conn net.Conn) N.ExtendedConn {

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"io" "io"
"net" "net"
"syscall"
"time" "time"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
@ -85,6 +86,15 @@ type BindPacketConn interface {
net.Conn net.Conn
} }
// EXP_UDPConn is a interface used x/net/ipv4 and quic-go
type EXP_UDPConn interface {
net.PacketConn
syscall.Conn
SetReadBuffer(bytes int) error
ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
}
type UDPHandler interface { type UDPHandler interface {
NewPacket(ctx context.Context, conn PacketConn, buffer *buf.Buffer, metadata M.Metadata) error NewPacket(ctx context.Context, conn PacketConn, buffer *buf.Buffer, metadata M.Metadata) error
} }