Compare commits

...

3 Commits

Author SHA1 Message Date
403ecb70b3 proxy: reintegrate do_con_srv
parses host header entry, connects to resolved ip:port
2024-09-09 12:30:32 +02:00
20c6ffe01b proxy: remove junk regex code 2024-09-08 22:20:06 +02:00
718a040d15 proxy: begin transition from regex to parslib 2024-09-08 22:00:58 +02:00
6 changed files with 246 additions and 408 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "parslib"]
path = parslib
url = http://git.0xdeadbeer.xyz/0xdeadbeer/parslib

View File

@ -1,14 +1,16 @@
CC=bear --append -- gcc CC=bear --append -- gcc
CFLAGS= -g3 -Wall -Werror CFLAGS= -g3 -Wall -Werror
CFILES=proxlib.c
CFILES_PARSLIB=parslib/parslib.final.o
OUT=proxlib
all: proxlib all: proxlib
proxy: proxlib.c proxlib: $(CFILES) $(CFILES_PARSLIB)
$(CC) $(CFLAGS) -o proxlib.o -c proxlib.c $(CC) $(CFLAGS) -o $(OUT) $^
$(CC) $(CFLAGS) -o proxlib proxlib.o
clean: clean:
rm -f proxlib *.o rm -f $(OUT)
tar: tar:
tar -cvzf proxlib.tgz proxlib.c proxlib.h README Makefile tar -cvzf proxlib.tgz $(CFILES) README Makefile

1
parslib Submodule

@ -0,0 +1 @@
Subproject commit 678a44228fe0c5b24b2d1b96576da0e44211c364

BIN
proxlib

Binary file not shown.

589
proxlib.c
View File

