2022-08-31 06:21:53 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
2022-09-09 10:45:10 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-31 06:21:53 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Outbound = (*ShadowTLS)(nil)
|
|
|
|
|
|
|
|
type ShadowTLS struct {
|
|
|
|
myOutboundAdapter
|
|
|
|
dialer N.Dialer
|
|
|
|
serverAddr M.Socksaddr
|
2022-09-09 10:45:10 +00:00
|
|
|
tlsConfig tls.Config
|
2022-08-31 06:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSOutboundOptions) (*ShadowTLS, error) {
|
|
|
|
outbound := &ShadowTLS{
|
|
|
|
myOutboundAdapter: myOutboundAdapter{
|
|
|
|
protocol: C.TypeShadowTLS,
|
|
|
|
network: []string{N.NetworkTCP},
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
},
|
2022-09-03 04:55:10 +00:00
|
|
|
dialer: dialer.New(router, options.DialerOptions),
|
2022-08-31 06:21:53 +00:00
|
|
|
serverAddr: options.ServerOptions.Build(),
|
|
|
|
}
|
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
|
|
|
return nil, C.ErrTLSRequired
|
|
|
|
}
|
|
|
|
options.TLS.MinVersion = "1.2"
|
|
|
|
options.TLS.MaxVersion = "1.2"
|
|
|
|
var err error
|
2022-09-09 10:45:10 +00:00
|
|
|
outbound.tlsConfig, err = tls.NewClient(router, options.Server, common.PtrValueOrDefault(options.TLS))
|
2022-08-31 06:21:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return outbound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
|
|
switch N.NetworkName(network) {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
default:
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
conn, err := s.dialer.DialContext(ctx, N.NetworkTCP, s.serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-09 10:45:10 +00:00
|
|
|
return tls.ClientHandshake(ctx, conn, s.tlsConfig)
|
2022-08-31 06:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
return NewConnection(ctx, s, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|