Last active
January 19, 2026 09:41
-
-
Save rikka0w0/2db9df5987f0d4c9ecce5884d9e73ee6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Add-Type -TypeDefinition @" | |
| using System; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| using System.Threading; | |
| public static class UDPForward | |
| { | |
| public static void Main(string[] args) | |
| { | |
| Console.WriteLine("Source: https://gitee.com/Rikka0w0/l4d2_ipv6"); | |
| Console.WriteLine("UDP forwarder, enables IPv4 UDP to IPv6 UDP forwarding."); | |
| Console.WriteLine("UDP转发器, 可实现纯IPv4 UDP客户端连接IPv6服务器."); | |
| // Default settings 默认设置 | |
| int localPort = 27014; | |
| string remoteHost = "localhost"; | |
| int remotePort = 27015; | |
| // Parse from args | |
| if (args.Length >= 3) { | |
| localPort = Int32.Parse(args[0]); | |
| remoteHost = args[1]; | |
| remotePort = Int32.Parse(args[2]); | |
| } else if (args.Length != 0) { | |
| Console.WriteLine("Invalid argument. The expected format <LocalPort>:<RemoteHost>:<RemotePort>."); | |
| Console.WriteLine("参数无效. 正确格式为 <本地监听端口> <服务器地址> <服务器端口>."); | |
| return; | |
| } | |
| // Define the local IPv4 listening endpoint | |
| IPAddress localIPAddress = IPAddress.Any; | |
| IPEndPoint localEndpoint = new IPEndPoint(localIPAddress, localPort); | |
| // Resolve the remote domain name to an IPv6 address | |
| IPAddress[] remoteIPAddresses = Dns.GetHostAddresses(remoteHost); | |
| IPEndPoint remoteEndpoint = new IPEndPoint(remoteIPAddresses[0], remotePort); | |
| // Create a UDP client socket to listen for incoming IPv4 traffic | |
| UdpClient udpClient = new UdpClient(localEndpoint); | |
| // Create a UDP client socket with IPv6 address family to send data to the IPv6 host | |
| UdpClient udpClientRemote = new UdpClient(remotePort, AddressFamily.InterNetworkV6); | |
| udpClientRemote.Connect(remoteEndpoint); | |
| // Start listening for incoming IPv4 traffic | |
| Console.WriteLine("Listening for incoming UDP IPv4 traffic on local port " + localEndpoint.Port + " and forward it to:" + remoteHost + ":" + remotePort); | |
| Console.WriteLine("开启UDP转发:从本地" + localPort + "到远程" + remoteHost + ":" + remotePort); | |
| // Start a thread for receiving IPv4 traffic and forwarding it | |
| Thread receiveAndForwardThread = new Thread(() => | |
| { | |
| while (true) | |
| { | |
| // Receive data from the IPv4 client | |
| byte[] receivedData = udpClient.Receive(ref localEndpoint); | |
| // Send the received data to the resolved IPv6 address | |
| udpClientRemote.Send(receivedData, receivedData.Length); | |
| } | |
| }); | |
| // Start a thread for receiving IPv6 traffic | |
| Thread receiveThreadRemote = new Thread(() => | |
| { | |
| while (true) | |
| { | |
| // Receive data from the IPv6 server | |
| byte[] receivedDataRemote = udpClientRemote.Receive(ref remoteEndpoint); | |
| // Forward the received data to the local IPv4 client | |
| udpClient.Send(receivedDataRemote, receivedDataRemote.Length, localEndpoint); | |
| } | |
| }); | |
| // Start both threads | |
| receiveAndForwardThread.Start(); | |
| receiveThreadRemote.Start(); | |
| // Wait for threads to finish (in this case, indefinitely) | |
| receiveAndForwardThread.Join(); | |
| receiveThreadRemote.Join(); | |
| } | |
| } | |
| "@; [UDPForward]::Main(@('27014','2408:8207:24c6:82f0::3','27014')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment