-
Notifications
You must be signed in to change notification settings - Fork 4
/
protocol.cpp
2147 lines (2038 loc) · 92.2 KB
/
protocol.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
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
//typedef bool pthread_mutex_t;
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten/wire.h>
#include <cstdint>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <iterator>
#include <type_traits>
#include <vector>
#include <algorithm>
#include "guac-bindings.hpp"
#include "pthread-noop.h"
#define pthread_mutex_t int
#include <capnp/dynamic.h>
#include <capnp/serialize.h>
#include "CollabVm.capnp.h"
#include "CollabVmCommon.hpp"
//char (*_nop)[sizeof(pthread_mutex_t)] = 1;
struct GuacamoleParameterWrapper {
GuacamoleParameterWrapper() = default;
GuacamoleParameterWrapper(std::string&& name, std::string&& value)
: name_(std::move(name)), value_(std::move(value)) {}
std::string getName() const { return name_; }
void setName(std::string&& name) { name_ = std::move(name); }
std::string getValue() const { return value_; }
void setValue(std::string&& value) { value_ = std::move(value); }
private:
std::string name_;
std::string value_;
};
struct VmInfoWrapper {
VmInfoWrapper() {}
VmInfoWrapper(CollabVmServerMessage::VmInfo::Reader vmInfo) : vmInfo_(vmInfo) {}
std::uint32_t getId() const { return vmInfo_.getId(); }
void setId(std::uint32_t&& id) { id_ = std::move(id); }
std::string getName() const { return vmInfo_.getName(); }
void setName(std::string&& name) { name_ = std::move(name); }
std::string getHost() const { return vmInfo_.getHost(); }
void setHost(std::string&& host) { host_ = std::move(host); }
std::string getAddress() const { return vmInfo_.getAddress(); }
void setAddress(std::string&& address) { address_ = std::move(address); }
std::string getOperatingSystem() const { return vmInfo_.getOperatingSystem(); }
void setOperatingSystem(std::string&& operatingSystem) { operatingSystem_ = std::move(operatingSystem); }
bool getUploads() const { return vmInfo_.getUploads(); }
void setUploads(bool&& uploads) { uploads_ = std::move(uploads); }
bool getInput() const { return vmInfo_.getInput(); }
void setInput(bool&& input) { input_ = std::move(input); }
std::uint8_t getRam() const { return vmInfo_.getRam(); }
void setRam(std::uint8_t&& ram) { ram_ = std::move(ram); }
std::uint8_t getDiskSpace() const { return vmInfo_.getDiskSpace(); }
void setDiskSpace(std::uint8_t&& disk) { disk_ = std::move(disk); }
bool getSafeForWork() const { return vmInfo_.getSafeForWork(); }
void setSafeForWork(bool&& safe_for_work) { safe_for_work_ = std::move(safe_for_work); }
std::uint16_t getViewerCount() const { return vmInfo_.getViewerCount(); }
void setViewerCount(std::uint16_t&& viewer_count) { viewer_count_ = std::move(viewer_count); }
private:
CollabVmServerMessage::VmInfo::Reader vmInfo_;
std::uint32_t id_;
std::string name_;
std::string host_;
std::string address_;
std::string operatingSystem_;
bool uploads_;
bool input_;
std::uint8_t ram_;
std::uint8_t disk_;
bool safe_for_work_;
std::uint16_t viewer_count_;
};
struct AdminVmInfoWrapper {
AdminVmInfoWrapper() {}
AdminVmInfoWrapper(CollabVmServerMessage::AdminVmInfo::Reader adminVmInfo) : adminVmInfo_(adminVmInfo) {}
std::uint32_t getId() const { return adminVmInfo_.getId(); }
void setId(std::uint32_t&& id) { id_ = std::move(id); }
std::string getName() const { return adminVmInfo_.getName(); }
void setName(std::string&& name) { name_ = std::move(name); }
std::uint8_t getStatus() const { return static_cast<std::uint8_t>(adminVmInfo_.getStatus()); }
void setStatus(std::uint8_t&& status) { status_ = std::move(status); }
private:
CollabVmServerMessage::AdminVmInfo::Reader adminVmInfo_;
std::uint32_t id_;
std::string name_;
std::uint8_t status_;
};
struct LoginWrapper {
LoginWrapper() {}
LoginWrapper(CollabVmClientMessage::Login::Reader login) : login_(login) {}
std::string getUsername() const { return login_.getUsername(); }
void setUsername(std::string&& username) { username_ = std::move(username); }
std::string getPassword() const { return login_.getPassword(); }
void setPassword(std::string&& password) { password_ = std::move(password); }
private:
CollabVmClientMessage::Login::Reader login_;
std::string username_;
std::string password_;
};
struct RegisterAccountWrapper {
RegisterAccountWrapper() {}
RegisterAccountWrapper(CollabVmClientMessage::RegisterAccount::Reader registerAccount) : registerAccount_(registerAccount) {}
std::string getUsername() const { return registerAccount_.getUsername(); }
void setUsername(std::string&& username) { username_ = std::move(username); }
std::string getPassword() const { return registerAccount_.getPassword(); }
void setPassword(std::string&& password) { password_ = std::move(password); }
std::string getTwoFactorToken() const { return std::string(reinterpret_cast<const char*>(registerAccount_.getTwoFactorToken().begin()), registerAccount_.getTwoFactorToken().size()); }
void setTwoFactorToken(std::string&& twoFactorToken) { twoFactorToken_ = std::move(twoFactorToken); }
private:
CollabVmClientMessage::RegisterAccount::Reader registerAccount_;
std::string username_;
std::string password_;
std::string twoFactorToken_;
};
struct UserInviteWrapper {
UserInviteWrapper() {}
UserInviteWrapper(CollabVmClientMessage::UserInvite::Reader userInvite) : userInvite_(userInvite) {}
std::string getId() const { return std::string(reinterpret_cast<const char*>(userInvite_.getId().begin()), userInvite_.getId().size()); }
void setId(std::string&& id) { id_ = std::move(id); }
std::string getInviteName() const { return userInvite_.getInviteName(); }
void setInviteName(std::string&& inviteName) { inviteName_ = std::move(inviteName); }
std::string getUsername() const { return userInvite_.getUsername(); }
void setUsername(std::string&& username) { username_ = std::move(username); }
bool getAdmin() const { return userInvite_.getAdmin(); }
void setAdmin(bool&& admin) { admin_ = std::move(admin); }
CollabVmClientMessage::UserInvite::Reader userInvite_;
std::string id_;
std::string inviteName_;
std::string username_;
bool admin_;
};
struct CaptchaWrapper {
CaptchaWrapper() {}
CaptchaWrapper(ServerSetting::Captcha::Reader captcha) {
enabled_ = captcha.getEnabled();
https_ = captcha.getHttps();
urlHost_ = captcha.getUrlHost();
urlPort_ = captcha.getUrlPort();
urlPath_ = captcha.getUrlPath();
postParams_ = captcha.getPostParams();
validJSONVariableName_ = captcha.getValidJSONVariableName();
}
bool getEnabled() const { return enabled_; }
void setEnabled(bool&& enabled) { enabled_ = std::forward<bool>(enabled); }
bool getHttps() const { return https_; }
void setHttps(bool&& https) { https_ = std::forward<bool>(https); }
std::string getUrlHost() const { return urlHost_; }
void setUrlHost(std::string&& urlHost) { urlHost_ = std::forward<std::string>(urlHost); }
std::uint16_t getUrlPort() const { return urlPort_; }
void setUrlPort(std::uint16_t&& urlPort) { urlPort_ = std::forward<std::uint16_t>(urlPort); }
std::string getUrlPath() const { return urlPath_; }
void setUrlPath(std::string&& urlPath) { urlPath_ = std::forward<std::string>(urlPath); }
std::string getPostParams() const { return postParams_; }
void setPostParams(std::string&& postParams) { postParams_ = std::forward<std::string>(postParams); }
std::string getValidJSONVariableName() const { return validJSONVariableName_; }
void setValidJSONVariableName(std::string&& validJSONVariableName) { validJSONVariableName_ = std::forward<std::string>(validJSONVariableName); }
void serialize(ServerSetting::Captcha::Builder builder) {
builder.setEnabled(enabled_);
builder.setHttps(https_);
builder.setUrlHost(urlHost_);
builder.setUrlPort(urlPort_);
builder.setUrlPath(urlPath_);
builder.setPostParams(postParams_);
builder.setValidJSONVariableName(validJSONVariableName_);
}
private:
bool enabled_;
bool https_;
std::string urlHost_;
std::uint16_t urlPort_;
std::string urlPath_;
std::string postParams_;
std::string validJSONVariableName_;
};
struct RecordingsWrapper {
RecordingsWrapper() {}
RecordingsWrapper(ServerSetting::Recordings::Reader recordings) {
keyframeInterval_ = recordings.getKeyframeInterval();
fileDuration_ = recordings.getFileDuration();
captureDisplay_ = recordings.getCaptureDisplay();
captureAudio_ = recordings.getCaptureAudio();
captureInput_ = recordings.getCaptureInput();
}
std::uint32_t getKeyframeInterval() const { return keyframeInterval_; }
void setKeyframeInterval(std::uint32_t&& keyframeInterval) { keyframeInterval_ = std::forward<std::uint32_t>(keyframeInterval); }
std::uint32_t getFileDuration() const { return fileDuration_; }
void setFileDuration(std::uint32_t&& fileDuration) { fileDuration_ = std::forward<std::uint32_t>(fileDuration); }
bool getCaptureDisplay() const { return captureDisplay_; }
void setCaptureDisplay(bool&& captureDisplay) { captureDisplay_ = std::forward<bool>(captureDisplay); }
bool getCaptureAudio() const { return captureAudio_; }
void setCaptureAudio(bool&& captureAudio) { captureAudio_ = std::forward<bool>(captureAudio); }
bool getCaptureInput() const { return captureInput_; }
void setCaptureInput(bool&& captureInput) { captureInput_ = std::forward<bool>(captureInput); }
void serialize(ServerSetting::Recordings::Builder builder) {
builder.setKeyframeInterval(keyframeInterval_);
builder.setFileDuration(fileDuration_);
builder.setCaptureDisplay(captureDisplay_);
builder.setCaptureAudio(captureAudio_);
builder.setCaptureInput(captureInput_);
}
private:
std::uint32_t keyframeInterval_;
std::uint32_t fileDuration_;
bool captureDisplay_;
bool captureAudio_;
bool captureInput_;
};
struct ServerSettingsWrapper {
ServerSettingsWrapper(){}
ServerSettingsWrapper(capnp::List<ServerSetting>::Reader settings) {
for (auto&& server_setting : settings) {
const auto setting = server_setting.getSetting();
switch (setting.which()) {
case ServerSetting::Setting::ALLOW_ACCOUNT_REGISTRATION:
allowAccountRegistration_ = setting.getAllowAccountRegistration();
break;
case ServerSetting::Setting::CAPTCHA:
captcha_ = CaptchaWrapper(setting.getCaptcha());
break;
case ServerSetting::Setting::CAPTCHA_REQUIRED:
captchaRequired_ = setting.getCaptchaRequired();
break;
case ServerSetting::Setting::USER_VMS_ENABLED:
userVmsEnabled_ = setting.getUserVmsEnabled();
break;
case ServerSetting::Setting::ALLOW_USER_VM_REQUESTS:
allowUserVmRequests_ = setting.getAllowUserVmRequests();
break;
case ServerSetting::Setting::BAN_IP_COMMAND:
banIpCommand_ = setting.getBanIpCommand();
break;
case ServerSetting::Setting::UNBAN_IP_COMMAND:
unbanIpCommand_ = setting.getUnbanIpCommand();
break;
case ServerSetting::Setting::MAX_CONNECTIONS_ENABLED:
maxConnectionsEnabled_ = setting.getMaxConnectionsEnabled();
break;
case ServerSetting::Setting::MAX_CONNECTIONS:
maxConnections_ = setting.getMaxConnections();
break;
case ServerSetting::Setting::RECORDINGS:
recordings_ = RecordingsWrapper(setting.getRecordings());
break;
}
}
}
void getServerConfigModifications(capnp::MallocMessageBuilder& message_builder) {
auto message = message_builder.initRoot<CollabVmClientMessage::Message>();
auto config_mod = message.initServerConfigModifications(modified_settings_.size());
auto i = 0;
for (const auto setting_type : modified_settings_) {
auto setting = config_mod[i].initSetting();
switch (static_cast<const ServerSetting::Setting::Which>(setting_type)) {
case ServerSetting::Setting::ALLOW_ACCOUNT_REGISTRATION:
setting.setAllowAccountRegistration(allowAccountRegistration_);
break;
case ServerSetting::Setting::CAPTCHA:
captcha_.serialize(setting.initCaptcha());
break;
case ServerSetting::Setting::CAPTCHA_REQUIRED:
setting.setCaptchaRequired(captchaRequired_);
break;
case ServerSetting::Setting::USER_VMS_ENABLED:
setting.setUserVmsEnabled(userVmsEnabled_);
break;
case ServerSetting::Setting::ALLOW_USER_VM_REQUESTS:
setting.setAllowUserVmRequests(allowUserVmRequests_);
break;
case ServerSetting::Setting::BAN_IP_COMMAND:
setting.setBanIpCommand(banIpCommand_);
break;
case ServerSetting::Setting::UNBAN_IP_COMMAND:
setting.setUnbanIpCommand(unbanIpCommand_);
break;
case ServerSetting::Setting::MAX_CONNECTIONS_ENABLED:
setting.setMaxConnectionsEnabled(maxConnectionsEnabled_);
break;
case ServerSetting::Setting::MAX_CONNECTIONS:
setting.setMaxConnections(maxConnections_);
break;
case ServerSetting::Setting::RECORDINGS:
recordings_.serialize(setting.initRecordings());
break;
default:
assert(false);
}
i++;
}
modified_settings_.clear();
}
bool getAllowAccountRegistration() const {
return allowAccountRegistration_;
}
void setAllowAccountRegistration(bool&& allowAccountRegistration) {
allowAccountRegistration_ = std::move(allowAccountRegistration);
modified_settings_.emplace(ServerSetting::Setting::ALLOW_ACCOUNT_REGISTRATION);
}
CaptchaWrapper getCaptcha() const {
return captcha_;
}
void setCaptcha(CaptchaWrapper&& captcha) {
captcha_ = std::forward<CaptchaWrapper>(captcha);
modified_settings_.emplace(ServerSetting::Setting::CAPTCHA);
}
bool getCaptchaRequired() const {
return captchaRequired_;
}
void setCaptchaRequired(bool enabled) {
captchaRequired_ = enabled;
modified_settings_.emplace(ServerSetting::Setting::CAPTCHA_REQUIRED);
}
bool getUserVmsEnabled() const {
return userVmsEnabled_;
}
void setUserVmsEnabled(bool&& userVmsEnabled) {
userVmsEnabled_ = std::move(userVmsEnabled);
modified_settings_.emplace(ServerSetting::Setting::USER_VMS_ENABLED);
}
bool getAllowUserVmRequests() const {
return allowUserVmRequests_;
}
void setAllowUserVmRequests(bool&& allowUserVmRequests) {
allowUserVmRequests_ = std::move(allowUserVmRequests);
modified_settings_.emplace(ServerSetting::Setting::ALLOW_USER_VM_REQUESTS);
}
std::string getBanIpCommand() const {
return banIpCommand_;
}
void setBanIpCommand(std::string&& banIpCommand) {
banIpCommand_ = std::move(banIpCommand);
modified_settings_.emplace(ServerSetting::Setting::BAN_IP_COMMAND);
}
std::string getUnbanIpCommand() const {
return unbanIpCommand_;
}
void setUnbanIpCommand(std::string&& unbanIpCommand) {
unbanIpCommand_ = std::move(unbanIpCommand);
modified_settings_.emplace(ServerSetting::Setting::UNBAN_IP_COMMAND);
}
bool getMaxConnectionsEnabled() const {
return maxConnectionsEnabled_;
}
void setMaxConnectionsEnabled(bool maxConnectionsEnabled) {
maxConnectionsEnabled_ = std::move(maxConnectionsEnabled);
modified_settings_.emplace(ServerSetting::Setting::MAX_CONNECTIONS_ENABLED);
}
std::uint8_t getMaxConnections() const {
return maxConnections_;
}
void setMaxConnections(std::uint8_t&& maxConnections) {
maxConnections_ = std::move(maxConnections);
modified_settings_.emplace(ServerSetting::Setting::MAX_CONNECTIONS);
}
RecordingsWrapper getRecordings() const {
return recordings_;
}
void setRecordings(RecordingsWrapper&& recordings) {
recordings_ = std::forward<RecordingsWrapper>(recordings);
modified_settings_.emplace(ServerSetting::Setting::RECORDINGS);
}
private:
bool allowAccountRegistration_;
bool captchaRequired_;
CaptchaWrapper captcha_;
bool recaptchaEnabled_;
std::string recaptchaKey_;
bool userVmsEnabled_;
bool allowUserVmRequests_;
std::string banIpCommand_;
std::string unbanIpCommand_;
bool maxConnectionsEnabled_;
std::uint8_t maxConnections_;
RecordingsWrapper recordings_;
std::set<int> modified_settings_;
};
struct VmSettingsWrapper {
VmSettingsWrapper(){}
VmSettingsWrapper(capnp::List<VmSetting>::Reader settings) {
for (auto&& server_setting : settings) {
const auto setting = server_setting.getSetting();
switch (setting.which()) {
case VmSetting::Setting::AUTO_START:
auto_start_ = setting.getAutoStart();
break;
case VmSetting::Setting::NAME:
name_ = setting.getName();
break;
case VmSetting::Setting::DESCRIPTION:
description_ = setting.getDescription();
break;
case VmSetting::Setting::HOST:
host_ = setting.getHost();
break;
case VmSetting::Setting::OPERATING_SYSTEM:
operatingSystem_ = setting.getOperatingSystem();
break;
case VmSetting::Setting::RAM:
ram_ = setting.getRam();
break;
case VmSetting::Setting::DISK_SPACE:
disk_ = setting.getDiskSpace();
break;
case VmSetting::Setting::START_COMMAND:
startCommand_ = setting.getStartCommand();
break;
case VmSetting::Setting::STOP_COMMAND:
stopCommand_ = setting.getStopCommand();
break;
case VmSetting::Setting::RESTART_COMMAND:
restartCommand_ = setting.getRestartCommand();
break;
case VmSetting::Setting::SNAPSHOT_COMMANDS:
break;
case VmSetting::Setting::TURNS_ENABLED:
turnsEnabled_ = setting.getTurnsEnabled();
break;
case VmSetting::Setting::TURN_TIME:
turnTime_ = setting.getTurnTime();
break;
case VmSetting::Setting::UPLOADS_ENABLED:
uploadsEnabled_ = setting.getUploadsEnabled();
break;
case VmSetting::Setting::UPLOAD_COOLDOWN_TIME:
uploadCooldownTime_ = setting.getUploadCooldownTime();
break;
case VmSetting::Setting::MAX_UPLOAD_SIZE:
maxUploadSize_ = setting.getMaxUploadSize();
break;
case VmSetting::Setting::VOTES_ENABLED:
votesEnabled_ = setting.getVotesEnabled();
break;
case VmSetting::Setting::VOTE_TIME:
voteTime_ = setting.getVoteTime();
break;
case VmSetting::Setting::VOTE_COOLDOWN_TIME:
voteCooldownTime_ = setting.getVoteCooldownTime();
break;
case VmSetting::Setting::DISALLOW_GUEST_VOTES:
disallowGuestVotes_ = setting.getDisallowGuestVotes();
break;
case VmSetting::Setting::PROTOCOL:
protocol_ = static_cast<std::uint16_t>(setting.getProtocol());
break;
case VmSetting::Setting::AGENT_ADDRESS:
address_ = setting.getAgentAddress();
break;
case VmSetting::Setting::GUACAMOLE_PARAMETERS:
for (auto guac_param : setting.getGuacamoleParameters()) {
guacamole_parameters_.emplace_back(guac_param.getName(), guac_param.getValue());
}
break;
case VmSetting::Setting::SAFE_FOR_WORK:
safe_for_work_ = setting.getSafeForWork();
break;
case VmSetting::Setting::DISALLOW_GUESTS:
disallow_guests_ = setting.getDisallowGuests();
break;
case VmSetting::Setting::RECORDINGS_ENABLED:
recordings_enabled_ = setting.getRecordingsEnabled();
break;
case VmSetting::Setting::RUN_START_COMMAND_AFTER_DISCONNECT:
run_start_command_after_disconnect_ = setting.getRunStartCommandAfterDisconnect();
break;
}
}
}
std::size_t size() const {
return modified_settings_.size();
}
void getCreateVmRequest(capnp::List<VmSetting>::Builder settings) {
auto i = 0;
for (const auto setting_type : modified_settings_) {
auto setting = settings[i].initSetting();
switch (static_cast<const VmSetting::Setting::Which>(setting_type)) {
case VmSetting::Setting::AUTO_START:
setting.setAutoStart(auto_start_);
break;
case VmSetting::Setting::NAME:
setting.setName(name_);
break;
case VmSetting::Setting::DESCRIPTION:
setting.setDescription(description_);
break;
case VmSetting::Setting::HOST:
setting.setHost(host_);
break;
case VmSetting::Setting::OPERATING_SYSTEM:
setting.setOperatingSystem(operatingSystem_);
break;
case VmSetting::Setting::RAM:
setting.setRam(ram_);
break;
case VmSetting::Setting::DISK_SPACE:
setting.setDiskSpace(disk_);
break;
case VmSetting::Setting::START_COMMAND:
setting.setStartCommand(startCommand_);
break;
case VmSetting::Setting::STOP_COMMAND:
setting.setStopCommand(stopCommand_);
break;
case VmSetting::Setting::RESTART_COMMAND:
setting.setRestartCommand(restartCommand_);
break;
case VmSetting::Setting::SNAPSHOT_COMMANDS:
break;
case VmSetting::Setting::TURNS_ENABLED:
setting.setTurnsEnabled(turnsEnabled_);
break;
case VmSetting::Setting::TURN_TIME:
setting.setTurnTime(turnTime_);
break;
case VmSetting::Setting::UPLOADS_ENABLED:
setting.setUploadsEnabled(uploadsEnabled_);
break;
case VmSetting::Setting::UPLOAD_COOLDOWN_TIME:
setting.setUploadCooldownTime(uploadCooldownTime_);
break;
case VmSetting::Setting::MAX_UPLOAD_SIZE:
setting.setMaxUploadSize(maxUploadSize_);
break;
case VmSetting::Setting::VOTES_ENABLED:
setting.setVotesEnabled(votesEnabled_);
break;
case VmSetting::Setting::VOTE_TIME:
setting.setVoteTime(voteTime_);
break;
case VmSetting::Setting::VOTE_COOLDOWN_TIME:
setting.setVoteCooldownTime(voteCooldownTime_);
break;
case VmSetting::Setting::DISALLOW_GUEST_VOTES:
setting.setDisallowGuestVotes(disallowGuestVotes_);
break;
case VmSetting::Setting::PROTOCOL:
setting.setProtocol(VmSetting::Protocol(protocol_));
break;
case VmSetting::Setting::AGENT_ADDRESS:
setting.setAgentAddress(address_);
break;
case VmSetting::Setting::GUACAMOLE_PARAMETERS:
{
auto guac_params = setting.initGuacamoleParameters(guacamole_parameters_.size());
for (auto i = 0u; i < guacamole_parameters_.size(); i++) {
guac_params[i].setName(guacamole_parameters_[i].getName());
guac_params[i].setValue(guacamole_parameters_[i].getValue());
}
}
break;
case VmSetting::Setting::SAFE_FOR_WORK:
setting.setSafeForWork(safe_for_work_);
break;
case VmSetting::Setting::DISALLOW_GUESTS:
setting.setDisallowGuests(disallow_guests_);
break;
case VmSetting::Setting::RECORDINGS_ENABLED:
setting.setRecordingsEnabled(recordings_enabled_);
break;
case VmSetting::Setting::RUN_START_COMMAND_AFTER_DISCONNECT:
setting.setRunStartCommandAfterDisconnect(run_start_command_after_disconnect_);
break;
default:
std::cerr << "unknown setting" << std::endl;
assert(false);
}
i++;
}
modified_settings_.clear();
}
bool getAutoStart() const {
return auto_start_;
}
void setAutoStart(bool&& auto_start) {
auto_start_ = auto_start;
modified_settings_.emplace(VmSetting::Setting::AUTO_START);
}
std::string getName() const {
return name_;
}
void setName(std::string&& name) {
name_ = std::move(name);
modified_settings_.emplace(VmSetting::Setting::NAME);
}
std::string getDescription() const {
return description_;
}
void setDescription(std::string&& description) {
description_ = std::move(description);
modified_settings_.emplace(VmSetting::Setting::DESCRIPTION);
}
std::string getHost() const {
return host_;
}
void setHost(std::string&& host) {
host_ = std::move(host);
modified_settings_.emplace(VmSetting::Setting::HOST);
}
std::string getOperatingSystem() const {
return operatingSystem_;
}
void setOperatingSystem(std::string&& operatingSystem) {
operatingSystem_ = std::move(operatingSystem);
modified_settings_.emplace(VmSetting::Setting::OPERATING_SYSTEM);
}
std::uint8_t getRam() const {
return ram_;
}
void setRam(std::uint8_t&& ram) {
ram_ = std::move(ram);
modified_settings_.emplace(VmSetting::Setting::RAM);
}
std::uint8_t getDiskSpace() const {
return disk_;
}
void setDiskSpace(std::uint8_t&& disk) {
disk_ = std::move(disk);
modified_settings_.emplace(VmSetting::Setting::DISK_SPACE);
}
std::string getStartCommand() const {
return startCommand_;
}
void setStartCommand(std::string&& startCommand) {
startCommand_ = std::move(startCommand);
modified_settings_.emplace(VmSetting::Setting::START_COMMAND);
}
std::string getStopCommand() const {
return stopCommand_;
}
void setStopCommand(std::string&& stopCommand) {
stopCommand_ = std::move(stopCommand);
modified_settings_.emplace(VmSetting::Setting::STOP_COMMAND);
}
std::string getRestartCommand() const {
return restartCommand_;
}
void setRestartCommand(std::string&& restartCommand) {
restartCommand_ = std::move(restartCommand);
modified_settings_.emplace(VmSetting::Setting::RESTART_COMMAND);
}
bool getTurnsEnabled() const {
return turnsEnabled_;
}
void setTurnsEnabled(bool&& turnsEnabled) {
turnsEnabled_ = std::move(turnsEnabled);
modified_settings_.emplace(VmSetting::Setting::TURNS_ENABLED);
}
std::uint16_t getTurnTime() const {
return turnTime_;
}
void setTurnTime(std::uint16_t&& turnTime) {
turnTime_ = std::move(turnTime);
modified_settings_.emplace(VmSetting::Setting::TURN_TIME);
}
bool getUploadsEnabled() const {
return uploadsEnabled_;
}
void setUploadsEnabled(bool&& uploadsEnabled) {
uploadsEnabled_ = std::move(uploadsEnabled);
modified_settings_.emplace(VmSetting::Setting::UPLOADS_ENABLED);
}
std::uint16_t getUploadCooldownTime() const {
return uploadCooldownTime_;
}
void setUploadCooldownTime(std::uint16_t&& uploadCooldownTime) {
uploadCooldownTime_ = std::move(uploadCooldownTime);
modified_settings_.emplace(VmSetting::Setting::UPLOAD_COOLDOWN_TIME);
}
std::uint32_t getMaxUploadSize() const {
return maxUploadSize_;
}
void setMaxUploadSize(std::uint32_t&& maxUploadSize) {
maxUploadSize_ = std::move(maxUploadSize);
modified_settings_.emplace(VmSetting::Setting::MAX_UPLOAD_SIZE);
}
bool getVotesEnabled() const {
return votesEnabled_;
}
void setVotesEnabled(bool&& votesEnabled) {
votesEnabled_ = std::move(votesEnabled);
modified_settings_.emplace(VmSetting::Setting::VOTES_ENABLED);
}
std::uint16_t getVoteTime() const {
return voteTime_;
}
void setVoteTime(std::uint16_t&& voteTime) {
voteTime_ = std::move(voteTime);
modified_settings_.emplace(VmSetting::Setting::VOTE_TIME);
}
std::uint16_t getVoteCooldownTime() const {
return voteCooldownTime_;
}
void setVoteCooldownTime(std::uint16_t&& voteCooldownTime) {
voteCooldownTime_ = std::move(voteCooldownTime);
modified_settings_.emplace(VmSetting::Setting::VOTE_COOLDOWN_TIME);
}
bool getDisallowGuestVotes() const {
return disallowGuestVotes_;
}
void setDisallowGuestVotes(bool&& disallow_guest_votes) {
disallowGuestVotes_ = std::move(disallow_guest_votes);
modified_settings_.emplace(VmSetting::Setting::DISALLOW_GUEST_VOTES);
}
auto getProtocol() const {
return protocol_;
}
void setProtocol(std::uint16_t protocol) {
protocol_ = protocol;
modified_settings_.emplace(VmSetting::Setting::PROTOCOL);
}
std::string getAgentAddress() const {
return address_;
}
void setAgentAddress(std::string&& address) {
address_ = std::move(address);
modified_settings_.emplace(VmSetting::Setting::AGENT_ADDRESS);
}
std::vector<GuacamoleParameterWrapper> getGuacamoleParameters() const { return guacamole_parameters_; }
void setGuacamoleParameters(std::vector<GuacamoleParameterWrapper>&& guacParams) {
modified_settings_.emplace(VmSetting::Setting::GUACAMOLE_PARAMETERS);
guacamole_parameters_ = guacParams;
}
bool getSafeForWork() const {
return safe_for_work_;
}
void setSafeForWork(bool safe_for_work) {
safe_for_work_ = safe_for_work;
modified_settings_.emplace(VmSetting::Setting::SAFE_FOR_WORK);
}
bool getDisallowGuests() const {
return disallow_guests_;
}
void setDisallowGuests(bool disallow_guests) {
disallow_guests_ = disallow_guests;
modified_settings_.emplace(VmSetting::Setting::DISALLOW_GUESTS);
}
bool getRecordingsEnabled() const {
return recordings_enabled_;
}
void setRecordingsEnabled(bool recordings_enabled) {
recordings_enabled_ = recordings_enabled;
modified_settings_.emplace(VmSetting::Setting::RECORDINGS_ENABLED);
}
bool getRunStartCommandAfterDisconnect() const {
return run_start_command_after_disconnect_;
}
void setRunStartCommandAfterDisconnect(bool run_start_command_after_disconnect) {
run_start_command_after_disconnect_ = run_start_command_after_disconnect;
modified_settings_.emplace(VmSetting::Setting::RUN_START_COMMAND_AFTER_DISCONNECT);
}
private:
bool auto_start_;
std::string name_;
std::string description_;
std::string host_;
std::string operatingSystem_;
std::uint8_t ram_;
std::uint8_t disk_;
std::string startCommand_;
std::string stopCommand_;
std::string restartCommand_;
bool turnsEnabled_;
std::uint16_t turnTime_;
bool uploadsEnabled_;
std::uint16_t uploadCooldownTime_;
std::uint32_t maxUploadSize_;
bool votesEnabled_;
std::uint16_t voteTime_;
std::uint16_t voteCooldownTime_;
bool disallowGuestVotes_;
std::uint16_t protocol_;
std::string address_;
std::uint16_t socket_type_;
std::set<int> modified_settings_;
std::vector<GuacamoleParameterWrapper> guacamole_parameters_;
bool safe_for_work_;
bool disallow_guests_;
bool recordings_enabled_;
bool run_start_command_after_disconnect_;
};
struct Deserializer {
template<typename TWrapper, typename TList, typename TTransformer=std::nullptr_t>
static auto to_vector(TList list, TTransformer transform={}) {
auto vector = std::vector<TWrapper>();
vector.reserve(list.size());
if constexpr (std::is_same_v<TTransformer, std::nullptr_t>) {
for (auto vmInfo : list) {
vector.emplace_back(vmInfo);
}
} else {
std::transform(list.begin(), list.end(), std::back_inserter(vector), transform);
}
return vector;
}
static std::vector<std::uint8_t> convertIpAddressToBytes(IpAddress::Reader ip_address) {
auto bytes = std::vector<std::uint8_t>();
bytes.reserve(16);
for (auto i = 0u; i < 8; i++) {
bytes.push_back(ip_address.getFirst() >> ((7 - i) * 8));
}
for (auto i = 0u; i < 8; i++) {
bytes.push_back(ip_address.getSecond() >> ((7 - i) * 8));
}
return bytes;
}
void deserialize(const std::string& message_bytes) {
auto word_array = kj::ArrayPtr<const capnp::word>(reinterpret_cast<const capnp::word*>(message_bytes.c_str()), message_bytes.size()/sizeof(capnp::word));
auto message_count = 0u;
while (word_array.size()) {
auto reader = capnp::FlatArrayMessageReader(word_array);
const auto message = reader.getRoot<CollabVmServerMessage>().getMessage();
switch (message.which())
{
case CollabVmServerMessage::Message::VM_LIST_RESPONSE:
{
const auto vmInfoList = message.getVmListResponse();
onVmInfo(to_vector<VmInfoWrapper>(vmInfoList));
break;
}
case CollabVmServerMessage::Message::VM_TURN_INFO:
{
const auto vm_turn_info = message.getVmTurnInfo();
onVmTurnInfo(to_vector<std::string>(vm_turn_info.getUsers()), vm_turn_info.getTimeRemaining(), vm_turn_info.getState() == CollabVmServerMessage::TurnState::PAUSED);
break;
}
case CollabVmServerMessage::Message::VM_THUMBNAIL:
{
const auto thumbnail = message.getVmThumbnail();
const auto png_bytes = thumbnail.getPngBytes();
onVmThumbnail(
thumbnail.getId(),
emscripten::val(emscripten::typed_memory_view(
png_bytes.size(),
png_bytes.begin())
));
break;
}
case CollabVmServerMessage::Message::ACCOUNT_REGISTRATION_RESPONSE:
{
const auto resp = message.getAccountRegistrationResponse().getResult();
if (resp.hasSession()) {
const auto session = resp.getSession();
const auto session_id = session.getSessionId();
const auto username = session.getUsername();
//onRegisterAccountSucceeded(emscripten::val(emscripten::typed_memory_view(session_id.size(), session_id.begin())));
onRegisterAccountSucceeded(std::string(reinterpret_cast<const char*>(session_id.begin()), session_id.size()), username);
} else if (resp.isErrorStatus()) {
std::string error_message;
switch (resp.getErrorStatus()) {
case CollabVmServerMessage::RegisterAccountResponse::RegisterAccountError::USERNAME_TAKEN:
error_message = "That username is taken";
break;
case CollabVmServerMessage::RegisterAccountResponse::RegisterAccountError::USERNAME_INVALID:
error_message = "There was a problem with your username";
break;
case CollabVmServerMessage::RegisterAccountResponse::RegisterAccountError::PASSWORD_INVALID:
error_message = "There was a problem with your password";
break;
case CollabVmServerMessage::RegisterAccountResponse::RegisterAccountError::TOTP_ERROR:
error_message = "There was a problem with 2FA";
break;
}
onRegisterAccountFailed(error_message);
}
break;
}
case CollabVmServerMessage::Message::LOGIN_RESPONSE:
{
const auto response = message.getLoginResponse().getResult();
if (response.hasSession()) {
const auto session = response.getSession();
const auto session_id = session.getSessionId();
const auto username = session.getUsername();
const auto is_admin = session.getIsAdmin();
onLoginSucceeded(std::string(reinterpret_cast<const char*>(session_id.begin()), session_id.size()), username, is_admin);
} else switch (response.getResult()) {
case CollabVmServerMessage::LoginResponse::LoginResult::INVALID_PASSWORD:
onLoginFailed("invalid password");
break;
case CollabVmServerMessage::LoginResponse::LoginResult::INVALID_USERNAME:
onLoginFailed("invalid username");
break;
case CollabVmServerMessage::LoginResponse::LoginResult::INVALID_CAPTCHA_TOKEN:
onLoginFailed("captcha verification failed");
break;
}
break;
}
case CollabVmServerMessage::Message::SERVER_SETTINGS:
onServerConfig(ServerSettingsWrapper(message.getServerSettings()));
break;
case CollabVmServerMessage::Message::READ_VMS_RESPONSE:
{
onAdminVms(to_vector<AdminVmInfoWrapper>(message.getReadVmsResponse()));
break;
}
case CollabVmServerMessage::Message::READ_VM_CONFIG_RESPONSE:
onVmConfig(VmSettingsWrapper(message.getReadVmConfigResponse()));
break;
case CollabVmServerMessage::Message::GUAC_INSTR:
{
callGuacInstrHandler(message.getGuacInstr());
break;
}
case CollabVmServerMessage::Message::CREATE_VM_RESPONSE:
onVmCreated(message.getCreateVmResponse());
break;
case CollabVmServerMessage::Message::CONNECT_RESPONSE:
{
const auto result = message.getConnectResponse().getResult();
if (result.which() == CollabVmServerMessage::ChannelConnectResponse::Result::SUCCESS) {
auto success = result.getSuccess();
onConnect(success.getUsername(), success.getCaptchaRequired());
for (auto chat_message :
success.getChatMessages()) {
onChatMessage(0, chat_message.getSender(), static_cast<std::uint8_t>(chat_message.getUserType()), chat_message.getMessage(), chat_message.getTimestamp());
}
} else if (result.which() == CollabVmServerMessage::ChannelConnectResponse::Result::FAIL) {
onConnectFail();
}
break;
}
case CollabVmServerMessage::Message::CHAT_MESSAGE:
{
auto chat_message = message.getChatMessage();
onChatMessage(chat_message.getChannel(),
chat_message.getMessage().getSender(),
static_cast<std::uint8_t>(chat_message.getMessage().getUserType()),
chat_message.getMessage().getMessage(),
chat_message.getMessage().getTimestamp());
break;
}
case CollabVmServerMessage::Message::CHAT_MESSAGES:
{
auto chat_messages = message.getChatMessages();
const auto channel_id = chat_messages.getChannel();
const auto first_message_index = chat_messages.getFirstMessage();
if (first_message_index == 0) {
for (auto chat_message : chat_messages.getMessages()) {
if (!chat_message.getMessage().size()) {
break;
}
onChatMessage(channel_id, chat_message.getSender(), static_cast<std::uint8_t>(chat_message.getUserType()), chat_message.getMessage(), chat_message.getTimestamp());
}
} else {
auto i = first_message_index;
do {
onChatMessage(channel_id,
chat_messages.getMessages()[i].getSender(),
static_cast<std::uint8_t>(chat_messages.getMessages()[i].getUserType()),
chat_messages.getMessages()[i].getMessage(),
chat_messages.getMessages()[i].getTimestamp());
i = (i + 1) % chat_messages.getMessages().size();
} while (i != first_message_index);
}
break;
}
case CollabVmServerMessage::Message::USER_LIST_ADD:
{
const auto added_user = message.getUserListAdd();
onUserListAdd(added_user.getChannel(), added_user.getUsername(), static_cast<std::uint8_t>(added_user.getUserType()));
break;
}
case CollabVmServerMessage::Message::USER_LIST_REMOVE:
{
const auto removed_user = message.getUserListRemove();
onUserListRemove(removed_user.getChannel(), removed_user.getUsername());