2023-10-22 20:11:41 -04:00
|
|
|
package tun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
2023-10-22 20:44:36 -04:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/tun/device"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/tun/tunsorter"
|
2023-10-22 20:11:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewDeviceWithSorter(overlay device.Device, sorter *tunsorter.TunSorter) device.Device {
|
|
|
|
return &packetAddrDevice{
|
|
|
|
Device: overlay,
|
|
|
|
sorter: sorter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type packetAddrDevice struct {
|
|
|
|
device.Device
|
|
|
|
sorter *tunsorter.TunSorter
|
|
|
|
|
|
|
|
secondaryDispatcher stack.NetworkDispatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *packetAddrDevice) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) {
|
|
|
|
buf := pkt.ToBuffer()
|
|
|
|
_, err := p.sorter.OnPacketReceived(buf.Flatten())
|
|
|
|
if err != nil {
|
|
|
|
p.secondaryDispatcher.DeliverNetworkPacket(protocol, pkt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *packetAddrDevice) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) {
|
2023-10-22 20:44:36 -04:00
|
|
|
// TODO implement me
|
2023-10-22 20:11:41 -04:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *packetAddrDevice) Attach(dispatcher stack.NetworkDispatcher) {
|
|
|
|
p.secondaryDispatcher = dispatcher
|
|
|
|
p.Device.Attach(p)
|
|
|
|
}
|