示例#1
0
 private EndPoint ConvertChannelIdToEp(BinaryNumber channelId)
 {
     using (MemoryStream mS = new MemoryStream(channelId.Value, false))
     {
         return(EndPointExtension.Parse(new BinaryReader(mS)));
     }
 }
示例#2
0
            private async Task DoConnectAsync(NetworkStream localStream, HttpRequest httpRequest)
            {
                string host;
                int    port;

                {
                    string[] parts = httpRequest.RequestPath.Split(':');

                    host = parts[0];

                    if (parts.Length > 1)
                    {
                        port = int.Parse(parts[1]);
                    }
                    else
                    {
                        port = 80;
                    }
                }

                //connect to remote server
                _remoteSocket = await _connectionManager.ConnectAsync(EndPointExtension.GetEndPoint(host, port));

                //signal client 200 OK
                await localStream.WriteAsync(Encoding.ASCII.GetBytes(httpRequest.Protocol + " 200 OK\r\nConnection: close\r\n\r\n"));

                //pipe sockets
                _ = _localSocket.CopyToAsync(_remoteSocket).ContinueWith(delegate(Task prevTask) { Dispose(); });
                _ = _remoteSocket.CopyToAsync(_localSocket).ContinueWith(delegate(Task prevTask) { Dispose(); });
            }
示例#3
0
        public DhtManager(int localServicePort, IDhtConnectionManager connectionManager, NetProxy proxy, IEnumerable <EndPoint> ipv4BootstrapNodes, IEnumerable <EndPoint> ipv6BootstrapNodes, IEnumerable <EndPoint> torBootstrapNodes, string torOnionAddress, bool enableTorMode)
        {
            _localServicePort = localServicePort;

            //init internet dht nodes
            _ipv4InternetDhtNode = new DhtNode(connectionManager, new IPEndPoint(IPAddress.Any, localServicePort));
            _ipv6InternetDhtNode = new DhtNode(connectionManager, new IPEndPoint(IPAddress.IPv6Any, localServicePort));

            //add known bootstrap nodes
            _ipv4InternetDhtNode.AddNode(ipv4BootstrapNodes);
            _ipv6InternetDhtNode.AddNode(ipv6BootstrapNodes);

            if (enableTorMode)
            {
                //init tor dht node
                _torInternetDhtNode = new DhtNode(connectionManager, new DomainEndPoint(torOnionAddress, localServicePort));

                //add known bootstrap nodes
                _torInternetDhtNode.AddNode(torBootstrapNodes);

                //set higher timeout value for internet and tor DHT nodes since they will be using tor network
                _ipv4InternetDhtNode.QueryTimeout = 10000;
                _ipv6InternetDhtNode.QueryTimeout = 10000;
                _torInternetDhtNode.QueryTimeout  = 10000;
            }
            else
            {
                //start network watcher
                _networkWatcher = new Timer(NetworkWatcherAsync, null, 1000, NETWORK_WATCHER_INTERVAL);
            }

            //add bootstrap nodes via web
            _bootstrapRetryTimer = new Timer(delegate(object state)
            {
                try
                {
                    using (WebClientEx wC = new WebClientEx())
                    {
                        wC.Proxy   = proxy;
                        wC.Timeout = 10000;

                        using (BinaryReader bR = new BinaryReader(new MemoryStream(wC.DownloadData(DHT_BOOTSTRAP_URL))))
                        {
                            int count = bR.ReadByte();
                            for (int i = 0; i < count; i++)
                            {
                                AddNode(EndPointExtension.Parse(bR));
                            }
                        }
                    }

                    //bootstrap success, stop retry timer
                    _bootstrapRetryTimer.Dispose();
                }
                catch (Exception ex)
                {
                    Debug.Write(this.GetType().Name, ex);
                }
            }, null, BOOTSTRAP_RETRY_TIMER_INITIAL_INTERVAL, BOOTSTRAP_RETRY_TIMER_INTERVAL);
        }
