1 Star 0 Fork 1

陈佳龙/tls_proxy

forked from lrioxh/tls_proxy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
agent.c 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
// socket in block mode without epoll
#include "include/proxy_lib.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#define CLIENT_PORT 9527
#define SERVER_PORT 9528
#define SERVER_IP "192.168.144.1"
const char *const certificate_path = "ssl/agent.crt";
const char *const private_key_path = "ssl/agent.key";
int SocketInitClnt(int *fd)
{
int tfd = 0;
tfd = socket(AF_INET, SOCK_STREAM, 0); //创建一个监听的套接字
if (tfd == -1) return 0;
struct sockaddr_in ServeraAddr;
ServeraAddr.sin_family = AF_INET;
ServeraAddr.sin_port = htons(SERVER_PORT);
ServeraAddr.sin_addr.s_addr = htonl(INADDR_ANY);
socklen_t serverAddrLen = sizeof(ServeraAddr);
if (bind(tfd, (struct sockaddr *)&ServeraAddr, serverAddrLen) == -1 || listen(tfd, 128) == -1)
{
return 0;
}
int cfd = 0;
struct sockaddr_in ClientAddr;
socklen_t clientLen = sizeof(ClientAddr);
cfd = accept(tfd, (struct sockaddr *)&ClientAddr, &clientLen);
if (cfd == -1)
{
return 0;
}
else
{
*fd = cfd;
return 1;
}
}
int SocketInitServ(int *fd)
{
*fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in Agentaddr;
Agentaddr.sin_family = AF_INET;
Agentaddr.sin_port = htons(CLIENT_PORT);
inet_pton(AF_INET, SERVER_IP, &Agentaddr.sin_addr.s_addr);
socklen_t agentAddrLen = sizeof(Agentaddr);
if (connect(*fd, (struct sockaddr *)&Agentaddr, agentAddrLen) == -1)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
ProxyStates *states = InitProxyStates();
if ( // load cert
!loadCertFile(states, certificate_path) || !loadKeyFile(states, private_key_path))
{
return 0;
}
int clientFd = -1, serverFd = -1;
if (!SocketInitClnt(&clientFd) || !SocketInitServ(&serverFd))
{
handle_error("socket connect failed");
return 0;
}
while (1)
{
unsigned char *recvData = NULL;
recvData = (unsigned char *)malloc(BUFSIZ);
int recvBytes = 0;
recvBytes = read(clientFd, recvData, BUFSIZ);
if (recvBytes > 0)
{
if (!HandleMsg(states, recvData, &recvBytes, C2S))
{
FreeProxyStates(states);
break;
}
int ret = write(serverFd, recvData, recvBytes);
if (ret < 0)
{
handle_error("client write failed");
break;
}
}
else if (recvBytes == 0)
{
}
else
{
}
recvBytes = read(serverFd, recvData, BUFSIZ);
if (recvBytes > 0)
{
if (!HandleMsg(states, recvData, &recvBytes, S2C))
{
FreeProxyStates(states);
break;
}
int ret = write(clientFd, recvData, recvBytes);
if (ret < 0)
{
handle_error("server write failed");
break;
}
}
else if (recvBytes == 0)
{
}
else
{
}
free(recvData);
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/cjlionel/tls_proxy.git
git@gitee.com:cjlionel/tls_proxy.git
cjlionel
tls_proxy
tls_proxy
main

搜索帮助