tpws: always compile PF support, use --enable-pf flag in FreeBSD

This commit is contained in:
bol-van
2022-01-23 23:11:11 +03:00
parent 0f55960460
commit cc5f0af5a8
9 changed files with 34 additions and 31 deletions

View File

@@ -7,7 +7,6 @@ all: tpws
tpws: $(SRC_FILES)
$(CC) $(CFLAGS) -Iepoll-shim/include -o $@ $(SRC_FILES) epoll-shim/src/*.c $(LDFLAGS) $(LIBS)
$(CC) $(CFLAGS) -Iepoll-shim/include -o $@_pf $(SRC_FILES) -DUSE_PF epoll-shim/src/*.c $(LDFLAGS) $(LIBS)
clean:
rm -f tpws tpws_pf *.o
rm -f tpws *.o

View File

@@ -3,6 +3,7 @@
#include <net/if.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/param.h>
#include "strpool.h"
enum splithttpreq { split_none = 0, split_method, split_host };
@@ -46,6 +47,10 @@ struct params_s
strpool *hostlist;
int debug;
#if defined(BSD)
bool pf_enable;
#endif
};
extern struct params_s params;

View File

@@ -10,25 +10,19 @@
#include "params.h"
#include "helpers.h"
//#if !defined(USE_PF) && defined(__OpenBSD__)
#if !defined(USE_PF) && (defined(__OpenBSD__) || defined(__APPLE__))
#define USE_PF 1
#endif
#ifdef __linux__
#include <linux/netfilter_ipv4.h>
#ifndef IP6T_SO_ORIGINAL_DST
#define IP6T_SO_ORIGINAL_DST 80
#endif
#endif
#ifdef USE_PF
#include <net/if.h>
#include <net/pfvar.h>
#endif
#if defined(BSD)
#include <net/if.h>
#include <net/pfvar.h>
#if defined(USE_PF)
static int redirector_fd=-1;
void redir_close()
@@ -54,7 +48,7 @@ static bool redir_open_private(const char *fname, int flags)
}
bool redir_init()
{
return redir_open_private("/dev/pf", O_RDONLY);
return params.pf_enable ? redir_open_private("/dev/pf", O_RDONLY) : true;
}
static bool destination_from_pf(const struct sockaddr *accept_sa, struct sockaddr_storage *orig_dst)
@@ -209,8 +203,8 @@ bool get_dest_addr(int sockfd, const struct sockaddr *accept_sa, struct sockaddr
}
if (orig_dst->ss_family==AF_INET6)
((struct sockaddr_in6*)orig_dst)->sin6_scope_id=0; // or MacOS will not connect()
#ifdef USE_PF
if (!destination_from_pf(accept_sa, orig_dst))
#ifdef BSD
if (params.pf_enable && !destination_from_pf(accept_sa, orig_dst))
DBGPRINT("pf filter destination_from_pf failed");
#endif
#ifdef __linux__

View File

@@ -139,6 +139,9 @@ static void exithelp()
" --pidfile=<filename>\t\t; write pid to file\n"
" --user=<username>\t\t; drop root privs\n"
" --uid=uid[:gid]\t\t; drop root privs\n"
#if defined(BSD) && !defined(__OpenBSD__) && !defined(__APPLE__)
" --enable-pf\t\t\t; enable PF redirector support. required in FreeBSD when used with PF firewall.\n"
#endif
" --debug=0|1|2\t\t\t; 0(default)=silent 1=verbose 2=debug\n"
"\nTAMPERING:\n"
" --hostlist=<filename>\t\t; only act on host in the list (one host per line, subdomains auto apply)\n"
@@ -205,6 +208,9 @@ void parse_params(int argc, char *argv[])
params.maxconn = DEFAULT_MAX_CONN;
params.max_orphan_time = DEFAULT_MAX_ORPHAN_TIME;
params.binds_last = -1;
#if defined(__OpenBSD__) || defined(__APPLE__)
params.pf_enable = true; // OpenBSD and MacOS have no other choice
#endif
if (can_drop_root())
{
params.uid = params.gid = 0x7FFFFFFF; // default uid:gid
@@ -252,6 +258,9 @@ void parse_params(int argc, char *argv[])
{ "socks",no_argument,0,0 },// optidx=37
{ "no-resolve",no_argument,0,0 },// optidx=38
{ "skip-nodelay",no_argument,0,0 },// optidx=39
#if defined(BSD) && !defined(__OpenBSD__) && !defined(__APPLE__)
{ "enable-pf",no_argument,0,0 },// optidx=40
#endif
{ NULL,0,NULL,0 }
};
while ((v = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1)
@@ -485,6 +494,11 @@ void parse_params(int argc, char *argv[])
case 39: /* skip-nodelay */
params.skip_nodelay = true;
break;
#if defined(BSD) && !defined(__OpenBSD__)
case 40: /* enable-pf */
params.pf_enable = true;
break;
#endif
}
}
if (!params.bind_wait_only && !params.port)