@ -7,210 +7,97 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <regex.h>
#include "proxlib.h" #include "proxlib.h"
#include "parslib/parslib.h"
int on = 1; int on = 1;
int debug = 1; int debug = 2;
struct request *clt_data;
regex_t preg;
regmatch_t pmatch[REGEX_MATCHN];
int statem; int statem;
int clt_sock = -1; #define SEGMENT_LEN 512
int srv_sock = -1; #define MAX_BUFF_LEN 128 * 1024
char *clt_msg = NULL; int _read_line(int fd, char **outbuff) {
char *srv_msg = NULL; char tmp_buff[SEGMENT_LEN];
int clt_msg_len = 0; int tmp_buff_len = 0;
int srv_msg_len = 0; char *output_buff = NULL;
int output_buff_len = 0;
char *ptr = NULL;
int whead_pos = 0;
char *whead = NULL;
int diff = 0;
int ret = 0;
int end = 0;
void *extractsub(const char *msg, regmatch_t match) { while (!end) {
void *buf = NULL; ret = recv(fd, tmp_buff, SEGMENT_LEN, MSG_PEEK);
int buflen = match.rm_eo - match.rm_so; if (ret <= 0) {
if (!buflen) break;
goto _return; }
buf = (void *) calloc(1, buflen); ptr = strstr(tmp_buff, "\r\n");
if (buf == NULL) if (ptr >= tmp_buff+SEGMENT_LEN) {
goto _return; diff = ret;
} else {
diff = ptr - tmp_buff + 2;
end = 1;
}
sprintf(buf, "%.*s", buflen, &msg[match.rm_so]); tmp_buff_len = diff;
whead_pos = output_buff_len;
output_buff_len += tmp_buff_len;
_return: if (tmp_buff_len > MAX_BUFF_LEN) {
return buf; return -1;
} }
int parse_header(char *msgbuff) { output_buff = (char *) realloc(output_buff, output_buff_len);
int ret; if (!output_buff) {
return -1;
}
ret = regcomp(&preg, REGEX_HEADER, REG_EXTENDED); whead = output_buff+whead_pos;
if (ret != 0) ret = recv(fd, whead, diff, 0);
goto _err; if (ret <= 0) {
break;
ret = regexec(&preg, msgbuff, REGEX_MATCHN, pmatch, 0); }
if (ret != 0)
goto _ok;
char *key = extractsub(msgbuff, pmatch[1]);
if (key == NULL)
goto _err;
char *value = extractsub(msgbuff, pmatch[2]);
if (value == NULL)
goto _err;
struct header new_header = {
.key = key,
.value = value
};
int last_index = clt_data->header_num;
clt_data->header_num++;
clt_data->headers = (void *) realloc(clt_data->headers,
clt_data->header_num*sizeof(struct header));
clt_data->headers[last_index] = new_header;
_ok:
regfree(&preg);
return 0;
_err:
regfree(&preg);
return -1;
}
int parse_host(char *buff) {
int ret;
ret = regcomp(&preg, REGEX_HOST, REG_EXTENDED);
if (ret != 0)
goto _err;
ret = regexec(&preg, buff, REGEX_MATCHN, pmatch, 0);
if (ret != 0)
goto _err;
char *host_name = extractsub(buff, pmatch[1]);
if (!host_name)
goto _err;
char *host_port = extractsub(buff, pmatch[2]);
if (!host_port) {
host_port = PROXY_DEF_PORT;
} }
clt_data->host_name = host_name; *outbuff = output_buff;
clt_data->host_port = host_port; return output_buff_len;
regfree(&preg);
return 0;
_err:
regfree(&preg);
return -1;
} }
int parse_title(char *msgbuff) { /* easy wrapper for _read_line(int fd, void **outbuff) */
int ret; int read_line(int fd,
int *line_len, char **line,
int *msgbuff_len, char **msgbuff) {
int ret = 0;
ret = regcomp(&preg, REGEX_TITLE, REG_EXTENDED); ret = *line_len = _read_line(fd, line);
if (ret != 0) if (ret < 0) {
goto _err; return -1;
ret = regexec(&preg, msgbuff, REGEX_MATCHN, pmatch, 0);
if (ret != 0)
goto _err;
clt_data->method = extractsub(msgbuff, pmatch[1]);
if (clt_data->method == NULL)
goto _err;
clt_data->uri = extractsub(msgbuff, pmatch[2]);
if (clt_data->uri == NULL)
goto _err;
clt_data->ver = extractsub(msgbuff, pmatch[3]);
if (clt_data->ver == NULL)
goto _err;
regfree(&preg);
return 0;
_err:
regfree(&preg);
return -1;
}
void free_host(void) {
free(clt_data->host_name);
free(clt_data->host_port);
}
void free_title(void) {
free(clt_data->method);
free(clt_data->uri);
free(clt_data->ver);
}
void free_headers(void) {
for (int i = 0; i < clt_data->header_num; i++) {
struct header *header = &clt_data->headers[i];
free(header->key);
free(header->value);
} }
free(clt_data->headers);
}
void free_clt_data(void) { *msgbuff = (char *) realloc(*msgbuff, *msgbuff_len+*line_len);
free_host(); if (!*msgbuff) {
free_title(); free(*line);
free_headers(); return -1;
free(clt_data); }
}
void free_srv_data(void) { memcpy(*msgbuff+*msgbuff_len, *line, *line_len);
} *msgbuff_len += *line_len;
((char *) *line)[(*line_len)-2] = '\0';
*line_len -= 2;
void free_msg_buffs(void) { return 0;
free(clt_msg);
free(srv_msg);
}
void free_data(void) {
free_clt_data();
free_srv_data();
} }
int parse_line(char *line, int line_count) { int parse_line(char *line, int line_count) {
int ret = 0; int ret = 0;
if (line_count == 0) {
ret = parse_title(line);
} else {
ret = parse_header(line);
}
return ret; return ret;
} }
char *getheader(char *key) {
char *ret = NULL;
for (int i = 0; i < clt_data->header_num; i++) {
struct header *hdr = &clt_data->headers[i];
if (strcmp(hdr->key, key))
continue;
ret = hdr->value;
}
return ret;
}
void do_err(void) { void do_err(void) {
int statem_code = statem & (~STATEM_ERR); int statem_code = statem & (~STATEM_ERR);
fprintf(stderr, "[%d,%d,%d] Errored out!\n", statem, statem_code, fprintf(stderr, "[%d,%d,%d] Errored out!\n", statem, statem_code,
@ -218,7 +105,7 @@ void do_err(void) {
} }
int do_fwd_clt(void) { int do_fwd_clt(void) {
int bytes = 0; /*int bytes = 0;
int ret = 0; int ret = 0;
while (bytes < srv_msg_len) { while (bytes < srv_msg_len) {
ret = write(clt_sock, srv_msg+bytes, srv_msg_len-bytes); ret = write(clt_sock, srv_msg+bytes, srv_msg_len-bytes);
@ -226,7 +113,7 @@ int do_fwd_clt(void) {
return -1; return -1;
bytes += ret; bytes += ret;
} }*/
return 0; return 0;
} }
@ -238,7 +125,7 @@ int do_prs_srv(void) {
} }
int do_rcv_srv(void) { int do_rcv_srv(void) {
int bytes = 0; /*int bytes = 0;
int ret = 0; int ret = 0;
while (bytes < PROXY_MAX_MSGLEN) { while (bytes < PROXY_MAX_MSGLEN) {
ret = recv(srv_sock, srv_msg+bytes, PROXY_MAX_MSGLEN-bytes, MSG_PEEK); ret = recv(srv_sock, srv_msg+bytes, PROXY_MAX_MSGLEN-bytes, MSG_PEEK);
@ -253,22 +140,42 @@ int do_rcv_srv(void) {
srv_msg_len = bytes; srv_msg_len = bytes;
if (debug) if (debug == 1)
fprintf(stdout, "[%d] Received server message of size %d bytes\n", statem, srv_msg_len); fprintf(stdout, "[%d] Received server message of size %d bytes\n", statem, srv_msg_len);
*/
return 0; return 0;
} }
int do_con_srv(void) { // TODO
int ret; int do_con_srv(struct conn *conn) {
char *host = getheader("Host"); int ret = 0;
if (!host) struct httpareq *req = &conn->cltreq;
return -1; struct point *host = &req->hentries[header_host];
if (host->er == NULL) {
if (debug <= 2) {
fprintf(stderr, "debug - request does not have HOST header\n");
}
goto _exit;
}
struct hostinfo *info = (struct hostinfo *) calloc(1, sizeof(struct hostinfo));
if (!info) {
goto _exit;
}
ret = pahostinfo(host->er, host->len, info);
if (ret < 0) {
if (debug <= 2) {
fprintf(stderr, "Failed parsing upstream host header\n");
}
goto _exit_hostinfo;
}
if (debug <= 2) {
fprintf(stdout, "Establishing connection with upstream: %.*s : %.*s\n", info->hostname_len, info->hostname, info->service_len, info->service);
}
ret = parse_host(host);
if (ret < 0)
return -1;
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *res; struct addrinfo *res;
@ -276,24 +183,34 @@ int do_con_srv(void) {
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(clt_data->host_name, clt_data->host_port, &hints, &res); ret = getaddrinfo(info->hostname, info->service, &hints, &res);
if (ret < 0) if (ret < 0) {
return -1; goto _exit_hostinfo;
}
ret = srv_sock = socket(res->ai_family, res->ai_socktype, ret = conn->srvfd = socket(res->ai_family, res->ai_socktype,
res->ai_protocol); res->ai_protocol);
if (ret < 0) if (ret < 0) {
return -1; goto _exit_getaddrinfo;
}
ret = connect(srv_sock, res->ai_addr, res->ai_addrlen); ret = connect(conn->srvfd, res->ai_addr, res->ai_addrlen);
if (ret < 0) if (ret < 0) {
return -1; goto _exit_getaddrinfo;
}
return 0; _exit_getaddrinfo:
freeaddrinfo(res);
_exit_hostinfo:
free(info->hostname);
free(info->service);
free(info);
_exit:
return ret;
} }
int do_fwd_srv(void) { int do_fwd_srv(void) {
int bytes = 0; /*int bytes = 0;
int ret = 0; int ret = 0;
while (bytes < clt_msg_len) { while (bytes < clt_msg_len) {
ret = write(srv_sock, clt_msg+bytes, clt_msg_len-bytes); ret = write(srv_sock, clt_msg+bytes, clt_msg_len-bytes);
@ -301,115 +218,103 @@ int do_fwd_srv(void) {
return -1; return -1;
bytes += ret; bytes += ret;
} }*/
return 0; return 0;
} }
int do_prs_clt(void) { int do_rcv_clt(struct conn *conn) {
int ret;
int ln_cnt = 0;
char *ln = strdup(clt_msg);
if (!ln)
return -1;
ln = strtok(ln, "\n");
while (ln) {
ret = parse_line(ln, ln_cnt);
if (ret < 0)
return -1;
ln_cnt++;
ln = strtok(NULL, "\n");
}
return 0;
}
int do_rcv_clt(void) {
int bytes = 0;
int ret = 0; int ret = 0;
while (bytes < PROXY_MAX_MSGLEN) { char *line = NULL;
ret = recv(clt_sock, clt_msg+bytes, PROXY_MAX_MSGLEN-bytes, MSG_PEEK); char *msgbuff = NULL;
if (ret < 0) int line_len = 0;
return -1; int msgbuff_len = 0;
if (!ret)
break;
ret = recv(clt_sock, clt_msg+bytes, PROXY_MAX_MSGLEN-bytes, 0); // request line
ret = read_line(conn->cltfd, &line_len, &line, &msgbuff_len, &msgbuff);
bytes += ret; if (ret < 0) {
fprintf(stderr, "Failed receiving request line\n");
return -1;
} }
clt_msg_len = bytes; if (debug == 1) {
fprintf(stdout, "debug - received line: %s\n", line);
}
if (debug) ret = pareqtitl(line, line_len, &(conn->cltreq.titl));
fprintf(stdout, "[%d] Received client message of size %d bytes\n", statem, clt_msg_len); if (ret < 0) {
fprintf(stderr, "Failed parsing request line\n");
return -1;
}
if (debug == 1) {
fprintf(stdout, "debug - parsed request line\n");
}
free(line);
// headers
int next_header = 1;
while (next_header) {
ret = read_line(conn->cltfd, &line_len, &line, &msgbuff_len, &msgbuff);
if (ret < 0) {
fprintf(stderr, "Failed receiving header line\n");
return -1;
}
if (line_len == 0) {
if (debug == 1) {
fprintf(stdout, "debug - reached end of headers\n");
}
next_header = 0;
continue;
}
if (debug == 1) {
fprintf(stdout, "debug - received line: %s\n", line);
}
ret = parshfield(line, line_len, conn->cltreq.hentries);
if (ret < 0) {
fprintf(stderr, "Failed parsing header field\n");
return -1;
}
if (debug == 1) {
fprintf(stdout, "debug - parsed header field\n");
}
free(line);
}
// body
// TODO
if (debug <= 2) {
fprintf(stdout, "printing parsed request\n");
printfpareq(&conn->cltreq);
}
return 0; return 0;
} }
int do_alloc(void) { void do_clear(struct conn *conn) {
clt_msg = (char *) calloc(1, PROXY_MAX_MSGLEN);
if (!clt_msg)
return -1;
srv_msg = (char *) calloc(1, PROXY_MAX_MSGLEN);
if (!srv_msg)
return -1;
clt_data = (struct request *) calloc(1, sizeof(struct request));
if (!clt_data)
return -1;
return 0;
}
void do_clear(void) {
statem = STATEM_RCV_CLT; statem = STATEM_RCV_CLT;
frepareq(&conn->cltreq);
memset(clt_msg, 0, PROXY_MAX_MSGLEN); frepares(&conn->srvres);
memset(srv_msg, 0, PROXY_MAX_MSGLEN);
memset(clt_data, 0, sizeof(struct request));
clt_msg_len = 0;
srv_msg_len = 0;
} }
void do_statem() { void do_statem(struct conn *conn) {
int ret = do_alloc(); int ret = 0;
if (ret < 0) {
do_err();
return;
}
for (int counter = 0; counter < MAX_BOUND; counter++) { for (int counter = 0; counter < MAX_BOUND; counter++) {
switch (statem & (~STATEM_ERR)) { switch (statem & (~STATEM_ERR)) {
case STATEM_RCV_CLT: case STATEM_RCV_CLT:
ret = do_rcv_clt(); ret = do_rcv_clt(conn);
break; break;
case STATEM_PRS_CLT: case STATEM_CON_SRV:
ret = do_prs_clt(); ret = do_con_srv(conn);
break;
case STATEM_CON_SRV:
ret = do_con_srv();
break; break;
case STATEM_FWD_SRV:
ret = do_fwd_srv();
break;
case STATEM_RCV_SRV:
ret = do_rcv_srv();
break;
case STATEM_PRS_SRV:
ret = do_prs_srv();
break;
case STATEM_FWD_CLT:
ret = do_fwd_clt();
break;
default:
ret = -1;
break;
} }
if (ret < 0) if (ret < 0)
@ -421,28 +326,12 @@ void do_statem() {
} }
if (statem & STATEM_FWD_CLT) { if (statem & STATEM_FWD_CLT) {
do_clear(); do_clear(conn);
continue; continue;
} }
statem <<= 1; statem <<= 1;
} }
free_msg_buffs();
free_data();
}
void dohelp() {
printf(
"+====================+\n"
"| HTTP/1.0 PROXY |\n"
"+=====@0xdeadbeer====+\n"
"usage:\n"
" ./proxy [mode]\n"
"mode:\n"
" * server -> start listening as proxy\n"
" * client -> send test requests to server\n"
);
} }
int do_srv(void) { int do_srv(void) {
@ -502,78 +391,42 @@ int do_srv(void) {
return -1; return -1;
} }
if (!ret) { if (ret > 0) {
clt_sock = new_clt_sock; fprintf(stdout, "[PROGRAM] Successfully forked a new child process"
statem = STATEM_RCV_CLT; " with PID %d\n", ret);
do_statem(); continue;
return 0;
} }
fprintf(stdout, "[PROGRAM] Successfully forked a new child process" // child
" with PID %d\n", ret); struct conn *conn = (struct conn *) calloc(1, sizeof(struct conn));
if (!conn) {
fprintf(stderr, "Not enough dynamic memory to establish connection\n");
return -1;
}
conn->cltfd = new_clt_sock;
statem = STATEM_RCV_CLT;
do_statem(conn);
free(conn);
if (debug == 1) {
fprintf(stdout, "Finished proxying client\n");
}
return 0;
} }
return 0; return 0;
} }
int do_clt(void) {
int ret = 0;
int client_socket;
struct sockaddr_in serv_addr;
ret = client_socket = socket(AF_INET, SOCK_STREAM, 0);
if(ret < 0)
{
fprintf(stderr, "Failed creating socket\n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PROXY_PORT);
ret = inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
if(ret <= 0)
{
fprintf(stderr, "Inet_pton error\n");
return -1;
}
ret = connect(client_socket, (struct sockaddr *) &serv_addr,
sizeof(serv_addr));
if(ret < 0)
{
fprintf(stderr, "Failed connecting to remote server\n");
return -1;
}
int bytes = 0;
do {
bytes += send(client_socket, CLIENT_MESSAGE,
sizeof(CLIENT_MESSAGE), 0);
} while (bytes != sizeof(CLIENT_MESSAGE));
fprintf(stdout, "Sent %d bytes to server\n", bytes);
return 0;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int ret; int ret = initres();
if (argc != 2) { if (ret < 0) {
dohelp(); fprintf(stderr, "Failed generating trees\n");
return 0; return -1;
} }
const char *mode = argv[1]; return do_srv();
ret = strcmp(mode, SERVER_MODE);
if (ret == 0)
return do_srv();
ret = strcmp(mode, CLIENT_MODE); fretres();
if (ret == 0)
return do_clt();
fprintf(stderr, "Unknown proxy mode\n");
} }

View File

@ -1,49 +1,28 @@
#ifndef STRUCTS_H #ifndef STRUCTS_H
#define STRUCTS_H #define STRUCTS_H
#define MAX_BOUND 10000 #include "parslib/parslib.h"
#define SERVER_MODE "server" #ifndef MAX_BOUND
#define CLIENT_MODE "client" #define MAX_BOUND 10000
#endif
#define PROXY_PORT 2020 #define PROXY_PORT 2020
#define PROXY_CONN 20 #define PROXY_CONN 20
#define PROXY_MAX_MSGLEN 10000*1024
#define PROXY_DEF_PORT "80"
#define PROXY_BASE_PORT 10
#define REGEX_MATCHN 4
#define REGEX_TITLE "^([A-Z]+)[ ]+([a-zA-Z0-9\\:/_.,-]+)"\
"[ ]+([a-zA-Z0-9_.,/-]+)[\n\r]*$"
#define REGEX_HEADER "^([a-zA-Z0-9_-]*):[ \t]+([^\r\n]*)"
#define REGEX_HOST "^([a-zA-Z0-9_/,.-]+)(:[0-9]+)?$"
#define CLIENT_MESSAGE "GET http://archive.0xdeadbeer.org/ HTTP/1.0\r\n\r\n"\
"Host: archive.0xdeadbeer.org\r\n"\
#define STATEM_RCV_CLT 0b00000001 #define STATEM_RCV_CLT 0b00000001
#define STATEM_PRS_CLT 0b00000010 #define STATEM_CON_SRV 0b00000010
#define STATEM_CON_SRV 0b00000100 #define STATEM_FWD_SRV 0b00000100
#define STATEM_FWD_SRV 0b00001000 #define STATEM_RCV_SRV 0b00001000
#define STATEM_RCV_SRV 0b00010000 #define STATEM_FWD_CLT 0b00010000
#define STATEM_PRS_SRV 0b00100000 #define STATEM_ERR 0b00100000
#define STATEM_FWD_CLT 0b01000000
#define STATEM_ERR 0b10000000
struct header { struct conn {
char *key; int cltfd;
char *value; int srvfd;
};
struct request { struct httpareq cltreq;
char *host_name; struct httpares srvres;
char *host_port;
char *method;
char *uri;
char *ver;
int header_num;
struct header *headers;
}; };
#endif #endif