xukc commited on
Commit
c8aad12
·
1 Parent(s): 57ff546

[fix]tcp runtime error

Browse files
.vscode/launch.json CHANGED
@@ -6,7 +6,7 @@
6
  "type": "cppdbg",
7
  "request": "launch",
8
  "program": "${workspaceFolder}/proxyServer",
9
- "args": ["0", "8080"],
10
  "stopAtEntry": false,
11
  "cwd": "${workspaceFolder}",
12
  "environment": [],
 
6
  "type": "cppdbg",
7
  "request": "launch",
8
  "program": "${workspaceFolder}/proxyServer",
9
+ "args": ["0", "8000"],
10
  "stopAtEntry": false,
11
  "cwd": "${workspaceFolder}",
12
  "environment": [],
main.cpp CHANGED
@@ -17,23 +17,25 @@
17
  #include "spdlog/async.h"
18
  #include "spdlog/sinks/stdout_color_sinks.h"
19
 
20
- static const char* host = "0.0.0.0";
21
  static int port = 8080;
22
  static int thread_num = 4;
23
  static hloop_t *tcpaccept_loop = NULL, *udpbind_loop = NULL;
24
 
25
- static void new_conn_event(hevent_t* ev) {
26
- hloop_t* loop = ev->loop;
27
- hio_t* io = (hio_t*)hevent_userdata(ev);
 
28
  hio_attach(loop, io);
29
 
30
  tcp_on_accept(io, ev);
31
  }
32
 
