2022-09-09 10:45:10 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-30 03:27:18 +00:00
|
|
|
"crypto/tls"
|
2022-09-09 10:45:10 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-30 03:27:18 +00:00
|
|
|
"github.com/sagernet/sing-box/common/badtls"
|
2022-09-09 10:45:10 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewDialerFromOptions(router adapter.Router, dialer N.Dialer, serverAddress string, options option.OutboundTLSOptions) (N.Dialer, error) {
|
2022-11-13 03:24:37 +00:00
|
|
|
if !options.Enabled {
|
|
|
|
return dialer, nil
|
|
|
|
}
|
2022-09-09 10:45:10 +00:00
|
|
|
config, err := NewClient(router, serverAddress, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewDialer(dialer, config), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(router adapter.Router, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
|
2022-11-13 03:24:37 +00:00
|
|
|
if !options.Enabled {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-09-09 10:19:50 +00:00
|
|
|
if options.ECH != nil && options.ECH.Enabled {
|
2022-11-09 03:43:03 +00:00
|
|
|
return NewECHClient(router, serverAddress, options)
|
2023-02-25 08:24:08 +00:00
|
|
|
} else if options.Reality != nil && options.Reality.Enabled {
|
|
|
|
return NewRealityClient(router, serverAddress, options)
|
2022-09-10 02:27:00 +00:00
|
|
|
} else if options.UTLS != nil && options.UTLS.Enabled {
|
2022-11-09 03:43:03 +00:00
|
|
|
return NewUTLSClient(router, serverAddress, options)
|
2022-09-09 10:19:50 +00:00
|
|
|
} else {
|
2023-02-21 06:53:00 +00:00
|
|
|
return NewSTDClient(router, serverAddress, options)
|
2022-09-09 10:19:50 +00:00
|
|
|
}
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 02:22:52 +00:00
|
|
|
func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, error) {
|
2022-09-09 10:45:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
|
|
|
|
defer cancel()
|
2023-02-25 08:24:08 +00:00
|
|
|
tlsConn, err := config.Client(conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = tlsConn.HandshakeContext(ctx)
|
2022-09-30 03:27:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if stdConn, isSTD := tlsConn.(*tls.Conn); isSTD {
|
|
|
|
var badConn badtls.TLSConn
|
|
|
|
badConn, err = badtls.Create(stdConn)
|
|
|
|
if err == nil {
|
|
|
|
return badConn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tlsConn, nil
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dialer struct {
|
|
|
|
dialer N.Dialer
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDialer(dialer N.Dialer, config Config) N.Dialer {
|
|
|
|
return &Dialer{dialer, config}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
|
|
if network != N.NetworkTCP {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
conn, err := d.dialer.DialContext(ctx, network, destination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ClientHandshake(ctx, conn, d.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|