[Skip CI]

This commit is contained in:
MetaCubeX 2022-03-15 11:47:42 +08:00
commit 3e89bee524

View file

@ -54,6 +54,7 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, tcpIn chan<- C.ConnContex
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol}, NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol}, TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol},
}) })
adapter := &gvisorAdapter{ adapter := &gvisorAdapter{
device: device, device: device,
ipstack: ipstack, ipstack: ipstack,
@ -95,7 +96,8 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, tcpIn chan<- C.ConnContex
r.Complete(true) r.Complete(true)
return return
} }
r.Complete(false)
defer r.Complete(false)
conn := gonet.NewTCPConn(&wq, ep) conn := gonet.NewTCPConn(&wq, ep)
@ -110,6 +112,7 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, tcpIn chan<- C.ConnContex
target := getAddr(ep.Info().(*stack.TransportEndpointInfo).ID) target := getAddr(ep.Info().(*stack.TransportEndpointInfo).ID)
tcpIn <- inbound.NewSocket(target, conn, C.TUN) tcpIn <- inbound.NewSocket(target, conn, C.TUN)
}) })
ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket) ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket)
// UDP handler // UDP handler
@ -200,24 +203,31 @@ func (t *gvisorAdapter) AsLinkEndpoint() (result stack.LinkEndpoint, err error)
log.Errorln("can not read from tun: %v", err) log.Errorln("can not read from tun: %v", err)
continue continue
} }
var p tcpip.NetworkProtocolNumber var p tcpip.NetworkProtocolNumber
switch header.IPVersion(packet) { switch header.IPVersion(packet) {
case header.IPv4Version: case header.IPv4Version:
p = header.IPv4ProtocolNumber p = header.IPv4ProtocolNumber
case header.IPv6Version: case header.IPv6Version:
p = header.IPv6ProtocolNumber p = header.IPv6ProtocolNumber
default:
log.Warnln("invalid IP version:%d", header.IPVersion(packet))
continue
} }
if linkEP.IsAttached() { if linkEP.IsAttached() {
packetBuffer := stack.NewPacketBuffer(stack.PacketBufferOptions{ pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Data: buffer.View(packet[:n]).ToVectorisedView(), Data: buffer.View(packet[:n]).ToVectorisedView(),
}) })
linkEP.InjectInbound(p, packetBuffer) linkEP.InjectInbound(p, pkt)
packetBuffer.DecRef() // release memory
pkt.DecRef()
} else { } else {
log.Debugln("received packet from tun when %s is not attached to any dispatcher.", t.device.Name()) log.Debugln("received packet from tun when %s is not attached to any dispatcher.", t.device.Name())
} }
} }
t.wg.Done() t.wg.Done()
t.Close() t.Close()
log.Debugln("%v stop read loop", t.device.Name()) log.Debugln("%v stop read loop", t.device.Name())