示例#4
0
        public static NetProxy CreateProxy(NetProxyType type, string address, int port, NetworkCredential credential = null)
        {
            switch (type)
            {
            case NetProxyType.Http:
                return(new HttpProxy(EndPointExtension.GetEndPoint(address, port), credential));

            case NetProxyType.Socks5:
                return(new SocksProxy(EndPointExtension.GetEndPoint(address, port), credential));

            default:
                throw new NotSupportedException("Proxy type not supported.");
            }
        }
        public NameServerAddress(BinaryReader bR)
        {
            switch (bR.ReadByte())
            {
            case 1:
                if (bR.ReadBoolean())
                {
                    _dohEndPoint = new Uri(bR.ReadShortString());
                }

                if (bR.ReadBoolean())
                {
                    _domainEndPoint = EndPointExtension.Parse(bR) as DomainEndPoint;
                }

                if (bR.ReadBoolean())
                {
                    _ipEndPoint = EndPointExtension.Parse(bR) as IPEndPoint;
                }

                if (_dohEndPoint != null)
                {
                    _originalAddress = _dohEndPoint.AbsoluteUri;
                }
                else if (_ipEndPoint != null)
                {
                    _originalAddress = _ipEndPoint.ToString();
                }
                else if (_domainEndPoint != null)
                {
                    _originalAddress = _domainEndPoint.ToString();
                }

                GuessProtocol();
                break;

            case 2:
                Parse(bR.ReadShortString());
                GuessProtocol();
                break;

            case 3:
                _protocol = (DnsTransportProtocol)bR.ReadByte();
                Parse(bR.ReadShortString());
                break;

            default:
                throw new InvalidDataException("NameServerAddress version not supported");
            }
        }
示例#6
0
        public MeshNetworkPeerInfo(BinaryReader bR)
        {
            _peerUserId = new BinaryNumber(bR.BaseStream);

            _peerName = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadByte()));
            if (_peerName == "")
            {
                _peerName = null;
            }

            {
                _peerEPs = new EndPoint[bR.ReadByte()];

                for (int i = 0; i < _peerEPs.Length; i++)
                {
                    _peerEPs[i] = EndPointExtension.Parse(bR);
                }
            }
        }
示例#7
0
        public static NetProxy CreateSystemHttpProxy()
        {
            IWebProxy proxy = WebRequest.DefaultWebProxy;

            if (proxy == null)
            {
                return(null); //no proxy configured
            }
            Uri testUri = new Uri("https://www.google.com/");

            if (proxy.IsBypassed(testUri))
            {
                return(null); //no proxy configured
            }
            Uri proxyAddress = proxy.GetProxy(testUri);

            if (proxyAddress.Equals(testUri))
            {
                return(null); //no proxy configured
            }
            return(new HttpProxy(EndPointExtension.GetEndPoint(proxyAddress.Host, proxyAddress.Port), proxy.Credentials.GetCredential(proxyAddress, "BASIC")));
        }
