2023-06-12 17:44:22 +08:00
|
|
|
package v5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-08-03 23:07:30 +08:00
|
|
|
"sync"
|
|
|
|
|
2023-12-02 17:07:36 +08:00
|
|
|
"github.com/metacubex/mihomo/common/lru"
|
2023-06-12 17:44:22 +08:00
|
|
|
|
|
|
|
"github.com/metacubex/quic-go"
|
|
|
|
)
|
|
|
|
|
2023-09-21 23:49:45 +08:00
|
|
|
// MaxFragSize is a safe udp relay packet size
|
|
|
|
// because tuicv5 support udp fragment so we unneeded to do a magic modify for quic-go to increase MaxDatagramFrameSize
|
|
|
|
// it may not work fine in some platform
|
|
|
|
var MaxFragSize = 1200 - PacketOverHead
|
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
func fragWriteNative(quicConn quic.Connection, packet Packet, buf *bytes.Buffer, fragSize int) (err error) {
|
|
|
|
fullPayload := packet.DATA
|
|
|
|
off := 0
|
|
|
|
fragID := uint8(0)
|
|
|
|
fragCount := uint8((len(fullPayload) + fragSize - 1) / fragSize) // round up
|
|
|
|
packet.FRAG_TOTAL = fragCount
|
|
|
|
for off < len(fullPayload) {
|
|
|
|
payloadSize := len(fullPayload) - off
|
|
|
|
if payloadSize > fragSize {
|
|
|
|
payloadSize = fragSize
|
|
|
|
}
|
|
|
|
frag := packet
|
|
|
|
frag.FRAG_ID = fragID
|
|
|
|
frag.SIZE = uint16(payloadSize)
|
|
|
|
frag.DATA = fullPayload[off : off+payloadSize]
|
|
|
|
off += payloadSize
|
|
|
|
fragID++
|
|
|
|
buf.Reset()
|
|
|
|
err = frag.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := buf.Bytes()
|
2023-11-30 22:22:19 +08:00
|
|
|
err = quicConn.SendDatagram(data)
|
2023-06-12 17:44:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
packet.ADDR.TYPE = AtypNone // avoid "fragment 2/2: address in non-first fragment"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type deFragger struct {
|
2023-12-02 17:07:36 +08:00
|
|
|
lru *lru.LruCache[uint16, *packetBag]
|
2023-08-03 23:07:30 +08:00
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
type packetBag struct {
|
2023-06-12 17:44:22 +08:00
|
|
|
frags []*Packet
|
|
|
|
count uint8
|
2023-08-03 23:07:30 +08:00
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPacketBag() *packetBag {
|
|
|
|
return new(packetBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *deFragger) init() {
|
|
|
|
if d.lru == nil {
|
2023-12-02 17:07:36 +08:00
|
|
|
d.lru = lru.New(
|
|
|
|
lru.WithAge[uint16, *packetBag](10),
|
|
|
|
lru.WithUpdateAgeOnGet[uint16, *packetBag](),
|
2023-08-03 23:07:30 +08:00
|
|
|
)
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:07:30 +08:00
|
|
|
func (d *deFragger) Feed(m *Packet) *Packet {
|
2023-06-12 17:44:22 +08:00
|
|
|
if m.FRAG_TOTAL <= 1 {
|
2023-08-03 23:07:30 +08:00
|
|
|
return m
|
2023-06-12 17:44:22 +08:00
|
|
|
}
|
|
|
|
if m.FRAG_ID >= m.FRAG_TOTAL {
|
|
|
|
// wtf is this?
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-03 23:07:30 +08:00
|
|
|
d.once.Do(d.init) // lazy init
|
|
|
|
bag, _ := d.lru.GetOrStore(m.PKT_ID, newPacketBag)
|
|
|
|
bag.mutex.Lock()
|
|
|
|
defer bag.mutex.Unlock()
|
|
|
|
if int(m.FRAG_TOTAL) != len(bag.frags) {
|
2023-06-12 17:44:22 +08:00
|
|
|
// new message, clear previous state
|
2023-08-03 23:07:30 +08:00
|
|
|
bag.frags = make([]*Packet, m.FRAG_TOTAL)
|
|
|
|
bag.count = 1
|
|
|
|
bag.frags[m.FRAG_ID] = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if bag.frags[m.FRAG_ID] != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bag.frags[m.FRAG_ID] = m
|
|
|
|
bag.count++
|
|
|
|
if int(bag.count) != len(bag.frags) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// all fragments received, assemble
|
|
|
|
var data []byte
|
|
|
|
for _, frag := range bag.frags {
|
|
|
|
data = append(data, frag.DATA...)
|
2023-06-12 17:44:22 +08:00
|
|
|
}
|
2023-08-03 23:07:30 +08:00
|
|
|
p := *bag.frags[0] // recover from first fragment
|
|
|
|
p.SIZE = uint16(len(data))
|
|
|
|
p.DATA = data
|
|
|
|
p.FRAG_ID = 0
|
|
|
|
p.FRAG_TOTAL = 1
|
|
|
|
bag.frags = nil
|
|
|
|
d.lru.Delete(m.PKT_ID)
|
|
|
|
return &p
|
2023-06-12 17:44:22 +08:00
|
|
|
}
|