修改: Makefile

修改:     README.md
	新文件:   conf.c
	新文件:   conf.h
	修改:     cproxy.c
	修改:     cproxy.h
	修改:     cproxy_request.c
	新文件:   cproxy_request.h
	新文件:   log/cproxy.pid
This commit is contained in:
aixiao 2019-01-19 16:13:58 +08:00
parent daa409875b
commit 266aade849
9 changed files with 273 additions and 118 deletions

View File

@ -1,15 +1,15 @@
CC = gcc
CFLAGS = -g -Wall -Werror -I./iniparser/src -L ./iniparser
CFLAGS = -g -Wall -Werror -I../iniparser/src -L../iniparser
LIBS = -liniparser
OBJ := cproxy
all: cproxy
cproxy: cproxy.o
$(CC) $(CFLAGS) cproxy.o cproxy_request.o -o cproxy $(LIBS)
cproxy.o:
$(CC) $(CFLAGS) -c cproxy.c cproxy_request.c
all: cproxy.o cproxy_request.o conf.o
$(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS)
strip $(OBJ)
-chmod 755 $(OBJ)
.c.o:
$(CC) $(CFLAGS) -c $< $(LIBS)
clean:
rm -rf *.o
rm cproxy
rm $(OBJ)

View File

@ -5,9 +5,8 @@
# Build
git clone https://github.com/niuyuling/cproxy.git
cd cproxy
git clone https://github.com/ndevilla/iniparser.git
cd iniparser
make
cd ..
cd ../cproxy
make clean; make

113
conf.c Normal file
View File

@ -0,0 +1,113 @@
#include "conf.h"
void read_conf(char *file, conf * p)
{
dictionary *ini = iniparser_load(file);
// server module
p->server_port = iniparser_getint(ini, "server:PORT", 0);
p->len_server_pid_file =
strlen(iniparser_getstring(ini, "server:PID_FILE", NULL)) + 1;
p->server_pid_file = (char *)malloc(p->len_server_pid_file);
if (p->server_pid_file == NULL) {
goto err;
}
memset(p->server_pid_file, 0, p->len_server_pid_file);
memcpy(p->server_pid_file,
iniparser_getstring(ini, "server:PID_FILE", NULL),
p->len_server_pid_file);
// http module
p->len_http_ip = strlen(iniparser_getstring(ini, "http:http_ip", NULL)) + 1;
p->http_ip = (char *)malloc(p->len_http_ip);
if (p->http_ip == NULL) {
goto err;
}
memset(p->http_ip, 0, p->len_http_ip);
memcpy(p->http_ip, iniparser_getstring(ini, "http:http_ip", NULL),
p->len_http_ip);
p->http_port = iniparser_getint(ini, "http:http_port", 0);
p->len_http_del =
strlen(iniparser_getstring(ini, "http:http_del", NULL)) + 1;
p->http_del = (char *)malloc(p->len_http_del);
if (p->http_del == NULL) {
goto err;
}
memset(p->http_del, 0, p->len_http_del);
memcpy(p->http_del, iniparser_getstring(ini, "http:http_del", NULL),
p->len_http_del);
p->len_http_first =
strlen(iniparser_getstring(ini, "http:http_first", NULL)) + 1;
p->http_first = (char *)malloc(p->len_http_first);
if (p->http_first == NULL) {
goto err;
}
memset(p->http_first, 0, p->len_http_first);
memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL),
p->len_http_first);
// https module
p->len_https_ip =
strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1;
p->https_ip = (char *)malloc(p->len_https_ip);
if (p->https_ip == NULL) {
goto err;
}
memset(p->https_ip, 0, p->len_http_ip);
memcpy(p->https_ip, iniparser_getstring(ini, "https:https_ip", NULL),
p->len_https_ip);
p->https_port = iniparser_getint(ini, "https:https_port", 0);
p->len_https_del =
strlen(iniparser_getstring(ini, "https:https_del", NULL)) + 1;
p->https_del = (char *)malloc(p->len_https_del);
if (p->https_del == NULL) {
goto err;
}
memset(p->https_del, 0, p->len_https_del);
memcpy(p->https_del, iniparser_getstring(ini, "https:https_del", NULL),
p->len_https_del);
p->len_https_first =
strlen(iniparser_getstring(ini, "https:https_first", NULL)) + 1;
p->https_first = (char *)malloc(p->len_https_first);
if (p->https_first == NULL) {
goto err;
}
memset(p->https_first, 0, p->len_https_first);
memcpy(p->https_first, iniparser_getstring(ini, "https:https_first", NULL),
p->len_https_first);
err:
if (p->server_pid_file == NULL)
free(p->server_pid_file);
if (p->http_ip == NULL)
free(p->http_ip);
if (p->http_del == NULL)
free(p->http_del);
if (p->http_first == NULL)
free(p->http_first);
if (p->https_ip == NULL)
free(p->https_ip);
if (p->https_del == NULL)
free(p->https_del);
if (p->https_first == NULL)
free(p->https_first);
iniparser_freedict(ini);
}
void free_conf(conf * p)
{
free(p->server_pid_file);
free(p->http_ip);
free(p->http_del);
free(p->http_first);
free(p->https_ip);
free(p->https_del);
free(p->https_first);
}

