2024-11-02 00:39:02 +08:00
|
|
|
package shadowtls
|
2022-08-31 14:21:53 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter/inbound"
|
2022-08-31 14:21:53 +08:00
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/common/listener"
|
2022-08-31 14:21:53 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-02-20 13:53:06 +08:00
|
|
|
"github.com/sagernet/sing-shadowtls"
|
2023-02-21 16:07:08 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-09-24 12:00:00 +08:00
|
|
|
"github.com/sagernet/sing/common/auth"
|
2024-10-21 23:38:34 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2024-11-09 21:16:11 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-08-31 14:21:53 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func RegisterInbound(registry *inbound.Registry) {
|
|
|
|
inbound.Register[option.ShadowTLSInboundOptions](registry, C.TypeShadowTLS, NewInbound)
|
2022-08-31 14:21:53 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
type Inbound struct {
|
|
|
|
inbound.Adapter
|
|
|
|
router adapter.Router
|
|
|
|
logger logger.ContextLogger
|
|
|
|
listener *listener.Listener
|
|
|
|
service *shadowtls.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSInboundOptions) (adapter.Inbound, error) {
|
|
|
|
inbound := &Inbound{
|
|
|
|
Adapter: inbound.NewAdapter(C.TypeShadowTLS, tag),
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
2022-10-06 22:47:11 +08:00
|
|
|
}
|
2022-08-31 14:21:53 +08:00
|
|
|
|
2023-03-11 10:12:46 +08:00
|
|
|
if options.Version == 0 {
|
|
|
|
options.Version = 1
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:07:08 +08:00
|
|
|
var handshakeForServerName map[string]shadowtls.HandshakeConfig
|
|
|
|
if options.Version > 1 {
|
|
|
|
handshakeForServerName = make(map[string]shadowtls.HandshakeConfig)
|
|
|
|
for serverName, serverOptions := range options.HandshakeForServerName {
|
2024-11-09 21:16:11 +08:00
|
|
|
handshakeDialer, err := dialer.New(ctx, serverOptions.DialerOptions)
|
2023-08-08 16:14:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-21 16:07:08 +08:00
|
|
|
handshakeForServerName[serverName] = shadowtls.HandshakeConfig{
|
|
|
|
Server: serverOptions.ServerOptions.Build(),
|
2023-08-08 16:14:03 +08:00
|
|
|
Dialer: handshakeDialer,
|
2023-02-21 16:07:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 21:16:11 +08:00
|
|
|
handshakeDialer, err := dialer.New(ctx, options.Handshake.DialerOptions)
|
2023-08-08 16:14:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-20 13:53:06 +08:00
|
|
|
service, err := shadowtls.NewService(shadowtls.ServiceConfig{
|
2023-02-21 16:07:08 +08:00
|
|
|
Version: options.Version,
|
|
|
|
Password: options.Password,
|
|
|
|
Users: common.Map(options.Users, func(it option.ShadowTLSUser) shadowtls.User {
|
|
|
|
return (shadowtls.User)(it)
|
|
|
|
}),
|
|
|
|
Handshake: shadowtls.HandshakeConfig{
|
|
|
|
Server: options.Handshake.ServerOptions.Build(),
|
2023-08-08 16:14:03 +08:00
|
|
|
Dialer: handshakeDialer,
|
2023-02-21 16:07:08 +08:00
|
|
|
},
|
|
|
|
HandshakeForServerName: handshakeForServerName,
|
2023-02-21 16:34:41 +08:00
|
|
|
StrictMode: options.StrictMode,
|
2024-11-09 21:16:11 +08:00
|
|
|
Handler: (*inboundHandler)(inbound),
|
2023-02-21 16:07:08 +08:00
|
|
|
Logger: logger,
|
2023-02-20 13:53:06 +08:00
|
|
|
})
|
2022-08-31 14:21:53 +08:00
|
|
|
if err != nil {
|
2023-02-20 13:53:06 +08:00
|
|
|
return nil, err
|
2022-08-31 14:21:53 +08:00
|
|
|
}
|
2023-02-20 13:53:06 +08:00
|
|
|
inbound.service = service
|
2024-11-02 00:39:02 +08:00
|
|
|
inbound.listener = listener.New(listener.Options{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
|
|
|
Network: []string{N.NetworkTCP},
|
|
|
|
Listen: options.ListenOptions,
|
|
|
|
ConnectionHandler: inbound,
|
|
|
|
})
|
2023-02-20 13:53:06 +08:00
|
|
|
return inbound, nil
|
2022-08-31 14:21:53 +08:00
|
|
|
}
|
2022-10-06 22:47:11 +08:00
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (h *Inbound) Start(stage adapter.StartStage) error {
|
|
|
|
if stage != adapter.StartStateStart {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
return h.listener.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Inbound) Close() error {
|
|
|
|
return h.listener.Close()
|
|
|
|
}
|
|
|
|
|
2024-11-09 21:16:11 +08:00
|
|
|
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
err := h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, metadata.Source, metadata.Destination, onClose)
|
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
|
|
if err != nil {
|
|
|
|
if E.IsClosedOrCanceled(err) {
|
|
|
|
h.logger.DebugContext(ctx, "connection closed: ", err)
|
|
|
|
} else {
|
|
|
|
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 22:47:11 +08:00
|
|
|
}
|
2023-09-24 12:00:00 +08:00
|
|
|
|
2024-11-09 21:16:11 +08:00
|
|
|
type inboundHandler Inbound
|
|
|
|
|
|
|
|
func (h *inboundHandler) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
var metadata adapter.InboundContext
|
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
|
|
|
metadata.Source = source
|
|
|
|
metadata.Destination = destination
|
2023-09-24 12:00:00 +08:00
|
|
|
if userName, _ := auth.UserFromContext[string](ctx); userName != "" {
|
|
|
|
metadata.User = userName
|
|
|
|
h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
|
|
|
|
}
|
2024-11-09 21:16:11 +08:00
|
|
|
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|