Improve tls dialer and listener

This commit is contained in:
世界
2022-07-25 08:14:09 +08:00
parent 32e2730ec6
commit 1f05420745
8 changed files with 265 additions and 28 deletions

View File

@@ -2,6 +2,7 @@ package inbound
import (
"context"
"crypto/tls"
"net"
"os"
@@ -20,8 +21,9 @@ var _ adapter.Inbound = (*VMess)(nil)
type VMess struct {
myInboundAdapter
service *vmess.Service[int]
users []option.VMessUser
service *vmess.Service[int]
users []option.VMessUser
tlsConfig *tls.Config
}
func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VMessInboundOptions) (*VMess, error) {
@@ -46,12 +48,21 @@ func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogg
if err != nil {
return nil, err
}
if options.TLS != nil {
inbound.tlsConfig, err = NewTLSConfig(common.PtrValueOrDefault(options.TLS))
if err != nil {
return nil, err
}
}
inbound.service = service
inbound.connHandler = inbound
return inbound, nil
}
func (h *VMess) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
if h.tlsConfig != nil {
conn = tls.Server(conn, h.tlsConfig)
}
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
}