Skip to content

Commit b488519

Browse files
feat: Implement daemon mode
1 parent baffa1e commit b488519

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fakehttp -h www.example.com -i eth0
1818
Usage: fakehttp [options]
1919
2020
Options:
21+
-d run as daemon
2122
-h <hostname> hostname for obfuscation (required)
2223
-i <interface> network interface name (required)
2324
-m <mark> fwmark for bypassing the queue

src/fakehttp.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
static FILE *g_logfp = NULL;
5353
static int g_sockfd = 0;
5454
static int g_exit = 0;
55+
static int g_daemon = 0;
5556
static int g_silent = 0;
5657
static int g_repeat = 3;
5758
static uint32_t g_fwmark = 0x8000;
@@ -67,6 +68,7 @@ static void print_usage(const char *name)
6768
"Usage: %s [options]\n"
6869
"\n"
6970
"Options:\n"
71+
" -d run as a daemon\n"
7072
" -h <hostname> hostname for obfuscation (required)\n"
7173
" -i <interface> network interface name (required)\n"
7274
" -m <mark> fwmark for bypassing the queue\n"
@@ -712,8 +714,11 @@ int main(int argc, char *argv[])
712714
return EXIT_FAILURE;
713715
}
714716

715-
while ((opt = getopt(argc, argv, "h:i:m:n:r:st:w:x:")) != -1) {
717+
while ((opt = getopt(argc, argv, "dh:i:m:n:r:st:w:x:")) != -1) {
716718
switch (opt) {
719+
case 'd':
720+
g_daemon = 1;
721+
break;
717722
case 'h':
718723
if (strlen(optarg) > _POSIX_HOST_NAME_MAX) {
719724
fprintf(stderr, "%s: hostname is too long.\n", argv[0]);
@@ -814,6 +819,19 @@ int main(int argc, char *argv[])
814819
return EXIT_FAILURE;
815820
}
816821

822+
if (g_daemon) {
823+
res = daemon(0, 0);
824+
if (res < 0) {
825+
fprintf(stderr, "%s: failed to daemonize: %s\n", argv[0],
826+
strerror(errno));
827+
return EXIT_FAILURE;
828+
}
829+
830+
if (!g_logfp) {
831+
g_silent = 1;
832+
}
833+
}
834+
817835
E("FakeHTTP version " VERSION);
818836

819837
srand(time(NULL));

0 commit comments

Comments
 (0)