腾讯云的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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
 
namespace FirewallWindowsServer.Methods
{
    public static class MyConvert
    {
        /// <summary>
        /// 其他类型转json字符串
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// json转model
        /// </summary>
        /// <typeparam name="T">转换后的类型</typeparam>
        /// <param name="str">json字符串</param>
        /// <returns></returns>
        public static T ToModel<T>(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                var res = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
                if (res == null) {
                    return default;
                } else 
                {
                    return res;
                }
            }
            return default;
        }
        /// <summary>
        /// 其他类型转model
        /// </summary>
        /// <typeparam name="T">转换后的对象</typeparam>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static T ToModel<T>(this object obj)
        {
            if (obj == null)
            {
                return default(T);
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            T model = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            return model;
        }
        /// <summary>
        /// 其他类型转DataTable
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static DataTable ToTable(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(json);
        }
        /// <summary>
        /// json转DataTable
        /// </summary>
        /// <param name="str">json字符串</param>
        /// <returns></returns>
        public static DataTable ToTable(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(str);
            }
            return default(DataTable);
        }
        /// <summary>
        /// 字符串转整形
        /// </summary>
        /// <param name="str">数值字符串</param>
        /// <returns></returns>
        /// <exception cref="Exception">异常:非法的数据</exception>
        public static int ToInt(this string str)
        {
            int resInt = 0;
            if (int.TryParse(str, out resInt))
            {
                return resInt;
            }
            else
            {
                throw new Exception("该字符串无法转换成整形");
            }
        }
        /// <summary>
        /// 其他类型转整形
        /// </summary>
        /// <param name="str">数值对象</param>
        /// <returns></returns>
        /// <exception cref="Exception">异常:非法的数据</exception>
        public static int ToInt(this object str)
        {
            int resInt = 0;
            if (int.TryParse(str.ToString(), out resInt))
            {
                return resInt;
            }
            else
            {
                throw new Exception("该字符串无法转换成整形");
            }
        }
        /// <summary>
        /// datetime转字符串(yyyy-MM-dd HH:mm:ss.fff)
        /// </summary>
        /// <param name="dt">待转换的时间</param>
        /// <returns></returns>
        public static string GetTime(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
        /// <summary>
        /// 其他类型转时间
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static DateTime ToDateTime(this object obj)
        {
            if (obj == null)
            {
                throw new Exception("对象不能为空");
            }
            string str = obj.ToString();
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }
            else
            {
                throw new Exception("非法字符串");
            }
        }
        /// <summary>
        /// 字符串转时间
        /// </summary>
        /// <param name="str">待转换的字符串对象</param>
        /// <returns></returns>
        public static DateTime FormatDate(this string str)
        {
            return DateTime.Parse(str);
        }
        /// <summary>
        /// 时间转固定格式字符串
        /// </summary>
        /// <param name="dt">等转换的时间</param>
        /// <returns></returns>
        public static string ToTimeStr(this DateTime dt) 
        {
            return GetTime(dt);
        }
        /// <summary>
        /// 对象转日期字符串(yyyy-MM-dd)
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string GetDate(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            string str = obj.ToString();
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(str, out dt))
            {
                return dt.ToString("yyyy-MM-dd");
            }
            else
            {
                throw new Exception("无法转换的内容");
            }
        }
        /// <summary>
        /// 流转Byte数组
        /// </summary>
        /// <param name="stream">待转换的流</param>
        /// <returns></returns>
        public static byte[] ToByte(this Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
 
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        /// <summary>
        /// 将字符串转成二进制数据
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <returns>一个二进制数组</returns>
        public static byte[] ToByte(this string str) 
        {
            byte[] bytes = new byte[str.Length];
            for (int i = 0; i < str.Length; i++) 
            {
                bytes[i] = Convert.ToByte(str[i]);
            }
            return bytes;
        }
        /// <summary>
        /// Byte数组转流
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream ToStream(this byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        /// <summary>
        /// json转list数组
        /// </summary>
        /// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
        /// <param name="str">待转换的json字符串</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
            }
            return default(List<T>);
        }
        /// <summary>
        /// 其他类型转List
        /// </summary>
        /// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this object obj)
        {
            string str = obj.ToJson();
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
            }
            return default(List<T>);
        }
        /// <summary>
        /// DataTable首列转List<string>
        /// </summary>
        /// <param name="dt">待转换的Datatable</param>
        /// <returns></returns>
        public static List<string> ToList(this DataTable dt)
        {
            List<string> res = new List<string>();
            if (dt == null || dt.Rows == null || dt.Rows.Count == 0) { return res; }
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(Convert.ToString(dr[0]))) { continue; }
                res.Add(Convert.ToString(dr[0]));
            }
            return res;
        }
        /// <summary>
        /// 将List<string>转成DataTable
        /// </summary>
        /// <param name="list">新列的数据</param>
        /// <param name="ColumnName">列名</param>
        /// <returns></returns>
        public static DataTable ToTable(this List<string> list, string ColumnName)
        {
            DataTable res = new DataTable();
            res.Columns.Add(ColumnName);
            if (list == null || list.Count == 0) { return res; }
            foreach (var a in list)
            {
                if (string.IsNullOrWhiteSpace(a)) { continue; }
                var dr = res.NewRow();
                dr[0] = a;
                res.Rows.Add(dr);
            }
            return res;
        }
        /// <summary>
        /// 将DataTable中的空行移除
        /// </summary>
        /// <param name="dt">待处理的DataTable</param>
        /// <returns></returns>
        public static DataTable RemoveEmpty(this DataTable dt)
        {
            int count = dt.Rows.Count - 1;
            while (count >= 0)
            {
                bool b = true;
                foreach (var a in dt.Rows[count].ItemArray)
                {
                    if (!string.IsNullOrWhiteSpace(a.ToString()))
                    {
                        b = b & false;
                        break;
                    }
                }
                if (b)
                {
                    dt.Rows.Remove(dt.Rows[count]);
                }
                count--;
            }
            return dt;
        }
        /// <summary>
        /// 将字符串列表转成以splitChar分隔拼接的字符串
        /// </summary>
        /// <param name="list">待转换处理的列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="IsSkipNullValue">空值是否跳过,默认false(不跳过)</param>
        /// <returns>一个拼接后的字符串</returns>
        public static string ToSplitString(this IEnumerable<string> list,string splitChar,bool IsSkipNullValue = false) 
        {
            var res = string.Empty;
            if (list.Count() != 0)
            {
                foreach (var a in list) 
                {
                    if (IsSkipNullValue && string.IsNullOrWhiteSpace(a)) 
                    {
                        continue;
                    }
                    res += a + splitChar;
                }
                if (!string.IsNullOrWhiteSpace(res)) 
                {
                    res = res.Remove(res.Length - splitChar.Length);
                }
            }
            else 
            { }
 
            return res; 
        }
        /// <summary>
        /// 将实体列表转成以splitChar分隔拼接的字符串
        /// </summary>
        /// <typeparam name="T">实体模型</typeparam>
        /// <param name="list">待转换处理的实体列表</param>
        /// <param name="key">实体中的字段名</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="IsSkipNullValue">空值是否跳过,默认false(不跳过)</param>
        /// <returns>一个拼接后的字符串</returns>
        public static string ToSplitString<T>(this IEnumerable<T> list,string key,string splitChar, bool IsSkipNullValue = false) where T : class
        {
            var res = string.Empty;
            if (list.Count() > 0) 
            {
                foreach (var a in list) 
                {
                    foreach (var c in a.GetType().GetProperties()) 
                    {
                        if (c.Name == key) 
                        {
                            if (IsSkipNullValue && string.IsNullOrWhiteSpace(Convert.ToString(c.GetValue(a))))
                            {
                                continue;
                            }
                            res += Convert.ToString(c.GetValue(a)) + splitChar;
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(res))
                    {
                        res = res.Remove(res.Length - splitChar.Length);
                    }
                }
            }
            else { }
 
            return res;
        }
    }
}