Improve udpnat write back

This commit is contained in:
世界 2022-06-01 15:52:42 +08:00
parent ee9be8af94
commit e4d76a44eb
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
7 changed files with 40 additions and 64 deletions

View file

@ -225,16 +225,16 @@ func (s *Service) newPacket(ctx context.Context, conn N.PacketConn, buffer *buf.
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
s.udpNat.NewPacket(ctx, metadata.Source.AddrPort(), func() N.PacketWriter {
return &serverPacketWriter{s, conn, metadata.Source}
}, buffer, metadata)
s.udpNat.NewPacket(ctx, metadata.Source.AddrPort(), buffer, metadata, func(natConn N.PacketConn) N.PacketWriter {
return &serverPacketWriter{s, conn, natConn}
})
return nil
}
type serverPacketWriter struct {
*Service
N.PacketConn
source M.Socksaddr
source N.PacketConn
nat N.PacketConn
}
func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
@ -250,5 +250,9 @@ func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socks
common.KeepAlive(key)
c.Seal(buffer.From(w.keySaltLength)[:0], rw.ZeroBytes[:c.NonceSize()], buffer.From(w.keySaltLength), nil)
buffer.Extend(Overhead)
return w.PacketConn.WritePacket(buffer, w.source)
return w.source.WritePacket(buffer, M.SocksaddrFromNet(w.nat.LocalAddr()))
}
func (w *serverPacketWriter) Upstream() any {
return w.source
}