Skip to content

Commit df90425

Browse files
feat: Improve iptables rules management
1 parent 4c80e69 commit df90425

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

src/fakehttp.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static int execute_command(char **argv, int silent)
291291

292292
execvp(argv[0], argv);
293293

294-
E("ERROR: execvp(): %s", strerror(errno));
294+
E("ERROR: execvp(): %s: %s", argv[0], strerror(errno));
295295

296296
_exit(EXIT_FAILURE);
297297
}
@@ -318,25 +318,35 @@ static int execute_command(char **argv, int silent)
318318
}
319319

320320

321-
static void ipt_rules_cleanup(void)
321+
static int ipt_rules_flush(int auto_create)
322322
{
323-
size_t i, ipt_cmds_cnt;
324-
char *ipt_cmds[][32] = {
325-
{"iptables", "-w", "-t", "mangle", "-F", "FAKEHTTP", NULL},
326-
327-
{"iptables", "-w", "-t", "mangle", "-D", "INPUT", "-j", "FAKEHTTP",
328-
NULL},
323+
int res;
324+
size_t i, cnt;
325+
char *ipt_flush_cmd[] = {"iptables", "-w", "-t", "mangle", "-F",
326+
"FAKEHTTP", NULL};
327+
char *ipt_create_cmds[][32] = {
328+
{"iptables", "-w", "-t", "mangle", "-N", "FAKEHTTP", NULL},
329329

330-
{"iptables", "-w", "-t", "mangle", "-D", "FORWARD", "-j", "FAKEHTTP",
330+
{"iptables", "-w", "-t", "mangle", "-I", "INPUT", "-j", "FAKEHTTP",
331331
NULL},
332332

333-
{"iptables", "-w", "-t", "mangle", "-X", "FAKEHTTP", NULL}};
334-
335-
ipt_cmds_cnt = sizeof(ipt_cmds) / sizeof(*ipt_cmds);
336-
337-
for (i = 0; i < ipt_cmds_cnt; i++) {
338-
execute_command(ipt_cmds[i], 1);
333+
{"iptables", "-w", "-t", "mangle", "-I", "FORWARD", "-j", "FAKEHTTP",
334+
NULL}
335+
};
336+
337+
res = execute_command(ipt_flush_cmd, 1);
338+
if (res < 0 && auto_create) {
339+
cnt = sizeof(ipt_create_cmds) / sizeof(*ipt_create_cmds);
340+
for (i = 0; i < cnt; i++) {
341+
res = execute_command(ipt_create_cmds[i], 0);
342+
if (res) {
343+
E("ERROR: execute_command()");
344+
return -1;
345+
}
346+
}
339347
}
348+
349+
return 0;
340350
}
341351

342352

@@ -346,14 +356,6 @@ static int ipt_rules_setup(void)
346356
size_t i, ipt_cmds_cnt, ipt_opt_cmds_cnt;
347357
int res;
348358
char *ipt_cmds[][32] = {
349-
{"iptables", "-w", "-t", "mangle", "-N", "FAKEHTTP", NULL},
350-
351-
{"iptables", "-w", "-t", "mangle", "-I", "INPUT", "-j", "FAKEHTTP",
352-
NULL},
353-
354-
{"iptables", "-w", "-t", "mangle", "-I", "FORWARD", "-j", "FAKEHTTP",
355-
NULL},
356-
357359
/*
358360
exclude marked packets
359361
*/
@@ -1042,11 +1044,16 @@ int main(int argc, char *argv[])
10421044
/*
10431045
Iptables
10441046
*/
1045-
ipt_rules_cleanup();
1047+
res = ipt_rules_flush(1);
1048+
if (res) {
1049+
E("ERROR: ipt_rules_flush()");
1050+
goto destroy_queue;
1051+
}
1052+
10461053
res = ipt_rules_setup();
10471054
if (res) {
10481055
E("ERROR: ipt_rules_setup()");
1049-
goto cleanup_iptables;
1056+
goto flush_iptables;
10501057
}
10511058

10521059
/*
@@ -1064,7 +1071,7 @@ int main(int argc, char *argv[])
10641071
res = signal_setup();
10651072
if (res) {
10661073
E("ERROR: signal_setup()");
1067-
goto cleanup_iptables;
1074+
goto flush_iptables;
10681075
}
10691076

10701077
E("listening on %s, netfilter queue number %" PRIu32 "...", g_iface,
@@ -1077,7 +1084,7 @@ int main(int argc, char *argv[])
10771084
while (!g_exit) {
10781085
if (err_cnt >= 20) {
10791086
E("too many errors, exiting...");
1080-
goto cleanup_iptables;
1087+
goto flush_iptables;
10811088
}
10821089

10831090
recv_len = recv(fd, buff, buffsize, 0);
@@ -1094,7 +1101,7 @@ int main(int argc, char *argv[])
10941101
default:
10951102
E("ERROR: recv(): %s", strerror(errno));
10961103
err_cnt++;
1097-
goto cleanup_iptables;
1104+
goto flush_iptables;
10981105
}
10991106
}
11001107

@@ -1111,8 +1118,8 @@ int main(int argc, char *argv[])
11111118
E("exiting normally...");
11121119
exitcode = EXIT_SUCCESS;
11131120

1114-
cleanup_iptables:
1115-
ipt_rules_cleanup();
1121+
flush_iptables:
1122+
ipt_rules_flush(0);
11161123

11171124
destroy_queue:
11181125
nfq_destroy_queue(qh);

0 commit comments

Comments
 (0)