xukc commited on
Commit
a8ba146
·
1 Parent(s): 506d0f7

[fix]support tcp

Browse files
.vscode/settings.json CHANGED
@@ -71,6 +71,14 @@
71
  "algorithm": "cpp",
72
  "shared_mutex": "cpp",
73
  "__memory": "cpp",
74
- "thread": "cpp"
 
 
 
 
 
 
 
 
75
  }
76
  }
 
71
  "algorithm": "cpp",
72
  "shared_mutex": "cpp",
73
  "__memory": "cpp",
74
+ "thread": "cpp",
75
+ "bit": "cpp",
76
+ "charconv": "cpp",
77
+ "condition_variable": "cpp",
78
+ "format": "cpp",
79
+ "future": "cpp",
80
+ "functional": "cpp",
81
+ "memory_resource": "cpp",
82
+ "utility": "cpp"
83
  }
84
  }
.vscode/tasks.json CHANGED
@@ -12,8 +12,11 @@
12
  "test",
13
  "-I",
14
  "include",
 
 
15
  "hv_utils.cpp",
16
  "tcp_inbound.cpp",
 
17
  "main.cpp",
18
  "-lhv",
19
  "-lspdlog"
 
12
  "test",
13
  "-I",
14
  "include",
15
+ "-I",
16
+ "include/bolt",
17
  "hv_utils.cpp",
18
  "tcp_inbound.cpp",
19
+ "tcp_client.cpp",
20
  "main.cpp",
21
  "-lhv",
22
  "-lspdlog"
include/bolt/crypt.h CHANGED
@@ -7,7 +7,7 @@
7
 
8
  enum CRYPT_TYPE {
9
  NONE = 0,
10
- XOR = '1'
11
  };
12
 
