File size: 31,601 Bytes
020c337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#!/www/server/panel/pyenv/bin/python3.7
# coding: utf-8
# -------------------------------------------------------------------
# 宝塔Linux面板
# -------------------------------------------------------------------
# Copyright (c) 2014-2099 宝塔软件(http://bt.cn) All rights reserved.
# -------------------------------------------------------------------
# Author: wzz <wzz@bt.cn>
# -------------------------------------------------------------------

# ------------------------------
# 系统防火墙模型 - ufw封装库
# ------------------------------

import subprocess
import os
import sys
if "/www/server/panel/class" not in sys.path:
    sys.path.insert(0, "/www/server/panel/class")
import public
# import re
from firewallModel.app.appBase import Base

class Ufw(Base):
    def __init__(self):
        self.cmd_str = self._set_cmd_str()

    def _set_cmd_str(self):
        return "ufw"

    # 2024/3/19 下午 5:00 获取系统防火墙的运行状态
    def status(self):
        '''
            @name 获取系统防火墙的运行状态
            @author wzz <2024/3/19 下午 5:00>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run([self.cmd_str, "status"], capture_output=True, text=True, check=True)
            if "Status: active" in result.stdout:
                return "running"
            elif "状态: 激活" in result.stdout:
                return "running"
            else:
                return "not running"
        except subprocess.CalledProcessError:
            return "not running"
        except Exception as e:
            return "not running"

    # 2024/3/19 下午 5:00 获取系统防火墙的版本号
    def version(self):
        '''
            @name 获取系统防火墙的版本号
            @author wzz <2024/3/19 下午 5:00>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run([self.cmd_str, "version"], capture_output=True, text=True, check=True)
            info = result.stdout.replace("\n", "")
            return info.replace("ufw ", "")
        except Exception as e:
            return "未知版本"

    # 2024/3/19 下午 5:00 启动防火墙
    def start(self):
        '''
            @name 启动防火墙
            @author wzz <2024/3/19 下午 5:00>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            stdout, stderr = public.ExecShell("echo y | {} enable".format(self.cmd_str))
            if stderr and "setlocale: LC_ALL: cannot change locale (en_US.UTF-8)" not in stderr:
                return public.returnMsg(False, "启动防火墙失败: {}".format(stderr))
            return self._result(True, "启动防火墙成功")
        except Exception as e:
            return self._result(False, "启动防火墙失败:{}".format(str(e)))

    # 2024/3/19 下午 5:00 停止防火墙
    def stop(self):
        '''
            @name 停止防火墙
            @author wzz <2024/3/19 下午 5:00>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            stdout, stderr = public.ExecShell("{} disable".format(self.cmd_str))
            if stderr and "setlocale: LC_ALL: cannot change locale (en_US.UTF-8)" not in stderr:
                return public.returnMsg(False, "停止防火墙失败: {}".format(stderr))
            return self._result(True, "停止防火墙成功")
        except Exception as e:
            return self._result(False, "停止防火墙失败:{}".format(str(e)))

    # 2024/3/19 下午 4:59 重启防火墙
    def restart(self):
        '''
            @name 重启防火墙
            @author wzz <2024/3/19 下午 4:59>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            self.stop()
            self.start()
        except Exception as e:
            return self._result(False, "重启防火墙失败:{}".format(str(e)))

    # 2024/3/19 下午 4:59 重载防火墙
    def reload(self):
        '''
            @name 重载防火墙
            @author wzz <2024/3/19 下午 4:59>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            subprocess.run([self.cmd_str, "reload"], check=True, stdout=subprocess.PIPE)
        except Exception as e:
            return self._result(False, "重载防火墙失败:{}".format(str(e)))

    # 2024/3/19 上午 10:39 列出防火墙中所有端口规则
    def list_port(self):
        '''
            @name 列出防火墙中所有端口规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"], capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                item_fire = self._load_info(line, "port")
                if item_fire.get("Port") and item_fire["Port"] != "Anywhere" and "." not in item_fire["Port"]:
                    item_fire["Port"] = item_fire["Port"].replace(":", "-")
                    item_fire["Address"] = "all" if item_fire["Address"] == "Anywhere" else item_fire["Address"]

                    datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 上午 10:39 列出防火墙中所有input端口规则
    def list_input_port(self):
        '''
            @name 列出防火墙中所有input端口规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"], capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                item_fire = self._load_info(line, "port")
                if item_fire.get("Port") and item_fire["Port"] != "Anywhere" and "." not in item_fire["Port"]:
                    item_fire["Port"] = item_fire["Port"].replace(":", "-")

                    if item_fire["Chain"] == "INPUT":
                        datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 上午 10:39 列出防火墙中所有output端口规则
    def list_output_port(self):
        '''
            @name 列出防火墙中所有output端口规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"], capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                item_fire = self._load_info(line, "port")
                if item_fire.get("Port") and item_fire["Port"] != "Anywhere" and "." not in item_fire["Port"]:
                    item_fire["Port"] = item_fire["Port"].replace(":", "-")

                    if item_fire["Chain"] == "OUTPUT":
                        datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 上午 10:39 列出防火墙中所有的ip规则
    def list_address(self):
        '''
            @name 列出防火墙中所有的ip规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"],  capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                item_fire = self._load_info(line, "address")
                if "Port" in item_fire: continue
                if item_fire.get("Address"):
                    datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 上午 10:39 列出防火墙中所有input的ip规则
    def list_input_address(self):
        '''
            @name 列出防火墙中所有input的ip规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"],  capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                if " IN" not in line:
                    continue
                item_fire = self._load_info(line, "address")
                if "Port" in item_fire: continue
                if item_fire.get("Address"):
                    datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 上午 10:39 列出防火墙中所有output的ip规则
    def list_output_address(self):
        '''
            @name 列出防火墙中所有output的ip规则
            @author wzz <2024/3/19 上午 10:39>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            result = subprocess.run(
                [self.cmd_str, "status", "verbose"],  capture_output=True, text=True, check=True
            )
            port_infos = result.stdout.split("\n")
            datas = []
            is_start = False
            for line in port_infos:
                if "fail2ban" in line.lower(): continue
                if line.startswith("-"):
                    is_start = True
                    continue
                if not is_start:
                    continue
                if " OUT" not in line:
                    continue
                item_fire = self._load_info(line, "address")
                if "Port" in item_fire: continue
                if item_fire.get("Address"):
                    datas.append(item_fire)
            return datas
        except Exception as e:
            return []

    # 2024/3/19 下午 4:59 添加端口规则
    def input_port(self, info, operation):
        '''
            @name 添加端口规则
            @author wzz <2024/3/19 下午 4:59>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            if info["Strategy"] == "accept":
                info["Strategy"] = "allow"
            elif info["Strategy"] == "drop":
                info["Strategy"] = "deny"

            if "Port" in info and info["Port"].find('-') != -1:
                info["Port"] = info["Port"].replace('-', ':')

            if operation == "add":
                if info['Protocol'] == "tcp/udp":
                    cmd = "{cmd_str} allow {port}/tcp;{cmd_str} allow {port}/udp".format(cmd_str = self.cmd_str,port=info['Port'])
                    stdout, stderr = public.ExecShell(cmd)
                else:
                    stdout, stderr = public.ExecShell(self.cmd_str + " allow " + info['Port'] + "/" + info['Protocol'])
            else:
                if info['Protocol'] == "tcp/udp":
                    cmd = "{cmd_str} delete allow {port}/tcp;{cmd_str} delete allow {port}/udp".format(cmd_str=self.cmd_str,port=info['Port'])
                    stdout, stderr = public.ExecShell(cmd)
                else:
                    stdout, stderr = public.ExecShell(self.cmd_str + " delete allow " + info['Port'] + "/" + info['Protocol'])

            if stderr:
                if "setlocale" in stderr:
                    return self._result(True, "设置端口规则成功")
                return self._result(False, "设置端口规则失败:{}".format(stderr))

            return self._result(True, "设置端口规则成功")

        except Exception as e:
            if "setlocale" in str(e):
                return self._result(True, "设置端口规则成功")
            return self._result(False, "设置端口规则失败:{}".format(str(e)))

    # 2024/3/24 下午 11:28 设置output端口策略
    def output_port(self, info, operation):
        '''
            @name 设置output端口策略
            @param info: 端口号
            @param operation: 操作
            @return None
        '''
        try:
            if info["Strategy"] == "accept":
                info["Strategy"] = "allow"
            elif info["Strategy"] == "drop":
                info["Strategy"] = "deny"

            if "Port" in info and info["Port"].find('-') != -1:
                info["Port"] = info["Port"].replace('-', ':')

            if operation == "add":
                if info['Protocol'].find('/') != -1:
                    cmd = "{cmd_str} {strategy} out {port}/tcp;{cmd_str} {strategy} out {port}/udp".format(cmd_str=self.cmd_str,strategy = info['Strategy'],port=info['Port'])
                else:
                    cmd = "{} {} out {}/{}".format(self.cmd_str, info['Strategy'], info['Port'], info['Protocol'])
            else:
                if info['Protocol'].find('/') != -1:
                    cmd = "{cmd_str} delete {strategy} out {port}/tcp;{cmd_str} delete {strategy} out {port}/udp".format(cmd_str=self.cmd_str, strategy=info['Strategy'], port=info['Port'])
                else:
                    cmd = "{} delete {} out {}/{}".format(self.cmd_str, info['Strategy'], info['Port'], info['Protocol'])
            stdout, stderr = public.ExecShell(cmd)
            if stderr:
                if "setlocale" in stderr:
                    return self._result(True, "设置端口规则成功")
                return self._result(False, "设置output端口规则失败:{}".format(stderr))
            return self._result(True, "设置output端口规则成功")
        except Exception as e:
            if "setlocale" in str(e):
                return self._result(True, "设置output端口规则成功")
            return self._result(False, "设置output端口规则失败:{}".format(str(e)))

    # 2024/3/19 下午 4:58 复杂一些的规则管理
    def rich_rules(self, info, operation):
        '''
            @name 复杂一些的规则管理
            @author wzz <2024/3/19 下午 4:58>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        try:
            if info["Strategy"] == "accept":
                info["Strategy"] = "allow"
            elif info["Strategy"] == "drop":
                info["Strategy"] = "deny"
            else:
                return self._result(False, "未知的策略参数:{}".format(info["Strategy"]))

            if "Port" in info and info["Port"].find('-') != -1:
                info["Port"] = info["Port"].replace('-', ':')

            rule_str = "{} insert 1 {} ".format(self.cmd_str, info["Strategy"])
            if "Address" in info and public.is_ipv6(info['Address']):
                rule_str = "{} {} ".format(self.cmd_str, info["Strategy"])
            if operation == "remove":
                rule_str = "{} delete {} ".format(self.cmd_str, info["Strategy"])

            if "Address" in info and info['Address'] != "all":
                rule_str += "from {} ".format(info['Address'])
            if len(info.get("Protocol", "")) != 0 and "/" not in info['Protocol']:
                rule_str += "proto {} ".format(info['Protocol'])
            if len(info.get("Protocol", "")) != 0 and "/" in info['Protocol']:
                if "Address" in info and info['Address'] != "all":
                    if not "from {} ".format(info['Address']) in rule_str:
                        rule_str += "from any "
            if len(info.get("Port", "")) != 0:
                rule_str += "to any port {} ".format(info['Port'])

            if len(info.get("Protocol", "")) != 0 and "/" in info['Protocol']:
                for i in ["tcp", "udp"]:
                    cmd_str = rule_str + "proto {}".format(i)
                    stdout, stderr = public.ExecShell(cmd_str)
                    if stderr:
                        if "Rule added" in stdout or "Rule deleted" in stdout or "Rule updated" in stdout or "Rule inserted" in stdout or "Skipping adding existing rule" in stdout:
                            return self._result(True, "设置规则成功")
                        if "setlocale" in stderr:
                            return self._result(True, "设置规则成功")
                        return self._result(False, "规则设置失败:{}".format(stderr))

                return self._result(True, "设置规则成功")

            stdout, stderr = public.ExecShell(rule_str)
            if stderr:
                if "Rule added" in stdout or "Rule deleted" in stdout or "Rule updated" in stdout or "Rule inserted" in stdout or "Skipping adding existing rule" in stdout:
                    return self._result(True, "设置规则成功")
                if "setlocale" in stderr:
                    return self._result(True, "设置规则成功")
                return self._result(False, "规则设置失败:{}".format(stderr))
            return self._result(True, "设置规则成功")
        except Exception as e:
            if "setlocale" in str(e):
                return self._result(True, "设置规则成功")
            return self._result(False, "规则设置失败:{}".format(e))

    # 2024/3/24 下午 11:29 设置output rich_rules
    def output_rich_rules(self, info, operation):
        '''
            @name 设置output rich_rules
            @param info: 规则
            @param operation: 操作
            @return None
        '''
        try:
            if info["Strategy"] == "accept":
                info["Strategy"] = "allow"
            elif info["Strategy"] == "drop":
                info["Strategy"] = "deny"
            else:
                return self._result(False, "未知的策略: {}".format(info["Strategy"]))

            if "Port" in info and info["Port"].find('-') != -1:
                info["Port"] = info["Port"].replace('-', ':')

            rule_str = "{} insert 1 {} ".format(self.cmd_str, info["Strategy"])
            if "Address" in info and public.is_ipv6(info['Address']):
                rule_str = "{} {} ".format(self.cmd_str, info["Strategy"])
            if operation == "remove":
                rule_str = "{} delete {} ".format(self.cmd_str, info["Strategy"])

            if len(info.get("Address", "")) != 0:
                rule_str += "out from {} ".format(info['Address'])
            if len(info.get("Protocol", "")) != 0:
                rule_str += "proto {} ".format(info['Protocol'])
            if len(info.get("Port", "")) != 0:
                rule_str += "to any port {} ".format(info['Port'])
            stdout, stderr = public.ExecShell(rule_str)
            if stderr:
                if "Rule added" in stdout or "Rule deleted" in stdout or "Rule updated" in stdout or "Rule inserted" in stdout or "Skipping adding existing rule" in stdout:
                    return self._result(True, "设置output规则成功")
                if "setlocale" in stderr:
                    return self._result(True, "设置规则成功")
                return self._result(False, "outpu规则设置失败:{}".format(stderr))
            return self._result(True, "设置output规则成功")
        except Exception as e:
            if "setlocale" in str(e):
                return self._result(True, "设置output规则成功")
            return self._result(False, "outpu规则设置失败:{}".format(e))

    # 2024/3/19 下午 5:01 解析防火墙规则信息,返回字典格式数据,用于添加或删除防火墙规则
    def _load_info(self, line, fire_type):
        '''
            @name 解析防火墙规则信息,返回字典格式数据,用于添加或删除防火墙规则
            @author wzz <2024/3/19 上午 10:38>
            @param "data":{"参数名":""} <数据类型> 参数描述
            @return dict{"status":True/False,"msg":"提示信息"}
        '''
        fields = line.split()
        item_info = {}
        if "LIMIT" in line or "ALLOW FWD" in line:
            return item_info
        if len(fields) < 4:
            return item_info
        if fields[0] == "Anywhere" and fire_type != "port":
            item_info["Strategy"] = "drop"

            if fields[1] != "(v6)":
                if fields[1] == "ALLOW":
                    item_info["Strategy"] = "accept"
                if fields[2] == "IN":
                    item_info["Chain"] = "INPUT"
                elif fields[2] == "OUT":
                    item_info["Chain"] = "OUTPUT"
                item_info["Address"] = fields[3] if fields[3] != "Anywhere" else "all"
                item_info["Family"] = "ipv4"
            else:
                if fields[2] == "ALLOW":
                    item_info["Strategy"] = "accept"
                if fields[3] == "IN":
                    item_info["Chain"] = "INPUT"
                elif fields[3] == "OUT":
                    item_info["Chain"] = "OUTPUT"
                item_info["Address"] = fields[4] if fields[4] != "Anywhere" else "all"
                item_info["Family"] = "ipv6"

            return item_info

        if "/" in fields[0]:
            item_info["Port"] = fields[0].split("/")[0]
            item_info["Protocol"] = fields[0].split("/")[1]
        else:
            item_info["Port"] = fields[0]
            item_info["Protocol"] = "tcp/udp"

        if "v6" in fields[1]:
            item_info["Family"] = "ipv6"
            if fields[2] == "ALLOW":
                item_info["Strategy"] = "accept"
            else:
                item_info["Strategy"] = "drop"

            if fields[3] == "IN":
                item_info["Chain"] = "INPUT"
            elif fields[3] == "OUT":
                item_info["Chain"] = "OUTPUT"
            item_info["Address"] = fields[4] if fields[4] != "Anywhere" else "all"

        else:
            item_info["Family"] = "ipv4" if ":" not in fields[3] else "ipv6"

            if fields[1] == "ALLOW":
                item_info["Strategy"] = "accept"
            else:
                item_info["Strategy"] = "drop"

            if fields[2] == "IN":
                item_info["Chain"] = "INPUT"
            elif fields[2] == "OUT":
                item_info["Chain"] = "OUTPUT"
            item_info["Address"] = fields[3] if fields[3] != "Anywhere" else "all"

        return item_info

    # 2024/3/25 下午 2:29 设置端口转发
    def port_forward(self, info, operation):
        '''
            @name 设置端口转发
            @param port: 端口号
            @param ip: ip地址
            @param operation: 操作
            @return None
        '''
        from firewallModel.app.iptables import Iptables
        self.firewall = Iptables()
        return self.firewall.port_forward(info, operation)

    # 2024/3/25 下午 2:34 获取所有端口转发列表
    def list_port_forward(self):
        '''
            @name 获取所有端口转发列表
            @return None
        '''
        from firewallModel.app.iptables import Iptables
        self.firewall = Iptables()
        return self.firewall.get_nat_prerouting_rules()

