forked from RoliSoft/Obfuscation-Tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_base.cpp
287 lines (247 loc) · 6.2 KB
/
tcp_base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#include "shared.cpp"
#include "transport_base.cpp"
#include "tls_helpers.cpp"
class tcp_base : virtual public transport_base
{
protected:
#if HAVE_TLS
SSL_CTX *ssl_ctx;
SSL *ssl;
#endif
public:
int encoding = LENGTH_VAR;
bool tls = false;
inline unsigned short read_14bit(int fd)
{
int shift = 0;
unsigned short value = 0;
unsigned char current = 0;
do
{
if (shift == 2 * 7) // cap at 16383
{
printf("Size header seems to be corrupted, abandoning read.\n");
break;
}
socklen_t msglen;
#if HAVE_TLS
if (this->tls)
{
msglen = SSL_read(this->ssl, ¤t, sizeof(unsigned char));
}
else
{
#endif
msglen = read(fd, ¤t, sizeof(unsigned char));
#if HAVE_TLS
}
#endif
if (msglen == 0)
{
// propagate TCP closed event
return 0;
}
value |= (current & 0x7f) << shift;
shift += 7;
}
while ((current & 0x80) != 0);
return value;
}
static inline void write_14bit(unsigned short size, char* buffer, int* length)
{
*length = 0;
unsigned short value = size;
int len = sizeof(unsigned short);
while (value >= 0x80)
{
buffer[-len + (*length)++] = value | 0x80;
value >>= 7;
}
buffer[-len + (*length)++] = value;
int sizediff = len - *length;
if (sizediff == 1)
{
buffer[-1] = buffer[-2];
}
}
inline unsigned short read_16bit(int fd)
{
unsigned short size = 0;
socklen_t msglen;
#if HAVE_TLS
if (this->tls)
{
msglen = SSL_read(this->ssl, &size, sizeof(unsigned short));
}
else
{
#endif
msglen = read(fd, &size, sizeof(unsigned short));
#if HAVE_TLS
}
#endif
if (msglen == 0)
{
// propagate TCP closed event
return 0;
}
if (msglen == 1)
{
#if HAVE_TLS
if (this->tls)
{
msglen = SSL_read(this->ssl, ((char*)&size) + 1, sizeof(unsigned char));
}
else
{
#endif
msglen = read(fd, ((char*)&size) + 1, sizeof(unsigned char));
#if HAVE_TLS
}
#endif
if (msglen == 0)
{
// propagate TCP closed event
return 0;
}
}
return ntohs(size);
}
static inline void write_16bit(unsigned short size, char* buffer, int* length)
{
*length = sizeof(unsigned short);
*((unsigned short*)&(buffer - *length)[0]) = htons(size);
}
protected:
inline int _send(int fd, char *buffer, ssize_t msglen)
{
int sizelen = 0;
if (this->encoding != LENGTH_NONE)
{
if (msglen > MTU_SIZE)
{
fprintf(stderr, "Refusing to send packet that is too large (%zd > MTU).\n", msglen);
return 0;
}
if (this->encoding == LENGTH_VAR)
{
write_14bit(msglen, buffer, &sizelen);
}
else
{
write_16bit(msglen, buffer, &sizelen);
}
}
int res;
#if HAVE_TLS
if (this->tls)
{
res = SSL_write(this->ssl, buffer - sizelen, msglen + sizelen);
}
else
{
#endif
res = write(fd, buffer - sizelen, msglen + sizelen);
#if HAVE_TLS
}
#endif
if (res < 0 && run)
{
perror("Failed to send TCP packet");
}
return res;
}
inline int _receive(int fd, char *buffer, int* offset)
{
int readsize;
if (this->encoding != LENGTH_NONE)
{
unsigned short toread = this->encoding == LENGTH_VAR
? read_14bit(fd)
: read_16bit(fd);
if (toread == 0)
{
printf("TCP connection to remote lost\n");
return -1;
}
if (toread > MTU_SIZE)
{
printf("Incorrect size read from buffer (%d > MTU), abandoning connection.\n", toread);
return 0;
}
readsize = toread;
while (run && toread > 0)
{
ssize_t msglen;
#if HAVE_TLS
if (this->tls)
{
msglen = SSL_read(this->ssl, buffer + (readsize - toread), toread);
}
else
{
#endif
msglen = read(fd, buffer + (readsize - toread), toread);
#if HAVE_TLS
}
#endif
if (this->verbose && toread != msglen)
{
printf("Read partially, need %d more bytes.\n", (int)(toread - msglen));
}
toread -= msglen;
}
}
else
{
#if HAVE_TLS
if (this->tls)
{
readsize = SSL_read(this->ssl, buffer, MTU_SIZE);
}
else
{
#endif
readsize = read(fd, buffer, MTU_SIZE);
#if HAVE_TLS
}
#endif
if (readsize < 1)
{
printf("TCP connection to remote lost\n");
return -1;
}
}
if (this->verbose) printf("Received %d bytes from remote\n", readsize);
*offset = 0;
return readsize;
}
protected:
tcp_base(int encoding = LENGTH_VAR, bool tls = false)
: encoding(encoding), tls(tls)
{
#if HAVE_TLS
if (this->tls)
{
SSL_library_init();
SSL_load_error_strings();
}
#endif
}
#if HAVE_TLS
void cleanup_ssl()
{
if (this->ssl != nullptr)
{
SSL_free(this->ssl);
this->ssl = nullptr;
}
if (this->ssl_ctx != nullptr)
{
SSL_CTX_free(this->ssl_ctx);
this->ssl_ctx = nullptr;
}
}
#endif
};