mirror of
https://github.com/bol-van/zapret.git
synced 2025-04-19 21:42:59 +03:00
support HUP in tpws for hostlist reload
This commit is contained in:
parent
d6324dfb46
commit
3a76122598
72
tpws/tpws.c
72
tpws/tpws.c
@ -20,6 +20,7 @@
|
|||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
#include "tpws.h"
|
#include "tpws.h"
|
||||||
#include "tpws_conn.h"
|
#include "tpws_conn.h"
|
||||||
@ -69,6 +70,7 @@ struct params_s
|
|||||||
enum splithttpreq split_http_req;
|
enum splithttpreq split_http_req;
|
||||||
int split_pos;
|
int split_pos;
|
||||||
int maxconn;
|
int maxconn;
|
||||||
|
char hostfile[256];
|
||||||
cptr *hostlist;
|
cptr *hostlist;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -86,6 +88,29 @@ unsigned char *find_bin(void *data, ssize_t len, const void *blk, ssize_t blk_le
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool bHup = false;
|
||||||
|
void onhup(int sig)
|
||||||
|
{
|
||||||
|
printf("HUP received !\n");
|
||||||
|
if (params.hostlist)
|
||||||
|
printf("Will reload hostlist on next request\n");
|
||||||
|
bHup = true;
|
||||||
|
}
|
||||||
|
// should be called in normal execution
|
||||||
|
void dohup()
|
||||||
|
{
|
||||||
|
if (bHup)
|
||||||
|
{
|
||||||
|
if (params.hostlist)
|
||||||
|
{
|
||||||
|
CharTreeDestroy(params.hostlist);
|
||||||
|
if (!LoadHostList(¶ms.hostlist, params.hostfile))
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
bHup = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t send_with_flush(int sockfd, const void *buf, size_t len, int flags)
|
ssize_t send_with_flush(int sockfd, const void *buf, size_t len, int flags)
|
||||||
{
|
{
|
||||||
int flag, err;
|
int flag, err;
|
||||||
@ -118,6 +143,8 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
|
|||||||
bool bOutgoing;
|
bool bOutgoing;
|
||||||
ssize_t rd = 0, wr = 0, bs;
|
ssize_t rd = 0, wr = 0, bs;
|
||||||
|
|
||||||
|
dohup();
|
||||||
|
|
||||||
//Easy way to determin which socket is ready for reading
|
//Easy way to determin which socket is ready for reading
|
||||||
//TODO: Optimize. This one allows me quick lookup for conn, but
|
//TODO: Optimize. This one allows me quick lookup for conn, but
|
||||||
//I need to make a system call to determin which socket
|
//I need to make a system call to determin which socket
|
||||||
@ -168,31 +195,26 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
|
|||||||
{
|
{
|
||||||
printf("Data block looks like http request start : %s\n", *method);
|
printf("Data block looks like http request start : %s\n", *method);
|
||||||
|
|
||||||
if (params.hostlist)
|
if (params.hostlist && (p = find_bin(buf, bs, "\nHost: ", 7)))
|
||||||
{
|
{
|
||||||
pHost = find_bin(buf, bs, "\nHost: ", 7);
|
bool bInHostList = false;
|
||||||
if (pHost)
|
p += 7;
|
||||||
|
while (p < (buf + bs) && (*p == ' ' || *p == '\t')) p++;
|
||||||
|
pp = p;
|
||||||
|
while (pp < (buf + bs) && (pp - p) < (sizeof(Host) - 1) && *pp != '\r' && *pp != '\n') pp++;
|
||||||
|
memcpy(Host, p, pp - p);
|
||||||
|
Host[pp - p] = '\0';
|
||||||
|
p = Host;
|
||||||
|
printf("Requested Host is : %s\n", Host);
|
||||||
|
while (p)
|
||||||
{
|
{
|
||||||
bool bInHostList = false;
|
bInHostList = CharTreeCheckStrLower(params.hostlist, p);
|
||||||
p = pHost + 7;
|
printf("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
|
||||||
while (p < (buf + bs) && (*p == ' ' || *p == '\t')) p++;
|
if (bInHostList) break;
|
||||||
pp = p;
|
p = strchr(p, '.');
|
||||||
while (pp < (buf + bs) && (pp - p) < (sizeof(Host) - 1) && *pp != '\r' && *pp != '\n') pp++;
|
if (p) p++;
|
||||||
memcpy(Host, p, pp - p);
|
|
||||||
Host[pp - p] = '\0';
|
|
||||||
p = Host;
|
|
||||||
printf("Requested Host is : %s\n", Host);
|
|
||||||
while (p)
|
|
||||||
{
|
|
||||||
bInHostList = CharTreeCheckStrLower(params.hostlist, p);
|
|
||||||
printf("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
|
|
||||||
if (bInHostList) break;
|
|
||||||
p = strchr(p, '.');
|
|
||||||
if (p) p++;
|
|
||||||
}
|
|
||||||
bBypass = !bInHostList;
|
|
||||||
}
|
}
|
||||||
|
bBypass = !bInHostList;
|
||||||
}
|
}
|
||||||
if (!bBypass)
|
if (!bBypass)
|
||||||
{
|
{
|
||||||
@ -212,7 +234,6 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
|
|||||||
}
|
}
|
||||||
pp = p;
|
pp = p;
|
||||||
}
|
}
|
||||||
pHost = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.methodspace)
|
if (params.methodspace)
|
||||||
@ -224,7 +245,6 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
|
|||||||
memmove(p + 1, p, bs - pos);
|
memmove(p + 1, p, bs - pos);
|
||||||
*p = ' '; // insert extra space
|
*p = ' '; // insert extra space
|
||||||
bs++; // block will grow by 1 byte
|
bs++; // block will grow by 1 byte
|
||||||
pHost = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// search for Host only if required (save some CPU)
|
// search for Host only if required (save some CPU)
|
||||||
@ -655,6 +675,8 @@ void parse_params(int argc, char *argv[])
|
|||||||
case 17: /* hostlist */
|
case 17: /* hostlist */
|
||||||
if (!LoadHostList(¶ms.hostlist, optarg))
|
if (!LoadHostList(¶ms.hostlist, optarg))
|
||||||
exit(1);
|
exit(1);
|
||||||
|
strncpy(params.hostfile,optarg,sizeof(params.hostfile));
|
||||||
|
params.hostfile[sizeof(params.hostfile)-1]='\0';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -818,6 +840,8 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
fprintf(stderr, "Will listen to port %d\n", params.port);
|
fprintf(stderr, "Will listen to port %d\n", params.port);
|
||||||
|
|
||||||
|
signal(SIGHUP, onhup);
|
||||||
|
|
||||||
retval = event_loop(listen_fd);
|
retval = event_loop(listen_fd);
|
||||||
close(listen_fd);
|
close(listen_fd);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user