Ejemplo n.º 1
0
        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.");
            }
        }
Ejemplo n.º 2
0
        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.");
            }
        }
Ejemplo n.º 3
0
        public bool IsUdpAvailable()
        {
            if (_isUdpAvailableChecked)
            {
                return(_isUdpAvailable);
            }

            switch (_type)
            {
            case NetProxyType.Http:
                _isUdpAvailable = false;
                break;

            case NetProxyType.Socks5:
                SocksUdpAssociateRequestHandler udpHandler = null;

                try
                {
                    udpHandler = _socksProxy.UdpAssociate();

                    _isUdpAvailable = true;
                }
                catch (SocksClientException ex)
                {
                    if (ex.ReplyCode == SocksReplyCode.CommandNotSupported)
                    {
                        _isUdpAvailable = false;
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    if (udpHandler != null)
                    {
                        udpHandler.Dispose();
                    }
                }
                break;

            default:
                throw new NotSupportedException("Proxy type not supported.");
            }

            _isUdpAvailableChecked = true;

            return(_isUdpAvailable);
        }