33
- static void on_accept(hio_t* io) {
 
34
  hio_detach(io);
35
 
36
- hloop_t* worker_loop = get_next_loop();
37
  hevent_t ev;
38
  memset(&ev, 0, sizeof(ev));
39
  ev.loop = worker_loop;
@@ -42,16 +44,19 @@ static void on_accept(hio_t* io) {
42
  hloop_post_event(worker_loop, &ev);
43
  }
44
 
45
- static HTHREAD_RETTYPE worker_thread(void* userdata) {
46
- hloop_t* loop = (hloop_t*)userdata;
 
47
  hloop_run(loop);
48
  return 0;
49
  }
50
 
51
- static HTHREAD_RETTYPE tcpaccept_thread(void* userdata) {
52
- hloop_t* loop = (hloop_t*)userdata;
53
- hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
54
- if (listenio == NULL) {
 
 
55
  exit(1);
56
  }
57
  int listeniofd = hio_fd(listenio);
@@ -60,10 +65,12 @@ static HTHREAD_RETTYPE tcpaccept_thread(void* userdata) {
60
  return 0;
61
  }
62
 
63
- static HTHREAD_RETTYPE udpbind_thread(void* userdata) {
64
- hloop_t* loop = (hloop_t*)userdata;
65
- hio_t* bindio = hloop_create_udp_server(loop, host, port);
66
- if (bindio == NULL) {
 
 
67
  exit(1);
68
  }
69
  int bindfd = hio_fd(bindio);
@@ -77,17 +84,20 @@ static HTHREAD_RETTYPE udpbind_thread(void* userdata) {
77
  return 0;
78
  }
79
 
80
- int main(int argc, char** argv) {
 
81
  int type = 0;
82
- if (argc == 3) {
 
83
  type = atoi(argv[1]);
84
  port = atoi(argv[2]);
85
  }
86
  int cores = std::thread::hardware_concurrency();
87
- if (cores > 0) {
 
88
  thread_num = cores;
89
  }
90
-
91
  auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
92
  auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);
93
 
@@ -98,24 +108,23 @@ int main(int argc, char** argv) {
98
  spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
99
  "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));
100
 
101
-
102
  spdlog::info("ProxyServer start: threadNum:{:d}", thread_num);
103
 
104
  if (type == 1)
105
  {
106
- //Udp
107
  spdlog::info("ProxyServer start: udp->{}", port);
108
  udpbind_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
109
  udpbind_thread(udpbind_loop);
110
  }
111
- else {
112
- //Tcp
 
113
  init_loop(thread_num, worker_thread);
114
  spdlog::info("ProxyServer start: tcp->{}", port);
115
  tcpaccept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
116
  tcpaccept_thread(tcpaccept_loop);
117
  }
118
-
119
 
120
  return 0;
121
  }
 
17
  #include "spdlog/async.h"
18
  #include "spdlog/sinks/stdout_color_sinks.h"
19
 
20
+ static const char *host = "0.0.0.0";
21
  static int port = 8080;
22
  static int thread_num = 4;
23
  static hloop_t *tcpaccept_loop = NULL, *udpbind_loop = NULL;
24
 
25
+ static void new_conn_event(hevent_t *ev)
26
+ {
27
+ hloop_t *loop = ev->loop;
28
+ hio_t *io = (hio_t *)hevent_userdata(ev);
29
  hio_attach(loop, io);
30
 
31
  tcp_on_accept(io, ev);
32
  }
33
 
34
+ static void on_accept(hio_t *io)
35
+ {
36
  hio_detach(io);
37
 
38
+ hloop_t *worker_loop = get_next_loop();
39
  hevent_t ev;
40
  memset(&ev, 0, sizeof(ev));
41
  ev.loop = worker_loop;
 
44
  hloop_post_event(worker_loop, &ev);
45
  }
46
 
47
+ static HTHREAD_RETTYPE worker_thread(void *userdata)
48
+ {
49
+ hloop_t *loop = (hloop_t *)userdata;
50
  hloop_run(loop);
51
  return 0;
52
  }
53
 
54
+ static HTHREAD_RETTYPE tcpaccept_thread(void *userdata)
55
+ {
56
+ hloop_t *loop = (hloop_t *)userdata;
57
+ hio_t *listenio = hloop_create_tcp_server(loop, host, port, on_accept);
58
+ if (listenio == NULL)
59
+ {
60
  exit(1);
61
  }
62
  int listeniofd = hio_fd(listenio);
 
65
  return 0;
66
  }
67
 
68
+ static HTHREAD_RETTYPE udpbind_thread(void *userdata)
69
+ {
70
+ hloop_t *loop = (hloop_t *)userdata;
71
+ hio_t *bindio = hloop_create_udp_server(loop, host, port);
72
+ if (bindio == NULL)
73
+ {
74
  exit(1);
75
  }
76
  int bindfd = hio_fd(bindio);
 
84
  return 0;
85
  }
86
 
87
+ int main(int argc, char **argv)
88
+ {
89
  int type = 0;
90
+ if (argc == 3)
91
+ {
92
  type = atoi(argv[1]);
93
  port = atoi(argv[2]);
94
  }
95
  int cores = std::thread::hardware_concurrency();
96
+ if (cores > 0)
97
+ {
98
  thread_num = cores;
99
  }
100
+
101
  auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
102
  auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);
103
 
 
108
  spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
109
  "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));
110
 
 
111
  spdlog::info("ProxyServer start: threadNum:{:d}", thread_num);
112
 
113
  if (type == 1)
114
  {
115
+ // Udp
116
  spdlog::info("ProxyServer start: udp->{}", port);
117
  udpbind_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
118
  udpbind_thread(udpbind_loop);
119
  }
120
+ else
121
+ {
122
+ // Tcp
123
  init_loop(thread_num, worker_thread);
124
  spdlog::info("ProxyServer start: tcp->{}", port);
125
  tcpaccept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
126
  tcpaccept_thread(tcpaccept_loop);
127
  }
 
128
 
129
  return 0;
130
  }
src/tcp_client.cpp CHANGED
@@ -14,7 +14,8 @@ void TcpClientBolt::onConnection(const hv::SocketChannelPtr &channel)
14
  int len = sizeof(buff);
15
 
16
  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);
17
- hio_write(io, buff, len);
 
18
  }
19
 
20
  void TcpClientBolt::onRecv(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
@@ -34,22 +35,28 @@ void TcpClientBolt::onRecv(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
34
  break;
35
  }
36
  }
 
