| 660 | } |
| 661 | |
| 662 | handleUDP(segment, srcIP, dstIP) { |
| 663 | const srcPort = segment.readUInt16BE(0); |
| 664 | const dstPort = segment.readUInt16BE(2); |
| 665 | const payload = segment.subarray(8); |
| 666 | |
| 667 | // this.emit('debug', `[UDP] ${srcIP.join('.')}:${srcPort} -> ${dstIP.join('.')}:${dstPort} (${payload.length} bytes)`); |
| 668 | |
| 669 | // Intercept DHCP: Client sends from port 68 to port 67 |
| 670 | if (srcPort === DHCP_CLIENT_PORT && dstPort === DHCP_SERVER_PORT) { |
| 671 | this.handleDHCP(payload); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Send UDP via main thread (which has access to event loop for async responses) |
| 676 | if (this.netPort) { |
| 677 | const key = `UDP:${srcIP.join('.')}:${srcPort}:${dstIP.join('.')}:${dstPort}`; |
| 678 | // this.emit('debug', `[UDP NAT] Sending ${payload.length} bytes to ${dstIP.join('.')}:${dstPort} via main thread`); |
| 679 | |
| 680 | this.netPort.postMessage({ |
| 681 | type: 'udp-send', |
| 682 | key, |
| 683 | dstIP: dstIP.join('.'), |
| 684 | dstPort, |
| 685 | srcIP: srcIP.join('.'), |
| 686 | srcPort, |
| 687 | payload: Array.from(payload) |
| 688 | }); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | handleDHCP(data) { |
| 693 | if (data.length < 240) return; // Minimum DHCP packet size |