Improve mux server

This commit is contained in:
世界 2022-08-02 15:11:51 +08:00
parent b1c2440371
commit 9a8918cb9e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -28,37 +28,38 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
if err != nil { if err != nil {
return err return err
} }
stream = &wrapStream{stream} go newConnection(ctx, router, errorHandler, logger, stream, metadata)
request, err := ReadRequest(stream) }
if err != nil { }
return err
func newConnection(ctx context.Context, router adapter.Router, errorHandler E.Handler, logger log.ContextLogger, stream net.Conn, metadata adapter.InboundContext) {
stream = &wrapStream{stream}
request, err := ReadRequest(stream)
if err != nil {
logger.ErrorContext(ctx, err)
return
}
metadata.Destination = request.Destination
if request.Network == N.NetworkTCP {
logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination)
hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata)
stream.Close()
if hErr != nil {
errorHandler.NewError(ctx, hErr)
} }
metadata.Destination = request.Destination } else {
if request.Network == N.NetworkTCP { var packetConn N.PacketConn
go func() { if !request.PacketAddr {
logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination) logger.InfoContext(ctx, "inbound multiplex packet connection to ", metadata.Destination)
hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata) packetConn = &ServerPacketConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: request.Destination}
stream.Close()
if hErr != nil {
errorHandler.NewError(ctx, hErr)
}
}()
} else { } else {
go func() { logger.InfoContext(ctx, "inbound multiplex packet connection")
var packetConn N.PacketConn packetConn = &ServerPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream)}
if !request.PacketAddr { }
logger.InfoContext(ctx, "inbound multiplex packet connection to ", metadata.Destination) hErr := router.RoutePacketConnection(ctx, packetConn, metadata)
packetConn = &ServerPacketConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: request.Destination} stream.Close()
} else { if hErr != nil {
logger.InfoContext(ctx, "inbound multiplex packet connection") errorHandler.NewError(ctx, hErr)
packetConn = &ServerPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream)}
}
hErr := router.RoutePacketConnection(ctx, packetConn, metadata)
stream.Close()
if hErr != nil {
errorHandler.NewError(ctx, hErr)
}
}()
} }
} }
} }