github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/network/conn.go (about)

     1  package network
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/sagernet/sing/common"
    10  	"github.com/sagernet/sing/common/buf"
    11  	M "github.com/sagernet/sing/common/metadata"
    12  )
    13  
    14  type AbstractConn interface {
    15  	Close() error
    16  	LocalAddr() net.Addr
    17  	RemoteAddr() net.Addr
    18  	SetDeadline(t time.Time) error
    19  	SetReadDeadline(t time.Time) error
    20  	SetWriteDeadline(t time.Time) error
    21  }
    22  
    23  type PacketReader interface {
    24  	ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error)
    25  }
    26  
    27  type TimeoutPacketReader interface {
    28  	PacketReader
    29  	SetReadDeadline(t time.Time) error
    30  }
    31  
    32  type NetPacketReader interface {
    33  	PacketReader
    34  	ReadFrom(p []byte) (n int, addr net.Addr, err error)
    35  }
    36  
    37  type NetPacketWriter interface {
    38  	PacketWriter
    39  	WriteTo(p []byte, addr net.Addr) (n int, err error)
    40  }
    41  
    42  type PacketWriter interface {
    43  	WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error
    44  }
    45  
    46  type PacketConn interface {
    47  	PacketReader
    48  	PacketWriter
    49  
    50  	Close() error
    51  	LocalAddr() net.Addr
    52  	SetDeadline(t time.Time) error
    53  	SetReadDeadline(t time.Time) error
    54  	SetWriteDeadline(t time.Time) error
    55  }
    56  
    57  type ExtendedReader interface {
    58  	io.Reader
    59  	ReadBuffer(buffer *buf.Buffer) error
    60  }
    61  
    62  type ExtendedWriter interface {
    63  	io.Writer
    64  	WriteBuffer(buffer *buf.Buffer) error
    65  }
    66  
    67  type ExtendedConn interface {
    68  	ExtendedReader
    69  	ExtendedWriter
    70  	net.Conn
    71  }
    72  
    73  type TCPConnectionHandler interface {
    74  	NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error
    75  }
    76  
    77  type NetPacketConn interface {
    78  	PacketConn
    79  	NetPacketReader
    80  	NetPacketWriter
    81  }
    82  
    83  type BindPacketConn interface {
    84  	NetPacketConn
    85  	net.Conn
    86  }
    87  
    88  type UDPHandler interface {
    89  	NewPacket(ctx context.Context, conn PacketConn, buffer *buf.Buffer, metadata M.Metadata) error
    90  }
    91  
    92  type UDPConnectionHandler interface {
    93  	NewPacketConnection(ctx context.Context, conn PacketConn, metadata M.Metadata) error
    94  }
    95  
    96  type CachedReader interface {
    97  	ReadCached() *buf.Buffer
    98  }
    99  
   100  type CachedPacketReader interface {
   101  	ReadCachedPacket() *PacketBuffer
   102  }
   103  
   104  type PacketBuffer struct {
   105  	Buffer      *buf.Buffer
   106  	Destination M.Socksaddr
   107  }
   108  
   109  type WithUpstreamReader interface {
   110  	UpstreamReader() any
   111  }
   112  
   113  type WithUpstreamWriter interface {
   114  	UpstreamWriter() any
   115  }
   116  
   117  type ReaderWithUpstream interface {
   118  	ReaderReplaceable() bool
   119  }
   120  
   121  type WriterWithUpstream interface {
   122  	WriterReplaceable() bool
   123  }
   124  
   125  func UnwrapReader(reader io.Reader) io.Reader {
   126  	if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() {
   127  		return reader
   128  	}
   129  	if u, ok := reader.(WithUpstreamReader); ok {
   130  		return UnwrapReader(u.UpstreamReader().(io.Reader))
   131  	}
   132  	if u, ok := reader.(common.WithUpstream); ok {
   133  		return UnwrapReader(u.Upstream().(io.Reader))
   134  	}
   135  	return reader
   136  }
   137  
   138  func CastReader[T io.Reader](reader io.Reader) (T, bool) {
   139  	if c, ok := reader.(T); ok {
   140  		return c, true
   141  	}
   142  	if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() {
   143  		return common.DefaultValue[T](), false
   144  	}
   145  	if u, ok := reader.(WithUpstreamReader); ok {
   146  		return CastReader[T](u.UpstreamReader().(io.Reader))
   147  	}
   148  	if u, ok := reader.(common.WithUpstream); ok {
   149  		return CastReader[T](u.Upstream().(io.Reader))
   150  	}
   151  	return common.DefaultValue[T](), false
   152  }
   153  
   154  func UnwrapPacketReader(reader PacketReader) PacketReader {
   155  	if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() {
   156  		return reader
   157  	}
   158  	if u, ok := reader.(WithUpstreamReader); ok {
   159  		return UnwrapPacketReader(u.UpstreamReader().(PacketReader))
   160  	}
   161  	if u, ok := reader.(common.WithUpstream); ok {
   162  		return UnwrapPacketReader(u.Upstream().(PacketReader))
   163  	}
   164  	return reader
   165  }
   166  
   167  func CastPacketReader[T PacketReader](reader PacketReader) (T, bool) {
   168  	if c, ok := reader.(T); ok {
   169  		return c, true
   170  	}
   171  	if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() {
   172  		return common.DefaultValue[T](), false
   173  	}
   174  	if u, ok := reader.(WithUpstreamReader); ok {
   175  		return CastPacketReader[T](u.UpstreamReader().(PacketReader))
   176  	}
   177  	if u, ok := reader.(common.WithUpstream); ok {
   178  		return CastPacketReader[T](u.Upstream().(PacketReader))
   179  	}
   180  	return common.DefaultValue[T](), false
   181  }
   182  
   183  func UnwrapWriter(writer io.Writer) io.Writer {
   184  	if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() {
   185  		return writer
   186  	}
   187  	if u, ok := writer.(WithUpstreamWriter); ok {
   188  		return UnwrapWriter(u.UpstreamWriter().(io.Writer))
   189  	}
   190  	if u, ok := writer.(common.WithUpstream); ok {
   191  		return UnwrapWriter(u.Upstream().(io.Writer))
   192  	}
   193  	return writer
   194  }
   195  
   196  func CastWriter[T io.Writer](writer io.Writer) (T, bool) {
   197  	if c, ok := writer.(T); ok {
   198  		return c, true
   199  	}
   200  	if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() {
   201  		return common.DefaultValue[T](), false
   202  	}
   203  	if u, ok := writer.(WithUpstreamWriter); ok {
   204  		return CastWriter[T](u.UpstreamWriter().(io.Writer))
   205  	}
   206  	if u, ok := writer.(common.WithUpstream); ok {
   207  		return CastWriter[T](u.Upstream().(io.Writer))
   208  	}
   209  	return common.DefaultValue[T](), false
   210  }
   211  
   212  func UnwrapPacketWriter(writer PacketWriter) PacketWriter {
   213  	if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() {
   214  		return writer
   215  	}
   216  	if u, ok := writer.(WithUpstreamWriter); ok {
   217  		return UnwrapPacketWriter(u.UpstreamWriter().(PacketWriter))
   218  	}
   219  	if u, ok := writer.(common.WithUpstream); ok {
   220  		return UnwrapPacketWriter(u.Upstream().(PacketWriter))
   221  	}
   222  	return writer
   223  }
   224  
   225  func CastPacketWriter[T PacketWriter](writer PacketWriter) (T, bool) {
   226  	if c, ok := writer.(T); ok {
   227  		return c, true
   228  	}
   229  	if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() {
   230  		return common.DefaultValue[T](), false
   231  	}
   232  	if u, ok := writer.(WithUpstreamWriter); ok {
   233  		return CastPacketWriter[T](u.UpstreamWriter().(PacketWriter))
   234  	}
   235  	if u, ok := writer.(common.WithUpstream); ok {
   236  		return CastPacketWriter[T](u.Upstream().(PacketWriter))
   237  	}
   238  	return common.DefaultValue[T](), false
   239  }