mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-16 22:13:41 +08:00
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
"github.com/sagernet/sing/common/buf"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
)
|
|
|
|
type myOutboundAdapter struct {
|
|
protocol string
|
|
logger log.Logger
|
|
tag string
|
|
network []string
|
|
}
|
|
|
|
func (a *myOutboundAdapter) Type() string {
|
|
return a.protocol
|
|
}
|
|
|
|
func (a *myOutboundAdapter) Tag() string {
|
|
return a.tag
|
|
}
|
|
|
|
func (a *myOutboundAdapter) Network() []string {
|
|
return a.network
|
|
}
|
|
|
|
func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error {
|
|
_payload := buf.StackNew()
|
|
payload := common.Dup(_payload)
|
|
err := conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = payload.ReadFrom(conn)
|
|
if err != nil && !E.IsTimeout(err) {
|
|
return E.Cause(err, "read payload")
|
|
}
|
|
err = conn.SetReadDeadline(time.Time{})
|
|
if err != nil {
|
|
payload.Release()
|
|
return err
|
|
}
|
|
_, err = serverConn.Write(payload.Bytes())
|
|
if err != nil {
|
|
return E.Cause(err, "client handshake")
|
|
}
|
|
runtime.KeepAlive(_payload)
|
|
return bufio.CopyConn(ctx, conn, serverConn)
|
|
}
|