Exemplo n.º 1
0
 public void Start(Node _destination, Node _serverNode)
 {
     destination = _destination;
     serverNode = _serverNode;
     destination.LastUpdate = Environment.TickCount;
     ThreadPool.QueueUserWorkItem(Process);
 }
Exemplo n.º 2
0
 public BrowserController(BrowserViewModel bvm, Model model, Node client, ShareInfoService i)
 {
     this.client = client;
     this.model = model;
     this.bvm = bvm;
     shareInfo = i;
     bvm.NoCache = model.AlwaysNoCacheBrowsing;
 }
Exemplo n.º 3
0
 public bool Execute(NetworkRequest req, Node destination, int timeout)
 {
     destination.LastUpdate = Environment.TickCount;
     var output = new NetworkRequest();
     if (!string.IsNullOrEmpty(destination.Secret) && string.IsNullOrEmpty(req.AuthKey))
         req.AuthKey = destination.Secret;
     return DoRequest(destination.Location, req, out output, timeout);
 }
Exemplo n.º 4
0
 public Model()
 {
     node = new Node();
     downloadQueue = new DownloadQueue();
     shutdownLock = new ReaderWriterLockSlim();
     uiTransferSession = new SafeObservingCollection<TransferSession>(transferSessions);
     uiDownloads = new SafeObservingCollection<TransferLog>(downloads);
     uiUploads = new SafeObservingCollection<TransferLog>(uploads);
 }
Exemplo n.º 5
0
 public bool ReceiveResponse(NetworkRequest r)
 {
     try
     {
         var inc = Deserialise<InfoVerb>(r.Data);
         Node = inc.Node;
         return true;
     }
     catch
     {
         return false;
     }
 }
Exemplo n.º 6
0
        public bool Execute(IVerb verb, Node destination, int timeout)
        {
            try
            {
                NetworkRequest request = verb.CreateRequest();
                var output = new NetworkRequest();
                destination.LastUpdate = Environment.TickCount;

                if (!string.IsNullOrEmpty(destination.Secret) && string.IsNullOrEmpty(request.AuthKey))
                    request.AuthKey = destination.Secret;

                if (!DoRequest(destination.Location, request, out output, timeout))
                    return false;

                if (!verb.ReceiveResponse(output))
                    return false;
                return true;
            }
            catch
            {
                return false;
            }
        }
Exemplo n.º 7
0
 public void CreateConversation(Node n)
 {
     ConversationViewModel search = viewModels.Where(c => c.Conversation.OtherParty == n).FirstOrDefault();
     if (null == search)
     {
         //New conversation
         var c = new Conversation();
         c.OtherParty = n;
         conversations.Add(c);
     }
     else
     {
         //Converstation already open for this person so just switch to it
         windowController.SwitchToTab(search);
     }
 }
Exemplo n.º 8
0
 public DownloadWorkerService(Node n, Model m, BufferService b)
 {
     remoteNode = n;
     model = m;
     bufferService = b;
 }