37
  }
 
38
 
39
- hio_write(io, buf->data(), buf->size());
40
  }
41
 
42
  void TcpClientBolt::onWrited(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
43
  {
44
  if (channel.get()->isWriteComplete())
45
  {
46
-
47
  }
48
 
49
  }
50
 
51
  void TcpClientBolt::onDisConnection(const hv::SocketChannelPtr &channel)
52
  {
 
 
 
 
 
53
  hio_close(io);
54
  }
55
 
@@ -64,8 +71,10 @@ bool TcpClientBolt::handShake(void *buf, int readbytes)
64
  {
65
  if (dest_len == 1 && dest[0] == BOLT_CHANNEL_CMD_TCP_HANDSHAKE_REQUEST)
66
  {
 
67
  this->t_addr.sin_addr.s_addr = t_ip;
68
  this->t_addr.sin_port = t_port;
 
69
  this->u_addr.sin_addr.s_addr = u_ip;
70
  this->u_addr.sin_port = u_port;
71
  this->session_id = session_id;
@@ -83,6 +92,7 @@ bool TcpClientBolt::handShake(void *buf, int readbytes)
83
 
84
  if (!this->connect((struct sockaddr *)&t_addr))
85
  {
 
86
  char buff[2];
87
  buff[0] = BOLT_CHANNEL_CMD_TCP_HANDSHAKE_RESPONSE;
88
  buff[1] = BOLT_TCP_HANDSHAKE_FAIL_TIMEOUT;
@@ -114,9 +124,11 @@ bool TcpClientBolt::handShake(void *buf, int readbytes)
114
  config.ept_type = config.encrypt ? CRYPT_TYPE::XOR : CRYPT_TYPE::NONE;
115
  config.ept_key = config.encrypt ? generateRandomKey() : 0;
116
 
 
 
117
  GENERATE_DECRYPT_KEY(extend_response, extend_response_len, config.ept_type, config.ept_key)
118
 
119
- char result[1] = {BOLT_BIND_RESPONSE_CODE_SUCESS};
120
 
121
  PACK_BIND_RESPONSE_DATA(bind_response, bind_response_len, BOLT_CHANNEL_CMD_BIND_RESPONSE, request_id, session_id, result)
122
 
@@ -133,6 +145,7 @@ bool TcpClientBolt::handShake(void *buf, int readbytes)
133
  TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().remove(session_id);
134
  this->close();
135
  hio_close_async(io);
 
136
  }
137
  }
138
  }
 
14
  int len = sizeof(buff);
15
 
16
  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);
17
+ hio_write(io, boltdata, boltdata_len);
18
+ spdlog::info("BOLT_CHANNEL_CMD_TCP_HANDSHAKE_REQUEST: t_addr={}:{}, u_addr={}:{} ==> connect succ", inet_ntoa(t_addr.sin_addr), htons(t_addr.sin_port), inet_ntoa(u_addr.sin_addr), htons(u_addr.sin_port));
19
  }
20
 
21
  void TcpClientBolt::onRecv(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
 
35
  break;
36
  }
37
  }
38
+ hio_write(io, buf->data(), buf->size());
39
  }
40
+ spdlog::info("t_addr={}:{}, u_addr={}:{} onRecv==> {}={}", inet_ntoa(t_addr.sin_addr), htons(t_addr.sin_port), inet_ntoa(u_addr.sin_addr), htons(u_addr.sin_port), buf->len, (const char*)buf->data());
41
 
 
42
  }
43
 
44
  void TcpClientBolt::onWrited(const hv::SocketChannelPtr &channel, hv::Buffer *buf)
45
  {
46
  if (channel.get()->isWriteComplete())
47
  {
48
+ spdlog::info("t_addr={}:{}, u_addr={}:{} isWriteComplete==> {}={}", inet_ntoa(t_addr.sin_addr), htons(t_addr.sin_port), inet_ntoa(u_addr.sin_addr), htons(u_addr.sin_port), buf->len, (const char*)buf->data());
49
  }
50
 
51
  }