if __name__ == '__main__':
    args = sys.argv
    firewall = Ufw()
    ufw_status = firewall.status()
    if len(args) < 2:
        print("Welcome to the UFW (Uncomplicated Firewall) command-line interface!")
        print("Firewall status is :", ufw_status)
        print("Firewall version: ", firewall.version())
        if ufw_status == "not running":
            print("ufw未启动,请启动ufw后再执行命令!")
            print("启动命令: start")
            print()
            sys.exit(1)
        print()
        print("Available options:")
        print("1. Check Firewall Status: status")
        print("2. Check Firewall Version: version")
        print("3. Start Firewall: start")
        print("4. Stop Firewall: stop")
        print("5. Restart Firewall: restart")
        print("6. Reload Firewall: reload")
        print("7. List All Ports: list_port")
        print("8. List All IP Addresses: list_address")
        print("9. Add Port: add_port <port> <protocol>")
        print("10. Remove Port: remove_port <port> <protocol>")
        print("11. Add Port Rule: add_port_rule <address> <port> <protocol> <strategy> <operation>")
        print("12. Remove Port Rule: remove_port_rule <address> <port> <protocol> <strategy> <operation>")
        print("13. Add IP Rule: add_ip_rule <address> <strategy> <operation>")
        print("14. Remove IP Rule: remove_ip_rule <address> <strategy> <operation>")
        print()
        sys.exit(1)
    if args[1] == "status":
        print(firewall.status())
    elif args[1] == "version":
        print(firewall.version())
    elif args[1] == "start":
        error = firewall.start()
        if error:
            print(f"Error: {error}")
        else:
            print("Firewall started successfully.")
    elif args[1] == "stop":
        error = firewall.stop()
        if error:
            print(f"Error: {error}")
        else:
            print("Firewall stopped successfully.")
    elif args[1] == "restart":
        error = firewall.restart()
        if error:
            print(f"Error: {error}")
        else:
            print("Firewall restarted successfully.")
    elif args[1] == "reload":
        error = firewall.reload()
        if error:
            print(f"Error: {error}")
        else:
            print("Firewall reloaded successfully.")
    elif args[1] == "list_input_port":
        ports = firewall.list_input_port()
        for p in ports:
            print(p)
    elif args[1] == "list_output_port":
        ports = firewall.list_output_port()
        for p in ports:
            print(p)
    elif args[1] == "list_input_address":
        addresses = firewall.list_input_address()
        for a in addresses:
            print(a)
    elif args[1] == "list_output_address":
        addresses = firewall.list_output_address()
        for a in addresses:
            print(a)
    elif args[1] == "add_port":
        port = args[2]
        protocol = args[3]
        error = firewall.input_port(f"{port}/{protocol}", "allow")
        if error:
            print(f"Error: {error}")
        else:
            print(f"Port {port}/{protocol} added successfully.")
    elif args[1] == "remove_port":
        port = args[2]
        protocol = args[3]
        error = firewall.input_port(f"{port}/{protocol}", "remove")
        if error:
            print(f"Error: {error}")
        else:
            print(f"Port {port}/{protocol} removed successfully.")
    elif args[1] == "add_port_rule":
        address = args[2]
        port = args[3]
        protocol = args[4]
        strategy = args[5]
        operation = args[6]
        error = firewall.rich_rules(
            {"Address": address, "Port": port, "Protocol": protocol, "Strategy": strategy}, operation)
        if error:
            print(f"Error: {error}")
        else:
            print("Rich rule added successfully.")
    elif args[1] == "remove_port_rule":
        address = args[2]
        port = args[3]
        protocol = args[4]
        strategy = args[5]
        operation = args[6]
        error = firewall.rich_rules(
            {"Address": address, "Port": port, "Protocol": protocol, "Strategy": strategy}, operation)
        if error:
            print(f"Error: {error}")
        else:
            print("Rich rule removed successfully.")
    elif args[1] == "add_ip_rule":
        address = args[2]
        strategy = args[3]
        operation = args[4]
        error = firewall.rich_rules(
            {"Address": address, "Strategy": strategy}, operation)
        if error:
            print(f"Error: {error}")
        else:
            print("Rich rule added successfully.")
    elif args[1] == "remove_ip_rule":
        address = args[2]
        strategy = args[3]
        operation = args[4]
        error = firewall.rich_rules(
            {"Address": address, "Strategy": strategy}, operation)
        if error:
            print(f"Error: {error}")
        else:
            print("Rich rule removed successfully.")
    elif args[1] == "output_port":
        port = args[2]
        operation = args[3]
        error = firewall.output_port(port, operation)
        if error:
            print(f"Error: {error}")
        else:
            print(f"Output port {port} {operation} successfully.")
    elif args[1] == "output_rich_rules":
        address = args[2]
        port = args[3]
        protocol = args[4]
        strategy = args[5]
        operation = args[6]
        error = firewall.output_rich_rules(
            {"Address": address, "Port": port, "Protocol": protocol, "Strategy": strategy}, operation)
        if error:
            print(f"Error: {error}")
        else:
            print("Output rich rule added successfully.")
    else:
        print("Invalid args")
        sys.exit(1)