腾讯云的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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using FirewallWindowsServer.Model;
using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
 
namespace FirewallWindowsServer.Methods
{
    public class ListenPort
    {
 
        FirewallManage fw = new FirewallManage();//防火墙实例
 
        public Dictionary<string, Socket> sckdic = new Dictionary<string, Socket>();  //连接列队
        string hostip = "";
        int count = 0;
 
        public ListenPort()
        {
            #region 获取ip地址
            var config = Tool.GetSystemConfig<Config>("Config.json");
            var localIpList = Dns.GetHostEntry(config.Host).AddressList;
            if (localIpList != null && localIpList.Count() > 0)
            {
                foreach (var a in localIpList)
                {
                    hostip = $"{a}";
                }
 
                if (!string.IsNullOrWhiteSpace(hostip))
                {
                    hostip = hostip.Remove(hostip.Length - 1);
                }
            }
            else 
            {
                hostip = $"{config.Host}";
            }
            #endregion
        }
 
        public void InitListen(int port)
        {
            IPAddress ipaddr = IPAddress.Parse(hostip);
            IPEndPoint IPEP = new IPEndPoint(ipaddr, port);
            Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sk.Bind(IPEP);
            sk.Listen(100);
            while (true)
            {
                Socket socket = sk.Accept();//fun;
                var guid = socket.RemoteEndPoint.ToString();
                sckdic.Add(guid, socket);
 
                Task.Delay(500).Wait();
            }
        }
 
        private Socket ListenFun(Socket sk)
        {
            Socket socket;
            sk.Listen(0);
            socket = sk.Accept();
            //await Task.Delay(1);
            return socket;
        }
 
        /// <summary>
        /// 消息接收监听
        /// </summary>
        /// <param name="sck"></param>
        /// <param name="guid"></param>
        public async void ReceiveMsg(Socket sck, string guid)
        {
            await Task.Delay(1);
            var res = await ReceiveMsgAwaitFun(sck, guid);
        }
        //收到消息
        private async Task<int> ReceiveMsgAwaitFun(Socket sck, string guid)
        {
            while (true)
            {
                try
                {
                    string recStr = "";
                    byte[] recByte = new byte[4096];
                    while (true)
                    {
                        int bytes = sck.Receive(recByte, recByte.Length, 0);
                        if (bytes > 0)
                        {
                            recStr = Encoding.UTF8.GetString(recByte, 0, bytes);
                            Command.PrintLog(recStr);
                            try
                            {
                                if (recStr.IndexOf("PROXY ") == 0)
                                {
                                    var ss = recStr.Split(' ');
                                    var ip = ss[2];
                                    Command.PrintLog(ip);
                                    fw.SetBlackList(ip);  //将IP加入到防火墙名单中
                                    sck.Close();
                                }
                            }
                            catch (Exception ex)
                            {
                                Command.PrintLog(ex);
                            }
                        }
                        await Task.Delay(500);
                    }
                }
                catch
                {
                    sckdic.Remove(guid);
                    Console.WriteLine("【{0}】连接已断开", guid);
                    return 0;
                }
            }
        }
 
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sck"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendMsg(Socket sck, /*Models.Message msg*/string msg)
        {
            try
            {
                //string sendStr = Newtonsoft.Json.JsonConvert.SerializeObject(msg);
                //byte[] sendByte = Encoding.UTF8.GetBytes(sendStr);
                byte[] sendByte = Encoding.UTF8.GetBytes(msg);
                sck.Send(sendByte, sendByte.Length, 0);
                return true;
            }
            catch
            {
                if (count % 10 == 0)
                {
                    count = 1;
                    return false;
                }
                else
                {
                    return SendMsg(sck, msg);
                }
            }
        }
    }
}