Skip to content

Commit d992ce6

Browse files
feat: Add support for all interfaces
1 parent 47860d7 commit d992ce6

File tree

8 files changed

+70
-3
lines changed

8 files changed

+70
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ General Options:
3232
-w <file> write log to <file> instead of stderr
3333
3434
Advanced Options:
35+
-a work on all network interfaces (ignores -i)
3536
-b <file> use TCP payload from binary file (ignores -h)
3637
-f skip firewall rules
3738
-g disable hop count estimation

include/globvar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct fh_context {
3030
/* -1 */ int outbound;
3131
/* -4 */ int use_ipv4;
3232
/* -6 */ int use_ipv6;
33+
/* -a */ int alliface;
3334
/* -b */ const char *payloadpath;
3435
/* -d */ int daemon;
3536
/* -f */ int skipfw;

src/globvar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct fh_context g_ctx = {.exit = 0,
3030
/* -1 */ .outbound = 0,
3131
/* -4 */ .use_ipv4 = 0,
3232
/* -6 */ .use_ipv6 = 0,
33+
/* -a */ .alliface = 0,
3334
/* -b */ .payloadpath = NULL,
3435
/* -d */ .daemon = 0,
3536
/* -f */ .skipfw = 0,

src/ipv4ipt.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,22 @@ static int ipt4_iface_setup(void)
3333
char iface_str[IFNAMSIZ];
3434
size_t i, cnt;
3535
int res;
36+
char *ipt_alliface_cmd[] = {"iptables", "-w", "-t", "mangle", "-A",
37+
"FAKEHTTP", "-j", "FAKEHTTP_R", NULL};
38+
3639
char *ipt_iface_cmd[] = {"iptables", "-w", "-t", "mangle",
3740
"-A", "FAKEHTTP", "-i", iface_str,
3841
"-j", "FAKEHTTP_R", NULL};
3942

43+
if (g_ctx.alliface) {
44+
res = fh_execute_command(ipt_alliface_cmd, 0, NULL);
45+
if (res < 0) {
46+
E(T(fh_execute_command));
47+
return -1;
48+
}
49+
return 0;
50+
}
51+
4052
cnt = sizeof(g_ctx.iface) / sizeof(*g_ctx.iface);
4153

4254
for (i = 0; i < cnt && g_ctx.iface[i]; i++) {

src/ipv4nft.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ static int nft4_iface_setup(void)
3434
int res;
3535
char *nft_iface_cmd[] = {"nft", nftstr, NULL};
3636

37+
if (g_ctx.alliface) {
38+
res = snprintf(nftstr, sizeof(nftstr),
39+
"add rule ip fakehttp fh_prerouting jump fh_rules");
40+
if (res < 0 || (size_t) res >= sizeof(nftstr)) {
41+
E("ERROR: snprintf(): %s", "failure");
42+
return -1;
43+
}
44+
45+
res = fh_execute_command(nft_iface_cmd, 0, NULL);
46+
if (res < 0) {
47+
E(T(fh_execute_command));
48+
return -1;
49+
}
50+
return 0;
51+
}
52+
3753
cnt = sizeof(g_ctx.iface) / sizeof(*g_ctx.iface);
3854

3955
for (i = 0; i < cnt && g_ctx.iface[i]; i++) {

src/ipv6ipt.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,23 @@ static int ipt6_iface_setup(void)
3333
char iface_str[IFNAMSIZ];
3434
size_t i, cnt;
3535
int res;
36+
char *ipt_alliface_cmd[] = {"ip6tables", "-w", "-t",
37+
"mangle", "-A", "FAKEHTTP",
38+
"-j", "FAKEHTTP_R", NULL};
39+
3640
char *ipt_iface_cmd[] = {"ip6tables", "-w", "-t", "mangle",
3741
"-A", "FAKEHTTP", "-i", iface_str,
3842
"-j", "FAKEHTTP_R", NULL};
3943

44+
if (g_ctx.alliface) {
45+
res = fh_execute_command(ipt_alliface_cmd, 0, NULL);
46+
if (res < 0) {
47+
E(T(fh_execute_command));
48+
return -1;
49+
}
50+
return 0;
51+
}
52+
4053
cnt = sizeof(g_ctx.iface) / sizeof(*g_ctx.iface);
4154

4255
for (i = 0; i < cnt && g_ctx.iface[i]; i++) {

src/ipv6nft.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ static int nft6_iface_setup(void)
3434
int res;
3535
char *nft_iface_cmd[] = {"nft", nftstr, NULL};
3636

37+
if (g_ctx.alliface) {
38+
res = snprintf(nftstr, sizeof(nftstr),
39+
"add rule ip6 fakehttp fh_prerouting jump fh_rules");
40+
if (res < 0 || (size_t) res >= sizeof(nftstr)) {
41+
E("ERROR: snprintf(): %s", "failure");
42+
return -1;
43+
}
44+
45+
res = fh_execute_command(nft_iface_cmd, 0, NULL);
46+
if (res < 0) {
47+
E(T(fh_execute_command));
48+
return -1;
49+
}
50+
return 0;
51+
}
52+
3753
cnt = sizeof(g_ctx.iface) / sizeof(*g_ctx.iface);
3854

3955
for (i = 0; i < cnt && g_ctx.iface[i]; i++) {

src/mainfun.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static void print_usage(const char *name)
6868
" -w <file> write log to <file> instead of stderr\n"
6969
"\n"
7070
"Advanced Options:\n"
71+
" -a work on all network interfaces (ignores -i)\n"
7172
" -b <file> use TCP payload from binary file (ignores -h)\n"
7273
" -f skip firewall rules\n"
7374
" -g disable hop count estimation\n"
@@ -103,7 +104,7 @@ int main(int argc, char *argv[])
103104
memset(g_ctx.iface, 0, sizeof(g_ctx.iface));
104105
exitcode = EXIT_FAILURE;
105106

106-
while ((opt = getopt(argc, argv, "0146b:dfh:i:km:n:r:st:w:x:z")) != -1) {
107+
while ((opt = getopt(argc, argv, "0146ab:dfh:i:km:n:r:st:w:x:z")) != -1) {
107108
switch (opt) {
108109
case '0':
109110
g_ctx.inbound = 1;
@@ -121,6 +122,10 @@ int main(int argc, char *argv[])
121122
g_ctx.use_ipv6 = 1;
122123
break;
123124

125+
case 'a':
126+
g_ctx.alliface = 1;
127+
break;
128+
124129
case 'b':
125130
g_ctx.payloadpath = optarg;
126131
if (strlen(g_ctx.payloadpath) > PATH_MAX - 1) {
@@ -286,7 +291,7 @@ int main(int argc, char *argv[])
286291
return EXIT_FAILURE;
287292
}
288293

289-
if (!iface_cnt) {
294+
if (!g_ctx.alliface && !iface_cnt) {
290295
fprintf(stderr, "%s: option -i is required.\n", argv[0]);
291296
print_usage(argv[0]);
292297
return EXIT_FAILURE;
@@ -350,7 +355,9 @@ int main(int argc, char *argv[])
350355
EE("WARNING: setpriority(): %s", strerror(errno));
351356
}
352357

353-
if (iface_cnt > 1) {
358+
if (g_ctx.alliface) {
359+
iface_info = "all interfaces";
360+
} else if (iface_cnt > 1) {
354361
iface_info = "multiple interfaces";
355362
} else {
356363
iface_info = g_ctx.iface[0];

0 commit comments

Comments
 (0)