Fix endpoint lifecycle ordering and device reconfiguration

This commit is contained in:
世界
2026-11-24 22:45:20 +08:00
parent 37fffa325b
commit 4913f787fb
11 changed files with 138 additions and 91 deletions
+4 -4
View File
@@ -478,10 +478,10 @@ func (e *Endpoint) readLoop(ctx context.Context) {
err = e.device.WriteInboundBuffers(packetBuffers)
buf.ReleaseMulti(packetBuffers)
if err != nil {
err = E.Cause(err, "write packet to device")
e.logger.Error(err)
e.setTerminalError(err)
return
if E.IsCanceled(err) || ctx.Err() != nil {
return
}
e.logger.Debug(E.Cause(err, "write packet to device"))
}
}
}
+4 -4
View File
@@ -654,10 +654,10 @@ func (c *ClientEndpoint) readLoop(ctx context.Context) {
err = c.device.WriteInboundBuffers(packetBuffers)
buf.ReleaseMulti(packetBuffers)
if err != nil {
err = E.Cause(err, "write packet to device")
c.logger.Error(err)
c.setTerminalError(err)
return
if E.IsCanceled(err) || ctx.Err() != nil {
return
}
c.logger.Debug(E.Cause(err, "write packet to device"))
}
}
}
+4 -2
View File
@@ -627,8 +627,10 @@ func (s *ServerEndpoint) readLoop(ctx context.Context) {
err = s.device.WriteInboundBuffers(packetBuffers)
buf.ReleaseMulti(packetBuffers)
if err != nil {
s.logger.Error(E.Cause(err, "write packet to device"))
return
if E.IsCanceled(err) || ctx.Err() != nil {
return
}
s.logger.Debug(E.Cause(err, "write packet to device"))
}
}
}
+3 -3
View File
@@ -202,7 +202,7 @@ func (w *Endpoint) DetachReturn(returnPath tun.Return) error {
func (w *Endpoint) JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) tun.FlowVerdict {
for _, localPrefix := range w.localAddresses {
if localPrefix.Contains(destination.Addr()) {
if destination.Addr() == localPrefix.Addr() {
return tun.FlowVerdict{Action: tun.ActionAccept}
}
}
@@ -235,7 +235,7 @@ func (w *Endpoint) NewConnectionEx(ctx context.Context, conn net.Conn, source M.
metadata.InboundType = w.Type()
metadata.Source = source
for _, localPrefix := range w.localAddresses {
if localPrefix.Contains(destination.Addr) {
if destination.Addr == localPrefix.Addr() {
metadata.OriginDestination = destination
if destination.Addr.Is4() {
destination.Addr = netip.AddrFrom4([4]uint8{127, 0, 0, 1})
@@ -258,7 +258,7 @@ func (w *Endpoint) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
metadata.Source = source
metadata.Destination = destination
for _, localPrefix := range w.localAddresses {
if localPrefix.Contains(destination.Addr) {
if destination.Addr == localPrefix.Addr() {
metadata.OriginDestination = destination
if destination.Addr.Is4() {
metadata.Destination.Addr = netip.AddrFrom4([4]uint8{127, 0, 0, 1})