Exemplo n.º 9
0
 public Uplink(Node source, Node destination)
 {
     this.source = source;
     this.destination = destination;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Whilst connected to a network 
 /// </summary>
 public void CheckModelChanges()
 {
     if (model.Network.State == ConnectionState.Connected)
     {
         UpdateVerb verb = null;
         lock (sync)
         {
             var data = new Dictionary<string, string>();
             foreach (var entry in model.LocalNode.Data)
             {
                 if (transmitted.IsKeySet(entry.Key))
                 {
                     if (transmitted.GetData(entry.Key) != entry.Value)
                     {
                         data.Add(entry.Key, entry.Value);
                     }
                 }
                 else
                 {
                     data.Add(entry.Key, entry.Value);
                 }
             }
             //Data has changed, transmit the changes.
             if (data.Count > 0)
             {
                 verb = new UpdateVerb();
                 var n = new Node();
                 n.ID = model.LocalNode.ID;
                 foreach (var change in data)
                 {
                     n.SetData(change.Key, change.Value);
                     transmitted.SetData(change.Key, change.Value);
                 }
                 verb.Nodes.Add(n);
             }
         }
         if (null != verb)
         {
             var c = new Client(model.LocalNode);
             if (!c.Execute(verb, model.Network.Overlord))
                 model.Network.State = ConnectionState.Disconnected;
         }
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Scan the client machine for services such as HTTP or samba shares
        /// </summary>
        /// <param name="n"></param>
        private void ScanClient(Node n)
        {
            //Check for HTTP
            string webTitle = string.Empty;
            try
            {
                var wc = new WebClient();
                string html = wc.DownloadString("http://" + n.Host);

                if (!string.IsNullOrEmpty(html))
                {
                    webTitle = RegexEx.FindMatches("<title>.*</title>", html).FirstOrDefault();
                    if (null != webTitle && !string.IsNullOrEmpty(html) && webTitle.Length > 14)
                    {
                        webTitle = webTitle.Substring(7);
                        webTitle = webTitle.Substring(0, webTitle.Length - 8);
                    }
                }

                if (string.IsNullOrEmpty(webTitle))
                    webTitle = "Web";
            }
            catch
            {
            }

            //Check for FTP
            string ftp = string.Empty;
            try
            {
                var client = new TcpClient();
                client.Connect(n.Host, 21);
                ftp = "FTP";
                var sb = new StringBuilder();
                long start = Environment.TickCount + 3000;
                var data = new byte[20000];
                client.ReceiveBufferSize = data.Length;

                while (start > Environment.TickCount && client.Connected)
                {
                    if (client.GetStream().DataAvailable)
                    {
                        int length = client.GetStream().Read(data, 0, data.Length);
                        sb.Append(Encoding.ASCII.GetString(data, 0, length));
                    }
                    else
                    {
                        Thread.Sleep(50);
                    }
                }
                client.Close();

                string title = sb.ToString();
                if (!string.IsNullOrEmpty(title))
                    ftp = title;
                data = null;
            }
            catch
            {
            }

            //Check for samba shares

            string samba = string.Empty;
            try
            {
                ShareCollection shares = ShareCollection.GetShares(n.Host);
                var sb = new StringBuilder();
                foreach (SambaShare share in shares)
                {
                    if (share.IsFileSystem && share.ShareType == ShareType.Disk)
                    {
                        try
                        {
                            //Make sure its readable
                            DirectoryInfo[] Flds = share.Root.GetDirectories();
                            if (sb.Length > 0)
                                sb.Append("|");
                            sb.Append(share.NetName);
                        }
                        catch
                        {
                        }
                    }
                }
                samba = sb.ToString();
            }
            catch
            {
            }

            lock (sync)
            {
                //update clients and overlords
                var r = new Node();
                r.SetData("HTTP", webTitle.Replace("\n", "").Replace("\r", ""));
                r.SetData("FTP", ftp.Replace("\n", "").Replace("\r", ""));
                r.SetData("Shares", samba.Replace("\n", "").Replace("\r", ""));
                r.ID = n.ID;
                r.OverlordID = serverNode.ID;
                lock (sync)
                {
                    //Check the client is still connected..
                    if (connectedClientNodes.Where(nx => nx.Node.ID == r.ID).Count() > 0)
                    {
                        var verb = new UpdateVerb();
                        verb.Nodes.Add(r);
                        NetworkRequest req = verb.CreateRequest();
                        req.OverlordID = serverNode.ID;
                        SendToStandardClients(req);
                        //Dont updates about overlords to other overlords
                        if (n.NodeType != ClientType.Overlord)
                            SendToOverlordClients(req);
                    }
                }
                //Store info
                n.SetData("HTTP", webTitle.Replace("\n", "").Replace("\r", ""));
                n.SetData("FTP", ftp.Replace("\n", "").Replace("\r", ""));
                n.SetData("Shares", samba.Replace("\n", "").Replace("\r", ""));
            }
        }
Exemplo n.º 12
0
 public bool Execute(NetworkRequest req, Node destination)
 {
     return Execute(req, destination, 30000);
 }
Exemplo n.º 13
0
 public bool Execute(IVerb verb, Node destinationNode)
 {
     return Execute(verb, destinationNode, DEFAULT_TIMEOUT);
 }
Exemplo n.º 14
0
 public Client(Node _callingNode)
 {
     callingNode = _callingNode;
 }
Exemplo n.º 15
0
        public void Load()
        {
            lock (downloadQueue)
            {
                try
                {
                    if (File.Exists(DATA_FOLDER + saveLocation))
                    {
                        var saved = SafeLoad<Model>(saveLocation);

                        Shares.Clear();
                        Shares.AddRange(saved.Shares.OrderBy(s => s.Name).ToList());
                        Avatar = saved.Avatar;
                        Description = saved.Description;
                        Nickname = saved.Nickname;
                        DownloadFolder = saved.DownloadFolder;
                        MaxDownloads = saved.MaxDownloads;
                        MaxDownloadsPerUser = saved.MaxDownloadsPerUser;
                        MaxUploads = saved.MaxUploads;
                        MaxUploadsPerUser = saved.MaxUploadsPerUser;
                        DisableComparision = saved.DisableComparision;
                        LocalNode = saved.LocalNode;
                        AlwaysNoCacheBrowsing = saved.AlwaysNoCacheBrowsing;
                        OverlordPriority = saved.OverlordPriority;
                        DisplayedHelp = saved.DisplayedHelp;
                    }
                    else if (File.Exists(Legacy.Model.saveLocation))
                    {
                        //New config doesnt exist but an older version does, try to import.
                        var oldmodel = new Legacy.Model();
                        oldmodel.Load();

                        Shares.Clear();
                        Shares.AddRange(oldmodel.Shares.OrderBy(s => s.Name).ToList());
                        Avatar = oldmodel.Avatar;
                        Description = oldmodel.Description;
                        Nickname = oldmodel.Nickname;
                        DownloadFolder = oldmodel.DownloadFolder;
                        MaxDownloads = oldmodel.MaxDownloads;
                        MaxDownloadsPerUser = oldmodel.MaxDownloadsPerUser;
                        MaxUploads = oldmodel.MaxUploads;
                        MaxUploadsPerUser = oldmodel.MaxUploadsPerUser;
                        DisableComparision = oldmodel.DisableComparision;
                        LocalNode = new Node();
                        foreach (var data in oldmodel.Node.Data)
                            LocalNode.SetData(data.Key, data.Value);
                        AlwaysNoCacheBrowsing = oldmodel.AlwaysNoCacheBrowsing;
                        OverlordPriority = OverlordPriority.Normal;
                        Save();
                    }
                }
                catch (Exception e)
                {
                    LogManager.GetLogger("faplog").Warn("Failed to read config", e);
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Handle updates from local clients, external overlords and peers on external overlords from their overlord.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        private bool HandleUpdate(RequestEventArgs e, NetworkRequest req)
        {
            try
            {
                var verb = new UpdateVerb();
                verb.ProcessRequest(req);

                //Ignore updates about ourself
                if (verb.Nodes != null && verb.Nodes.Count == 1 && verb.Nodes[0].ID == serverNode.ID)
                {
                    SendResponse(e, null);
                    return true;
                }

                //Is the call from a local client?
                ClientStream localClient =
                    connectedClientNodes.ToList().Where(
                        n =>
                        n.Node.ID == req.SourceID && n.Node.Secret == req.AuthKey &&
                        n.Node.NodeType != ClientType.Overlord).FirstOrDefault();
                if (null != localClient)
                {
                    //Only allow updates about itself
                    Node client = verb.Nodes.Where(n => n.ID == localClient.Node.ID).FirstOrDefault();
                    if (null != client && verb.Nodes.Count == 1)
                    {
                        logger.Trace("Server got update from local client {0}", client.ID);
                        lock (sync)
                        {
                            //Copy to local store
                            foreach (var value in verb.Nodes[0].Data)
                                localClient.Node.SetData(value.Key, value.Value);

                            req.OverlordID = serverNode.ID;
                            //Retransmit
                            SendToOverlordClients(req);
                            SendToStandardClients(req);

                            //Has the client disconnected?
                            if (!localClient.Node.Online)
                            {
                                localClient.Kill();
                                connectedClientNodes.Remove(localClient);
                            }
                        }
                        SendResponse(e, null);
                        return true;
                    }
                }
                else
                {
                    //Is the update from an external overlord?
                    Uplink overlord =
                        extOverlordServers.ToList().Where(
                            n =>
                            n.Destination.ID == req.OverlordID && n.Destination.Secret == req.AuthKey &&
                            n.Destination.NodeType == ClientType.Overlord).FirstOrDefault();
                    if (null != overlord)
                    {
                        logger.Trace("Server got update from external overlord {0}", overlord.Destination.ID);
                        //Check each update
                        var nverb = new UpdateVerb();
                        foreach (Node update in verb.Nodes)
                        {
                            if (!string.IsNullOrEmpty(update.ID))
                            {
                                //Ignore updates about ourself
                                if (update.ID == serverNode.ID)
                                    continue;

                                lock (sync)
                                {
                                    //Is the update about the overlord itself?
                                    Uplink osearch =
                                        extOverlordServers.Where(
                                            o => o.Destination.ID == update.ID && o.Destination.Secret == req.AuthKey).
                                            FirstOrDefault();
                                    if (null != osearch)
                                    {
                                        logger.Trace("Server got update from external about itself: {0}",
                                                     osearch.Destination.ID);
                                        //Copy to local store
                                        foreach (var value in update.Data)
                                            osearch.Destination.SetData(value.Key, value.Value);
                                        //Retransmit changes
                                        nverb.Nodes.Add(update);

                                        //Overlord going offline
                                        if (!osearch.Destination.Online)
                                        {
                                            osearch.OnDisconnect -= uplink_OnDisconnect;
                                            osearch.Kill();

                                            //Remove associated external nodes
                                            foreach (Node enode in externalNodes.ToList())
                                            {
                                                if (enode.OverlordID == osearch.Destination.OverlordID)
                                                {
                                                    externalNodes.Remove(enode);
                                                    //Only signal disconnect is the node isnt a local node
                                                    //I.e. they connected locally without disconnecting externally.
                                                    ClientStream search =
                                                        connectedClientNodes.Where(n => n.Node.ID == enode.ID).
                                                            FirstOrDefault();
                                                    if (null == search)
                                                    {
                                                        //The node isn't connected locally, is it connected elsewhere externally?
                                                        Node bestExternal =
                                                            externalNodes.ToList().Where(n => n.ID == enode.ID).
                                                                OrderByDescending(n => n.LastUpdate).FirstOrDefault();
                                                        if (null != bestExternal)
                                                        {
                                                            //User has logged on elsewhere, update local clients of new details
                                                            nverb.Nodes.Add(bestExternal);
                                                        }
                                                        else
                                                        {
                                                            nverb.Nodes.Add(new Node {ID = enode.ID, Online = false});
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        logger.Trace("Server got update from external server about : {0}", update.ID);

                                        //Check to see if the external node is connected locally, if so then dont retransmit changes but store changes under the relevant object
                                        ClientStream localNode =
                                            connectedClientNodes.Where(n => n.Node.ID == update.ID).FirstOrDefault();

                                        //Update about an external node from an external overlord
                                        Node search =
                                            externalNodes.Where(n => n.ID == update.ID && n.OverlordID == req.OverlordID)
                                                .FirstOrDefault();
                                        if (null == search)
                                        {
                                            if (!string.IsNullOrEmpty(update.ID))
                                            {
                                                //New external node
                                                search = new Node();
                                                //Copy to local store
                                                foreach (var value in update.Data)
                                                    search.SetData(value.Key, value.Value);
                                                search.OverlordID = req.OverlordID;
                                                externalNodes.Add(search);
                                                //Dont retransmit external node as it is local
                                                if (null == localNode)
                                                    nverb.Nodes.Add(update);
                                            }
                                        }
                                        else
                                        {
                                            //Copy to local store
                                            foreach (var value in update.Data)
                                                search.SetData(value.Key, value.Value);
                                            //Has the external node changed to a different overlord?
                                            if (search.OverlordID != req.OverlordID)
                                            {
                                                search.OverlordID = req.OverlordID;
                                                update.OverlordID = req.OverlordID;
                                            }
                                            //Dont retransmit external node as it is local
                                            if (null == localNode)
                                            {
                                                //Retransmit changes
                                                nverb.Nodes.Add(update);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        //Only transmit external node info to local clients
                        if (nverb.Nodes.Count > 0)
                        {
                            NetworkRequest nreq = nverb.CreateRequest();
                            nreq.OverlordID = req.OverlordID;
                            SendToStandardClients(nreq);
                        }
                        SendResponse(e, null);
                        return true;
                    }
                }
            }
            catch
            {
            }
            logger.Debug("Server received an invalid update");
            SendError(e);
            return false;
        }
Exemplo n.º 17
0
 /// <summary>
 /// Called by either the server or an end client to decode incoming data
 /// </summary>
 /// <param name="r"></param>
 /// <returns></returns>
 public NetworkRequest ProcessRequest(NetworkRequest r)
 {
     var inc = Deserialise<InfoVerb>(r.Data);
     Node = inc.Node;
     return null;
 }