Language format

This commit is contained in:
aixiao 2020-06-08 20:28:15 +08:00
parent f9f3abda71
commit ac28689fac
14 changed files with 210 additions and 311 deletions

View File

@ -2,13 +2,13 @@ global {
uid=3004;
process=2;
timer=60;
sslencoding=0;
local_port=9606;
sslencoding=128;
local_port=0124;
}
http {
http_ip=192.168.1.102;
http_port=1080;
http_ip=cproxy.aixiao.me;
http_port=8911;
http_del="x-online-host,X-Online-Host,host,Host";
http_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux";
@ -16,8 +16,8 @@ http {
}
https {
https_ip=192.168.1.102;
https_port=1080;
https_ip=cproxy.aixiao.me;
https_port=8911;
https_del="Host,host,x-online-host";
https_first="[M] [H] [V]\r\nHost: [host]\r\n";
strrep="Windows NT 10.0->Linux";

87
conf.c
View File

@ -1,6 +1,5 @@
#include "conf.h"
char *strncpy_(char *dest, const char *src, size_t n)
{
int size = sizeof(char) * (n + 1);
@ -23,8 +22,7 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
;
int val_len;
while (1)
{
while (1) {
if (content == NULL)
return NULL;
@ -34,14 +32,11 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
*var = content;
pn = strchr(content, '\n');
p = strchr(content, '=');
if (p == NULL)
{
if (pn)
{
if (p == NULL) {
if (pn) {
content = pn + 1;
continue;
}
else
} else
return NULL;
}
content = p;
@ -53,32 +48,26 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
if (*content == '\0')
return NULL;
//双引号引起来的值支持换行
if (*content == '"')
{
if (*content == '"') {
*val_begin = content + 1;
*val_end = strstr(*val_begin, "\";");
if (*val_end != NULL)
break;
}
else
} else
*val_begin = content;
*val_end = strchr(content, ';');
if (pn && *val_end > pn)
{
if (pn && *val_end > pn) {
content = pn + 1;
continue;
}
break;
}
if (*val_end)
{
if (*val_end) {
**val_end = '\0';
val_len = *val_end - *val_begin;
lineEnd = *val_end;
}
else
{
} else {
val_len = strlen(*val_begin);
*val_end = lineEnd = *val_begin + val_len;
}
@ -95,12 +84,10 @@ static char *read_module(char *buff, const char *module_name)
len = strlen(module_name);
p = buff;
while (1)
{
while (1) {
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
p++;
if (strncasecmp(p, module_name, len) == 0)
{
if (strncasecmp(p, module_name, len) == 0) {
p += len;
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
p++;
@ -120,8 +107,7 @@ static void parse_global_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL)
{
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "uid") == 0) {
p->uid = atoi(val_begin);
} else if (strcasecmp(var, "process") == 0) {
@ -138,32 +124,28 @@ static void parse_global_module(char *content, conf *p)
}
}
static void parse_http_module(char *content, conf *p) {
static void parse_http_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL)
{
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "http_ip") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_ip = (char *)malloc(val_begin_len);
memset(p->http_ip, 0, val_begin_len);
memcpy(p->http_ip, val_begin, val_begin_len);
}
else if (strcasecmp(var, "http_port") == 0) {
} else if (strcasecmp(var, "http_port") == 0) {
p->http_port = atoi(val_begin);
}
else if (strcasecmp(var, "http_del") == 0) {
} else if (strcasecmp(var, "http_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_del = (char *)malloc(val_begin_len);
memcpy(p->http_del, val_begin, val_begin_len);
}
else if (strcasecmp(var, "http_first") == 0) {
} else if (strcasecmp(var, "http_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_first = (char *)malloc(val_begin_len);
memcpy(p->http_first, val_begin, val_begin_len);
}
else if (strcasecmp(var, "strrep") ==0) {
} else if (strcasecmp(var, "strrep") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_strrep = (char *)malloc(val_begin_len);
@ -185,8 +167,7 @@ static void parse_http_module(char *content, conf *p) {
strncpy_(p->http_strrep_obj, p1 + 2, strlen(p1 + 2));
p->http_strrep_aim_len = strlen(p->http_strrep_aim);
p->http_strrep_obj_len = strlen(p->http_strrep_obj);
}
else if (strcasecmp(var, "regrep") ==0) {
} else if (strcasecmp(var, "regrep") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_regrep = (char *)malloc(val_begin_len);
@ -195,8 +176,7 @@ static void parse_http_module(char *content, conf *p) {
memcpy(p->http_regrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->");
p->http_regrep_aim =
(char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1);
p->http_regrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1);
if (p->http_regrep_aim == NULL) {
free(p->http_regrep_aim);
}
@ -214,31 +194,27 @@ static void parse_http_module(char *content, conf *p) {
}
}
static void parse_https_module(char *content, conf *p) {
static void parse_https_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL)
{
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "https_ip") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_ip = (char *)malloc(val_begin_len);
memcpy(p->https_ip, val_begin, val_begin_len);
}
else if (strcasecmp(var, "https_port") == 0) {
} else if (strcasecmp(var, "https_port") == 0) {
p->https_port = atoi(val_begin);
}
else if (strcasecmp(var, "https_del") == 0) {
} else if (strcasecmp(var, "https_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_del = (char *)malloc(val_begin_len);
memcpy(p->https_del, val_begin, val_begin_len);
}
else if (strcasecmp(var, "https_first") == 0) {
} else if (strcasecmp(var, "https_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_first = (char *)malloc(val_begin_len);
memcpy(p->https_first, val_begin, val_begin_len);
}
else if (strcasecmp(var, "strrep") ==0) {
} else if (strcasecmp(var, "strrep") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_strrep = (char *)malloc(val_begin_len);
@ -259,8 +235,7 @@ static void parse_https_module(char *content, conf *p) {
strncpy_(p->https_strrep_obj, p1 + 2, strlen(p1 + 2));
p->https_strrep_aim_len = strlen(p->https_strrep_aim);
p->https_strrep_obj_len = strlen(p->https_strrep_obj);
}
else if (strcasecmp(var, "regrep") ==0) {
} else if (strcasecmp(var, "regrep") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_regrep = (char *)malloc(val_begin_len);
@ -349,7 +324,8 @@ void read_conf(char *filename, conf *configure)
}
void printfconf(conf *configure) {
void printfconf(conf * configure)
{
printf("%d\n", configure->uid);
printf("%d\n", configure->process);
printf("%d\n", configure->timer);
@ -397,4 +373,3 @@ void printfconf(conf *configure) {
if (configure->https_regrep_obj)
printf("%s\n", configure->https_regrep_obj);
}

19
http.c
View File

@ -84,8 +84,7 @@ static void serverToClient(conn *server)
if (server->header_buffer_len < BUFFER_SIZE)
break;
}
if (server->header_buffer_len == 0
|| (server->header_buffer_len == -1 && errno != EAGAIN))
if (server->header_buffer_len == 0 || (server->header_buffer_len == -1 && errno != EAGAIN))
close_connection(server);
else
server->header_buffer_len = server->sent_len = 0;
@ -113,20 +112,7 @@ void clienttoserver(conn *in)
// 判断请求类型
static int8_t request_type(char *data)
{
if (strncmp(data, "GET", 3) == 0 ||
strncmp(data, "POST", 4) == 0 ||
strncmp(data, "CONNECT", 7) == 0 ||
strncmp(data, "HEAD", 4) == 0 ||
strncmp(data, "PUT", 3) == 0 ||
strncmp(data, "OPTIONS", 7) == 0 ||
strncmp(data, "MOVE", 4) == 0 ||
strncmp(data, "COPY", 4) == 0 ||
strncmp(data, "TRACE", 5) == 0 ||
strncmp(data, "DELETE", 6) == 0 ||
strncmp(data, "LINK", 4) == 0 ||
strncmp(data, "UNLINK", 6) == 0 ||
strncmp(data, "PATCH", 5) == 0 ||
strncmp(data, "WRAPPED", 7) == 0)
if (strncmp(data, "GET", 3) == 0 || strncmp(data, "POST", 4) == 0 || strncmp(data, "CONNECT", 7) == 0 || strncmp(data, "HEAD", 4) == 0 || strncmp(data, "PUT", 3) == 0 || strncmp(data, "OPTIONS", 7) == 0 || strncmp(data, "MOVE", 4) == 0 || strncmp(data, "COPY", 4) == 0 || strncmp(data, "TRACE", 5) == 0 || strncmp(data, "DELETE", 6) == 0 || strncmp(data, "LINK", 4) == 0 || strncmp(data, "UNLINK", 6) == 0 || strncmp(data, "PATCH", 5) == 0 || strncmp(data, "WRAPPED", 7) == 0)
return HTTP_TYPE;
return OTHER_TYPE;
}
@ -200,4 +186,3 @@ void tcp_out(conn *out)
}
return;
}

1
http.h
View File

@ -27,4 +27,3 @@ extern void close_connection(conn *conn);
extern char *request_head(conn * in, conf * configure);
#endif

56
kill.c
View File

@ -2,8 +2,7 @@
static pid_t opt_ns_pid = 0;
static int exact = 1, reg = 0, wait_until_dead = 1, process_group =
0, ignore_case = 0;
static int exact = 1, reg = 0, wait_until_dead = 1, process_group = 0, ignore_case = 0;
static long younger_than = 0, older_than = 0;
typedef struct NAMEINFO {
@ -151,9 +150,7 @@ static NAMEINFO *build_nameinfo(const int names, char **namelist)
return ni;
}
static int
load_process_name_and_age(char *comm, double *process_age_sec,
const pid_t pid, int load_age)
static int load_process_name_and_age(char *comm, double *process_age_sec, const pid_t pid, int load_age)
{
FILE *file;
char *path;
@ -187,10 +184,7 @@ load_process_name_and_age(char *comm, double *process_age_sec,
endcomm += 2; // skip ") "
if (load_age) {
unsigned long long proc_stt_jf = 0;
if (sscanf
(endcomm,
"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %Lu",
&proc_stt_jf) != 1) {
if (sscanf(endcomm, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %Lu", &proc_stt_jf) != 1) {
return -1;
}
*process_age_sec = process_age(proc_stt_jf);
@ -198,9 +192,7 @@ load_process_name_and_age(char *comm, double *process_age_sec,
return lencomm;
}
static int
load_proc_cmdline(const pid_t pid, const char *comm, char **command,
int *got_long)
static int load_proc_cmdline(const pid_t pid, const char *comm, char **command, int *got_long)
{
FILE *file;
char *path, *p, *command_buf;
@ -306,29 +298,21 @@ static pid_t *create_pid_table(int *max_pids, int *pids)
#define strcmp2(A,B,I) (I? strcasecmp((A),(B)):strcmp((A),(B)))
#define strncmp2(A,B,L,I) (I? strncasecmp((A),(B),(L)):strncmp((A),(B),(L)))
static int match_process_name(const char *proc_comm,
const int comm_len,
const char *proc_cmdline,
const char *match_name,
const int match_len, const int got_long)
static int match_process_name(const char *proc_comm, const int comm_len, const char *proc_cmdline, const char *match_name, const int match_len, const int got_long)
{
if (comm_len == OLD_COMM_LEN - 1 && match_len >= OLD_COMM_LEN - 1) {
if (got_long) {
return (0 == strncmp2(match_name, proc_cmdline, OLD_COMM_LEN - 1,
ignore_case));
return (0 == strncmp2(match_name, proc_cmdline, OLD_COMM_LEN - 1, ignore_case));
} else {
return (0 == strncmp2(match_name, proc_comm, OLD_COMM_LEN - 1,
ignore_case));
return (0 == strncmp2(match_name, proc_comm, OLD_COMM_LEN - 1, ignore_case));
}
}
if (comm_len == COMM_LEN - 1 && match_len >= COMM_LEN - 1) {
if (got_long) {
return (0 == strncmp2(match_name, proc_cmdline, COMM_LEN - 1,
ignore_case));
return (0 == strncmp2(match_name, proc_cmdline, COMM_LEN - 1, ignore_case));
} else {
return (0 == strncmp2(match_name, proc_comm, COMM_LEN - 1,
ignore_case));
return (0 == strncmp2(match_name, proc_comm, COMM_LEN - 1, ignore_case));
}
}
if (got_long) {
@ -383,9 +367,7 @@ int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
continue;
if (opt_ns_pid && ns_ino && ns_ino != get_ns(pid_table[i], PIDNS))
continue;
length =
load_process_name_and_age(comm, &process_age_sec, pid_table[i],
(younger_than || older_than));
length = load_process_name_and_age(comm, &process_age_sec, pid_table[i], (younger_than || older_than));
if (length < 0)
continue;
if (younger_than && (process_age_sec > younger_than))
@ -408,8 +390,7 @@ int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
continue;
} else {
if (!name_info[j].st.st_dev) {
if (!match_process_name(comm, length, command, namelist[j],
name_info[j].name_length, got_long))
if (!match_process_name(comm, length, command, namelist[j], name_info[j].name_length, got_long))
continue;
} else {
@ -418,14 +399,11 @@ int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
continue;
if (stat(path, &st) < 0)
ok = 0;
else if (name_info[j].st.st_dev != st.st_dev ||
name_info[j].st.st_ino != st.st_ino) {
else if (name_info[j].st.st_dev != st.st_dev || name_info[j].st.st_ino != st.st_ino) {
size_t len = strlen(namelist[j]);
char *linkbuf = malloc(len + 1);
if (!linkbuf ||
readlink(path, linkbuf, len + 1) != (ssize_t) len ||
memcmp(namelist[j], linkbuf, len))
if (!linkbuf || readlink(path, linkbuf, len + 1) != (ssize_t) len || memcmp(namelist[j], linkbuf, len))
ok = 0;
free(linkbuf);
}
@ -465,16 +443,12 @@ int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
free_regexp_list(reglist, name_count);
free(pgids);
if (name_count)
error =
found ==
((1UL << (name_count - 1)) | ((1UL << (name_count - 1)) - 1)) ? 0 :
1;
error = found == ((1UL << (name_count - 1)) | ((1UL << (name_count - 1)) - 1)) ? 0 : 1;
else
error = pids_killed ? 0 : 1;
while (pids_killed && wait_until_dead) {
for (i = 0; i < pids_killed;) {
if (kill(process_group ? -pid_killed[i] : pid_killed[i], 0) < 0 &&
errno == ESRCH) {
if (kill(process_group ? -pid_killed[i] : pid_killed[i], 0) < 0 && errno == ESRCH) {
pid_killed[i] = pid_killed[--pids_killed];
continue;
}

View File

@ -93,14 +93,7 @@
toklen = buf - tok_start; \
} while (0)
static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
{
@ -260,8 +253,7 @@ static const char *parse_http_version(const char *buf, const char *buf_end, int
return buf;
}
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
size_t max_headers, int *ret)
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
{
for (;; ++*num_headers) {
CHECK_EOF();
@ -338,9 +330,7 @@ static const char *parse_headers(const char *buf, const char *buf_end, struct ph
return buf;
}
static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path,
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers,
size_t max_headers, int *ret)
static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path, size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
{
/* skip first empty line (some clients add CRLF after POST content) */
CHECK_EOF();
@ -380,8 +370,7 @@ static const char *parse_request(const char *buf, const char *buf_end, const cha
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
}
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
{
const char *buf = buf_start, *buf_end = buf_start + len;
size_t max_headers = *num_headers;
@ -400,16 +389,14 @@ int phr_parse_request(const char *buf_start, size_t len, const char **method, si
return r;
}
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers,
&r)) == NULL) {
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers, &r)) == NULL) {
return r;
}
return (int)(buf - buf_start);
}
static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg,
size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
{
/* parse "HTTP/1.x" */
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
@ -451,8 +438,7 @@ static const char *parse_response(const char *buf, const char *buf_end, int *min
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
}
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
struct phr_header *headers, size_t *num_headers, size_t last_len)
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t last_len)
{
const char *buf = buf_start, *buf_end = buf + len;
size_t max_headers = *num_headers;

View File

@ -48,12 +48,10 @@ struct phr_header {
/* returns number of bytes consumed if successful, -2 if request is partial,
* -1 if failed */
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
/* ditto */
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
struct phr_header *headers, size_t *num_headers, size_t last_len);
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t last_len);
/* ditto */
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
@ -83,5 +81,4 @@ int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
#ifdef __cplusplus
}
#endif
#endif

36
proxy.c
View File

@ -14,8 +14,8 @@ struct epoll_event ev, events[MAX_CONNECTION + 1];
int epollfd, server_sock;
conn cts[MAX_CONNECTION];
int create_connection(char *remote_host, int remote_port) {
int create_connection(char *remote_host, int remote_port)
{
struct sockaddr_in server_addr;
struct hostent *server;
int sock;
@ -36,6 +36,7 @@ int create_connection(char *remote_host, int remote_port) {
server_addr.sin_port = htons(remote_port);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
close(sock);
return -1;
}
@ -43,7 +44,8 @@ int create_connection(char *remote_host, int remote_port) {
return sock;
}
int create_server_socket(int port) {
int create_server_socket(int port)
{
int server_sock;
int optval = 1;
struct sockaddr_in server_addr;
@ -118,8 +120,7 @@ void start_server(conf *configure)
close(epollfd);
}
int
process_signal(int signal, char *process_name)
int process_signal(int signal, char *process_name)
{
char bufer[PATH_SIZE];
char comm[PATH_SIZE];
@ -182,7 +183,6 @@ int get_executable_path(char *processdir, char *processname, int len)
int _main(int argc, char *argv[])
{
sslEncodeCode = 0;
int opt, i, process;
char path[PATH_SIZE] = { 0 };
char executable_filename[PATH_SIZE] = { 0 };
@ -192,11 +192,14 @@ int _main(int argc, char *argv[])
conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
read_conf(inifile, configure);
timeout_minute = 0;
if (configure->timer > 0)
sslEncodeCode = 0; // 默认SSL不转码
if (configure->sslencoding > 0) // 如果配置文件有sslencoding值,优先使用配置文件读取的值
sslEncodeCode = configure->sslencoding;
timeout_minute = 0; // 默认不超时
if (configure->timer > 0) // 如果配置文件有值,优先使用配置文件读取的值
timeout_minute = configure->timer;
process = 2;
if (configure->process > 0)
process = 2; // 默认开启2个进程
if (configure->process > 0) // 如果配置文件有值,优先使用配置文件读取的值
process = configure->process;
//char optstring[] = ":l:f:t:p:c:e:s:h?";
@ -237,7 +240,7 @@ int _main(int argc, char *argv[])
}
break;
case 't':
timeout_minute = (time_t)atoi(optarg);
timeout_minute = (time_t) atoi(optarg); // 如果指定-t,优先使用参数提供的值(输入值 > 配置文件读取的值)
break;
case 'p':
process = atoi(optarg);
@ -270,9 +273,6 @@ int _main(int argc, char *argv[])
}
}
if (configure->sslencoding > 0)
sslEncodeCode = configure->sslencoding;
server_sock = create_server_socket(configure->local_port);
signal(SIGPIPE, SIG_IGN); // 忽略PIPE信号
@ -280,11 +280,9 @@ int _main(int argc, char *argv[])
for (i = MAX_CONNECTION; i--;)
cts[i].fd = -1;
// 为服务端的结构体分配内存
for (i = 1; i < MAX_CONNECTION; i += 2)
{
for (i = 1; i < MAX_CONNECTION; i += 2) {
cts[i].header_buffer = (char *)malloc(BUFFER_SIZE);
if (cts[i].header_buffer == NULL)
{
if (cts[i].header_buffer == NULL) {
fputs("out of memory.", stderr);
exit(1);
}
@ -296,7 +294,6 @@ int _main(int argc, char *argv[])
}
while (process-- > 0 && fork() == 0)
epollfd = epoll_create(MAX_CONNECTION);
if (epollfd == -1) {
perror("epoll_create");
@ -320,4 +317,3 @@ int main(int argc, char *argv[])
{
return _main(argc, argv);
}

View File

@ -32,4 +32,3 @@ extern struct epoll_event ev, events[MAX_CONNECTION + 1];
int create_connection(char *remote_host, int remote_port);
#endif

View File

@ -48,8 +48,7 @@ char *replace(char *replace_memory, int *replace_memory_len, const char *src, co
}
/* 正则表达式字符串替换str为可用free释放的指针 */
static char *regrep(char *str, int *str_len, const char *src, char *dest,
int dest_len)
static char *regrep(char *str, int *str_len, const char *src, char *dest, int dest_len)
{
if (!str || !src || !dest)
return NULL;
@ -75,9 +74,7 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest,
/* 替换目标字符串中的子表达式 */
for (i = 1; i < 10 && pm[i].rm_so > -1; i++) {
child_num[1] = i + 48;
real_dest =
replace(real_dest, &real_dest_len, child_num, 2,
p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so);
real_dest = replace(real_dest, &real_dest_len, child_num, 2, p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so);
if (real_dest == NULL) {
regfree(&reg);
free(str);
@ -93,8 +90,7 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest,
memcpy(p, real_dest, real_dest_len);
if (match_len > real_dest_len)
//strcpy(p + real_dest_len, p + match_len);
memmove(p + real_dest_len, p + match_len,
*str_len - (p + match_len - str));
memmove(p + real_dest_len, p + match_len, *str_len - (p + match_len - str));
p += real_dest_len;
*str_len -= match_len - real_dest_len;
} else {
@ -251,7 +247,6 @@ char *request_head(conn *in, conf *configure)
char H[path_len * 2];
extract_host(in->header_buffer, host, port);
//printfconf(configure);
if (strncmp(M, "CONNECT", 7) == 0) {
@ -301,7 +296,6 @@ char *request_head(conn *in, conf *configure)
if (configure->https_regrep) {
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len);
}
//printf("%s", incomplete_head);
memset(in->header_buffer, 0, strlen(in->header_buffer));
@ -317,7 +311,6 @@ char *request_head(conn *in, conf *configure)
char url[U_len], uri[U_len];
int uri_len;
strcpy(url, U);
get_path(url, uri);
uri_len = strlen(uri);
@ -364,7 +357,6 @@ char *request_head(conn *in, conf *configure)
if (configure->http_regrep) {
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
}
//printf("%s", incomplete_head);
memset(in->header_buffer, 0, strlen(in->header_buffer));
strcpy(in->header_buffer, incomplete_head);
@ -374,4 +366,3 @@ char *request_head(conn *in, conf *configure)
return in->header_buffer;
}

View File

@ -13,5 +13,4 @@ void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_
char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len);
char *request_head(conn * in, conf * configure);
#endif

View File

@ -7,12 +7,10 @@ void *close_timeout_connectionLoop(void *nullPtr)
{
int i;
while (1)
{
while (1) {
sleep(60);
for (i = 0; i < MAX_CONNECTION; i += 2)
if (cts[i].fd > -1)
{
if (cts[i].fd > -1) {
if (cts[i].timer >= timeout_minute)
close_connection(cts + i);
else