2022-07-03 12:59:25 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2022-07-08 15:03:57 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-07-03 12:59:25 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-07 15:36:32 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-03 12:59:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type myOutboundAdapter struct {
|
|
|
|
protocol string
|
|
|
|
logger log.Logger
|
|
|
|
tag string
|
2022-07-03 15:23:18 +00:00
|
|
|
network []string
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myOutboundAdapter) Type() string {
|
|
|
|
return a.protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myOutboundAdapter) Tag() string {
|
|
|
|
return a.tag
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:23:18 +00:00
|
|
|
func (a *myOutboundAdapter) Network() []string {
|
|
|
|
return a.network
|
|
|
|
}
|
|
|
|
|
2022-07-07 15:36:32 +00:00
|
|
|
func NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = adapter.WithContext(ctx, &metadata)
|
|
|
|
var outConn net.Conn
|
|
|
|
var err error
|
|
|
|
if len(metadata.DestinationAddresses) > 0 {
|
2022-07-11 10:44:59 +00:00
|
|
|
outConn, err = N.DialSerial(ctx, this, C.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
|
2022-07-07 15:36:32 +00:00
|
|
|
} else {
|
|
|
|
outConn, err = this.DialContext(ctx, C.NetworkTCP, metadata.Destination)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bufio.CopyConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEarlyConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = adapter.WithContext(ctx, &metadata)
|
|
|
|
var outConn net.Conn
|
|
|
|
var err error
|
|
|
|
if len(metadata.DestinationAddresses) > 0 {
|
2022-07-11 10:44:59 +00:00
|
|
|
outConn, err = N.DialSerial(ctx, this, C.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
|
2022-07-07 15:36:32 +00:00
|
|
|
} else {
|
|
|
|
outConn, err = this.DialContext(ctx, C.NetworkTCP, metadata.Destination)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return CopyEarlyConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = adapter.WithContext(ctx, &metadata)
|
|
|
|
var outConn net.PacketConn
|
|
|
|
var err error
|
|
|
|
if len(metadata.DestinationAddresses) > 0 {
|
2022-07-11 10:44:59 +00:00
|
|
|
outConn, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
|
2022-07-07 15:36:32 +00:00
|
|
|
} else {
|
|
|
|
outConn, err = this.ListenPacket(ctx, metadata.Destination)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
|
|
|
|
}
|
|
|
|
|
2022-07-03 12:59:25 +00:00
|
|
|
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)
|
|
|
|
}
|