github.com/metacubex/mihomo@v1.18.5/common/net/sing.go (about)

     1  package net
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"runtime"
     7  
     8  	"github.com/metacubex/mihomo/common/net/deadline"
     9  
    10  	"github.com/sagernet/sing/common"
    11  	"github.com/sagernet/sing/common/bufio"
    12  	"github.com/sagernet/sing/common/network"
    13  )
    14  
    15  var NewExtendedConn = bufio.NewExtendedConn
    16  var NewExtendedWriter = bufio.NewExtendedWriter
    17  var NewExtendedReader = bufio.NewExtendedReader
    18  
    19  type ExtendedConn = network.ExtendedConn
    20  type ExtendedWriter = network.ExtendedWriter
    21  type ExtendedReader = network.ExtendedReader
    22  
    23  var WriteBuffer = bufio.WriteBuffer
    24  
    25  func NewDeadlineConn(conn net.Conn) ExtendedConn {
    26  	if deadline.IsPipe(conn) || deadline.IsPipe(network.UnwrapReader(conn)) {
    27  		return NewExtendedConn(conn) // pipe always have correctly deadline implement
    28  	}
    29  	if deadline.IsConn(conn) || deadline.IsConn(network.UnwrapReader(conn)) {
    30  		return NewExtendedConn(conn) // was a *deadline.Conn
    31  	}
    32  	return deadline.NewConn(conn)
    33  }
    34  
    35  func NeedHandshake(conn any) bool {
    36  	if earlyConn, isEarlyConn := common.Cast[network.EarlyConn](conn); isEarlyConn && earlyConn.NeedHandshake() {
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  type CountFunc = network.CountFunc
    43  
    44  var Pipe = deadline.Pipe
    45  
    46  // Relay copies between left and right bidirectionally.
    47  func Relay(leftConn, rightConn net.Conn) {
    48  	defer runtime.KeepAlive(leftConn)
    49  	defer runtime.KeepAlive(rightConn)
    50  	_ = bufio.CopyConn(context.TODO(), leftConn, rightConn)
    51  }