public async Task UpdateNodes()
        {
            PushMessageBase msg = new PushMessageBase()
            {
                Type      = PushMessageID.UpdateNodes,
                DataNodes = new NodeList()
            };
            var nodes = await Natfrp.Request <Natfrp.GetNodes>("get_nodes");

            lock (this)
            {
                Clear();
                foreach (var j in nodes.Data)
                {
                    this[j.Id] = new Node()
                    {
                        Id        = j.Id,
                        Name      = j.Name,
                        AcceptNew = j.AcceptNew
                    };
                }
                msg.DataNodes.Nodes.Add(Values);
            }
            Main.LogManager.Log(LogManager.CATEGORY_SERVICE_INFO, Tag, "节点信息同步成功");
            Main.Pipe.PushMessage(msg);
        }
Example #2
0
        public async Task UpdateTunnels(bool loadEnabled = false)
        {
            var tunnels = await Natfrp.Request <Natfrp.GetTunnels>("get_tunnels");

            lock (this)
            {
                bool changed = false;
                var  tmp     = new List <int>();
                foreach (var j in tunnels.Data)
                {
                    tmp.Add(j.Id);
                    if (!ContainsKey(j.Id))
                    {
                        changed    = true;
                        this[j.Id] = CreateFromApi(j);
                    }
                }
                var remove = Keys.Where(k => !tmp.Contains(k)).ToList();
                foreach (var k in remove)
                {
                    changed = true;
                    Remove(k);
                }
                if (changed)
                {
                    Main.LogManager.Log(1, "Service", "TunnelManager: 隧道列表同步成功");
                }
                if (loadEnabled && Properties.Settings.Default.EnabledTunnels != null)
                {
                    foreach (var i in Properties.Settings.Default.EnabledTunnels)
                    {
                        if (ContainsKey(i))
                        {
                            this[i].Enabled = true;
                        }
                    }
                }
            }
            Push();
        }
Example #3
0
        public async Task UpdateTunnels(bool loadEnabled = false)
        {
            var tunnels = await Natfrp.Request <Natfrp.GetTunnels>("get_tunnels");

            lock (this)
            {
                bool changed = false;

                // Add new tunnels
                var tmp = new List <int>();
                foreach (var j in tunnels.Data)
                {
                    tmp.Add(j.Id);
                    if (!ContainsKey(j.Id))
                    {
                        changed    = true;
                        this[j.Id] = CreateFromApi(j);
                    }
                }

                // Remove deleted tunnels
                var remove = Keys.Where(k => !tmp.Contains(k)).ToList();
                foreach (var k in remove)
                {
                    changed = true;
                    Remove(k);
                }

                // Update tunnel details and set the changed flag accordingly
                void update <T>(ref T dst, T src)
                {
                    if (Comparer <T> .Default.Compare(dst, src) != 0)
                    {
                        changed = true;
                        dst     = src;
                    }
                }

                foreach (var j in tunnels.Data)
                {
                    var r = this[j.Id];
                    update(ref r.Id, j.Id);
                    update(ref r.Node, j.Node);
                    update(ref r.Name, j.Name);
                    update(ref r.Type, j.Type);
                    update(ref r.Note, j.Note);
                    update(ref r.Description, j.Description);
                }

                if (changed)
                {
                    Main.LogManager.Log(LogManager.CATEGORY_SERVICE_INFO, Tag, "隧道列表已更新");
                }
                if (loadEnabled && Properties.Settings.Default.EnabledTunnels != null)
                {
                    foreach (var i in Properties.Settings.Default.EnabledTunnels)
                    {
                        if (ContainsKey(i))
                        {
                            this[i].Enabled = true;
                        }
                    }
                }
            }
            Push();
        }