mirror of
https://github.com/bol-van/zapret.git
synced 2025-04-19 21:42:59 +03:00
strpool moved to uthash
This commit is contained in:
parent
667afc814e
commit
4d7f41980a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -124,7 +124,7 @@ tpws - это transparent proxy.
|
|||||||
--unixeol ; конвертировать 0D0A в 0A и использовать везде 0A
|
--unixeol ; конвертировать 0D0A в 0A и использовать везде 0A
|
||||||
--hostlist=<filename> ; действовать только над доменами, входящими в список из filename. поддомены автоматически учитываются. в файле должен быть хост на каждой строке.
|
--hostlist=<filename> ; действовать только над доменами, входящими в список из filename. поддомены автоматически учитываются. в файле должен быть хост на каждой строке.
|
||||||
; список читается 1 раз при старте и хранится в памяти в виде иерархической структуры для быстрого поиска.
|
; список читается 1 раз при старте и хранится в памяти в виде иерархической структуры для быстрого поиска.
|
||||||
; для списка РКН может потребоваться система с 128 Mb памяти ! расчитывайте требование RAM для процесса как 10-15 кратный размер файла списка.
|
; для списка РКН может потребоваться система с 128 Mb памяти ! расчитывайте требование RAM для процесса как 3-5 кратный размер файла списка.
|
||||||
; по сигналу HUP список будет перечитан при следующем принятом соединении
|
; по сигналу HUP список будет перечитан при следующем принятом соединении
|
||||||
Параметры манипуляции могут сочетаться в любых комбинациях.
|
Параметры манипуляции могут сочетаться в любых комбинациях.
|
||||||
Есть исключения : split-pos заменяет split-http-req. hostdot и hosttab взаимоисключающи.
|
Есть исключения : split-pos заменяет split-http-req. hostdot и hosttab взаимоисключающи.
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
#include "chartree.h"
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static cptr *CharTreeInit(char c)
|
|
||||||
{
|
|
||||||
cptr *p;
|
|
||||||
p=(cptr *)calloc(1,sizeof(cptr));
|
|
||||||
if (p) p->chr = c;
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
void CharTreeDestroy(cptr *p)
|
|
||||||
{
|
|
||||||
cptr *p2;
|
|
||||||
while (p)
|
|
||||||
{
|
|
||||||
CharTreeDestroy(p->leaf);
|
|
||||||
p2 = p;
|
|
||||||
p = p->next;
|
|
||||||
free(p2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static cptr *CharTreeFindChar(cptr *p,char c)
|
|
||||||
{
|
|
||||||
while (p)
|
|
||||||
{
|
|
||||||
if (p->chr==c) return p;
|
|
||||||
p = p->next;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
bool CharTreeAddStr(cptr **pp,const char *s)
|
|
||||||
{
|
|
||||||
cptr *p;
|
|
||||||
if (*pp)
|
|
||||||
{
|
|
||||||
if (!(p=CharTreeFindChar(*pp,*s)))
|
|
||||||
{
|
|
||||||
// already present. append to list head
|
|
||||||
if (!(p = CharTreeInit(*s)))
|
|
||||||
return false;
|
|
||||||
p->next = *pp;
|
|
||||||
*pp = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if (!(p = *pp = CharTreeInit(*s))) return false;
|
|
||||||
if (!*s) return true;
|
|
||||||
return CharTreeAddStr(&p->leaf,s+1);
|
|
||||||
}
|
|
||||||
bool CharTreeCheckStr(cptr *p,const char *s)
|
|
||||||
{
|
|
||||||
p = CharTreeFindChar(p,*s);
|
|
||||||
if (!p) return false;
|
|
||||||
if (!*s) return true;
|
|
||||||
return CharTreeCheckStr(p->leaf,s+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *DupLower(const char *s)
|
|
||||||
{
|
|
||||||
char *sp,*sl = strdup(s);
|
|
||||||
if (!sl) return false;
|
|
||||||
for(sp=sl;*sp;sp++) *sp=tolower(*sp);
|
|
||||||
return sl;
|
|
||||||
}
|
|
||||||
bool CharTreeAddStrLower(cptr **pp,const char *s)
|
|
||||||
{
|
|
||||||
bool b;
|
|
||||||
char *sl = DupLower(s);
|
|
||||||
if (!sl) return false;
|
|
||||||
b=CharTreeAddStr(pp,sl);
|
|
||||||
free(sl);
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
bool CharTreeCheckStrLower(cptr *pp,const char *s)
|
|
||||||
{
|
|
||||||
bool b;
|
|
||||||
char *sl = DupLower(s);
|
|
||||||
if (!sl) return false;
|
|
||||||
b=CharTreeCheckStr(pp,sl);
|
|
||||||
free(sl);
|
|
||||||
return b;
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
typedef struct cptr
|
|
||||||
{
|
|
||||||
char chr;
|
|
||||||
struct cptr *leaf,*next;
|
|
||||||
} cptr;
|
|
||||||
|
|
||||||
void CharTreeDestroy(cptr *p);
|
|
||||||
bool CharTreeAddStr(cptr **pp,const char *s);
|
|
||||||
bool CharTreeAddStrLower(cptr **pp,const char *s);
|
|
||||||
bool CharTreeCheckStr(cptr *p,const char *s);
|
|
||||||
bool CharTreeCheckStrLower(cptr *pp,const char *s);
|
|
50
tpws/strpool.c
Normal file
50
tpws/strpool.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "strpool.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#undef uthash_nonfatal_oom
|
||||||
|
#define uthash_nonfatal_oom(elt) ut_oom_recover(elt)
|
||||||
|
|
||||||
|
static bool oom=false;
|
||||||
|
static void ut_oom_recover(strpool *elem)
|
||||||
|
{
|
||||||
|
oom=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StrPoolAddStr(strpool **pp,const char *s)
|
||||||
|
{
|
||||||
|
strpool *elem;
|
||||||
|
if (!(elem = (strpool*)malloc(sizeof(strpool))))
|
||||||
|
return false;
|
||||||
|
if (!(elem->str = strdup(s)))
|
||||||
|
{
|
||||||
|
free(elem);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
oom = false;
|
||||||
|
HASH_ADD_KEYPTR( hh, *pp, elem->str, strlen(elem->str), elem );
|
||||||
|
if (oom)
|
||||||
|
{
|
||||||
|
free(elem->str);
|
||||||
|
free(elem);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool StrPoolCheckStr(strpool *p,const char *s)
|
||||||
|
{
|
||||||
|
strpool *elem;
|
||||||
|
HASH_FIND_STR( p, s, elem);
|
||||||
|
return elem!=NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StrPoolDestroy(strpool **p)
|
||||||
|
{
|
||||||
|
strpool *elem,*tmp;
|
||||||
|
HASH_ITER(hh, *p, elem, tmp) {
|
||||||
|
free(elem->str);
|
||||||
|
HASH_DEL(*p, elem);
|
||||||
|
free(elem);
|
||||||
|
}
|
||||||
|
*p = NULL;
|
||||||
|
}
|
18
tpws/strpool.h
Normal file
18
tpws/strpool.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
//#define HASH_BLOOM 20
|
||||||
|
#define HASH_NONFATAL_OOM 1
|
||||||
|
#define HASH_FUNCTION HASH_BER
|
||||||
|
#include "uthash.h"
|
||||||
|
|
||||||
|
typedef struct strpool {
|
||||||
|
char *str; /* key */
|
||||||
|
UT_hash_handle hh; /* makes this structure hashable */
|
||||||
|
} strpool;
|
||||||
|
|
||||||
|
void StrPoolDestroy(strpool **p);
|
||||||
|
bool StrPoolAddStr(strpool **pp,const char *s);
|
||||||
|
bool StrPoolCheckStr(strpool *p,const char *s);
|
16
tpws/tpws.c
16
tpws/tpws.c
@ -24,9 +24,9 @@
|
|||||||
|
|
||||||
#include "tpws.h"
|
#include "tpws.h"
|
||||||
#include "tpws_conn.h"
|
#include "tpws_conn.h"
|
||||||
#include "chartree.h"
|
#include "strpool.h"
|
||||||
|
|
||||||
bool LoadHostList(cptr **hostlist, char *filename)
|
bool LoadHostList(strpool **hostlist, char *filename)
|
||||||
{
|
{
|
||||||
char *p, s[256];
|
char *p, s[256];
|
||||||
FILE *F = fopen(filename, "rt");
|
FILE *F = fopen(filename, "rt");
|
||||||
@ -42,9 +42,9 @@ bool LoadHostList(cptr **hostlist, char *filename)
|
|||||||
{
|
{
|
||||||
for (p = s + strlen(s) - 1; p >= s && (*p == '\r' || *p == '\n'); p--) *p = 0;
|
for (p = s + strlen(s) - 1; p >= s && (*p == '\r' || *p == '\n'); p--) *p = 0;
|
||||||
for (p = s; *p; p++) *p=tolower(*p);
|
for (p = s; *p; p++) *p=tolower(*p);
|
||||||
if (!CharTreeAddStr(hostlist, s))
|
if (!StrPoolAddStr(hostlist, s))
|
||||||
{
|
{
|
||||||
CharTreeDestroy(*hostlist);
|
StrPoolDestroy(hostlist);
|
||||||
*hostlist = NULL;
|
*hostlist = NULL;
|
||||||
fprintf(stderr, "Not enough memory to store host list : %s\n", filename);
|
fprintf(stderr, "Not enough memory to store host list : %s\n", filename);
|
||||||
fclose(F);
|
fclose(F);
|
||||||
@ -72,7 +72,7 @@ struct params_s
|
|||||||
int split_pos;
|
int split_pos;
|
||||||
int maxconn;
|
int maxconn;
|
||||||
char hostfile[256];
|
char hostfile[256];
|
||||||
cptr *hostlist;
|
strpool *hostlist;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct params_s params;
|
struct params_s params;
|
||||||
@ -104,7 +104,7 @@ void dohup()
|
|||||||
{
|
{
|
||||||
if (params.hostlist)
|
if (params.hostlist)
|
||||||
{
|
{
|
||||||
CharTreeDestroy(params.hostlist);
|
StrPoolDestroy(¶ms.hostlist);
|
||||||
if (!LoadHostList(¶ms.hostlist, params.hostfile))
|
if (!LoadHostList(¶ms.hostlist, params.hostfile))
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -221,7 +221,7 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
|
|||||||
p = Host;
|
p = Host;
|
||||||
while (p)
|
while (p)
|
||||||
{
|
{
|
||||||
bInHostList = CharTreeCheckStr(params.hostlist, p);
|
bInHostList = StrPoolCheckStr(params.hostlist, p);
|
||||||
printf("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
|
printf("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
|
||||||
if (bInHostList) break;
|
if (bInHostList) break;
|
||||||
p = strchr(p, '.');
|
p = strchr(p, '.');
|
||||||
@ -851,7 +851,7 @@ int main(int argc, char *argv[]) {
|
|||||||
retval = event_loop(listen_fd);
|
retval = event_loop(listen_fd);
|
||||||
close(listen_fd);
|
close(listen_fd);
|
||||||
|
|
||||||
if (params.hostlist) CharTreeDestroy(params.hostlist);
|
if (params.hostlist) StrPoolDestroy(¶ms.hostlist);
|
||||||
|
|
||||||
fprintf(stderr, "Will exit\n");
|
fprintf(stderr, "Will exit\n");
|
||||||
|
|
||||||
|
1217
tpws/uthash.h
Normal file
1217
tpws/uthash.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user