示例#8
0
        private void ReadFrameAsync()
        {
            try
            {
                //frame parameters
                int          signal;
                BinaryNumber channelId        = new BinaryNumber(new byte[32]);
                byte[]       dataLengthBuffer = new byte[2];
                OffsetStream dataStream       = new OffsetStream(_baseStream, 0, 0, true, false);

                while (true)
                {
                    #region read frame from base stream

                    //read frame signal
                    signal = _baseStream.ReadByte();
                    if (signal == -1)
                    {
                        return; //End of stream
                    }
                    //read channel id
                    _baseStream.ReadBytes(channelId.Value, 0, 32);

                    //read data length
                    _baseStream.ReadBytes(dataLengthBuffer, 0, 2);
                    dataStream.Reset(0, BitConverter.ToUInt16(dataLengthBuffer, 0), 0);

                    #endregion

                    switch ((ConnectionSignal)signal)
                    {
                    case ConnectionSignal.PingRequest:
                        WriteFrame(ConnectionSignal.PingResponse, channelId, null, 0, 0);
                        break;

                    case ConnectionSignal.PingResponse:
                        //do nothing!
                        break;

                    case ConnectionSignal.ConnectChannelMeshNetwork:
                        #region ConnectChannelMeshNetwork

                        lock (_channels)
                        {
                            if (_channels.ContainsKey(channelId))
                            {
                                WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0);
                            }
                            else
                            {
                                ChannelStream channel = new ChannelStream(this, channelId.Clone());
                                _channels.Add(channel.ChannelId, channel);

                                ThreadPool.QueueUserWorkItem(delegate(object state)
                                {
                                    try
                                    {
                                        //done async since the call is blocking and will block the current read thread which can cause DOS
                                        _connectionManager.Node.MeshNetworkRequest(this, channel.ChannelId, channel);
                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.Write(this.GetType().Name, ex);

                                        channel.Dispose();
                                    }
                                });
                            }
                        }

                        //check if tcp relay is hosted for the channel. reply back tcp relay peers list if available
                        Connection[] connections = _connectionManager.GetTcpRelayServerHostedNetworkConnections(channelId);

                        if (connections.Length > 0)
                        {
                            int count = connections.Length;

                            for (int i = 0; i < connections.Length; i++)
                            {
                                if (connections[i].RemotePeerEP.Equals(_remotePeerEP))
                                {
                                    connections[i] = null;
                                    count--;
                                    break;
                                }
                            }

                            using (MemoryStream mS = new MemoryStream(128))
                            {
                                BinaryWriter bW = new BinaryWriter(mS);

                                bW.Write(Convert.ToByte(count));

                                foreach (Connection connection in connections)
                                {
                                    if (connection != null)
                                    {
                                        connection.RemotePeerEP.WriteTo(bW);
                                    }
                                }

                                byte[] data = mS.ToArray();

                                WriteFrame(ConnectionSignal.MeshNetworkPeers, channelId, data, 0, data.Length);
                            }
                        }

                        #endregion
                        break;

                    case ConnectionSignal.ChannelData:
                        #region ChannelData

                        try
                        {
                            ChannelStream channel = null;

                            lock (_channels)
                            {
                                channel = _channels[channelId];
                            }

                            channel.FeedReadBuffer(dataStream, _channelWriteTimeout);
                        }
                        catch
                        { }

                        #endregion
                        break;

                    case ConnectionSignal.DisconnectChannel:
                        #region DisconnectChannel

                        try
                        {
                            ChannelStream channel;

                            lock (_channels)
                            {
                                channel = _channels[channelId];
                                _channels.Remove(channelId);
                            }

                            channel.SetDisconnected();
                            channel.Dispose();
                        }
                        catch
                        { }

                        #endregion
                        break;

                    case ConnectionSignal.ConnectChannelTunnel:
                        #region ConnectChannelTunnel

                        if (IsStreamVirtualConnection(_baseStream))
                        {
                            //nesting virtual connections not allowed
                            WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0);
                        }
                        else
                        {
                            ChannelStream remoteChannel1 = null;

                            lock (_channels)
                            {
                                if (_channels.ContainsKey(channelId))
                                {
                                    WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0);
                                }
                                else
                                {
                                    //add first stream into list
                                    remoteChannel1 = new ChannelStream(this, channelId.Clone());
                                    _channels.Add(remoteChannel1.ChannelId, remoteChannel1);
                                }
                            }

                            if (remoteChannel1 != null)
                            {
                                EndPoint   tunnelToRemotePeerEP = ConvertChannelIdToEp(channelId);                                //get remote peer ep
                                Connection remotePeerConnection = _connectionManager.GetExistingConnection(tunnelToRemotePeerEP); //get remote channel service

                                if (remotePeerConnection == null)
                                {
                                    remoteChannel1.Dispose();
                                }
                                else
                                {
                                    try
                                    {
                                        //get remote proxy connection channel stream
                                        ChannelStream remoteChannel2 = remotePeerConnection.MakeVirtualConnection(_remotePeerEP);

                                        //join current and remote stream
                                        Joint joint = new Joint(remoteChannel1, remoteChannel2);

                                        joint.Disposed += delegate(object sender, EventArgs e)
                                        {
                                            lock (_tunnelJointList)
                                            {
                                                _tunnelJointList.Remove(sender as Joint);
                                            }
                                        };

                                        lock (_tunnelJointList)
                                        {
                                            _tunnelJointList.Add(joint);
                                        }

                                        joint.Start();
                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.Write(this.GetType().Name, ex);

                                        remoteChannel1.Dispose();
                                    }
                                }
                            }
                        }

                        #endregion
                        break;

                    case ConnectionSignal.ConnectChannelVirtualConnection:
                        #region ConnectChannelVirtualConnection

                        if (IsStreamVirtualConnection(_baseStream))
                        {
                            //nesting virtual connections not allowed
                            WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0);
                        }
                        else
                        {
                            lock (_channels)
                            {
                                if (_channels.ContainsKey(channelId))
                                {
                                    WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0);
                                }
                                else
                                {
                                    //add proxy channel stream into list
                                    ChannelStream channel = new ChannelStream(this, channelId.Clone());
                                    _channels.Add(channel.ChannelId, channel);

                                    //pass channel as connection async
                                    ThreadPool.QueueUserWorkItem(delegate(object state)
                                    {
                                        try
                                        {
                                            _connectionManager.AcceptConnectionInitiateProtocol(channel, ConvertChannelIdToEp(channel.ChannelId));
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.Write(this.GetType().Name, ex);

                                            channel.Dispose();
                                        }
                                    });
                                }
                            }
                        }

                        #endregion
                        break;

                    case ConnectionSignal.TcpRelayServerRegisterHostedNetwork:
                        #region TcpRelayServerRegisterHostedNetwork

                        _connectionManager.TcpRelayServerRegisterHostedNetwork(this, channelId.Clone());

                        _tcpRelayServerModeEnabled = true;

                        #endregion
                        break;

                    case ConnectionSignal.TcpRelayServerUnregisterHostedNetwork:
                        #region TcpRelayServerUnregisterHostedNetwork

                        _connectionManager.TcpRelayServerUnregisterHostedNetwork(this, channelId);

                        #endregion
                        break;

                    case ConnectionSignal.MeshNetworkPeers:
                        #region MeshNetworkPeers
                    {
                        BinaryReader bR = new BinaryReader(dataStream);

                        int             count   = bR.ReadByte();
                        List <EndPoint> peerEPs = new List <EndPoint>(count);

                        for (int i = 0; i < count; i++)
                        {
                            peerEPs.Add(EndPointExtension.Parse(bR));
                        }

                        _connectionManager.Node.ReceivedMeshNetworkPeersViaTcpRelay(this, channelId, peerEPs);
                    }
                        #endregion
                        break;

                    default:
                        throw new IOException("Invalid frame signal.");
                    }

                    //discard any unread data
                    if (dataStream.Length > dataStream.Position)
                    {
                        dataStream.CopyTo(Stream.Null, 1024, Convert.ToInt32(dataStream.Length - dataStream.Position));
                    }
                }
            }
            catch (ThreadAbortException)
            {
                //stopping
            }
            catch (Exception ex)
            {
                Debug.Write(this.GetType().Name, ex);
            }
            finally
            {
                Dispose();
            }
        }
