Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UoT read waiter support #40

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion common/bufio/copy_direct_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (w *syscallPacketReadWaiter) InitializeReadWaiter(newBuffer func() *buf.Buf
buffer.Truncate(readN)
} else {
buffer.Release()
buffer = nil
}
if w.readErr == syscall.EAGAIN {
return false
Expand Down
63 changes: 52 additions & 11 deletions common/bufio/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,95 @@ import (
N "github.com/sagernet/sing/common/network"
)

type NATPacketConn struct {
type NATPacketConn interface {
N.NetPacketConn
origin M.Socksaddr
destination M.Socksaddr
UpdateDestination(destinationAddress netip.Addr)
}

func NewNATPacketConn(conn N.NetPacketConn, origin M.Socksaddr, destination M.Socksaddr) *NATPacketConn {
return &NATPacketConn{
func NewUnidirectionalNATPacketConn(conn N.NetPacketConn, origin M.Socksaddr, destination M.Socksaddr) NATPacketConn {
return &unidirectionalNATPacketConn{
NetPacketConn: conn,
origin: origin,
destination: destination,
}
}

func (c *NATPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
func NewNATPacketConn(conn N.NetPacketConn, origin M.Socksaddr, destination M.Socksaddr) NATPacketConn {
return &bidirectionalNATPacketConn{
NetPacketConn: conn,
origin: origin,
destination: destination,
}
}

type unidirectionalNATPacketConn struct {
N.NetPacketConn
origin M.Socksaddr
destination M.Socksaddr
}

func (c *unidirectionalNATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if M.SocksaddrFromNet(addr) == c.destination {
addr = c.origin.UDPAddr()
}
return c.NetPacketConn.WriteTo(p, addr)
}

func (c *unidirectionalNATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
if destination == c.destination {
destination = c.origin
}
return c.NetPacketConn.WritePacket(buffer, destination)
}

func (c *unidirectionalNATPacketConn) UpdateDestination(destinationAddress netip.Addr) {
c.destination = M.SocksaddrFrom(destinationAddress, c.destination.Port)
}

func (c *unidirectionalNATPacketConn) Upstream() any {
return c.NetPacketConn
}

type bidirectionalNATPacketConn struct {
N.NetPacketConn
origin M.Socksaddr
destination M.Socksaddr
}

func (c *bidirectionalNATPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
n, addr, err = c.NetPacketConn.ReadFrom(p)
if err == nil && M.SocksaddrFromNet(addr) == c.origin {
addr = c.destination.UDPAddr()
}
return
}

func (c *NATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
func (c *bidirectionalNATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if M.SocksaddrFromNet(addr) == c.destination {
addr = c.origin.UDPAddr()
}
return c.NetPacketConn.WriteTo(p, addr)
}

func (c *NATPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
func (c *bidirectionalNATPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
destination, err = c.NetPacketConn.ReadPacket(buffer)
if destination == c.origin {
destination = c.destination
}
return
}

func (c *NATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
func (c *bidirectionalNATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
if destination == c.destination {
destination = c.origin
}
return c.NetPacketConn.WritePacket(buffer, destination)
}

func (c *NATPacketConn) UpdateDestination(destinationAddress netip.Addr) {
func (c *bidirectionalNATPacketConn) UpdateDestination(destinationAddress netip.Addr) {
c.destination = M.SocksaddrFrom(destinationAddress, c.destination.Port)
}

func (c *NATPacketConn) Upstream() any {
func (c *bidirectionalNATPacketConn) Upstream() any {
return c.NetPacketConn
}
33 changes: 33 additions & 0 deletions common/uot/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import (
N "github.com/sagernet/sing/common/network"
)

var (
_ N.NetPacketConn = (*Conn)(nil)
_ N.PacketReadWaiter = (*Conn)(nil)
)

type Conn struct {
net.Conn
isConnect bool
destination M.Socksaddr
writer N.VectorisedWriter
newBuffer func() *buf.Buffer
}

func NewConn(conn net.Conn, request Request) *Conn {
Expand Down Expand Up @@ -141,6 +147,33 @@ func (c *Conn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
return c.writer.WriteVectorised([]*buf.Buffer{header, buffer})
}

func (c *Conn) InitializeReadWaiter(newBuffer func() *buf.Buffer) {
c.newBuffer = newBuffer
}

func (c *Conn) WaitReadPacket() (destination M.Socksaddr, err error) {
if c.isConnect {
destination = c.destination
} else {
destination, err = AddrParser.ReadAddrPort(c.Conn)
if err != nil {
return
}
}
var length uint16
err = binary.Read(c.Conn, binary.BigEndian, &length)
if err != nil {
return
}
buffer := c.newBuffer()
_, err = buffer.ReadFullFrom(c.Conn, int(length))
if err != nil {
buffer.Release()
return M.Socksaddr{}, E.Cause(err, "UoT read")
}
return
}

func (c *Conn) NeedAdditionalReadDeadline() bool {
return true
}
Expand Down
19 changes: 19 additions & 0 deletions common/upstream.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package common

import "net"

type WithUpstream interface {
Upstream() any
}

type stdWithUpstreamNetConn interface {
NetConn() net.Conn
}

func Cast[T any](obj any) (T, bool) {
if c, ok := obj.(T); ok {
return c, true
}
if u, ok := obj.(WithUpstream); ok {
return Cast[T](u.Upstream())
}
if u, ok := obj.(stdWithUpstreamNetConn); ok {
return Cast[T](u.NetConn())
}
return DefaultValue[T](), false
}

Expand All @@ -22,3 +31,13 @@ func MustCast[T any](obj any) T {
}
return value
}

func Top(obj any) any {
if u, ok := obj.(WithUpstream); ok {
return Top(u.Upstream())
}
if u, ok := obj.(stdWithUpstreamNetConn); ok {
return Top(u.NetConn())
}
return obj
}
7 changes: 7 additions & 0 deletions service/filemanager/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func (m *defaultManager) CreateTemp(pattern string) (*os.File, error) {
return file, nil
}

func (m *defaultManager) Chown(path string) error {
if m.chown {
return os.Chown(path, m.userID, m.groupID)
}
return nil
}

func (m *defaultManager) Mkdir(path string, perm os.FileMode) error {
path = m.BasePath(path)
err := os.Mkdir(path, perm)
Expand Down
9 changes: 9 additions & 0 deletions service/filemanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Manager interface {
OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
Create(name string) (*os.File, error)
CreateTemp(pattern string) (*os.File, error)
Chown(name string) error
Mkdir(path string, perm os.FileMode) error
MkdirAll(path string, perm os.FileMode) error
Remove(path string) error
Expand Down Expand Up @@ -50,6 +51,14 @@ func CreateTemp(ctx context.Context, pattern string) (*os.File, error) {
return manager.CreateTemp(pattern)
}

func Chown(ctx context.Context, name string) error {
manager := service.FromContext[Manager](ctx)
if manager == nil {
return nil
}
return manager.Chown(name)
}

func Mkdir(ctx context.Context, path string, perm os.FileMode) error {
manager := service.FromContext[Manager](ctx)
if manager == nil {
Expand Down
Loading