From 92f224e72151aa154382d7b07ae74e1034307eea Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 30 Mar 2026 12:31:19 +0800 Subject: [PATCH] fix --- Tun/Punchnet/Actors/SDLContextActor.swift | 44 +++++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index 397886e..a9bfb84 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -564,7 +564,8 @@ actor SDLContextActor { SDLLogger.shared.log("[SDLContext] get invalid arp packet", level: .debug) } case .ipv4: - guard let ipPacket = IPPacket(layerPacket.data), ipPacket.header.destination == networkAddr.ip else { + // 有数据是通过出口网关转发的,所有只判断是合法的ip包 + guard let ipPacket = IPPacket(layerPacket.data) else { return } @@ -676,19 +677,37 @@ actor SDLContextActor { SDLLogger.shared.log("[SDLContext] flow_session: \(flowSession)", level: .debug) } - // 查找arp缓存中是否有目标mac地址 - if let dstMac = await self.arpServer.query(ip: dstIp) { - SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()), dst_mac is: \(SDLUtil.formatMacAddress(mac: dstMac))", level: .debug) - await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data) + // 判断目标地址是否和当前的网络地址是否在同一个网段 + // 只有在同一个网段的ip数据才直接发送 + if SDLUtil.inSameNetwork(ip: dstIp, compareIp: networkAddr.ip, maskLen: networkAddr.maskLen) { + // 查找arp缓存中是否有目标mac地址 + if let dstMac = await self.arpServer.query(ip: dstIp) { + SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()), dst_mac is: \(SDLUtil.formatMacAddress(mac: dstMac))", level: .debug) + await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data) + } + else { + SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()) arp query not found, broadcast", level: .debug) + // // 构造arp广播 + // let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp) + // await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal()) + + try? await self.arpServer.arpRequest(targetIp: dstIp, use: self.quicClient) + } } + // 不在同一个网段的数据,看到是否配置了网络出口 + // 如果配置了,转发数据个网络出口,否则丢弃 else { - SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()) arp query not found, broadcast", level: .debug) - // // 构造arp广播 - // let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp) - // await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal()) - - try? await self.arpServer.arpRequest(targetIp: dstIp, use: self.quicClient) + let exitNodeIp: UInt32 = 1234 + SDLLogger.shared.log("[SDLContext] global dstIp: \(packet.header.destination.asIpAddress())", level: .debug) + // 查找arp缓存中是否有目标mac地址 + if let dstMac = await self.arpServer.query(ip: exitNodeIp) { + await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data) + } + else { + try? await self.arpServer.arpRequest(targetIp: exitNodeIp, use: self.quicClient) + } } + } private func routeLayerPacket(dstMac: Data, type: LayerPacket.PacketType, data: Data) async { @@ -739,6 +758,9 @@ actor SDLContextActor { let routes: [NEIPv4Route] = [ NEIPv4Route(destinationAddress: networkAddress.netAddress, subnetMask: networkAddress.maskAddress), NEIPv4Route(destinationAddress: dnsServer, subnetMask: "255.255.255.255"), + + // TODO测试代码 + NEIPv4Route(destinationAddress: "172.16.1.0", subnetMask: "255.255.255.0"), ] // Add code here to start the process of connecting the tunnel.