-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiptables.py
58 lines (45 loc) · 1.08 KB
/
iptables.py
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
import iptc
class IpTables:
def __init__(self):
self.table = iptc.Table(iptc.Table.MANGLE)
def create_chain(self, chain):
"""
:param chain:
:return:
"""
return iptc.Chain(self.table, chain)
@staticmethod
def create_rule(destination="127.0.0.1", destination_port=80, protocol="tcp"):
"""
:return:
"""
rule = iptc.Rule()
rule.dst = destination
rule.protocol = protocol
match = rule.create_match(protocol)
match.dport = str(destination_port)
return rule
@staticmethod
def create_target(rule, target):
"""
:param rule:
:param target:
:return:
"""
rule.create_target(target)
@staticmethod
def insert_rule(chain, rule):
"""
:param chain:
:param rule:
:return:
"""
chain.insert_rule(rule)
@staticmethod
def delete_rule(chain, rule):
"""
:param chain:
:param rule:
:return:
"""
chain.delete_rule(rule)