2023-01-16 10:50:31 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-04-25 23:01:05 +08:00
|
|
|
"runtime"
|
2023-01-16 10:50:31 +08:00
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-01-16 10:50:31 +08:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2023-04-09 22:58:05 +08:00
|
|
|
"github.com/sagernet/sing/common/bufio/deadline"
|
2023-01-16 10:50:31 +08:00
|
|
|
"github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var NewExtendedConn = bufio.NewExtendedConn
|
|
|
|
var NewExtendedWriter = bufio.NewExtendedWriter
|
|
|
|
var NewExtendedReader = bufio.NewExtendedReader
|
|
|
|
|
|
|
|
type ExtendedConn = network.ExtendedConn
|
|
|
|
type ExtendedWriter = network.ExtendedWriter
|
|
|
|
type ExtendedReader = network.ExtendedReader
|
|
|
|
|
2023-04-09 22:58:05 +08:00
|
|
|
func NewDeadlineConn(conn net.Conn) ExtendedConn {
|
2023-04-17 19:29:07 +08:00
|
|
|
return deadline.NewFallbackConn(conn)
|
2023-04-09 22:58:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeadlinePacketConn(pc net.PacketConn) net.PacketConn {
|
2023-04-17 19:29:07 +08:00
|
|
|
return deadline.NewFallbackPacketConn(bufio.NewPacketConn(pc))
|
2023-04-09 22:58:05 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
func NeedHandshake(conn any) bool {
|
|
|
|
if earlyConn, isEarlyConn := common.Cast[network.EarlyConn](conn); isEarlyConn && earlyConn.NeedHandshake() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:01:05 +08:00
|
|
|
type CountFunc = network.CountFunc
|
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
// Relay copies between left and right bidirectionally.
|
|
|
|
func Relay(leftConn, rightConn net.Conn) {
|
2023-04-25 23:01:05 +08:00
|
|
|
defer runtime.KeepAlive(leftConn)
|
|
|
|
defer runtime.KeepAlive(rightConn)
|
2023-01-16 10:50:31 +08:00
|
|
|
_ = bufio.CopyConn(context.TODO(), leftConn, rightConn)
|
|
|
|
}
|