7
conf.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef CONF_H
#define CONF_H
#include "iniparser.h"
#include "cproxy.h"
#endif

107
cproxy.c
View File

@ -1,37 +1,3 @@
#include <arpa/inet.h>
#include <errno.h>
#include <libgen.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <errno.h>
#include "iniparser.h"
#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#define BUF_SIZE 8192
#define MAX_HEADER_SIZE 8192
char remote_host[128];
int remote_port;
int local_port;
int server_sock;
int client_sock;
int remote_sock;
char *header_buffer;
int HTTPS = 0;
#include "cproxy.h"
ssize_t readLine(int fd, void *buffer, size_t n)
@ -264,11 +230,12 @@ int create_connection()
}
// 处理客户端的连接
void handle_client(int client_sock, struct sockaddr_in client_addr)
void handle_client(int client_sock, struct sockaddr_in client_addr, conf *m)
{
int HTTPS = 0;
read_header(client_sock, header_buffer);
extract_host(header_buffer);
replacement_http_head(header_buffer, remote_host, &remote_port, &HTTPS);
replacement_http_head(header_buffer, remote_host, &remote_port, &HTTPS, m);
printf("%s\n", remote_host);
printf("%d\n", remote_port);
@ -290,7 +257,16 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
write(remote_sock, buffer, n);
}
}
if (HTTPS == 1) {
forward_header(remote_sock); //普通的http请求先转发header
forward_data(client_sock, remote_sock);
} else {
char buffer[BUF_SIZE];
int n;
while ((n = read(client_sock, buffer, BUF_SIZE)) > 0) {
write(remote_sock, buffer, n);
}
}
exit(0);
}
if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端
@ -306,18 +282,9 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
}
// 守护
int init_daemon(int nochdir, int noclose)
int init_daemon(int nochdir, int noclose, conf * p)
{
char *inifile = "conf/cproxy.ini";
dictionary *ini = iniparser_load(inifile);
const char *PID_FILE = iniparser_getstring(ini, "server:PID_FILE", NULL);
FILE *fp = fopen(PID_FILE, "w");
if (NULL == fp) {
printf("Pid File Can Not Open To Write\n");
errno = 0;
mkdir("log", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
perror("mkdir");
}
FILE *fp = fopen(p->server_pid_file, "w");
int pid;
if ((pid = fork()) < 0) {
@ -360,10 +327,29 @@ int init_daemon(int nochdir, int noclose)
open("/dev/null", O_RDWR);
}
fclose(fp);
iniparser_freedict(ini);
return 0;
}
// 判断pid文件存放目录
void log_dir(conf *p)
{
char *d = strchr(p->server_pid_file, '/');
if (d != NULL) {
int l_d = p->len_server_pid_file - strlen(d);
char pid_dir[l_d];
strncpy(pid_dir, p->server_pid_file, l_d - 1);
printf("%d\n", l_d);
printf("%s\n", pid_dir);
if (access(pid_dir, F_OK) != 0) {
printf("Pid File Can Not Open To Write\n");
errno = 0;
mkdir("log", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
perror("mkdir");
}
}
}
void sigchld_handler(int signal)
{
while (waitpid(-1, NULL, WNOHANG) > 0) ;
@ -373,8 +359,21 @@ void sigchld_handler(int signal)
int _main(int argc, char *argv[])
{
char *inifile = "conf/cproxy.ini";
dictionary *ini = iniparser_load(inifile);
int PORT = iniparser_getint(ini, "server:PORT", 0);
conf *m = (struct CONF *)malloc(sizeof(struct CONF));
read_conf(inifile, m);
int PORT = m->server_port;
/*
printf("%d\n", m->server_port);
printf("%s\n", m->server_pid_file);
printf("%s\n", m->http_ip);
printf("%d\n", m->http_port);
printf("%s\n", m->http_del);
printf("%s\n", m->http_first);
printf("%s\n", m->https_ip);
printf("%d\n", m->https_port);
printf("%s\n", m->https_del);
printf("%s\n", m->https_first);
*/
int opt;
char optstrs[] = ":l:d";
@ -384,7 +383,7 @@ int _main(int argc, char *argv[])
PORT = atoi(optarg);
break;
case 'd':{
init_daemon(1, 1);
init_daemon(1, 1, m);
}
break;
default:
@ -425,13 +424,13 @@ int _main(int argc, char *argv[])
if (fork() == 0) { // 创建子进程处理客户端连接请求
close(server_sock);
handle_client(client_socket, clnt_addr);
handle_client(client_socket, clnt_addr, m);
exit(0);
}
close(client_socket);
}
free(header_buffer); // 收回内存
iniparser_freedict(ini);
free_conf(m);
}
int main(int argc, char *argv[])

View File

@ -1,3 +1,56 @@
#ifndef CPROXY_H
#define CPROXY_H
#include <arpa/inet.h>
#include <errno.h>
#include <libgen.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <errno.h>
#include "iniparser.h"
#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#define BUF_SIZE 8192
#define MAX_HEADER_SIZE 8192
typedef struct CONF {
int server_port; // server module
char *server_pid_file;
int http_port; // http module
char *http_ip, *http_del, *http_first;
int https_port; // https module
char *https_ip, *https_del, *https_first;
int len_server_pid_file; // length
int len_http_ip, len_http_del, len_http_first;
int len_https_ip, len_https_del, len_https_first;
} conf;
void read_conf(char *file, conf * p);
void free_conf(conf * p);
char remote_host[128];
int remote_port;
int local_port;
int server_sock;
int client_sock;
int remote_sock;
char *header_buffer;
ssize_t readLine(int fd, void *buffer, size_t n);
int read_header(int fd, void *buffer);
void extract_server_path(const char *header, char *output);
@ -10,9 +63,11 @@ void rewrite_header();
int receive_data(int socket, char *buffer, int len);
int send_data(int socket, char *buffer, int len);
int create_connection();
void handle_client(int client_sock, struct sockaddr_in client_addr);
int init_daemon(int nochdir, int noclose);
void handle_client(int client_sock, struct sockaddr_in client_addr, conf * p);
int init_daemon(int nochdir, int noclose, conf *p);
void sigchld_handler(int signal);
int _main(int argc, char *argv[]);
int replacement_http_head(char *header_buffer, char *remote_host,
int *remote_port, int *HTTPS);
int *remote_port, int *HTTPS, conf * p);
#endif

View File

@ -1,17 +1,4 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"
const char *http_ip;
int http_port;
const char *http_del;
const char *http_first;
const char *https_ip;
int https_port;
const char *https_del;
const char *https_first;
#include "cproxy_request.h"
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen);
@ -87,6 +74,7 @@ void del_chr(char *s, char ch)
*t = '\0'; //置目标串结束符。
}
// strncpy()封装
char *strncpy_(char *dest, const char *src, size_t n)
{
int size = sizeof(char) * (n + 1);
@ -129,37 +117,24 @@ char *delete_header(char *header_buffer, const char *ch, int str)
}
int replacement_http_head(char *header_buffer, char *remote_host,
int *remote_port, int *HTTPS)
int *remote_port, int *HTTPS, conf *p)
{
char *file = "conf/cproxy.ini";
dictionary *ini = iniparser_load(file);
// 读取配置
http_ip = iniparser_getstring(ini, "http:http_ip", NULL);
http_port = iniparser_getint(ini, "http:http_port", 0);
http_del = iniparser_getstring(ini, "http:http_del", NULL);
http_first = iniparser_getstring(ini, "http:http_first", NULL);
https_ip = iniparser_getstring(ini, "https:https_ip", NULL);
https_port = iniparser_getint(ini, "https:https_port", 0);
https_del = iniparser_getstring(ini, "https:https_del", NULL);
https_first = iniparser_getstring(ini, "https:https_first", NULL);
char *http_firsts = (char *)malloc(strlen(http_first) + 1);
strcpy(http_firsts, http_first); // 拷贝http_first
char *https_firsts = (char *)malloc(strlen(https_first) + 1);
strcpy(https_firsts, https_first); // 拷贝https_first
char *http_firsts = (char *)malloc(strlen(p->http_first) + 1);
strcpy(http_firsts, p->http_first); // 拷贝http_first
char *https_firsts = (char *)malloc(strlen(p->https_first) + 1);
strcpy(https_firsts, p->https_first); // 拷贝https_first
char *header_buffers = (char *)malloc(strlen(header_buffer) + 1); // 拷贝原字符串
strcpy(header_buffers, header_buffer);
char *new_http_del = malloc(strlen(http_del) + 1); // 拷贝http_del
strcpy(new_http_del, http_del);
char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del
strcpy(new_http_del, p->http_del);
char *new_https_del = malloc(strlen(https_del) + 1); // // 拷贝https_del
strcpy(new_https_del, https_del);
char *new_https_del = malloc(strlen(p->https_del) + 1); // // 拷贝https_del
strcpy(new_https_del, p->https_del);
char *p = strstr(header_buffers, "CONNECT"); // 判断是否是http 隧道请求
if (p == NULL) {
char *con = strstr(header_buffers, "CONNECT"); // 判断是否是http 隧道请求
if (con == NULL) {
char *result = NULL;
result = strtok(new_http_del, ",");
while (result != NULL) {
@ -224,8 +199,8 @@ int replacement_http_head(char *header_buffer, char *remote_host,
len = strlen(new_header_buffer);
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
stpcpy(remote_host, http_ip);
*remote_port = http_port;
stpcpy(remote_host, p->http_ip);
*remote_port = p->http_port;
memset(header_buffer, 0, strlen(header_buffer));
strcpy(header_buffer, new_header_buffer);
free(HTTP_HEAD);
@ -243,10 +218,8 @@ int replacement_http_head(char *header_buffer, char *remote_host,
}
//delete_header(header_buffers, "Host", '\n'); // 删除HOST,改变原字符串
//delete_header(header_buffers, "x-online-host", '\n'); // 删除HOST,改变原字符串
char *new_header_buffer = (char *)
malloc(strlen(splice_head(header_buffers, "\n", https_firsts)) + 1);
strcpy(new_header_buffer,
splice_head(header_buffers, "\n", https_firsts));
char *new_header_buffer = (char *)malloc(strlen(splice_head(header_buffers, "\n", https_firsts)) + 1);
strcpy(new_header_buffer, splice_head(header_buffers, "\n", https_firsts));
char *p2 = strstr(header_buffers, "\n");
p2 = p2 + 1;
@ -300,10 +273,10 @@ int replacement_http_head(char *header_buffer, char *remote_host,
len = strlen(new_header_buffer);
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
//stpcpy(remote_host, https_ip);
//*remote_port = 80;
//memset(header_buffer, 0, strlen(header_buffer));
//strcpy(header_buffer, new_header_buffer);
//stpcpy(remote_host, p->https_ip);
//*remote_port = p->https_port;
memset(header_buffer, 0, strlen(header_buffer));
strcpy(header_buffer, new_header_buffer);
free(HTTPS_HEAD);
free(M);
@ -317,6 +290,5 @@ int replacement_http_head(char *header_buffer, char *remote_host,
free(new_http_del);
free(new_https_del);
free(header_buffers);
iniparser_freedict(ini);
return *HTTPS;
}

9
cproxy_request.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef CPROXY_REQUEST_H
#define CPROXY_REQUEST_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cproxy.h"
#endif

1
log/cproxy.pid Normal file
View File

@ -0,0 +1 @@
3006