Update udpant usages

This commit is contained in:
世界 2024-11-26 11:37:44 +08:00
parent c177abb523
commit 59a6bdc1fa
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 34 additions and 20 deletions

View file

@ -16,8 +16,9 @@ import (
type Mixed struct {
*System
stack *stack.Stack
endpoint *channel.Endpoint
stack *stack.Stack
endpoint *channel.Endpoint
udpForwarder *UDPForwarder
}
func NewMixed(
@ -42,14 +43,29 @@ func (m *Mixed) Start() error {
if err != nil {
return err
}
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout).HandlePacket)
udpForwarder := NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
m.stack = ipStack
m.endpoint = endpoint
m.udpForwarder = udpForwarder
go m.tunLoop()
go m.packetLoop()
return nil
}
func (m *Mixed) Close() error {
if m.stack == nil {
return nil
}
m.endpoint.Attach(nil)
m.stack.Close()
for _, endpoint := range m.stack.CleanupEndpoints() {
endpoint.Abort()
}
m.udpNat.Close()
return m.System.Close()
}
func (m *Mixed) tunLoop() {
if winTun, isWinTun := m.tun.(WinTun); isWinTun {
m.wintunLoop(winTun)
@ -222,15 +238,3 @@ func (m *Mixed) packetLoop() {
packet.DecRef()
}
}
func (m *Mixed) Close() error {
if m.stack == nil {
return nil
}
m.endpoint.Attach(nil)
m.stack.Close()
for _, endpoint := range m.stack.CleanupEndpoints() {
endpoint.Abort()
}
return m.System.Close()
}