sing-box/inbound/shadowtls.go

53 lines
1.5 KiB
Go
Raw Normal View History

package inbound
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
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"
N "github.com/sagernet/sing/common/network"
)
type ShadowTLS struct {
myInboundAdapter
2023-02-20 13:53:06 +08:00
service *shadowtls.Service
}
func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSInboundOptions) (*ShadowTLS, error) {
inbound := &ShadowTLS{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeShadowTLS,
network: []string{N.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
2022-10-06 22:47:11 +08:00
}
2023-02-20 13:53:06 +08:00
service, err := shadowtls.NewService(shadowtls.ServiceConfig{
Version: options.Version,
Password: options.Password,
HandshakeServer: options.Handshake.ServerOptions.Build(),
HandshakeDialer: dialer.New(router, options.Handshake.DialerOptions),
Handler: inbound.upstreamContextHandler(),
Logger: logger,
})
if err != nil {
2023-02-20 13:53:06 +08:00
return nil, err
}
2023-02-20 13:53:06 +08:00
inbound.service = service
inbound.connHandler = inbound
return inbound, nil
}
2022-10-06 22:47:11 +08:00
2023-02-20 13:53:06 +08:00
func (h *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
2022-10-06 22:47:11 +08:00
}