public int UdpReceiveFrom(EndPoint remoteEP, byte[] request, int requestOffset, int requestCount, byte[] response, int responseOffset = 0, int timeout = 10000) { switch (_type) { case NetProxyType.Socks5: if (_viaProxy != null) { throw new NotSupportedException("Cannot chain proxies for SOCKS5 Udp protocol."); } using (SocksUdpAssociateRequestHandler proxyUdpRequestHandler = _socksProxy.UdpAssociate(timeout)) { proxyUdpRequestHandler.ReceiveTimeout = timeout; //send request proxyUdpRequestHandler.SendTo(request, requestOffset, requestCount, remoteEP); //receive request return(proxyUdpRequestHandler.ReceiveFrom(response, responseOffset, response.Length - responseOffset, out EndPoint ep)); } default: throw new NotSupportedException("Proxy type not supported."); } }
public int UdpReceiveFrom(EndPoint remoteEP, byte[] request, int requestOffset, int requestSize, byte[] response, int responseOffset, int timeout = 10000) { if (IsProxyBypassed(remoteEP)) { IPEndPoint hostEP = remoteEP.GetIPEndPoint(); using (Socket socket = new Socket(hostEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp)) { socket.ReceiveTimeout = timeout; //send request socket.SendTo(request, requestOffset, requestSize, SocketFlags.None, hostEP); //receive request EndPoint ep; if (hostEP.AddressFamily == AddressFamily.InterNetworkV6) { ep = new IPEndPoint(IPAddress.IPv6Any, 0); } else { ep = new IPEndPoint(IPAddress.Any, 0); } int bytesReceived; do { bytesReceived = socket.ReceiveFrom(response, responseOffset, response.Length, SocketFlags.None, ref ep); }while (!hostEP.Equals(ep)); return(bytesReceived); } } switch (_type) { case NetProxyType.Socks5: if (_viaProxy != null) { throw new NotSupportedException("Cannot chain proxies for SOCKS5 Udp protocol."); } using (SocksUdpAssociateRequestHandler proxyUdpRequestHandler = _socksProxy.UdpAssociate(timeout)) { proxyUdpRequestHandler.ReceiveTimeout = timeout; //send request proxyUdpRequestHandler.SendTo(request, requestOffset, requestSize, remoteEP); //receive request return(proxyUdpRequestHandler.ReceiveFrom(response, responseOffset, response.Length - responseOffset, out EndPoint ep)); } default: throw new NotSupportedException("Proxy type not supported."); } }