示例#9
0
 public NodeContact(BinaryReader bR)
 {
     _nodeEP = EndPointExtension.Parse(bR);
     _nodeId = GetNodeId(_nodeEP);
 }
示例#10
0
            public async Task StartAsync()
            {
                bool dontDispose = false;

                try
                {
                    NetworkStream localStream  = new NetworkStream(_localSocket);
                    Stream        remoteStream = null;

                    string lastHost = null;
                    int    lastPort = 0;

                    while (true)
                    {
                        HttpRequest httpRequest;
                        {
                            Task <HttpRequest> task = HttpRequest.ReadRequestAsync(localStream);

                            using (CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource())
                            {
                                if (await Task.WhenAny(task, Task.Delay(CLIENT_REQUEST_TIMEOUT, timeoutCancellationTokenSource.Token)) != task)
                                {
                                    return;                              //request timed out
                                }
                                timeoutCancellationTokenSource.Cancel(); //cancel delay task
                            }

                            httpRequest = await task;
                        }

                        if (httpRequest == null)
                        {
                            return; //connection closed gracefully by client
                        }
                        if (_authenticationManager != null)
                        {
                            string proxyAuth = httpRequest.Headers[HttpRequestHeader.ProxyAuthorization];
                            if (string.IsNullOrEmpty(proxyAuth))
                            {
                                await SendResponseAsync(407, "<h1>Proxy Authentication Required</h1>");

                                return;
                            }

                            string username;
                            string password;
                            {
                                string[] parts = proxyAuth.Split(new char[] { ' ' }, 2);

                                if (!parts[0].Equals("BASIC", StringComparison.OrdinalIgnoreCase) || (parts.Length < 2))
                                {
                                    await SendResponseAsync(407, "<h1>Proxy Authentication Required</h1><p>Proxy authentication method is not supported.</p>");

                                    return;
                                }

                                string[] credParts = Encoding.ASCII.GetString(Convert.FromBase64String(parts[1])).Split(new char[] { ':' }, 2);
                                if (credParts.Length != 2)
                                {
                                    await SendResponseAsync(407, "<h1>Proxy Authentication Required</h1><p>Proxy authentication method is not supported.</p>");

                                    return;
                                }

                                username = credParts[0];
                                password = credParts[1];
                            }

                            if (!_authenticationManager.Authenticate(username, password))
                            {
                                await SendResponseAsync(407, "<h1>Proxy Authentication Required</h1><p>Invalid username or password.</p>");

                                return;
                            }
                        }

                        if (httpRequest.HttpMethod.Equals("CONNECT", StringComparison.OrdinalIgnoreCase))
                        {
                            await DoConnectAsync(localStream, httpRequest);

                            dontDispose = true;
                            break;
                        }
                        else
                        {
                            #region connect to remote server

                            string host;
                            int    port;
                            string requestPathAndQuery;

                            if (Uri.TryCreate(httpRequest.RequestPathAndQuery, UriKind.Absolute, out Uri requestUri))
                            {
                                host = requestUri.Host;
                                port = requestUri.Port;
                                requestPathAndQuery = requestUri.PathAndQuery;
                            }
                            else
                            {
                                string hostHeader = httpRequest.Headers[HttpRequestHeader.Host];
                                if (string.IsNullOrEmpty(hostHeader))
                                {
                                    throw new HttpProxyServerException("Invalid proxy request.");
                                }

                                string[] parts = hostHeader.Split(':');

                                host = parts[0];

                                if (parts.Length > 1)
                                {
                                    port = int.Parse(parts[1]);
                                }
                                else
                                {
                                    port = 80;
                                }

                                requestPathAndQuery = httpRequest.RequestPathAndQuery;
                            }

                            if (!host.Equals(lastHost) || port != lastPort || !_remoteSocket.Connected)
                            {
                                if (_remoteSocket != null)
                                {
                                    if (_remoteSocket.Connected)
                                    {
                                        try
                                        {
                                            _remoteSocket.Shutdown(SocketShutdown.Both);
                                        }
                                        catch
                                        { }
                                    }

                                    _remoteSocket.Dispose();
                                }

                                _remoteSocket = await _connectionManager.ConnectAsync(EndPointExtension.GetEndPoint(host, port));

                                remoteStream = new WriteBufferedStream(new NetworkStream(_remoteSocket), 512);

                                lastHost = host;
                                lastPort = port;

                                //pipe response stream
                                _ = _remoteSocket.CopyToAsync(_localSocket).ContinueWith(delegate(Task prevTask) { Dispose(); });
                            }

                            #endregion

                            #region relay client request to server

                            foreach (string header in httpRequest.Headers.AllKeys)
                            {
                                if (header.StartsWith("Proxy-", StringComparison.OrdinalIgnoreCase))
                                {
                                    httpRequest.Headers.Remove(header);
                                }
                            }

                            await remoteStream.WriteAsync(Encoding.ASCII.GetBytes(httpRequest.HttpMethod + " " + requestPathAndQuery + " " + httpRequest.Protocol + "\r\n"));

                            await remoteStream.WriteAsync(httpRequest.Headers.ToByteArray());

                            if (httpRequest.InputStream != null)
                            {
                                await httpRequest.InputStream.CopyToAsync(remoteStream);
                            }

                            await remoteStream.FlushAsync();

                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    await SendResponseAsync(ex);
                }
                finally
                {
                    if (!dontDispose)
                    {
                        Dispose();
                    }
                }
            }
示例#11
0
 public static NetProxy CreateHttpProxy(string address, int port = 8080, NetworkCredential credential = null)
 {
     return(new HttpProxy(EndPointExtension.GetEndPoint(address, port), credential));
 }
示例#12
0
        public new async Task SendMailAsync(MailMessage message)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SmtpClientEx");
            }

            if (DeliveryMethod == SmtpDeliveryMethod.Network)
            {
                if (string.IsNullOrEmpty(_host))
                {
                    if (_dnsClient == null)
                    {
                        _dnsClient = new DnsClient();
                    }

                    IReadOnlyList <string> mxServers = await _dnsClient.ResolveMXAsync(message.To[0].Host);

                    if (mxServers.Count > 0)
                    {
                        _host = mxServers[0];
                    }
                    else
                    {
                        _host = message.To[0].Host;
                    }

                    _port       = 25;
                    Credentials = null;
                }

                if (_proxy == null)
                {
                    if (_enableSslWrapper)
                    {
                        EndPoint remoteEP = EndPointExtension.GetEndPoint(_host, _port);

                        if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                        {
                            _tunnelProxy.Dispose();
                            _tunnelProxy = null;
                        }

                        if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                        {
                            IPEndPoint ep = await remoteEP.GetIPEndPointAsync();

                            Socket socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                            await socket.ConnectAsync(ep);

                            _tunnelProxy = new TunnelProxy(socket, remoteEP, _enableSslWrapper, _ignoreCertificateErrors);
                        }

                        base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                        base.Port = _tunnelProxy.TunnelEndPoint.Port;
                    }
                    else
                    {
                        base.Host = _host;
                        base.Port = _port;
                    }

                    await base.SendMailAsync(message);
                }
                else
                {
                    EndPoint remoteEP = EndPointExtension.GetEndPoint(_host, _port);

                    if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                    {
                        _tunnelProxy.Dispose();
                        _tunnelProxy = null;
                    }

                    if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                    {
                        _tunnelProxy = await _proxy.CreateTunnelProxyAsync(remoteEP, _enableSslWrapper, _ignoreCertificateErrors);
                    }

                    base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                    base.Port = _tunnelProxy.TunnelEndPoint.Port;

                    await base.SendMailAsync(message);
                }
            }
            else
            {
                await base.SendMailAsync(message);
            }
        }
