我有一个 .NET 6 Web API 应用程序,我想在托管系统中编辑我的 iptables 规则。 (Web API运行的系统)
我有如下规则
-A INPUT -m tcp --protocol tcp --source 2.2.2.2 --destination 1.1.1.1 --source-port 111 --destination-port 222 --jump DROP
我在 bash 中的
iptables
命令中尝试此规则并正常工作,但是当我尝试为此创建规则时,如下所示
IpTablesRule rule = IpTablesRule.Parse($"-A INPUT -m tcp--protocol tcp --source 2.2.2.2 --destination 1.1.1.1 --source-port 111 --destination-port 222 --jump DROP", null, chains);
然后尝试添加到系统中,如下所示
ipTablesSystem = new IpTablesSystem(system: new LocalFactory(), tableAdapter: new IPTablesBinaryAdapter());
inputChain = ipTablesSystem.GetChain(table: Table.FILTER, chain: Chain.INPUT, ipVersion: (int)IpVersion.V4);
inputChain.AddRule(rule);
系统上没有出现任何错误或类似的情况,但是在我尝试使用 bash (cli) 使用
iptables -L
查看系统规则后,我看不到我的规则
为什么系统中不存在我的规则?以及如何解决这个问题?
这是API使用不当造成的,正确的做法如下
IpTablesRule rule = IpTablesRule.Parse($"-A INPUT -m tcp --protocol tcp --source 2.2.2.2 --destination 1.1.1.1 --source-port 111 --destination-port 222 --jump DROP", null, chains);
var ipTablesSystem = new IpTablesSystem(system: new LocalFactory(), tableAdapter: ipTablesAdapter);
IIPTablesAdapterClient table = ipTablesSystem.GetTableAdapter((int)IpVersion.V4);
table.AddRule(rule);
注意的是,您必须从
IpTablesSystem
获取表格,并从表格中添加或删除规则。