52
 
53
  void TcpClientBolt::onDisConnection(const hv::SocketChannelPtr &channel)
54
  {
55
+ if (is_bolt_server)
56
+ {
57
+ TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().remove(session_id);
58
+ }
59
+
60
  hio_close(io);
61
  }
62
 
 
71
  {
72
  if (dest_len == 1 && dest[0] == BOLT_CHANNEL_CMD_TCP_HANDSHAKE_REQUEST)
73
  {
74
+ this->t_addr.sin_family = AF_INET;
75
  this->t_addr.sin_addr.s_addr = t_ip;
76
  this->t_addr.sin_port = t_port;
77
+ this->u_addr.sin_family = AF_INET;
78
  this->u_addr.sin_addr.s_addr = u_ip;
79
  this->u_addr.sin_port = u_port;
80
  this->session_id = session_id;
 
92
 
93
  if (!this->connect((struct sockaddr *)&t_addr))
94
  {
95
+ spdlog::info("BOLT_CHANNEL_CMD_TCP_HANDSHAKE_REQUEST: t_addr={}:{}, u_addr={}:{} ==> connect fail", inet_ntoa(t_addr.sin_addr), htons(t_addr.sin_port), inet_ntoa(u_addr.sin_addr), htons(u_addr.sin_port));
96
  char buff[2];
97
  buff[0] = BOLT_CHANNEL_CMD_TCP_HANDSHAKE_RESPONSE;
98
  buff[1] = BOLT_TCP_HANDSHAKE_FAIL_TIMEOUT;
 
124
  config.ept_type = config.encrypt ? CRYPT_TYPE::XOR : CRYPT_TYPE::NONE;
125
  config.ept_key = config.encrypt ? generateRandomKey() : 0;
126
 
127
+ spdlog::info("BOLT_CHANNEL_CMD_BIND_REQUEST: requestId:{}, session_id:{}, encrypt:{}", request_id, session_id, config.encrypt);
128
+
129
  GENERATE_DECRYPT_KEY(extend_response, extend_response_len, config.ept_type, config.ept_key)
130
 
131
+ uint32_t result = BOLT_BIND_RESPONSE_CODE_SUCESS;
132
 
133
  PACK_BIND_RESPONSE_DATA(bind_response, bind_response_len, BOLT_CHANNEL_CMD_BIND_RESPONSE, request_id, session_id, result)
134
 
 
145
  TcpClientMap<uint32_t, tcpBoltConfig>::getInstance().remove(session_id);
146
  this->close();
147
  hio_close_async(io);
148
+ spdlog::info("BOLT_CHANNEL_CMD_UNBIND_REQUEST: t_addr={}:{}, u_addr={}:{}", inet_ntoa(t_addr.sin_addr), htons(t_addr.sin_port), inet_ntoa(u_addr.sin_addr), htons(u_addr.sin_port));
149
  }
150
  }
151
  }
src/tcp_inbound.cpp CHANGED
@@ -17,7 +17,7 @@ static void tcp_on_close(hio_t* io) {
17
  }
18
 
19
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
20
- spdlog::info("tcp_on_recv fd={} buf({})={}\n", hio_fd(io), readbytes, (const char*)buf);
21
  auto cli = TcpConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
22
  if(cli) {
23
  if (cli->hasHandshake())
@@ -27,7 +27,7 @@ static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
27
  cli->handShake(buf, readbytes);
28
  }
29
 
30
- }
31
  }
32
 
33
  void tcp_on_accept(hio_t* io, hevent_t* ev) {
 
17
  }
18
 
19
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
20
+ spdlog::info("tcp_on_recv fd={} buf({})={}", hio_fd(io), readbytes, (const char*)buf);
21
  auto cli = TcpConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
22
  if(cli) {
23
  if (cli->hasHandshake())
 
27
  cli->handShake(buf, readbytes);
28
  }