示例#13
0
 public async Task <Socket> ConnectAsync(string address, int port)
 {
     return(await ConnectAsync(EndPointExtension.GetEndPoint(address, port)));
 }
示例#14
0
 public bool IsBypassed(Uri host)
 {
     return(IsBypassed(EndPointExtension.GetEndPoint(host.Host, host.Port)));
 }
示例#15
0
        public new async Task SendMailAsync(MailMessage message)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SmtpClientEx");
            }

            if (message.To.Count == 0)
            {
                throw new ArgumentException("Message does not contain receipent email address.");
            }

            if (DeliveryMethod == SmtpDeliveryMethod.Network)
            {
                string host = _host;

                if (string.IsNullOrEmpty(host))
                {
                    //resolve MX for the receipent domain using IDnsClient
                    if (_dnsClient == null)
                    {
                        _dnsClient = new DnsClient()
                        {
                            Proxy = _proxy
                        }
                    }
                    ;

                    IReadOnlyList <string> mxDomains = await Dns.DnsClient.ResolveMXAsync(_dnsClient, message.To[0].Host);

                    if (mxDomains.Count > 0)
                    {
                        host = mxDomains[0];
                    }
                    else
                    {
                        host = message.To[0].Host;
                    }

                    _port       = 25;
                    Credentials = null;
                }

                if (_proxy == null)
                {
                    if (_smtpOverTls)
                    {
                        EndPoint remoteEP = EndPointExtension.GetEndPoint(host, _port);

                        if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                        {
                            _tunnelProxy.Dispose();
                            _tunnelProxy = null;
                        }

                        if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                        {
                            _tunnelProxy = await TunnelProxy.CreateTunnelProxyAsync(remoteEP, true, _ignoreCertificateErrors);
                        }

                        base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                        base.Port = _tunnelProxy.TunnelEndPoint.Port;
                    }
                    else
                    {
                        base.Host = host;
                        base.Port = _port;
                    }

                    await base.SendMailAsync(message);
                }
                else
                {
                    EndPoint remoteEP = EndPointExtension.GetEndPoint(host, _port);

                    if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                    {
                        _tunnelProxy.Dispose();
                        _tunnelProxy = null;
                    }

                    if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                    {
                        _tunnelProxy = await _proxy.CreateTunnelProxyAsync(remoteEP, _smtpOverTls, _ignoreCertificateErrors);
                    }

                    base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                    base.Port = _tunnelProxy.TunnelEndPoint.Port;

                    await base.SendMailAsync(message);
                }
            }
            else
            {
                await base.SendMailAsync(message);
            }
        }
