Refine Trojan packet reader & writer (#142)

This commit is contained in:
maskedeken 2021-01-08 11:55:25 +08:00 committed by GitHub
parent 161e18299c
commit d5aeb6c545
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 50 deletions

View file

@ -250,7 +250,9 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error {
udpServer := udp.NewDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) {
common.Must(clientWriter.WriteMultiBufferWithMetadata(buf.MultiBuffer{packet.Payload}, packet.Source))
udpPayload := packet.Payload
udpPayload.UDP = &packet.Source
common.Must(clientWriter.WriteMultiBuffer(buf.MultiBuffer{udpPayload}))
})
inbound := session.InboundFromContext(ctx)
@ -263,7 +265,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
case <-ctx.Done():
return nil
default:
p, err := clientReader.ReadMultiBufferWithMetadata()
mb, err := clientReader.ReadMultiBuffer()
if err != nil {
if errors.Cause(err) != io.EOF {
return newError("unexpected EOF").Base(err)
@ -271,21 +273,24 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
return nil
}
mb2, b := buf.SplitFirst(mb)
destination := *b.UDP
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: inbound.Source,
To: p.Target,
To: destination,
Status: log.AccessAccepted,
Reason: "",
Email: user.Email,
})
newError("tunnelling request to ", p.Target).WriteToLog(session.ExportIDToError(ctx))
newError("tunnelling request to ", destination).WriteToLog(session.ExportIDToError(ctx))
if !buf.Cone || dest == nil {
dest = &p.Target
dest = &destination
}
for _, b := range p.Buffer {
udpServer.Dispatch(ctx, *dest, b)
udpServer.Dispatch(ctx, *dest, b) // first packet
for _, payload := range mb2 {
udpServer.Dispatch(ctx, *dest, payload)
}
}
}