29
 
30
+ }
31
  }
32
 
33
  void tcp_on_accept(hio_t* io, hevent_t* ev) {
src/udp_client.cpp CHANGED
@@ -30,10 +30,10 @@ bool UdpServerBoltProxy::analyzeData(struct sockaddr_in t_addr, struct sockaddr_
30
  client = new_client.get();
31
  }
32
 
33
- if (getConfig().ept_type == CRYPT_TYPE::XOR)
34
- {
35
- xor_::crypt((char *)dest, dest_len, getConfig().ept_key);
36
- }
37
 
38
  client->getClient()->sendto(dest, dest_len);
39
 
@@ -43,10 +43,10 @@ bool UdpServerBoltProxy::analyzeData(struct sockaddr_in t_addr, struct sockaddr_
43
  int UdpServerBoltProxy::sendData(struct sockaddr_in t_addr, struct sockaddr_in u_addr, void *data, int data_len)
44
  {
45
 
46
- if (getConfig().ept_type == CRYPT_TYPE::XOR)
47
- {
48
- xor_::crypt((char *)data, data_len, getConfig().ept_key);
49
- }
50
  PACK_TUNNEL_DATA(boltdata, boltdata_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_UDP, t_addr.sin_addr.s_addr, t_addr.sin_port, u_addr.sin_addr.s_addr, u_addr.sin_port, config.session_id, 0, 0, data, data_len)
51
  return hio_write(io, boltdata, boltdata_len);
52
 
 
30
  client = new_client.get();
31
  }
32
 
33
+ // if (getConfig().ept_type == CRYPT_TYPE::XOR)
34
+ // {
35
+ // xor_::crypt((char *)dest, dest_len, getConfig().ept_key);
36
+ // }
37
 
38
  client->getClient()->sendto(dest, dest_len);
39
 
 
43
  int UdpServerBoltProxy::sendData(struct sockaddr_in t_addr, struct sockaddr_in u_addr, void *data, int data_len)
44
  {
45
 
46
+ // if (getConfig().ept_type == CRYPT_TYPE::XOR)
47
+ // {
48
+ // xor_::crypt((char *)data, data_len, getConfig().ept_key);
49
+ // }
50
  PACK_TUNNEL_DATA(boltdata, boltdata_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_UDP, t_addr.sin_addr.s_addr, t_addr.sin_port, u_addr.sin_addr.s_addr, u_addr.sin_port, config.session_id, 0, 0, data, data_len)
51
  return hio_write(io, boltdata, boltdata_len);
52
 
src/udp_inbound.cpp CHANGED
@@ -62,8 +62,10 @@ void udp_on_recvfrom(hio_t *io, void *buf, int readbytes)
62
 
63
  auto serverProxy = UdpConnMap<uint32_t, UdpServerBoltProxy>::getInstance().get(session_id);
64
  struct sockaddr_in t_addr = {0}, u_addr = {0};
 
65
  t_addr.sin_addr.s_addr = t_ip;
66
  t_addr.sin_port = t_port;
 
67
  u_addr.sin_addr.s_addr = u_ip;
68
  u_addr.sin_port = u_port;
69
  if (serverProxy && serverProxy->analyzeData(t_addr, u_addr, session_id, dest, dest_len, extend, extend_len))
 
62
 
63
  auto serverProxy = UdpConnMap<uint32_t, UdpServerBoltProxy>::getInstance().get(session_id);
64
  struct sockaddr_in t_addr = {0}, u_addr = {0};
65
+ t_addr.sin_family = AF_INET;
66
  t_addr.sin_addr.s_addr = t_ip;
67
  t_addr.sin_port = t_port;
68
+ u_addr.sin_family = AF_INET;
69
  u_addr.sin_addr.s_addr = u_ip;
70
  u_addr.sin_port = u_port;
71
  if (serverProxy && serverProxy->analyzeData(t_addr, u_addr, session_id, dest, dest_len, extend, extend_len))