Crazy sekai overturns the small pond

This commit is contained in:
世界 2024-10-21 23:38:34 +08:00
parent 253b41936e
commit 8304295c48
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
139 changed files with 2866 additions and 1559 deletions

View file

@ -10,15 +10,16 @@ import (
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2raygrpc"
"github.com/sagernet/sing-box/transport/v2raygrpclite"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
func NewGRPCServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
func NewGRPCServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
if options.ForceLite {
return v2raygrpclite.NewServer(ctx, options, tlsConfig, handler)
return v2raygrpclite.NewServer(ctx, logger, options, tlsConfig, handler)
}
return v2raygrpc.NewServer(ctx, options, tlsConfig, handler)
return v2raygrpc.NewServer(ctx, logger, options, tlsConfig, handler)
}
func NewGRPCClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {

View file

@ -9,12 +9,13 @@ import (
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2raygrpclite"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
func NewGRPCServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
return v2raygrpclite.NewServer(ctx, options, tlsConfig, handler)
func NewGRPCServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
return v2raygrpclite.NewServer(ctx, logger, options, tlsConfig, handler)
}
func NewGRPCClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {

View file

@ -7,6 +7,7 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
@ -21,11 +22,11 @@ func RegisterQUICConstructor(server ServerConstructor[option.V2RayQUICOptions],
quicClientConstructor = client
}
func NewQUICServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
func NewQUICServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
if quicServerConstructor == nil {
return nil, os.ErrInvalid
}
return quicServerConstructor(ctx, options, tlsConfig, handler)
return quicServerConstructor(ctx, logger, options, tlsConfig, handler)
}
func NewQUICClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {

View file

@ -11,33 +11,34 @@ import (
"github.com/sagernet/sing-box/transport/v2rayhttpupgrade"
"github.com/sagernet/sing-box/transport/v2raywebsocket"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
type (
ServerConstructor[O any] func(ctx context.Context, options O, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error)
ServerConstructor[O any] func(ctx context.Context, logger logger.ContextLogger, options O, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error)
ClientConstructor[O any] func(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options O, tlsConfig tls.Config) (adapter.V2RayClientTransport, error)
)
func NewServerTransport(ctx context.Context, options option.V2RayTransportOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
func NewServerTransport(ctx context.Context, logger logger.ContextLogger, options option.V2RayTransportOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
if options.Type == "" {
return nil, nil
}
switch options.Type {
case C.V2RayTransportTypeHTTP:
return v2rayhttp.NewServer(ctx, options.HTTPOptions, tlsConfig, handler)
return v2rayhttp.NewServer(ctx, logger, options.HTTPOptions, tlsConfig, handler)
case C.V2RayTransportTypeWebsocket:
return v2raywebsocket.NewServer(ctx, options.WebsocketOptions, tlsConfig, handler)
return v2raywebsocket.NewServer(ctx, logger, options.WebsocketOptions, tlsConfig, handler)
case C.V2RayTransportTypeQUIC:
if tlsConfig == nil {
return nil, C.ErrTLSRequired
}
return NewQUICServer(ctx, options.QUICOptions, tlsConfig, handler)
return NewQUICServer(ctx, logger, options.QUICOptions, tlsConfig, handler)
case C.V2RayTransportTypeGRPC:
return NewGRPCServer(ctx, options.GRPCOptions, tlsConfig, handler)
return NewGRPCServer(ctx, logger, options.GRPCOptions, tlsConfig, handler)
case C.V2RayTransportTypeHTTPUpgrade:
return v2rayhttpupgrade.NewServer(ctx, options.HTTPUpgradeOptions, tlsConfig, handler)
return v2rayhttpupgrade.NewServer(ctx, logger, options.HTTPUpgradeOptions, tlsConfig, handler)
default:
return nil, E.New("unknown transport type: " + options.Type)
}

View file

@ -105,7 +105,7 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
cancel(err)
return nil, err
}
return NewGRPCConn(stream, cancel), nil
return NewGRPCConn(stream), nil
}
func (c *Client) Close() error {

View file

@ -5,7 +5,6 @@ import (
"os"
"time"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/baderror"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -15,17 +14,15 @@ var _ net.Conn = (*GRPCConn)(nil)
type GRPCConn struct {
GunService
cancel common.ContextCancelCauseFunc
cache []byte
cache []byte
}
func NewGRPCConn(service GunService, cancel common.ContextCancelCauseFunc) *GRPCConn {
func NewGRPCConn(service GunService) *GRPCConn {
if client, isClient := service.(GunService_TunClient); isClient {
service = &clientConnWrapper{client}
}
return &GRPCConn{
GunService: service,
cancel: cancel,
}
}
@ -38,7 +35,6 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
hunk, err := c.Recv()
err = baderror.WrapGRPC(err)
if err != nil {
c.cancel(err)
return
}
n = copy(b, hunk.Data)
@ -51,14 +47,12 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
func (c *GRPCConn) Write(b []byte) (n int, err error) {
err = baderror.WrapGRPC(c.Send(&Hunk{Data: b}))
if err != nil {
c.cancel(err)
return
}
return len(b), nil
}
func (c *GRPCConn) Close() error {
c.cancel(net.ErrClosed)
return nil
}

View file

@ -9,8 +9,10 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -25,11 +27,12 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
handler N.TCPConnectionHandler
logger logger.ContextLogger
handler adapter.V2RayServerTransportHandler
server *grpc.Server
}
func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler N.TCPConnectionHandler) (*Server, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
var serverOptions []grpc.ServerOption
if tlsConfig != nil {
if !common.Contains(tlsConfig.NextProtos(), http2.NextProtoTLS) {
@ -43,17 +46,16 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig t
Timeout: time.Duration(options.PingTimeout),
}))
}
server := &Server{ctx, handler, grpc.NewServer(serverOptions...)}
server := &Server{ctx, logger, handler, grpc.NewServer(serverOptions...)}
RegisterGunServiceCustomNameServer(server.server, server, options.ServiceName)
return server, nil
}
func (s *Server) Tun(server GunService_TunServer) error {
ctx, cancel := common.ContextWithCancelCause(s.ctx)
conn := NewGRPCConn(server, cancel)
var metadata M.Metadata
conn := NewGRPCConn(server)
var source M.Socksaddr
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
metadata.Source = M.SocksaddrFromNet(remotePeer.Addr)
source = M.SocksaddrFromNet(remotePeer.Addr)
}
if grpcMetadata, loaded := gM.FromIncomingContext(server.Context()); loaded {
forwardFrom := strings.Join(grpcMetadata.Get("X-Forwarded-For"), ",")
@ -61,13 +63,16 @@ func (s *Server) Tun(server GunService_TunServer) error {
for _, from := range strings.Split(forwardFrom, ",") {
originAddr := M.ParseSocksaddr(from)
if originAddr.IsValid() {
metadata.Source = originAddr.Unwrap()
source = originAddr.Unwrap()
}
}
}
}
go s.handler.NewConnection(ctx, conn, metadata)
<-ctx.Done()
done := make(chan struct{})
go s.handler.NewConnectionEx(log.ContextWithNewID(s.ctx), conn, source, M.Socksaddr{}, N.OnceClose(func(it error) {
close(done)
}))
<-done
return nil
}

View file

@ -10,10 +10,12 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2rayhttp"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
aTLS "github.com/sagernet/sing/common/tls"
@ -26,18 +28,19 @@ import (
var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
tlsConfig tls.ServerConfig
handler adapter.V2RayServerTransportHandler
errorHandler E.Handler
httpServer *http.Server
h2Server *http2.Server
h2cHandler http.Handler
path string
tlsConfig tls.ServerConfig
logger logger.ContextLogger
handler adapter.V2RayServerTransportHandler
httpServer *http.Server
h2Server *http2.Server
h2cHandler http.Handler
path string
}
func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
server := &Server{
tlsConfig: tlsConfig,
logger: logger,
handler: handler,
path: "/" + options.ServiceName + "/Tun",
h2Server: &http2.Server{
@ -49,6 +52,9 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig t
BaseContext: func(net.Listener) context.Context {
return ctx
},
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return log.ContextWithNewID(ctx)
},
}
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
return server, nil
@ -74,10 +80,12 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/grpc")
writer.Header().Set("TE", "trailers")
writer.WriteHeader(http.StatusOK)
var metadata M.Metadata
metadata.Source = sHttp.SourceAddress(request)
done := make(chan struct{})
conn := v2rayhttp.NewHTTP2Wrapper(newGunConn(request.Body, writer, writer.(http.Flusher)))
s.handler.NewConnection(request.Context(), conn, metadata)
s.handler.NewConnectionEx(request.Context(), conn, sHttp.SourceAddress(request), M.Socksaddr{}, N.OnceClose(func(it error) {
close(done)
}))
<-done
conn.CloseWrapper()
}
@ -85,7 +93,7 @@ func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Reques
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
s.logger.ErrorContext(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Network() []string {

View file

@ -11,11 +11,13 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
aTLS "github.com/sagernet/sing/common/tls"
@ -29,6 +31,7 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
logger logger.ContextLogger
tlsConfig tls.ServerConfig
handler adapter.V2RayServerTransportHandler
httpServer *http.Server
@ -40,7 +43,7 @@ type Server struct {
headers http.Header
}
func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayHTTPOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
server := &Server{
ctx: ctx,
tlsConfig: tlsConfig,
@ -63,6 +66,9 @@ func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig t
BaseContext: func(net.Listener) context.Context {
return ctx
},
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return log.ContextWithNewID(ctx)
},
}
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
return server, nil
@ -95,8 +101,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
}
var metadata M.Metadata
metadata.Source = sHttp.SourceAddress(request)
source := sHttp.SourceAddress(request)
if h, ok := writer.(http.Hijacker); ok {
var requestBody *buf.Buffer
if contentLength := int(request.ContentLength); contentLength > 0 {
@ -127,14 +132,18 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if requestBody != nil {
conn = bufio.NewCachedConn(conn, requestBody)
}
s.handler.NewConnection(request.Context(), conn, metadata)
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, nil)
} else {
writer.WriteHeader(http.StatusOK)
done := make(chan struct{})
conn := NewHTTP2Wrapper(&ServerHTTPConn{
NewHTTPConn(request.Body, writer),
writer.(http.Flusher),
})
s.handler.NewConnection(request.Context(), conn, metadata)
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, N.OnceClose(func(it error) {
close(done)
}))
<-done
conn.CloseWrapper()
}
}
@ -143,7 +152,7 @@ func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Reques
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
s.logger.ErrorContext(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Network() []string {

View file

@ -10,9 +10,11 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
aTLS "github.com/sagernet/sing/common/tls"
@ -23,6 +25,7 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
logger logger.ContextLogger
tlsConfig tls.ServerConfig
handler adapter.V2RayServerTransportHandler
httpServer *http.Server
@ -31,7 +34,7 @@ type Server struct {
headers http.Header
}
func NewServer(ctx context.Context, options option.V2RayHTTPUpgradeOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayHTTPUpgradeOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
server := &Server{
ctx: ctx,
tlsConfig: tlsConfig,
@ -50,6 +53,9 @@ func NewServer(ctx context.Context, options option.V2RayHTTPUpgradeOptions, tlsC
BaseContext: func(net.Listener) context.Context {
return ctx
},
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return log.ContextWithNewID(ctx)
},
TLSNextProto: make(map[string]func(*http.Server, *tls.STDConn, http.Handler)),
}
return server, nil
@ -104,16 +110,14 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
s.invalidRequest(writer, request, http.StatusInternalServerError, E.Cause(err, "hijack failed"))
return
}
var metadata M.Metadata
metadata.Source = sHttp.SourceAddress(request)
s.handler.NewConnection(request.Context(), conn, metadata)
s.handler.NewConnectionEx(request.Context(), conn, sHttp.SourceAddress(request), M.Socksaddr{}, nil)
}
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
s.logger.ErrorContext(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Network() []string {

View file

@ -15,6 +15,8 @@ import (
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-quic"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
@ -23,6 +25,7 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
logger logger.ContextLogger
tlsConfig tls.ServerConfig
quicConfig *quic.Config
handler adapter.V2RayServerTransportHandler
@ -30,7 +33,7 @@ type Server struct {
quicListener qtls.Listener
}
func NewServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
@ -39,6 +42,7 @@ func NewServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig t
}
server := &Server{
ctx: ctx,
logger: logger,
tlsConfig: tlsConfig,
quicConfig: quicConfig,
handler: handler,
@ -73,8 +77,8 @@ func (s *Server) acceptLoop() {
}
go func() {
hErr := s.streamAcceptLoop(conn)
if hErr != nil {
s.handler.NewError(conn.Context(), hErr)
if hErr != nil && !E.IsClosedOrCanceled(hErr) {
s.logger.ErrorContext(conn.Context(), hErr)
}
}()
}
@ -86,7 +90,7 @@ func (s *Server) streamAcceptLoop(conn quic.Connection) error {
if err != nil {
return err
}
go s.handler.NewConnection(conn.Context(), &StreamWrapper{Conn: conn, Stream: stream}, M.Metadata{})
go s.handler.NewConnectionEx(conn.Context(), &StreamWrapper{Conn: conn, Stream: stream}, M.SocksaddrFromNet(conn.RemoteAddr()), M.Socksaddr{}, nil)
}
}

View file

@ -11,11 +11,13 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
aTLS "github.com/sagernet/sing/common/tls"
@ -27,6 +29,7 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
logger logger.ContextLogger
tlsConfig tls.ServerConfig
handler adapter.V2RayServerTransportHandler
httpServer *http.Server
@ -36,9 +39,10 @@ type Server struct {
upgrader ws.HTTPUpgrader
}
func NewServer(ctx context.Context, options option.V2RayWebsocketOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayWebsocketOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
server := &Server{
ctx: ctx,
logger: logger,
tlsConfig: tlsConfig,
handler: handler,
path: options.Path,
@ -59,6 +63,9 @@ func NewServer(ctx context.Context, options option.V2RayWebsocketOptions, tlsCon
BaseContext: func(net.Listener) context.Context {
return ctx
},
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return log.ContextWithNewID(ctx)
},
}
return server, nil
}
@ -102,20 +109,19 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
s.invalidRequest(writer, request, 0, E.Cause(err, "upgrade websocket connection"))
return
}
var metadata M.Metadata
metadata.Source = sHttp.SourceAddress(request)
conn = NewConn(wsConn, metadata.Source.TCPAddr(), ws.StateServerSide)
source := sHttp.SourceAddress(request)
conn = NewConn(wsConn, source, ws.StateServerSide)
if len(earlyData) > 0 {
conn = bufio.NewCachedConn(conn, buf.As(earlyData))
}
s.handler.NewConnection(request.Context(), conn, metadata)
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, nil)
}
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
s.logger.ErrorContext(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Network() []string {