腾讯云的Windows服务器防火墙,通过设置陷阱端口,屏蔽恶意访问的IP,将IP加入到腾讯云的安全组规则中,同时加入到服务器的防火墙中
unknown
2022-06-29 00ebde85aa3d586be2d2dfb755d01bf1b4b3025b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using FirewallWindowsServer.Methods;
using FirewallWindowsServer.Model;
using System;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
 
namespace FirewallWindowsServer
{
    public partial class Service1 : ServiceBase
    {
        private Task t;
 
        public Service1()
        {
            InitializeComponent();
            t = new Task(() => {
                Start();
            });
        }
 
        protected override void OnStart(string[] args)
        {
            t.Start();
        }
 
        protected override void OnStop()
        {
            t.Dispose();
        }
 
        private void Start()
        {
            Console.WriteLine("Hello, World!");
            var config = Tool.GetSystemConfig<Config>("Config.json");
            if (config.Ports == null)
            {
                Console.WriteLine("配置读取错误");
                return;
            }
            ListenPort lp = new ListenPort();
            Console.WriteLine("等待连接...");
            var ports = config.Ports;
 
            foreach (var a in ports)
            {
                if (a.IndexOf("-") > -1)
                {
                    var left = int.Parse(a.Split('-')[0]);
                    var right = int.Parse(a.Split('-')[1]);
 
                    for (int i = left; i < right; i++)
                    {
                        new Thread(() =>
                        {
                            lp.InitListen(i);
                        }).Start();
                        Task.Delay(50).Wait();
                    }
                }
                else
                {
                    new Thread(() =>
                    {
                        lp.InitListen(int.Parse(a));
                    }).Start();
                    Task.Delay(50).Wait();
                }
 
            }
 
            int dicNumber = 0;
            while (true)
            {
                if (lp.sckdic.Count != dicNumber)
                {
                    dicNumber = lp.sckdic.Count;
                    if (lp.sckdic.Count == 0) { continue; }
                    System.Net.Sockets.Socket sk = lp.sckdic.Last().Value;
                    string guid = lp.sckdic.Last().Key;
                    Console.WriteLine("【{0}】已连接", guid);
                    lp.ReceiveMsg(sk, guid);
                }
                Task.Delay(500).Wait();  //间隔500ms监听一次,避免无意义的占用资源
            }
        }
    }
}