Spaces:
Paused
Paused
xukc
commited on
Commit
·
2faa101
1
Parent(s):
3b47efe
[fix]support tcp
Browse files- Dockerfile +1 -1
- include/tcp_client.h +48 -0
- include/tcp_client_map.h +50 -0
- tcp_client.cpp +220 -0
Dockerfile
CHANGED
@@ -50,7 +50,7 @@ WORKDIR /app
|
|
50 |
COPY . .
|
51 |
|
52 |
# Compile the C++ program
|
53 |
-
RUN g++ -std=c++14 -g -o proxyServer hv_utils.cpp tcp_inbound.cpp main.cpp -I include
|
54 |
|
55 |
RUN apt-get install -y tzdata
|
56 |
|
|
|
50 |
COPY . .
|
51 |
|
52 |
# Compile the C++ program
|
53 |
+
RUN g++ -std=c++14 -g -o proxyServer hv_utils.cpp tcp_inbound.cpp main.cpp -I include -I include/bolt -lhv -lspdlog
|
54 |
|
55 |
RUN apt-get install -y tzdata
|
56 |
|
include/tcp_client.h
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef PROXYSERVER_TCP_CLIENT_H
|
2 |
+
#define PROXYSERVER_TCP_CLIENT_H
|
3 |
+
|
4 |
+
#include "hv/hloop.h"
|
5 |
+
#include "hv/TcpClient.h"
|
6 |
+
#include "bolt/crypt.h"
|
7 |
+
|
8 |
+
class tcpBoltConfig
|
9 |
+
{
|
10 |
+
public:
|
11 |
+
uint32_t request_id;
|
12 |
+
uint32_t session_id;
|
13 |
+
std::string signal_id;
|
14 |
+
std::string data_st;
|
15 |
+
bool encrypt = false;
|
16 |
+
CRYPT_TYPE ept_type;
|
17 |
+
char ept_key;
|
18 |
+
};
|
19 |
+
|
20 |
+
class TcpClientBolt {
|
21 |
+
public:
|
22 |
+
TcpClientBolt(hio_t* _io) : io(_io) {}
|
23 |
+
|
24 |
+
bool hasHandshake() { return cli.isConnected(); }
|
25 |
+
|
26 |
+
bool handShake(void* buf, int readbytes);
|
27 |
+
|
28 |
+
int send(char* data, int size);
|
29 |
+
|
30 |
+
void close();
|
31 |
+
|
32 |
+
private:
|
33 |
+
bool connect(struct sockaddr* addr);
|
34 |
+
void onConnection(const hv::SocketChannelPtr &channel);
|
35 |
+
void onDisConnection(const hv::SocketChannelPtr &channel);
|
36 |
+
void onRecv(const hv::SocketChannelPtr &channel, hv::Buffer *buf);
|
37 |
+
void onWrited(const hv::SocketChannelPtr &channel, hv::Buffer *buf);
|
38 |
+
|
39 |
+
private:
|
40 |
+
hv::TcpClient cli;
|
41 |
+
hio_t* io;
|
42 |
+
struct sockaddr_in t_addr, u_addr;
|
43 |
+
uint32_t session_id;
|
44 |
+
bool is_bolt_server;
|
45 |
+
tcpBoltConfig *config;
|
46 |
+
};
|
47 |
+
|
48 |
+
#endif //PROXYSERVER_TCP_CLIENT_H
|
include/tcp_client_map.h
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef PROXYSERVER_CONN_MAP_H
|
2 |
+
#define PROXYSERVER_CONN_MAP_H
|
3 |
+
|
4 |
+
#include <iostream>
|
5 |
+
#include <map>
|
6 |
+
#include <shared_mutex>
|
7 |
+
#include <mutex>
|
8 |
+
|
9 |
+
template <typename Key, typename Value>
|
10 |
+
class TcpClientMap {
|
11 |
+
public:
|
12 |
+
static TcpClientMap& getInstance() {
|
13 |
+
static TcpClientMap instance; // 在首次使用时创建
|
14 |
+
return instance;
|
15 |
+
}
|
16 |
+
|
17 |
+
TcpClientMap(TcpClientMap const&) = delete;
|
18 |
+
void operator=(TcpClientMap const&) = delete;
|
19 |
+
|
20 |
+
void add(const Key& key, const Value& value) {
|
21 |
+
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
|
22 |
+
map_[key] = value;
|
23 |
+
}
|
24 |
+
|
25 |
+
Value* get(const Key& key) const {
|
26 |
+
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
|
27 |
+
auto it = map_.find(key);
|
28 |
+
if (it != map_.end()) {
|
29 |
+
return (Value*)(&(it->second));
|
30 |
+
}
|
31 |
+
return nullptr;
|
32 |
+
}
|
33 |
+
|
34 |
+
void remove(const Key& key) {
|
35 |
+
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
|
36 |
+
map_.erase(key);
|
37 |
+
}
|
38 |
+
|
39 |
+
void clear() {
|
40 |
+
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
|
41 |
+
map_.clear();
|
42 |
+
}
|
43 |
+
|
44 |
+
private:
|
45 |
+
TcpClientMap() = default;
|
46 |
+
mutable std::shared_timed_mutex mutex_;
|
47 |
+
std::map<Key, Value> map_;
|
48 |
+
};
|
49 |
+
|
50 |
+
#endif //PROXYSERVER_CONN_MAP_H
|
tcp_client.cpp
ADDED
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "include/tcp_client.h"
|
2 |
+
#include "bolt/datagram.h"
|
3 |
+
#include "include/tcp_client_map.h"
|
4 |
+
#include "spdlog/spdlog.h"
|
5 |
+
#include <string>
|
6 |
+
#include <time.h>
|
7 |
+
|
8 |
+
char generateRandomKey()
|
9 |
+
{
|
10 |
+
// 使用时间作为随机数种子,确保每次运行生成的随机数都不同
|
11 |
+
srand((unsigned int)time(NULL));
|
12 |
+
|
13 |
+
// 生成 'A' 到 'Z' 之间的随机字符
|
14 |
+
return (char)('A' + rand() % 26);
|
15 |
+
}
|
16 |
+
|
17 |
+
void TcpClientBolt::onConnection(const hv::SocketChannelPtr &channel)
|
18 |
+
{
|
19 |
+
char buff[2];
|
20 |
+
buff[0] = BOLT_CHANNEL_CMD_TCP_HANDSHAKE_RESPONSE;
|
21 |
+
buff[1] = BOLT_TCP_HANDSHAKE_SUCESS;
|
22 |
+
int len = sizeof(buff);
|
23 |
+
|
24 |
+
PACK_TUNNEL_DATA(boltdata, boltdata_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_CMD, t_addr.sin_addr.s_addr, t_addr.sin_port, u_addr.sin_addr.s_addr, u_addr.sin_port, session_id, 0, 0, buff, len);
|
25 |
+
hio_write(io, buff, len);
|
26 |
+
}
|
27 |
+
|
28 |
+
void TcpClientBolt::onRecv(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
|
29 |
+
{
|
30 |
+
auto config = TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().get(session_id);
|
31 |
+
if (config)
|
32 |
+
{
|
33 |
+
if (config->encrypt)
|
34 |
+
{
|
35 |
+
switch (config->ept_type)
|
36 |
+
{
|
37 |
+
case CRYPT_TYPE::XOR:
|
38 |
+
xor_::crypt((char *)buf->data(), buf->size(), config->ept_key);
|
39 |
+
break;
|
40 |
+
|
41 |
+
default:
|
42 |
+
break;
|
43 |
+
}
|
44 |
+
}
|
45 |
+
}
|
46 |
+
|
47 |
+
hio_write(io, buf->data(), buf->size());
|
48 |
+
}
|
49 |
+
|
50 |
+
void TcpClientBolt::onWrited(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
|
51 |
+
{
|
52 |
+
|
53 |
+
}
|
54 |
+
|
55 |
+
void TcpClientBolt::onDisConnection(const hv::SocketChannelPtr &channel)
|
56 |
+
{
|
57 |
+
hio_close(io);
|
58 |
+
}
|
59 |
+
|
60 |
+
bool TcpClientBolt::handShake(void *buf, int readbytes)
|
61 |
+
{
|
62 |
+
if (readbytes > 0)
|
63 |
+
{
|
64 |
+
UNPACK_TUNNEL_DATA(dest, dest_len, ver, res, type, t_ip, t_port, u_ip, u_port, session_id, extend, extend_len, buf, readbytes)
|
65 |
+
if (ver == BOLT_VERSION && res == BOLT_RESERVE)
|
66 |
+
{
|
67 |
+
if (type == BOLT_PAYLOAD_TYPE_CMD)
|
68 |
+
{
|
69 |
+
if (dest_len == 1 && dest[0] == BOLT_CHANNEL_CMD_TCP_HANDSHAKE_REQUEST)
|
70 |
+
{
|
71 |
+
this->t_addr.sin_addr.s_addr = t_ip;
|
72 |
+
this->t_addr.sin_port = t_port;
|
73 |
+
this->u_addr.sin_addr.s_addr = u_ip;
|
74 |
+
this->u_addr.sin_port = u_port;
|
75 |
+
this->session_id = session_id;
|
76 |
+
this->config = TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().get(session_id);
|
77 |
+
if (!this->config)
|
78 |
+
{
|
79 |
+
char buff[1] = {BOLT_CHANNEL_CMD_INVALID_RESPONSE};
|
80 |
+
int len = sizeof(buff);
|
81 |
+
|
82 |
+
PACK_TUNNEL_DATA(boltdata, boltdata_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_CMD, t_addr.sin_addr.s_addr, t_addr.sin_port, u_addr.sin_addr.s_addr, u_addr.sin_port, session_id, 0, 0, buff, len);
|
83 |
+
hio_write(io, buff, len);
|
84 |
+
hio_close_async(io);
|
85 |
+
return false;
|
86 |
+
}
|
87 |
+
|
88 |
+
if (!this->connect((struct sockaddr *)&t_addr))
|
89 |
+
{
|
90 |
+
char buff[2];
|
91 |
+
buff[0] = BOLT_CHANNEL_CMD_TCP_HANDSHAKE_RESPONSE;
|
92 |
+
buff[1] = BOLT_TCP_HANDSHAKE_FAIL_TIMEOUT;
|
93 |
+
int len = sizeof(buff);
|
94 |
+
|
95 |
+
PACK_TUNNEL_DATA(boltdata, boltdata_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_CMD, t_addr.sin_addr.s_addr, t_addr.sin_port, u_addr.sin_addr.s_addr, u_addr.sin_port, session_id, 0, 0, buff, len);
|
96 |
+
hio_write(io, buff, len);
|
97 |
+
hio_close_async(io);
|
98 |
+
return false;
|
99 |
+
}
|
100 |
+
return true;
|
101 |
+
}
|
102 |
+
else if (dest_len == 73 && dest[0] == BOLT_CHANNEL_CMD_BIND_REQUEST)
|
103 |
+
{
|
104 |
+
UNPACK_BIND_DATA(command, request_id, signal_id, session_id, data_st, dest, dest_len)
|
105 |
+
this->session_id = session_id;
|
106 |
+
tcpBoltConfig config;
|
107 |
+
config.request_id = request_id;
|
108 |
+
config.session_id = session_id;
|
109 |
+
config.data_st = data_st;
|
110 |
+
config.signal_id = signal_id;
|
111 |
+
if (extend_len >= 5)
|
112 |
+
{
|
113 |
+
if (strcmp(extend, "ept=1") == 0)
|
114 |
+
{
|
115 |
+
config.encrypt = true;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
config.ept_type = config.encrypt ? CRYPT_TYPE::XOR : CRYPT_TYPE::NONE;
|
119 |
+
config.ept_key = config.encrypt ? generateRandomKey() : 0;
|
120 |
+
|
121 |
+
GENERATE_DECRYPT_KEY(extend_response, extend_response_len, config.ept_type, config.ept_key)
|
122 |
+
|
123 |
+
char result[1] = {BOLT_BIND_RESPONSE_CODE_SUCESS};
|
124 |
+
|
125 |
+
PACK_BIND_RESPONSE_DATA(bind_response, bind_response_len, BOLT_CHANNEL_CMD_BIND_RESPONSE, request_id, session_id, result)
|
126 |
+
|
127 |
+
PACK_TUNNEL_DATA(response, response_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_CMD, 0, 0, 0, 0, session_id, extend_response, extend_response_len, bind_response, bind_response_len)
|
128 |
+
hio_write(io, response, response_len);
|
129 |
+
|
130 |
+
TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().add(session_id, config);
|
131 |
+
is_bolt_server = true;
|
132 |
+
}
|
133 |
+
else if (dest_len == 9 && dest[0] == BOLT_CHANNEL_CMD_UNBIND_REQUEST)
|
134 |
+
{
|
135 |
+
UNPACK_UNBIND_DATA(command, session_id, code, dest, dest_len)
|
136 |
+
|
137 |
+
TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().remove(session_id);
|
138 |
+
this->close();
|
139 |
+
hio_close_async(io);
|
140 |
+
}
|
141 |
+
}
|
142 |
+
}
|
143 |
+
}
|
144 |
+
return false;
|
145 |
+
}
|
146 |
+
|
147 |
+
bool TcpClientBolt::connect(struct sockaddr *addr)
|
148 |
+
{
|
149 |
+
int connfd = cli.createsocket(addr);
|
150 |
+
if (connfd < 0)
|
151 |
+
{
|
152 |
+
return false;
|
153 |
+
}
|
154 |
+
cli.onConnection = [this](const hv::SocketChannelPtr &channel)
|
155 |
+
{
|
156 |
+
std::string peeraddr = channel->peeraddr();
|
157 |
+
if (channel->isConnected())
|
158 |
+
{
|
159 |
+
spdlog::info("connected to {}! connfd={}\n", peeraddr.c_str(), channel->fd());
|
160 |
+
|
161 |
+
onConnection(channel);
|
162 |
+
}
|
163 |
+
else
|
164 |
+
{
|
165 |
+
spdlog::info("disconnected to {}! connfd={}\n", peeraddr.c_str(), channel->fd());
|
166 |
+
|
167 |
+
onDisConnection(channel);
|
168 |
+
}
|
169 |
+
};
|
170 |
+
|
171 |
+
cli.onMessage = [this](const hv::SocketChannelPtr &channel, hv::Buffer *buf)
|
172 |
+
{
|
173 |
+
spdlog::info("<<< len:{} : {}}\n", (int)buf->size(), (char *)buf->data());
|
174 |
+
|
175 |
+
onRecv(channel, buf);
|
176 |
+
};
|
177 |
+
|
178 |
+
cli.onWriteComplete = [this](const hv::SocketChannelPtr &channel, hv::Buffer *buf)
|
179 |
+
{
|
180 |
+
spdlog::info(">>> len:{} : {}}\n", (int)buf->size(), (char *)buf->data());
|
181 |
+
|
182 |
+
onWrited(channel, buf);
|
183 |
+
};
|
184 |
+
|
185 |
+
cli.start();
|
186 |
+
return true;
|
187 |
+
}
|
188 |
+
|
189 |
+
int TcpClientBolt::send(char *data, int size)
|
190 |
+
{
|
191 |
+
if (cli.isConnected())
|
192 |
+
{
|
193 |
+
auto config = TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().get(session_id);
|
194 |
+
if (config)
|
195 |
+
{
|
196 |
+
if (config->encrypt)
|
197 |
+
{
|
198 |
+
switch (config->ept_type)
|
199 |
+
{
|
200 |
+
case CRYPT_TYPE::XOR:
|
201 |
+
xor_::crypt(data, size, config->ept_key);
|
202 |
+
break;
|
203 |
+
|
204 |
+
default:
|
205 |
+
break;
|
206 |
+
}
|
207 |
+
}
|
208 |
+
}
|
209 |
+
return cli.send(data, size);
|
210 |
+
}
|
211 |
+
else
|
212 |
+
{
|
213 |
+
return -1;
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
void TcpClientBolt::close()
|
218 |
+
{
|
219 |
+
cli.closesocket();
|
220 |
+
}
|