13
  namespace xor_ {
@@ -32,6 +32,14 @@ if(encrypt) {
32
  extend_len = sprintf(extend, "ept=1"); \
33
  } \
34
 
 
 
 
 
 
 
 
 
35
  #define EXTRACTION_DECRYPT_KEY(ept_type, ept_key, extend, extend_len) \
36
  CRYPT_TYPE ept_type = NONE; \
37
  char ept_key; \
 
7
 
8
  enum CRYPT_TYPE {
9
  NONE = 0,
10
+ XOR = 1
11
  };
12
 
13
  namespace xor_ {
 
32
  extend_len = sprintf(extend, "ept=1"); \
33
  } \
34
 
35
+ #define GENERATE_DECRYPT_KEY(extend, extend_len, ept_type, ept_key) \
36
+ char extend[21] = {0}; \
37
+ int extend_len = 0; \
38
+ int type_len = snprintf(extend, sizeof(extend), "ept_type=%d", ept_type); \
39
+ if (ept_type == XOR) { \
40
+ extend_len = snprintf(extend + type_len, sizeof(extend) - type_len, ";ept_key=%c", ept_key);\
41
+ } \
42
+
43
  #define EXTRACTION_DECRYPT_KEY(ept_type, ept_key, extend, extend_len) \
44
  CRYPT_TYPE ept_type = NONE; \
45
  char ept_key; \
include/bolt/datagram.h CHANGED
@@ -137,14 +137,33 @@ struct Bolt_Payload {
137
  memcpy(res + 37, &session_id, 4); \
138
  memcpy(res + 41, data_st, 32); \
139
 
140
- #define UNPACK_BIND_DATA(command, request_id, session_id, response_result, bind_resp, bind_resp_len) \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  uint8_t command = *(bind_resp); \
142
  uint32_t request_id = 0; \
143
  uint32_t session_id = 0; \
144
  uint32_t response_result = 0; \
145
  memcpy(&request_id, (char*)bind_resp + 1, 4); \
146
  memcpy(&session_id, (char*)bind_resp + 5, 4); \
147
- memcpy(&response_result, (char*)bind_resp + 9, 4); \
148
 
149
  #define PACK_UNBIND_DATA(res, res_len, session_id, code) \
150
  char res[9] = {0}; \
@@ -153,6 +172,13 @@ struct Bolt_Payload {
153
  memcpy(res + 1, &session_id, 4); \
154
  memcpy(res + 5, &code, 4); \
155
 
 
 
 
 
 
 
 
156
  #define PACK_ICMP_REQUEST_DATA(res, res_len, type, code, id, seqno, data, data_len) \
157
  int res_len = 6 + data_len; \
158
  char res[res_len]; \
 
137
  memcpy(res + 37, &session_id, 4); \
138
  memcpy(res + 41, data_st, 32); \
139
 
140
+ #define UNPACK_BIND_DATA(command, request_id, signal_id, session_id, data_st, data, data_len) \
141
+ uint8_t command = *(data); \
142
+ uint32_t request_id = 0; \
143
+ char signal_id[32] = {0}; \
144
+ uint32_t session_id = 0; \
145
+ char data_st[32] = {0}; \
146
+ memcpy(&request_id, (char*)data + 1, 4); \
147
+ memcpy(&signal_id, (char*)data + 5, 32); \
148
+ memcpy(&session_id, (char*)data + 37, 4); \
149
+ memcpy(&data_st, (char*)data + 41, 32); \
150
+
151
+ #define PACK_BIND_RESPONSE_DATA(res, res_len, command, request_id, session_id, response_result) \
152
+ char res[13] = {0}; \
153
+ int res_len = 13; \
154
+ res[0] = command; \
155
+ memcpy(res + 1, &request_id, 4); \
156
+ memcpy(res + 5, &session_id, 4); \
157
+ memcpy(res + 9, &response_result, 4); \
158
+
159
+ #define UNPACK_BIND_RESPONSE_DATA(command, request_id, session_id, response_result, bind_resp, bind_resp_len) \
160
  uint8_t command = *(bind_resp); \
161
  uint32_t request_id = 0; \
162
  uint32_t session_id = 0; \
163
  uint32_t response_result = 0; \
164
  memcpy(&request_id, (char*)bind_resp + 1, 4); \
165
  memcpy(&session_id, (char*)bind_resp + 5, 4); \
166
+ memcpy(&response_result, (char*)bind_resp + 9, 4);
167
 
168
  #define PACK_UNBIND_DATA(res, res_len, session_id, code) \
169
  char res[9] = {0}; \
 
172
  memcpy(res + 1, &session_id, 4); \
173
  memcpy(res + 5, &code, 4); \
174
 
175
+ #define UNPACK_UNBIND_DATA(command, session_id, code, data, data_len) \
176
+ uint8_t command = *(data); \
177
+ uint32_t session_id = 0; \
178
+ uint32_t code = 0; \
179
+ memcpy(&session_id, (char*)data + 1, 4); \
180
+ memcpy(&code, (char*)data + 5, 4); \
181
+
182
  #define PACK_ICMP_REQUEST_DATA(res, res_len, type, code, id, seqno, data, data_len) \
183
  int res_len = 6 + data_len; \
184
  char res[res_len]; \
include/conn_map.h CHANGED
@@ -17,16 +17,16 @@ public:
17
  ConnMap(ConnMap const&) = delete;
18
  void operator=(ConnMap 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
  }
@@ -44,7 +44,7 @@ public:
44
  private:
45
  ConnMap() = default;
46
  mutable std::shared_timed_mutex mutex_;
47
- std::map<Key, Value> map_;
48
  };
49
 
50
  #endif //PROXYSERVER_CONN_MAP_H
 
17
  ConnMap(ConnMap const&) = delete;
18
  void operator=(ConnMap const&) = delete;
19
 
20
+ void add(const Key& key, std::unique_ptr<Value> value) {
21
  std::unique_lock<std::shared_timed_mutex> lock(mutex_);
22
+ map_[key] = std::move(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 it->second.get();
30
  }
31
  return nullptr;
32
  }
 
44
  private:
45
  ConnMap() = default;
46
  mutable std::shared_timed_mutex mutex_;
47
+ std::map<Key, std::unique_ptr<Value>> map_;
48
  };
49
 
50
  #endif //PROXYSERVER_CONN_MAP_H
tcp_inbound.cpp CHANGED
@@ -1,77 +1,33 @@
1
  #include "include/tcp_inbound.h"
2
  #include "include/conn_map.h"
3
- #include "include/accumulator.h"
4
  #include "hv/hsocket.h"
5
  #include "hv/hthread.h"
6
  #include "hv/TcpClient.h"
7
  #include "spdlog/spdlog.h"
8
  #include <string>
9
 
10
- class TcpClientShell {
11
- public:
12
- bool init(hio_t* _io) {
13
- io = _io;
14
- int connfd = cli.createsocket(hio_peeraddr(io));
15
- if (connfd < 0) {
16
- return false;
17
- }
18
- cli.onConnection = [this](const hv::SocketChannelPtr& channel) {
19
- std::string peeraddr = channel->peeraddr();
20
- if (channel->isConnected()) {
21
- spdlog::info("connected to {}! connfd={}\n", peeraddr.c_str(), channel->fd());
22
- if (wait_send_buf.getDataSize() > 0)
23
- {
24
- cli.send(wait_send_buf.getData(), wait_send_buf.getDataSize());
25
- }
26
- } else {
27
- spdlog::info("disconnected to {}! connfd={}\n", peeraddr.c_str(), channel->fd());
28
- hio_close(io);
29
- }
30
- };
31
-
32
- cli.onMessage = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
33
- hio_write(io, buf->data(), buf->size());
34
- spdlog::info("< %.*s\n", (int)buf->size(), (char*)buf->data());
35
- };
36
-
37
- cli.onWriteComplete = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
38
-
39
- };
40
- cli.start();
41
- return true;
42
- }
43
-
44
- int send(const char* data, int size) {
45
- if (cli.isConnected())
46
- {
47
- return cli.send(data, size);
48
- } else {
49
- wait_send_buf.addData(data, size);
50
- return size;
51
- }
52
- }
53
-
54
- void close() {
55
- cli.closesocket();
56
- }
57
-
58
- private:
59
- hv::TcpClient cli;
60
- hio_t* io;
61
- Accumulator wait_send_buf;
62
- };
63
-
64
  static void tcp_on_close(hio_t* io) {
65
  spdlog::info("tcp_on_close fd={} error={}\n", hio_fd(io), hio_error(io));
66
- ConnMap<hio_t*, TcpClientShell>::getInstance().remove(io);
 
 
 
 
 
67
  }
