-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
4789 lines (3792 loc) · 126 KB
/
server.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Repository: https://github.com/lockedbyte/vroute
-------------------------------- [ User notes ] --------------------------------
A brief message
===================
It is advised, if you are using proxychains as your SOCKS proxy client, to change
in /etc/proxychains.conf these values to the following:
tcp_read_time_out 150000
tcp_connect_time_out 80000
Also, no need to mention you need to point the SOCKS proxy address and port to the
one set up with this project.
Future additions
==================
- change encryption to the new OpenSSL encryption escheme using EVP
- allow second-order connections for nodes to increase remote accessibility within the network map
Compilation
===================
Simply:
$ make
Usage and environment setup
===============================
Server:
- Use libvroute_server.so on your C2 server
- Use vroutesrv command line tool
Client:
- Use libvroute_client.so from your implant
- Use vrouteclt command line tool (not recommended)
About HTTPS
===============
You will need a certificate for setting up the HTTPS version.
If you just want to test, you can simply:
$ openssl req -newkey rsa:2048 -nodes -keyout cert.pem -x509 -days 365 -out cert.pem
And then pass the path to cert.pem to the server initialization entrypoing
------------------------------- [ End User notes ] -------------------------------
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include "util.h"
#include "crypt.h"
#include "tp/base64.h"
#include "server.h"
#define VROUTE_VERSION "1.0.0"
#define CHALLENGE_DEFAULT_SIZE 64
#define COMMAND_CHANNEL 0
#define POLL_TIMEOUT 2000
#define IN_ANY_ADDR "0.0.0.0"
#define MAX_HOSTNAME_SIZE 255
#define ROUTE_REQUEST_PROCESS_TIMEOUT 20 /* 10 seconds */
#define MAX_KEY_SIZE 64
#define PNG_FAKE_HDR "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
#define PNG_FAKE_HDR_SIZE 8
#define DATA_PREFIX "<img src='data:image/jpeg;base64,"
#define DATA_SUFFIX "' />"
#define DATA_INPUT_PREFIX "token="
#define DATA_INPUT_SUFFIX "; expire=0;"
#define MIN_CLIENT_ID 11111
#define MAX_CLIENT_ID 60000
#define MIN_CHANNEL_ID 11111
#define MAX_CHANNEL_ID 60000
#define VR_LOG(...) vr_log(__func__, __VA_ARGS__)
#if DEBUG
#define hexdump(tag, mem, size) __hexdump(__func__, tag, mem, size)
#else
#define hexdump(tag, mem, size) ((void)0)
#endif
#define MAX_LOG_MESSAGE_SIZE 4096
#define LOG_PREFIX_STR "[VROUTE]"
#define UNLIMITED_OPPORTUNITIES 1
/*
Source: https://www.openssh.com/txt/socks4.protocol
1) CONNECT
The client connects to the SOCKS server and sends a CONNECT request when
it wants to establish a connection to an application server. The client
includes in the request packet the IP address and the port number of the
destination host, and userid, in the following format.
+----+----+----+----+----+----+----+----+----+----+....+----+
| VN | CD | DSTPORT | DSTIP | USERID |NULL|
+----+----+----+----+----+----+----+----+----+----+....+----+
# of bytes: 1 1 2 4 variable 1
VN is the SOCKS protocol version number and should be 4. CD is the
SOCKS command code and should be 1 for CONNECT request. NULL is a byte
of all zero bits.
The SOCKS server checks to see whether such a request should be granted
based on any combination of source IP address, destination IP address,
destination port number, the userid, and information it may obtain by
consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
server makes a connection to the specified port of the destination host.
A reply packet is sent to the client when this connection is established,
or when the request is rejected or the operation fails.
+----+----+----+----+----+----+----+----+
| VN | CD | DSTPORT | DSTIP |
+----+----+----+----+----+----+----+----+
# of bytes: 1 1 2 4
VN is the version of the reply code and should be 0. CD is the result
code with one of the following values:
90: request granted
91: request rejected or failed
92: request rejected becasue SOCKS server cannot connect to
identd on the client
93: request rejected because the client program and identd
report different user-ids
The remaining fields are ignored.
The SOCKS server closes its connection immediately after notifying
the client of a failed or rejected request. For a successful request,
the SOCKS server gets ready to relay traffic on both directions. This
enables the client to do I/O on its connection as if it were directly
connected to the application server.
*/
/* VN=0, CD=0x5a (success), dstport, dstip (ignored) */
#define SOCKS_REPLY_SUCCESS "\x00\x5a\xff\xff\xff\xff\xff\xff"
/* VN=0, CD=0x5b (failure / rejected), dstport, dstip (ignored) */
#define SOCKS_REPLY_FAILURE "\x00\x5b\xff\xff\xff\xff\xff\xff"
#define AES_IV_SIZE 16
//#define MIN_IV_CHAR_RANGE 0x1
//#define MAX_IV_CHAR_RANGE 0xff
#define MIN_CHALL_CHR_VAL 0x01
#define MAX_CHALL_CHR_VAL 0xff
#define MAX_KEY_SIZE 64
#define LAST_CONN_GAP_THRESHOLD 60 * 5 // 5 minutes
cmd_def cmd_def_data[] = {
[UNKNOWN_CMD] = {
.cmd = UNKNOWN_CMD,
.value = 0,
.name = "UNKNOWN_CMD"
},
[CHANNEL_OPEN_CMD] = {
.cmd = CHANNEL_OPEN_CMD,
.value = 0xdd,
.name = "CHANNEL_OPEN_CMD"
},
[CHANNEL_CLOSE_CMD] = {
.cmd = CHANNEL_CLOSE_CMD,
.value = 0xcc,
.name = "CHANNEL_CLOSE_CMD"
},
[RELAY_CLOSE_CMD] = {
.cmd = RELAY_CLOSE_CMD,
.value = 0xc4,
.name = "RELAY_CLOSE_CMD"
},
[PING_CMD] = {
.cmd = PING_CMD,
.value = 0x70,
.name = "PING_CMD"
},
[FORWARD_CONNECTION_SUCCESS] = {
.cmd = FORWARD_CONNECTION_SUCCESS,
.value = 0xee,
.name = "FORWARD_CONNECTION_SUCCESS"
},
[FORWARD_CONNECTION_FAILURE] = {
.cmd = FORWARD_CONNECTION_FAILURE,
.value = 0xff,
.name = "FORWARD_CONNECTION_FAILURE"
},
[CMD_DATA_LAST_NULL_ENTRY] = {
.cmd = 0,
.value = 0,
.name = ""
}
};
int close_srv = 0;
conn_def *client_conns = NULL;
proxy_client_def *proxy_client_conns = NULL;
buffer_queue *queue_head = NULL;
http_handshake_def *handshake_defs = NULL;
conn_open_req *conn_req_glb = NULL;
pthread_mutex_t glb_structure_lock;
pthread_mutex_t proxy_glb_conn_lock;
pthread_mutex_t client_glb_conn_lock;
pthread_mutex_t handshake_glb_conn_lock;
pthread_mutex_t queue_glb_buf_lock;
pthread_mutex_t dead_client_worker;
pthread_mutex_t conn_req_lock;
pthread_mutex_t iopen_lock;
pthread_mutex_t route_open_lock;
char *_key = NULL;
size_t _key_sz = 0;
proto_t _proto = 0;
char *_cert_file = NULL;
SSL_CTX *_ctx = NULL;
void collect_dead_clients_worker(void) {
time_t last;
time_t curr = time(NULL);
long gap = 0;
// XXX: disable dead client collector
// to fix bug in which alive connections
// are being collected
while(1) {}
pthread_mutex_lock(&dead_client_worker);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(client_conns[i].client_id != 0 && (client_conns[i].proto == HTTP_COM_PROTO ||
client_conns[i].proto == HTTPS_COM_PROTO)) {
last = client_conns[i].last_conn_timestamp;
gap = curr - last;
if(gap > LAST_CONN_GAP_THRESHOLD) {
close_client(client_conns[i].client_id);
}
}
}
pthread_mutex_unlock(&dead_client_worker);
return;
}
void ping_worker(void) {
while(1) {
sleep(10);
if(close_srv) {
pthread_exit(NULL);
return;
}
collect_dead_clients_worker();
}
pthread_exit(NULL);
return;
}
void __ping_worker(void) {
pthread_t th;
pthread_create(&th, NULL, (void *)ping_worker, NULL);
return;
}
void shutdown_srv(void) {
close_srv = 1;
return;
}
void vr_log(const char *func_name, log_level_t log_level, const char *format_str, ...) {
char message[MAX_LOG_MESSAGE_SIZE + 1] = { 0 };
char *log_level_prefix = NULL;
if(!func_name || !format_str)
return;
if(log_level == UNKNOWN_LOG_LEVEL)
return;
#if !DEBUG
if(log_level == LOG_DEBUG)
return;
#endif
if(log_level == LOG_WARN) {
log_level_prefix = "[WARN]";
} else if(log_level == LOG_INFO) {
log_level_prefix = "[INFO]";
} else if(log_level == LOG_DEBUG) {
log_level_prefix = "[DEBUG]";
} else if(log_level == LOG_ERROR) {
log_level_prefix = "[ERROR]";
} else
return;
va_list args;
va_start(args, format_str);
vsnprintf(message, MAX_LOG_MESSAGE_SIZE, format_str, args);
va_end(args);
printf("%s %s %s: %s\n", LOG_PREFIX_STR, log_level_prefix, func_name, message);
return;
}
uint64_t generate_client_id(void) {
uint64_t client_id = (rand() % (MAX_CLIENT_ID - MIN_CLIENT_ID + 1)) + MIN_CLIENT_ID;
return client_id;
}
uint64_t generate_proxy_client_id(void) {
return generate_client_id();
}
uint64_t generate_queue_id(void) {
return generate_client_id();
}
uint64_t generate_channel_id(void) {
uint64_t channel_id = (rand() % (MAX_CHANNEL_ID - MIN_CHANNEL_ID + 1)) + MIN_CHANNEL_ID;
return channel_id;
}
int handshake_sess_exists(int h_id) {
int idx = -1;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
void generate_random_challenge(char *chall, size_t chall_sz) {
if(!chall || chall_sz == 0)
return;
for(int i = 0 ; i < chall_sz ; i++)
chall[i] = (rand() % (MAX_CHALL_CHR_VAL - MIN_CHALL_CHR_VAL + 1)) + MIN_CHALL_CHR_VAL;
return;
}
int is_challenge_solved(int h_id) {
int idx = -1;
int r = 0;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
r = handshake_defs[idx].is_solved;
if(r == 0) {
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
int is_challenge_failure(int h_id) {
int idx = -1;
int r = 0;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
r = handshake_defs[idx].fail;
if(r == 0) {
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
int mark_challenge_solved(int h_id) {
int idx = -1;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
handshake_defs[idx].is_solved = 1;
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
int mark_challenge_failure(int h_id) {
int idx = -1;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
handshake_defs[idx].fail = 1;
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
int create_handshake(int h_id) {
int idx = -1;
char chall[CHALLENGE_DEFAULT_SIZE + 1] = { 0 };
char *sol_a = NULL;
char *sol = NULL;
size_t sol_size_a = 0;
size_t sol_size = 0;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == 0) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find free slot for handshake definition");
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
generate_random_challenge(chall, CHALLENGE_DEFAULT_SIZE);
sol_a = encrypt_challenge((char *)chall, CHALLENGE_DEFAULT_SIZE, _key, _key_sz, &sol_size_a);
if(!sol_a) {
VR_LOG(LOG_ERROR, "Error trying to encrypt generated challenge");
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
sol = sha256_hash(sol_a, sol_size_a, &sol_size);
if(!sol) {
if(sol_a) {
free(sol_a);
sol_a = NULL;
}
VR_LOG(LOG_ERROR, "Error hashing solution in SHA-256");
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
if(sol_a) {
free(sol_a);
sol_a = NULL;
}
handshake_defs[idx].h_id = h_id;
handshake_defs[idx].challenge = memdup(chall, CHALLENGE_DEFAULT_SIZE);
handshake_defs[idx].solution = sol;
handshake_defs[idx].sol_size = sol_size;
handshake_defs[idx].is_solved = 0;
handshake_defs[idx].fail = 0;
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
int delete_handshake(int h_id) {
int idx = -1;
if(h_id <= 0)
return 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 0;
}
handshake_defs[idx].h_id = 0;
handshake_defs[idx].is_solved = 0;
handshake_defs[idx].fail = 0;
if(handshake_defs[idx].challenge) {
free(handshake_defs[idx].challenge);
handshake_defs[idx].challenge = NULL;
}
if(handshake_defs[idx].solution) {
free(handshake_defs[idx].solution);
handshake_defs[idx].solution = NULL;
}
handshake_defs[idx].sol_size = 0;
pthread_mutex_unlock(&handshake_glb_conn_lock);
return 1;
}
char *get_challenge(int h_id) {
int idx = -1;
char *chall = NULL;
if(h_id <= 0)
return NULL;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return NULL;
}
chall = handshake_defs[idx].challenge;
pthread_mutex_unlock(&handshake_glb_conn_lock);
return chall;
}
char *get_h_challenge_solution(int h_id, size_t *out_size) {
int idx = -1;
char *sol = NULL;
size_t sol_size = 0;
if(h_id <= 0 || !out_size)
return NULL;
*out_size = 0;
pthread_mutex_lock(&handshake_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(handshake_defs[i].h_id == h_id) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find handshake definition with handshake sess ID: %d", h_id);
pthread_mutex_unlock(&handshake_glb_conn_lock);
return NULL;
}
sol = handshake_defs[idx].solution;
sol_size = handshake_defs[idx].sol_size;
pthread_mutex_unlock(&handshake_glb_conn_lock);
*out_size = sol_size;
return sol;
}
/* XXX: locking relies on caller */
int channel_exists(int channel_id) {
int idx = -1;
int c_idx = -1;
if(channel_id <= 0)
return 0;
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
for(int x = 0 ; x < MAX_CONCURRENT_CHANNELS_PER_CLIENT ; x++) {
if(client_conns[i].c_channels && client_conns[i].c_channels[x].channel_id == channel_id) {
c_idx = i;
idx = x;
break;
}
}
}
if(idx == -1 || c_idx == -1) {
return 0;
}
return 1;
}
int create_channel(int client_id, int proxy_sock, char *host, int port) {
int c_idx = -1;
int idx = -1;
int channel_id = 0;
int32_t dst_ip_addr = 0;
if(client_id <= 0 || proxy_sock < 0 || !host || port <= 0)
return 0;
pthread_mutex_lock(&client_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(client_conns[i].client_id == client_id) {
c_idx = i;
break;
}
}
if(c_idx == -1) {
VR_LOG(LOG_ERROR, "Could not find client connection with client ID: %d", client_id);
pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
for(int i = 0 ; i < MAX_CONCURRENT_CHANNELS_PER_CLIENT ; i++) {
if(client_conns[c_idx].c_channels && client_conns[c_idx].c_channels[i].channel_id == 0) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find free slot for channel definition");
pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
dst_ip_addr = inet_addr(host);
while(channel_id = generate_channel_id()) {
if(!channel_exists(channel_id))
break;
}
client_conns[c_idx].c_channels[idx].channel_id = channel_id;
client_conns[c_idx].c_channels[idx].client_id = client_id;
client_conns[c_idx].c_channels[idx].dst_ip_addr = dst_ip_addr;
client_conns[c_idx].c_channels[idx].dst_port = (int16_t)port;
client_conns[c_idx].c_channels[idx].proxy_client_sock = proxy_sock;
pthread_mutex_unlock(&client_glb_conn_lock);
return channel_id;
}
int create_channel_custom(int channel_id, int client_id, int proxy_sock, char *host, int port) {
int c_idx = -1;
int idx = -1;
int32_t dst_ip_addr = 0;
if(client_id <= 0 || proxy_sock < 0 || !host || port <= 0)
return 0;
//pthread_mutex_lock(&client_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
if(client_conns[i].client_id == client_id) {
c_idx = i;
break;
}
}
if(c_idx == -1) {
VR_LOG(LOG_ERROR, "Could not find client connection with client ID: %d", client_id);
// pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
for(int i = 0 ; i < MAX_CONCURRENT_CHANNELS_PER_CLIENT ; i++) {
if(client_conns[c_idx].c_channels && client_conns[c_idx].c_channels[i].channel_id == 0) {
idx = i;
break;
}
}
if(idx == -1) {
VR_LOG(LOG_ERROR, "Could not find free slot for channel definition");
// pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
dst_ip_addr = inet_addr(host);
client_conns[c_idx].c_channels[idx].channel_id = channel_id;
client_conns[c_idx].c_channels[idx].client_id = client_id;
client_conns[c_idx].c_channels[idx].dst_ip_addr = dst_ip_addr;
client_conns[c_idx].c_channels[idx].dst_port = (int16_t)port;
client_conns[c_idx].c_channels[idx].proxy_client_sock = proxy_sock;
// pthread_mutex_unlock(&client_glb_conn_lock);
return channel_id;
}
int send_remote_cmd(scmd_t cmd, int channel_id) {
char *p = NULL;
size_t p_sz = 0;
tlv_header *tlv = NULL;
s_cmd *cmd_p = NULL;
int client_id = 0;
int rsock = 0;
ssize_t rx = 0;
char *enc = NULL;
size_t enc_sz = 0;
if(channel_id <= 0)
return 0;
switch(cmd) {
case CHANNEL_CLOSE_CMD:
case RELAY_CLOSE_CMD:
case PING_CMD:
break;
case CHANNEL_OPEN_CMD: /* issued independently */
case FORWARD_CONNECTION_SUCCESS: /* server-only command */
case FORWARD_CONNECTION_FAILURE: /* server-only command */
case UNKNOWN_CMD: /* unknown command */
default:
return 0;
}
p_sz = sizeof(tlv_header) + sizeof(s_cmd);
p = calloc(p_sz, sizeof(char));
if(!p)
return 0;
tlv = (tlv_header *)p;
cmd_p = ((s_cmd *)(p + sizeof(tlv_header)));
client_id = get_client_by_channel_id(channel_id);
if(client_id <= 0) {
VR_LOG(LOG_ERROR, "Error getting client ID by channel ID: %d", channel_id);
return 0;
}
tlv->client_id = client_id;
tlv->channel_id = COMMAND_CHANNEL;
tlv->tlv_data_len = sizeof(s_cmd);
cmd_p->cmd = cmd_def_data[cmd].value;
cmd_p->channel_id = channel_id;
VR_LOG(LOG_INFO, "Sending command '%s'", cmd_def_data[cmd].name);
if(_proto == TCP_COM_PROTO) {
rsock = get_relay_sock_by_client_id(client_id);
if(rsock < 0) {
VR_LOG(LOG_ERROR, "Error getting relay sock by client ID: %d", client_id);
return 0;
}
enc = encrypt_data(p, p_sz, _key, _key_sz, &enc_sz);
if(!enc) {
VR_LOG(LOG_ERROR, "Error trying to encrypt data");
return 0;
}
rx = write_all(rsock, &enc, &enc_sz);
if(rx < 0) {
VR_LOG(LOG_ERROR, "Error writing to TCP relay clients");
return 0;
}
if(enc) {
free(enc);
enc = NULL;
}
} else if(_proto == HTTP_COM_PROTO ||
_proto == HTTPS_COM_PROTO) {
if(!queue_data(client_id, p, p_sz, time(NULL))) {
VR_LOG(LOG_ERROR, "Error trying to queue data");
return 0;
}
}
// XXX: fix p memory leaks on this function
if(p) {
free(p);
p = NULL;
}
return 1;
}
int close_channel(int channel_id, int is_client_req) {
int idx = -1;
int c_idx = -1;
if(channel_id <= 0)
return 0;
// pthread_mutex_lock(&client_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
for(int x = 0 ; x < MAX_CONCURRENT_CHANNELS_PER_CLIENT ; x++) {
if(client_conns[i].c_channels && client_conns[i].c_channels[x].channel_id == channel_id) {
c_idx = i;
idx = x;
break;
}
}
}
if(idx == -1 || c_idx == -1) {
VR_LOG(LOG_ERROR, "Could not find channel definition with channel ID: %d", channel_id);
// pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
client_conns[c_idx].c_channels[idx].channel_id = 0;
client_conns[c_idx].c_channels[idx].client_id = 0;
client_conns[c_idx].c_channels[idx].dst_ip_addr = 0;
client_conns[c_idx].c_channels[idx].dst_port = 0;
if(client_conns[c_idx].c_channels[idx].proxy_client_sock != -1) {
close(client_conns[c_idx].c_channels[idx].proxy_client_sock);
client_conns[c_idx].c_channels[idx].proxy_client_sock = -1;
}
if(!is_client_req) {
if(!send_remote_cmd(CHANNEL_CLOSE_CMD, channel_id)) {
VR_LOG(LOG_ERROR, "Error sending remote cmd 'CHANNEL_CLOSE_CMD' to channel ID: %d", channel_id);
// pthread_mutex_unlock(&client_glb_conn_lock);
return 0;
}
}
// pthread_mutex_unlock(&client_glb_conn_lock);
return 1;
}
int get_client_by_channel_id(int channel_id) {
int idx = -1;
int c_idx = -1;
int client_id = 0;
if(channel_id <= 0)
return 0;
pthread_mutex_lock(&client_glb_conn_lock);
for(int i = 0 ; i < MAX_CONCURRENT_CLIENTS ; i++) {
for(int x = 0 ; x < MAX_CONCURRENT_CHANNELS_PER_CLIENT ; x++) {
if(client_conns[i].c_channels && client_conns[i].c_channels[x].channel_id == channel_id) {
c_idx = i;
idx = x;
break;
}
}
}