2022-12-02 16:56:17 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2023-04-02 22:24:46 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/buf"
|
2022-12-02 16:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type refConn struct {
|
2023-04-02 22:24:46 +08:00
|
|
|
conn ExtendedConn
|
2022-12-02 16:56:17 +08:00
|
|
|
ref any
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) Read(b []byte) (n int, err error) {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) Write(b []byte) (n int, err error) {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) Close() error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) LocalAddr() net.Addr {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) RemoteAddr() net.Addr {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) SetDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) SetReadDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:26:30 +08:00
|
|
|
func (c *refConn) Upstream() any {
|
|
|
|
return c.conn
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:24:46 +08:00
|
|
|
func (c *refConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.ReadBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
defer runtime.KeepAlive(c.ref)
|
|
|
|
return c.conn.WriteBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:01:05 +08:00
|
|
|
func (c *refConn) ReaderReplaceable() bool { // Relay() will handle reference
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refConn) WriterReplaceable() bool { // Relay() will handle reference
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:24:46 +08:00
|
|
|
var _ ExtendedConn = (*refConn)(nil)
|
|
|
|
|
2022-12-02 16:56:17 +08:00
|
|
|
func NewRefConn(conn net.Conn, ref any) net.Conn {
|
2023-04-02 22:24:46 +08:00
|
|
|
return &refConn{conn: NewExtendedConn(conn), ref: ref}
|
2022-12-02 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type refPacketConn struct {
|
2023-05-12 09:14:27 +08:00
|
|
|
pc EnhancePacketConn
|
2022-12-02 16:56:17 +08:00
|
|
|
ref any
|
|
|
|
}
|
|
|
|
|
2023-05-12 09:14:27 +08:00
|
|
|
func (pc *refPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.WaitReadFrom()
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:56:17 +08:00
|
|
|
func (pc *refPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.ReadFrom(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.WriteTo(p, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) Close() error {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) LocalAddr() net.Addr {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) SetDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) SetReadDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
defer runtime.KeepAlive(pc.ref)
|
|
|
|
return pc.pc.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
2023-05-12 09:14:27 +08:00
|
|
|
func (pc *refPacketConn) Upstream() any {
|
|
|
|
return pc.pc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) ReaderReplaceable() bool { // Relay() will handle reference
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *refPacketConn) WriterReplaceable() bool { // Relay() will handle reference
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:56:17 +08:00
|
|
|
func NewRefPacketConn(pc net.PacketConn, ref any) net.PacketConn {
|
2023-05-12 09:14:27 +08:00
|
|
|
return &refPacketConn{pc: NewEnhancePacketConn(pc), ref: ref}
|
2022-12-02 16:56:17 +08:00
|
|
|
}
|