68
 
69
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
70
  spdlog::info("tcp_on_recv fd={} buf({})={}\n", hio_fd(io), readbytes, (const char*)buf);
71
- hio_write(io, buf, readbytes);
72
- auto cli = ConnMap<hio_t*, TcpClientShell>::getInstance().get(io);
73
  if(cli) {
74
- cli->send((const char*) buf, readbytes);
 
 
 
 
 
 
75
  }
76
  }
77
 
@@ -86,6 +42,8 @@ void tcp_on_accept(hio_t* io, hevent_t* ev) {
86
  SOCKADDR_STR(hio_localaddr(io), localaddrstr),
87
  SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
88
 
 
 
89
  hio_setcb_close(io, tcp_on_close);
90
  hio_setcb_read(io, tcp_on_recv);
91
  hio_read(io);
 
1
  #include "include/tcp_inbound.h"
2
  #include "include/conn_map.h"
3
+ #include "include/tcp_client.h"
4
  #include "hv/hsocket.h"
5
  #include "hv/hthread.h"
6
  #include "hv/TcpClient.h"
7
  #include "spdlog/spdlog.h"
8
  #include <string>
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  static void tcp_on_close(hio_t* io) {
11
  spdlog::info("tcp_on_close fd={} error={}\n", hio_fd(io), hio_error(io));
12
+
13
+ auto cli = ConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
14
+ if(cli) {
15
+ ConnMap<hio_t*, TcpClientBolt>::getInstance().remove(io);
16
+ cli->close();
17
+ }
18
  }
19
 
20
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
21
  spdlog::info("tcp_on_recv fd={} buf({})={}\n", hio_fd(io), readbytes, (const char*)buf);
22
+ auto cli = ConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
 
23
  if(cli) {
24
+ if (cli->hasHandshake())
25
+ {
26
+ cli->send((char*) buf, readbytes);
27
+ } else {
28
+ cli->handShake(buf, readbytes);
29
+ }
30
+
31
  }
32
  }
33
 
 
42
  SOCKADDR_STR(hio_localaddr(io), localaddrstr),
43
  SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
44
 
45
+ ConnMap<hio_t*, TcpClientBolt>::getInstance().add(io, std::make_unique<TcpClientBolt>(io));
46
+
47
  hio_setcb_close(io, tcp_on_close);
48
  hio_setcb_read(io, tcp_on_recv);
49
  hio_read(io);