示例#16
0
        private void InitMeshNode(BinaryReader bR, TorController torController)
        {
            switch (bR.ReadByte()) //version
            {
            case 1:
                _type             = (MeshNodeType)bR.ReadByte();
                _privateKey       = bR.ReadBuffer();
                _supportedCiphers = (SecureChannelCipherSuite)bR.ReadByte();

                //
                _userId = new BinaryNumber(bR.BaseStream);

                //
                _localServicePort = bR.ReadUInt16();
                _downloadFolder   = bR.ReadShortString();

                //
                _profileDateModified  = bR.ReadDate();
                _profileDisplayName   = bR.ReadShortString();
                _profileStatus        = (MeshProfileStatus)bR.ReadByte();
                _profileStatusMessage = bR.ReadShortString();

                //
                _profileImageDateModified = bR.ReadDate();
                _profileDisplayImage      = bR.ReadBuffer();

                //
                _ipv4BootstrapDhtNodes = new EndPoint[bR.ReadInt32()];
                for (int i = 0; i < _ipv4BootstrapDhtNodes.Length; i++)
                {
                    _ipv4BootstrapDhtNodes[i] = EndPointExtension.Parse(bR);
                }

                _ipv6BootstrapDhtNodes = new EndPoint[bR.ReadInt32()];
                for (int i = 0; i < _ipv6BootstrapDhtNodes.Length; i++)
                {
                    _ipv6BootstrapDhtNodes[i] = EndPointExtension.Parse(bR);
                }

                _torBootstrapDhtNodes = new EndPoint[bR.ReadInt32()];
                for (int i = 0; i < _torBootstrapDhtNodes.Length; i++)
                {
                    _torBootstrapDhtNodes[i] = EndPointExtension.Parse(bR);
                }

                //
                _enableUPnP = bR.ReadBoolean();
                _allowInboundInvitations          = bR.ReadBoolean();
                _allowOnlyLocalInboundInvitations = bR.ReadBoolean();

                //
                if (bR.ReadBoolean())
                {
                    _proxy = new NetProxy((NetProxyType)bR.ReadByte(), bR.ReadShortString(), bR.ReadUInt16(), (bR.ReadBoolean() ? new NetworkCredential(bR.ReadShortString(), bR.ReadShortString()) : null));
                }

                //
                _appData = bR.ReadBuffer();

                //start connection manager
                _connectionManager = new ConnectionManager(this, torController);

                //
                int networkCount = bR.ReadInt32();

                for (int i = 0; i < networkCount; i++)
                {
                    MeshNetwork network = new MeshNetwork(_connectionManager, bR);
                    _networks.Add(network.NetworkId, network);
                }

                InitAnnounceTimer();
                break;

            default:
                throw new InvalidDataException("MeshNode format version not supported.");
            }
        }
