腾讯云的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
using System;
using System.IO;
using System.Text;
 
namespace FirewallWindowsServer.Methods
{
    public static class Command
    {
        public static void PrintLog(Exception ex)
        {
            try
            {
                Tool.PrintLog(ex.Message, ex.StackTrace, Path.Combine(AppContext.BaseDirectory, "log\\" + DateTime.Now.ToString("yyyyMMdd") + ".log"));
            }
            catch { }
        }
 
        public static void PrintLog(string msg) 
        {
            try
            {
                Tool.PrintLog("", msg, Path.Combine(AppContext.BaseDirectory, "log\\" + DateTime.Now.ToString("yyyyMMdd") + "_info.log"));
            }
            catch { }
        }
    }
 
    public static class Tool 
    {
        /// <summary>
        /// 打印日志
        /// </summary>
        /// <returns></returns>
        public static bool PrintLog(string title, string content, string path)
        {
            try
            {
                CreateLogDirectory(Path.GetDirectoryName(path));
                //往指定文件中追加日志
                StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
                sw.WriteLine("产生时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                sw.WriteLine("\t" + title);
                sw.WriteLine(content);
                sw.WriteLine();
                sw.WriteLine();
                sw.Close();
                sw.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// 创建全路径目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateLogDirectory(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            string[] paths = path.Split('\\');
            string tempPath = "";
            foreach (var a in paths)
            {
                tempPath += a + "\\";
                CreateDirectory(tempPath, "");  //目录不存在的话创建目录
            }
        }
 
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="folderName">文件夹名</param>
        /// <returns></returns>
        public static string CreateDirectory(string filePath, string folderName = "")
        {
 
            if (string.IsNullOrEmpty(filePath))
            {
                if (string.IsNullOrEmpty(folderName))
                {
                    return "";
                }
            }
            else
            {
                if (filePath.Substring(filePath.Length - 1) == @"\")
                {
                    filePath = filePath.Substring(0, filePath.Length - 1);
                }
            }
            string path = filePath + @"\" + folderName;
            try
            {
                if (!System.IO.Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return path;
        }
 
        /// <summary>
        /// 获取json配置文件内容
        /// </summary>
        /// <typeparam name="T">配置类实体</typeparam>
        /// <param name="ConfigFileName">要获取的配置文件名(相对当前根目录,appsettings.json)</param>
        /// <returns>配置类实体</returns>
        public static T GetSystemConfig<T>(string ConfigFileName = "appsettings.json")
        {
            string value = string.Empty;
            string Root = AppContext.BaseDirectory;
            string json = GetTxt(Root + "\\" + ConfigFileName);
            PrintLog("",json, Path.Combine(AppContext.BaseDirectory, "log\\" + DateTime.Now.ToString("yyyyMMdd") + "_info.log"));
            T config = json.ToModel<T>();
            return config;
        }
 
        /// <summary>
        /// 读取指定的文件(UTF-8)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static string GetTxt(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.UTF8);
            string str = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            return str;
        }
    }
}