-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcp_client.cpp
279 lines (229 loc) · 7.01 KB
/
tcp_client.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
#pragma once
#include "shared.cpp"
#include "tcp_base.cpp"
#include "tls_helpers.cpp"
class tcp_client : public tcp_base
{
friend class socks5_proxy;
private:
int fd;
struct sockaddr_in remote_addr;
char *tls_remote_host;
bool tls_no_verify = false;
char *tls_ca_path = nullptr;
bool tls_delay = false;
public:
tcp_client(struct sockaddr_in remote_addr, bool tls, struct session* session)
: transport_base(session->verbose), tcp_base(session->length_type, tls), remote_addr(remote_addr), tls_remote_host(session->remote_host), tls_no_verify(session->tls_no_verify), tls_ca_path(session->tls_ca_bundle)
{
}
tcp_client(struct sockaddr_in remote_addr, int encoding = LENGTH_VAR, bool tls = false, char* tls_remote_host = nullptr, bool tls_no_verify = false, char *tls_ca_path = nullptr, bool verbose = false)
: transport_base(verbose), tcp_base(encoding, tls), remote_addr(remote_addr), tls_remote_host(tls_remote_host), tls_no_verify(tls_no_verify), tls_ca_path(tls_ca_path)
{
}
private:
#if HAVE_TLS
int _do_tls()
{
ERR_clear_error();
const SSL_METHOD *method = TLS_client_method();
this->ssl_ctx = SSL_CTX_new(method);
if (SSL_CTX_set_min_proto_version(this->ssl_ctx, TLS1_2_VERSION) == 0)
{
fprintf(stderr, "Failed to set TLS minimum version: ");
ERR_print_errors_fp(stderr);
}
if (!this->tls_no_verify)
{
SSL_CTX_set_verify(this->ssl_ctx, SSL_VERIFY_PEER, NULL);
if (this->tls_ca_path == nullptr)
{
if (SSL_CTX_set_default_verify_paths(this->ssl_ctx) == 0)
{
fprintf(stderr, "Failed to load system-default CA certificate bundle.\n");
return EXIT_FAILURE;
}
}
else
{
if (SSL_CTX_load_verify_locations(this->ssl_ctx, this->tls_ca_path, NULL) == 0)
{
fprintf(stderr, "Failed to load specified CA certificate bundle.\n");
return EXIT_FAILURE;
}
}
}
this->ssl = SSL_new(this->ssl_ctx);
SSL_set_fd(this->ssl, this->fd);
if (!this->tls_no_verify)
{
SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
if (tls_remote_host == nullptr || !SSL_set1_host(this->ssl, this->tls_remote_host))
{
fprintf(stderr, "Failed to set hostname for TLS validation: ");
ERR_print_errors_fp(stderr);
return EXIT_FAILURE;
}
}
int res;
if ((res = SSL_connect(this->ssl)) == -1)
{
fprintf(stderr, "Failed to initiate TLS handshake: ");
int reason = ERR_GET_REASON(ERR_peek_error());
switch (reason)
{
default:
ERR_print_errors_fp(stderr);
break;
case SSL_R_CERTIFICATE_VERIFY_FAILED:
fprintf(stderr, "certificate verification failed.\n");
//fatal = true;
break;
case SSL_R_WRONG_VERSION_NUMBER:
fprintf(stderr, "endpoint not TLS-enabled or unsupported version.\n");
//fatal = true;
break;
}
close(this->fd);
cleanup_ssl();
return EXIT_FAILURE;
}
char name[256];
X509* cert = SSL_get_peer_certificate(this->ssl);
X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, (char*)&name, sizeof(name));
const char *version = SSL_get_version(this->ssl);
const char *cipher = SSL_get_cipher(this->ssl);
printf("Established %s with %s using %s.\n", version, name, cipher);
if (this->tls_no_verify)
{
printf("Fingerprint of certificate is ");
print_cert_hash(cert);
printf("\n");
}
X509_free(cert);
this->tls = true;
return EXIT_SUCCESS;
}
#endif
int _connect()
{
if (!run)
{
return EXIT_FAILURE;
}
if (this->tls_delay)
{
this->tls = false;
}
int res;
do
{
if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Client socket creation failed");
return EXIT_FAILURE;
}
res = connect(this->fd, (const struct sockaddr *)&this->remote_addr, IP_SIZE);
if (res == -1)
{
if (!run)
{
return EXIT_FAILURE;
}
if (errno == ECONNREFUSED)
{
close(this->fd);
sleep(1);
continue;
}
else
{
perror("Failed to connect to remote host");
return EXIT_FAILURE;
}
}
printf("Connected.\n");
#if HAVE_TLS
if (this->tls)
{
if (_do_tls() != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
res = 0;
}
#endif
}
while (res != 0 && run);
sockets.push_back(this->fd);
started = true;
connected = true;
int i = 1;
setsockopt(this->fd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
#ifdef TCP_QUICKACK
setsockopt(this->fd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i));
#endif
return EXIT_SUCCESS;
}
public:
int start()
{
printf("Connecting via TCP to ");
print_ip_port(&this->remote_addr);
printf("... ");
fflush(stdout);
return _connect();
}
int restart()
{
close(this->fd);
#if HAVE_TLS
if (this->tls) cleanup_ssl();
#endif
printf("Reconnecting via TCP to ");
print_ip_port(&this->remote_addr);
printf("... ");
fflush(stdout);
return _connect();
}
int stop()
{
close(this->fd);
#if HAVE_TLS
if (this->tls) cleanup_ssl();
#endif
started = false;
connected = false;
return EXIT_SUCCESS;
}
int send(char *buffer, ssize_t msglen)
{
if (!this->connected)
{
return 0;
}
int res = _send(this->fd, buffer, msglen);
if (res < 0)
{
this->connected = false;
}
return res;
}
int receive(char *buffer, int* offset)
{
if (!this->connected)
{
return 0;
}
int res = _receive(this->fd, buffer, offset);
if (res < 0)
{
this->connected = false;
}
return res;
}
int get_selectable()
{
return this->fd;
}
};