示例#17
0
        public DhtRpcPacket(BinaryReader bR)
        {
            int version = bR.ReadByte();

            switch (version)
            {
            case 1:
                _sourceNodeEP = EndPointExtension.Parse(bR);
                _type         = (DhtRpcType)bR.ReadByte();

                switch (_type)
                {
                case DhtRpcType.PING:
                    break;

                case DhtRpcType.FIND_NODE:
                    _networkId = new BinaryNumber(bR.BaseStream);

                    _contacts = new NodeContact[bR.ReadByte()];
                    for (int i = 0; i < _contacts.Length; i++)
                    {
                        _contacts[i] = new NodeContact(bR);
                    }

                    break;

                case DhtRpcType.FIND_PEERS:
                    _networkId = new BinaryNumber(bR.BaseStream);

                    _contacts = new NodeContact[bR.ReadByte()];
                    for (int i = 0; i < _contacts.Length; i++)
                    {
                        _contacts[i] = new NodeContact(bR);
                    }

                    _peers = new EndPoint[bR.ReadByte()];
                    for (int i = 0; i < _peers.Length; i++)
                    {
                        _peers[i] = EndPointExtension.Parse(bR);
                    }

                    break;

                case DhtRpcType.ANNOUNCE_PEER:
                    _networkId = new BinaryNumber(bR.BaseStream);

                    _peers = new EndPoint[bR.ReadByte()];

                    for (int i = 0; i < _peers.Length; i++)
                    {
                        _peers[i] = EndPointExtension.Parse(bR);
                    }

                    break;

                default:
                    throw new IOException("Invalid DHT-RPC type.");
                }

                break;

            default:
                throw new InvalidDataException("DHT-RPC packet version not supported: " + version);
            }
        }
示例#18
0
 public Task <TunnelProxy> CreateTunnelProxyAsync(string address, int port, bool enableSsl = false, bool ignoreCertificateErrors = false)
 {
     return(CreateTunnelProxyAsync(EndPointExtension.GetEndPoint(address, port), enableSsl, ignoreCertificateErrors));
 }