-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.php
1156 lines (1078 loc) · 47.3 KB
/
module.php
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
<?php
declare(strict_types=1);
// TODO KeepAlive defekt
require_once __DIR__ . '/../libs/NetworkTraits.php';
require_once __DIR__ . '/../libs/WebsocketClass.php'; // diverse Klassen
/*
* @addtogroup network
* @{
*
* @package Network
* @file module.php
* @author Michael Tröger <micha@nall-chan.net>get
* @copyright 2018 Michael Tröger
* @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC BY-NC-SA 4.0
* @version 1.4
*/
/**
* WebsocketServer Klasse implementiert das Websocket-Protokoll für einen ServerSocket.
* Erweitert IPSModule.
*
* @author Michael Tröger <micha@nall-chan.net>
* @copyright 2018 Michael Tröger
* @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC BY-NC-SA 4.0
*
* @version 1.4
*
* @example <b>Ohne</b>
*
* @property WebSocket_ClientList $Multi_Clients
* @property bool $UseTLS
* @property bool $UsePlain
* @property string {$ClientIP.$ClientPort}
* @property string {"Buffer".$ClientIP.$ClientPort} Buffer für Nutzdaten
* @property string {"BufferTLS".$ClientIP.$ClientPort} Buffer für TLS-Nutzdaten
* @property TLS {"Multi_TLS_".$ClientIP.$ClientPort} TLS-Object
* @property array {"BufferListe_Multi_TLS_".$ClientIP.$ClientPort}
* @property string $CertData
* @property string $KeyData
* @property string $KeyPassword
* @property int $ParentID
* @property int $PingInterval
* @property bool $NoNewClients
*/
class WebsocketServer extends IPSModule
{
use DebugHelper;
use InstanceStatus;
use BufferHelper;
use Semaphore;
/**
* Interne Funktion des SDK.
*/
public function Create()
{
parent::Create();
$this->RequireParent('{8062CF2B-600E-41D6-AD4B-1BA66C32D6ED}');
$this->Multi_Clients = new WebSocket_ClientList();
$this->NoNewClients = true;
$this->RegisterPropertyBoolean('Open', false);
$this->RegisterPropertyInteger('Port', 8080);
$this->RegisterPropertyInteger('Interval', 0);
$this->RegisterPropertyString('URI', '/');
$this->RegisterPropertyBoolean('BasisAuth', false);
$this->RegisterPropertyString('Username', '');
$this->RegisterPropertyString('Password', '');
$this->RegisterPropertyBoolean('TLS', false);
$this->RegisterPropertyBoolean('Plain', true);
$this->RegisterPropertyString('CertFile', '');
$this->RegisterPropertyString('KeyFile', '');
$this->RegisterPropertyString('KeyPassword', '');
$this->RegisterTimer('KeepAlivePing', 0, 'WSS_KeepAlive($_IPS[\'TARGET\']);');
}
/**
* Interne Funktion des SDK.
*/
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
switch ($Message) {
case IPS_KERNELMESSAGE:
if ($Data[0] != KR_READY) {
break;
}
// No break. Add additional comment above this line if intentional
case IPS_KERNELSTARTED:
$this->ApplyChanges();
break;
case IPS_KERNELSHUTDOWN:
$this->DisconnectAllClients();
break;
case FM_DISCONNECT:
$this->NoNewClients = true;
$this->RemoveAllClients();
$this->RegisterParent();
break;
case FM_CONNECT:
$this->ApplyChanges();
break;
case IM_CHANGESTATUS:
if ($SenderID == $this->ParentID) {
if ($Data[0] == IS_ACTIVE) {
$this->NoNewClients = false;
} else {
$this->NoNewClients = true;
$this->RemoveAllClients();
}
}
break;
}
}
/**
* Interne Funktion des SDK.
*/
public function GetConfigurationForm()
{
$data = json_decode(file_get_contents(__DIR__ . '/form.json'));
if ((float) IPS_GetKernelVersion() < 4.2) {
$data->elements[8]->type = 'ValidationTextBox';
$data->elements[8]->caption = 'Path to certificate';
unset($data->elements[8]->extensions);
$data->elements[9]->type = 'ValidationTextBox';
$data->elements[9]->caption = 'Path to private key';
unset($data->elements[9]->extensions);
}
return json_encode($data);
}
/**
* Interne Funktion des SDK.
*/
public function GetConfigurationForParent()
{
$Config['Port'] = $this->ReadPropertyInteger('Port');
$Config['Open'] = $this->ReadPropertyBoolean('Open');
return json_encode($Config);
}
/**
* Interne Funktion des SDK.
*/
public function ApplyChanges()
{
$this->NoNewClients = true;
if ((float) IPS_GetKernelVersion() < 4.2) {
$this->RegisterMessage(0, IPS_KERNELMESSAGE);
} else {
$this->RegisterMessage(0, IPS_KERNELSTARTED);
$this->RegisterMessage(0, IPS_KERNELSHUTDOWN);
}
$this->RegisterMessage($this->InstanceID, FM_CONNECT);
$this->RegisterMessage($this->InstanceID, FM_DISCONNECT);
if (IPS_GetKernelRunlevel() != KR_READY) {
return;
}
$this->SetTimerInterval('KeepAlivePing', 0);
$OldParentID = $this->ParentID;
if ($this->HasActiveParent() && ($OldParentID > 0)) {
$this->DisconnectAllClients();
}
parent::ApplyChanges();
$NewState = IS_ACTIVE;
$this->UseTLS = $this->ReadPropertyBoolean('TLS');
$this->UsePlain = $this->ReadPropertyBoolean('Plain');
//$this->SendDebug('UsePlain', ($this->UsePlain ? "true" : "false"), 0);
//$this->SendDebug('UseTLS', ($this->UseTLS ? "true" : "false"), 0);
if ($this->UseTLS) {
$basedir = IPS_GetKernelDir() . 'cert';
if (!file_exists($basedir)) {
mkdir($basedir);
}
if (($this->ReadPropertyString('CertFile') == '') && ($this->ReadPropertyString('KeyFile') == '')) {
return $this->CreateNewCert($basedir);
}
try {
if ((float) IPS_GetKernelVersion() < 4.2) {
$CertFile = @file_get_contents($this->ReadPropertyString('CertFile'));
$KeyFile = @file_get_contents($this->ReadPropertyString('KeyFile'));
} else {
//Convert old settings
$CertFile = $this->ReadPropertyString('CertFile');
$KeyFile = $this->ReadPropertyString('KeyFile');
if (is_file($CertFile)) {
IPS_SetProperty($this->InstanceID, 'CertFile', @file_get_contents($this->ReadPropertyString('CertFile')));
}
if (is_file($KeyFile)) {
IPS_SetProperty($this->InstanceID, 'KeyFile', @file_get_contents($this->ReadPropertyString('KeyFile')));
}
if (IPS_HasChanges($this->InstanceID)) {
IPS_ApplyChanges($this->InstanceID);
return;
}
// Read new settings
$CertFile = base64_decode($CertFile);
$KeyFile = base64_decode($KeyFile);
}
if ($CertFile) {
$this->CertData = 'data://text/plain;base64,' . base64_encode($CertFile);
} else {
throw new Exception('Certificate missing or not found');
}
if ($KeyFile) {
$this->KeyData = 'data://text/plain;base64,' . base64_encode($KeyFile);
} else {
throw new Exception('Private key missing or not found');
}
// if (strlen($this->ReadPropertyString("KeyPassword")) == 0)
// throw new Exception('Password for private key missing');
$this->KeyPassword = $this->ReadPropertyString('KeyPassword');
} catch (Exception $exc) {
echo $this->Translate($exc->getMessage());
$this->UseTLS = false;
$NewState = IS_EBASE + 1;
}
}
$Open = $this->ReadPropertyBoolean('Open');
$Port = $this->ReadPropertyInteger('Port');
$this->PingInterval = $this->ReadPropertyInteger('Interval');
if (!$Open) {
$NewState = IS_INACTIVE;
} else {
if (($Port < 1) || ($Port > 65535)) {
$NewState = IS_EBASE + 2;
$Open = false;
trigger_error($this->Translate('Port invalid'), E_USER_NOTICE);
} else {
if (($this->PingInterval != 0) && ($this->PingInterval < 5)) {
$this->PingInterval = 0;
$NewState = IS_EBASE + 4;
$Open = false;
trigger_error($this->Translate('Ping interval to small'), E_USER_NOTICE);
}
}
}
$ParentID = $this->RegisterParent();
// Zwangskonfiguration des ServerSocket
if ($ParentID > 0) {
if (IPS_GetProperty($ParentID, 'Port') != $Port) {
IPS_SetProperty($ParentID, 'Port', $Port);
}
if (IPS_GetProperty($ParentID, 'Open') != $Open) {
IPS_SetProperty($ParentID, 'Open', $Open);
}
if (IPS_HasChanges($ParentID)) {
@IPS_ApplyChanges($ParentID);
}
} else {
if ($Open) {
$NewState = IS_INACTIVE;
$Open = false;
}
}
if ($Open && !$this->HasActiveParent($ParentID)) {
$NewState = IS_EBASE + 2;
}
$this->SetStatus($NewState);
$this->NoNewClients = false;
}
//################# DATAPOINTS CHILDS
/**
* Interne Funktion des SDK. Nimmt Daten von Childs entgegen und sendet Diese weiter.
*
* @param string $JSONString
* @result bool true wenn Daten gesendet werden konnten, sonst false.
*/
public function ForwardData($JSONString)
{
$Data = json_decode($JSONString);
if ($Data->DataID == '{79827379-F36E-4ADA-8A95-5F8D1DC92FA9}') {
$this->SendDebug('Forward Broadcast', utf8_decode($Data->Buffer), 0);
$Clients = $this->Multi_Clients;
foreach ($Clients as $Client) {
$Data->FrameTyp = $this->{'OpCode' . $Client->ClientIP . $Client->ClientPort};
$Data->Fin = true;
$this->Send(utf8_decode($Data->Buffer), $Data->FrameTyp, $Client, $Data->Fin);
}
return true;
}
$Client = $this->Multi_Clients->GetByIpPort(new Websocket_Client($Data->ClientIP, $Data->ClientPort));
if ($Client === false) {
trigger_error($this->Translate('Unknow client') . ': ' . $Data->ClientIP . ':' . $Data->ClientPort, E_USER_NOTICE);
return false;
}
if ($Client->State != WebSocketState::Connected) {
trigger_error($this->Translate('Client not connected') . ': ' . $Data->ClientIP . ':' . $Data->ClientPort, E_USER_NOTICE);
return false;
}
$this->SendDebug('Forward', utf8_decode($Data->Buffer), 0);
if ($Data->DataID == '{714B71FB-3D11-41D1-AFAC-E06F1E983E09}') {
if ($Data->FrameTyp == WebSocketOPCode::close) {
return $this->SendDisconnect($Client);
}
if ($Data->FrameTyp == WebSocketOPCode::ping) {
return $this->SendPing($Client->ClientIP, $Client->ClientPort, utf8_decode($Data->Buffer));
}
if (($Data->FrameTyp < 0) || ($Data->FrameTyp > 2)) {
trigger_error($this->Translate('FrameTyp invalid') . ': ' . $Data->ClientIP . ':' . $Data->ClientPort, E_USER_NOTICE);
return false;
}
} else { //{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}
if (property_exists($Data, 'Type')) {
if ($Data->Type == 2) {
return $this->SendDisconnect($Client);
}
}
//Type fehlt
$Data->FrameTyp = $this->{'OpCode' . $Client->ClientIP . $Client->ClientPort};
$Data->Fin = true;
}
$this->Send(utf8_decode($Data->Buffer), $Data->FrameTyp, $Client, $Data->Fin);
return true;
}
//################# DATAPOINTS PARENT
/**
* Empfängt Daten vom Parent.
*
* @param string $JSONString Das empfangene JSON-kodierte Objekt vom Parent.
*/
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString);
unset($data->DataID);
//$this->SendDebug('incoming', $data, 0);
$Payload = utf8_decode($data->Buffer);
$Clients = $this->Multi_Clients;
$IncomingClient = new Websocket_Client($data->ClientIP, $data->ClientPort, WebSocketState::init);
$Client = $Clients->GetByIpPort($IncomingClient);
$this->SendDebug(($Client ? 'OLD' : 'NEW') . ' CLIENT', SocketType::ToString($data->Type), 0);
switch ($data->Type) {
case 0: /* Data */
if ($Client === false) {
$this->SendDebug('no Connection for Data found', $IncomingClient->ClientIP . ':' . $IncomingClient->ClientPort, 0);
$this->CloseConnection($IncomingClient);
} else {
$this->ProcessIncomingData($Client, $Payload);
$Clients->Update($Client);
}
break;
case 1: /* Connected */
if (!$this->NoNewClients) {
$this->SendDebug('new Connection', $IncomingClient->ClientIP . ':' . $IncomingClient->ClientPort, 0);
$this->ClearClientBuffer($IncomingClient);
$Clients->Update($IncomingClient);
}
break;
case 2: /* Disconnected */
if ($Client === false) {
$this->SendDebug('no Connection to disconnect found', $IncomingClient->ClientIP . ':' . $IncomingClient->ClientPort, 0);
} else {
$Clients->Remove($Client);
$this->ClearClientBuffer($Client);
}
break;
}
$this->Multi_Clients = $Clients;
$this->SetNextTimer();
}
//################# PUBLIC
/**
* Wird vom Timer aufgerufen.
* Sendet einen Ping an den Client welcher als nächstes das Timeout erreicht.
*/
public function KeepAlive()
{
$this->SendDebug('KeepAlive', 'start', 0);
$this->SetTimerInterval('KeepAlivePing', 0);
$Client = true;
while ($Client) {
$Clients = $this->Multi_Clients;
$Client = $Clients->GetNextTimeout(1);
if ($Client === false) {
break;
}
if (@$this->SendPing($Client->ClientIP, $Client->ClientPort, '') === false) {
$this->SendDebug('TIMEOUT ' . $Client->ClientIP . ':' . $Client->ClientPort, 'Ping timeout', 0);
}
}
$this->SendDebug('KeepAlive', 'end', 0);
}
/**
* Versendet einen Ping an einen Client.
*
* @param string $ClientIP Die IP-Adresse des Client.
* @param int $ClientPort Der Port des Client.
* @param string $Text Der Payload des Ping.
*
* @return bool True bei Erfolg, im Fehlerfall wird eine Warnung und false ausgegeben.
*/
public function SendPing(string $ClientIP, int $ClientPort, string $Text)
{
$Client = $this->Multi_Clients->GetByIpPort(new Websocket_Client($ClientIP, $ClientPort));
if ($Client === false) {
$this->SendDebug('Unknow client', $ClientIP . ':' . $ClientPort, 0);
trigger_error($this->Translate('Unknow client') . ': ' . $ClientIP . ':' . $ClientPort, E_USER_NOTICE);
return false;
}
if ($Client->State != WebSocketState::Connected) {
$this->SendDebug('Client not connected', $ClientIP . ':' . $ClientPort, 0);
trigger_error($this->Translate('Client not connected') . ': ' . $ClientIP . ':' . $ClientPort, E_USER_NOTICE);
return false;
}
$this->SendDebug('Send Ping' . $Client->ClientIP . ':' . $Client->ClientPort, $Text, 0);
$this->Send($Text, WebSocketOPCode::ping, $Client);
$Result = $this->WaitForPong($Client);
$this->{'Pong' . $Client->ClientIP . $Client->ClientPort} = '';
if ($Result === false) {
$this->SendDebug('Timeout ' . $Client->ClientIP . ':' . $Client->ClientPort, '', 0);
trigger_error($this->Translate('Timeout'), E_USER_NOTICE);
$this->RemoveOneClient($Client);
$this->CloseConnection($Client);
return false;
}
if ($Result !== $Text) {
$this->SendDebug('Error in Pong ' . $Client->ClientIP . ':' . $Client->ClientPort, $Result, 0);
trigger_error($this->Translate('Wrong pong received'), E_USER_NOTICE);
$this->RemoveOneClient($Client);
$this->SendDisconnect($Client);
return false;
}
return true;
}
//################# PRIVATE
/**
* Erzeugt ein selbst-signiertes Zertifikat.
*
* @param string $basedir Der Speicherort der Zertifikate.
*
* @return bool True bei Erflog, sonst false
*/
private function CreateNewCert(string $basedir)
{
$CN = 'IPSymcon';
$EMAIL = IPS_GetLicensee();
$basedir .= DIRECTORY_SEPARATOR . $this->InstanceID;
$configfile = $basedir . '.cnf';
$certfile = $basedir . '.cer';
$keyfile = $basedir . '.key';
$newLine = "\r\n";
$strCONFIG = 'default_md = sha256' . $newLine;
$strCONFIG .= 'default_days = 3650' . $newLine;
$strCONFIG .= $newLine;
$strCONFIG .= 'x509_extensions = x509v3' . $newLine;
$strCONFIG .= '[ req ]' . $newLine;
$strCONFIG .= 'default_bits = 2048' . $newLine;
$strCONFIG .= 'distinguished_name = req_DN' . $newLine;
$strCONFIG .= 'string_mask = nombstr' . $newLine;
$strCONFIG .= 'prompt = no' . $newLine;
$strCONFIG .= 'req_extensions = v3_req' . $newLine;
$strCONFIG .= $newLine;
$strCONFIG .= '[ req_DN ]' . $newLine;
$strCONFIG .= 'countryName = DE' . $newLine;
$strCONFIG .= 'stateOrProvinceName = none' . $newLine;
$strCONFIG .= 'localityName = none' . $newLine;
$strCONFIG .= '0.organizationName = "Home"' . $newLine;
$strCONFIG .= 'organizationalUnitName = "IPS"' . $newLine;
$strCONFIG .= 'commonName = ' . $CN . $newLine;
$strCONFIG .= 'emailAddress = ' . $EMAIL . $newLine;
$strCONFIG .= $newLine;
$strCONFIG .= '[ v3_req ]' . $newLine;
$strCONFIG .= 'basicConstraints=CA:FALSE' . $newLine;
$strCONFIG .= 'subjectKeyIdentifier=hash' . $newLine;
$strCONFIG .= $newLine;
$strCONFIG .= '[ x509v3 ]' . $newLine;
$strCONFIG .= 'basicConstraints=CA:FALSE' . $newLine;
$strCONFIG .= 'nsCertType = server' . $newLine;
$strCONFIG .= 'keyUsage = digitalSignature,nonRepudiation,keyEncipherment' . $newLine;
$strCONFIG .= 'extendedKeyUsage = msSGC,nsSGC,serverAuth' . $newLine;
$strCONFIG .= 'subjectKeyIdentifier=hash' . $newLine;
$strCONFIG .= 'authorityKeyIdentifier=keyid,issuer:allways' . $newLine;
$strCONFIG .= 'issuerAltName = issuer:copy' . $newLine;
$strCONFIG .= 'subjectAltName = IP:192.168.201.34' . $newLine;
$strCONFIG .= $newLine;
// $strCONFIG .= '[alt_names]' . $newLine;
// $strCONFIG .= 'email = '.$EMAIL . $newLine;
// $strCONFIG .= 'IP = 192.168.201.34' . $newLine;
// $strCONFIG .= $newLine;
$fp = fopen($configfile, 'w');
fwrite($fp, $strCONFIG);
fclose($fp);
$dn = [
'countryName' => 'DE',
'stateOrProvinceName' => 'none',
'localityName' => 'none',
'organizationName' => 'Home',
'organizationalUnitName' => 'IPS',
'commonName' => "$CN",
'emailAddress' => "$EMAIL"
];
$config = [
'config' => "$configfile",
'encrypt_key' => true];
$configKey = [
'config' => "$configfile",
'encrypt_key' => true,
'digest_alg' => 'sha512',
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
];
$pk = openssl_pkey_new($configKey);
openssl_pkey_export($pk, $pkexport, 'Symcon', $config);
if ((float) IPS_GetKernelVersion() < 4.2) {
$fp = fopen($keyfile, 'w');
fwrite($fp, $pkexport);
fclose($fp);
IPS_SetProperty($this->InstanceID, 'KeyFile', $basedir . '.key');
} else {
IPS_SetProperty($this->InstanceID, 'KeyFile', base64_encode($pkexport));
}
$csr = openssl_csr_new($dn, $pk, $config);
if ($csr) {
$cert = openssl_csr_sign($csr, null, $pk, 730, $config);
if ($cert) {
openssl_x509_export($cert, $certout);
if ((float) IPS_GetKernelVersion() < 4.2) {
$fp = fopen($certfile, 'w');
fwrite($fp, $certout);
fclose($fp);
IPS_SetProperty($this->InstanceID, 'CertFile', $basedir . '.cer');
} else {
IPS_SetProperty($this->InstanceID, 'CertFile', base64_encode($certout));
}
} else {
unlink($configfile);
return false;
}
} else {
unlink($configfile);
return false;
}
unlink($configfile);
IPS_SetProperty($this->InstanceID, 'KeyPassword', 'Symcon');
IPS_ApplyChanges($this->InstanceID);
return true;
}
/**
* Wertet den Handshake des Clients aus.
*
* @param string $Data Die Daten des Clients.
*
* @return bool|HTTP_ERROR_CODES True bei Erfolg, HTTP_ERROR_CODES bei Fehler, false wenn nicht genug Daten.
*/
private function ReceiveHandshake(string $Data)
{
$this->SendDebug('Receive Handshake', $Data, 0);
if (preg_match("/^GET ?([^?#]*) HTTP\/1.1\r\n/", $Data, $match)) {
if (substr($Data, -4) != "\r\n\r\n") {
$this->SendDebug('WAIT', $Data, 0);
return false;
}
if (trim($match[1]) != trim($this->ReadPropertyString('URI'))) {
$this->SendDebug('Wrong URI requested', $Data, 0);
return HTTP_ERROR_CODES::Not_Found;
}
if ($this->ReadPropertyBoolean('BasisAuth')) {
$realm = base64_encode($this->ReadPropertyString('Username') . ':' . $this->ReadPropertyString('Password'));
if (preg_match("/Authorization: Basic (.*)\r\n/", $Data, $match)) {
if ($match[1] != $realm) {
$this->SendDebug('Unauthorized Connection:', base64_decode($match[1]), 0);
return HTTP_ERROR_CODES::Forbidden;
}
} else {
$this->SendDebug('Authorization missing', '', 0);
return HTTP_ERROR_CODES::Unauthorized;
}
}
if (preg_match("/Connection: (.*)\r\n/", $Data, $match)) {
if (strtolower($match[1]) != 'upgrade') {
$this->SendDebug('WRONG Connection:', $match[1], 0);
return HTTP_ERROR_CODES::Method_Not_Allowed;
}
} else {
$this->SendDebug('MISSING', 'Connection: Upgrade', 0);
return HTTP_ERROR_CODES::Bad_Request;
}
if (preg_match("/Upgrade: (.*)\r\n/", $Data, $match)) {
if (strtolower($match[1]) != 'websocket') {
$this->SendDebug('WRONG Upgrade:', $match[1], 0);
return HTTP_ERROR_CODES::Method_Not_Allowed;
}
} else {
$this->SendDebug('MISSING', 'Upgrade: websocket', 0);
return HTTP_ERROR_CODES::Bad_Request;
}
if (preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $Data, $match)) {
if (strpos($match[1], '13') === false) {
$this->SendDebug('WRONG Version:', $match[1], 0);
return HTTP_ERROR_CODES::Not_Acceptable;
}
} else {
$this->SendDebug('MISSING', 'Sec-WebSocket-Version', 0);
return HTTP_ERROR_CODES::Bad_Request;
}
if (!preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $Data, $match)) {
$this->SendDebug('MISSING', 'Sec-WebSocket-Key', 0);
return HTTP_ERROR_CODES::Bad_Request;
}
return true;
}
$this->SendDebug('Invalid HTTP-Request', $Data, 0);
return HTTP_ERROR_CODES::Bad_Request;
}
/**
* Sendet den HTTP-Response an den Client.
*
* @param HTTP_ERROR_CODES $Code Der HTTP Code welcher versendet werden soll.
* @param string $Data Die empfangenen Daten des Clients.
* @param Websocket_Client $Client Der Client vom welchen die Daten empfangen wurden.
*/
private function SendHandshake(int $Code, string $Data, Websocket_Client $Client)
{
preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $Data, $match);
$SendKey = base64_encode(sha1($match[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
$Header[] = 'HTTP/1.1 ' . HTTP_ERROR_CODES::ToString($Code);
if ($Code == HTTP_ERROR_CODES::Unauthorized) {
$Header[] = 'WWW-Authenticate: Basic';
}
//$Header[] = 'Date: '; // Datum fehlt !
$Header[] = 'Server: IP-Symcon Websocket Gateway';
if ($Code == HTTP_ERROR_CODES::Web_Socket_Protocol_Handshake) {
$Header[] = 'Connection: Upgrade';
$Header[] = 'Sec-WebSocket-Accept: ' . $SendKey;
$Header[] = 'Upgrade: websocket';
$Header[] = "\r\n";
$SendHeader = implode("\r\n", $Header);
} else {
$Header[] = 'Content-Length:' . strlen(HTTP_ERROR_CODES::ToString($Code));
$Header[] = "\r\n";
$SendHeader = implode("\r\n", $Header) . HTTP_ERROR_CODES::ToString($Code);
}
$this->SendDebug('SendHandshake ' . $Client->ClientIP . ':' . $Client->ClientPort, $SendHeader, 0);
$SendData = $this->MakeJSON($Client, $SendHeader);
if ($SendData) {
$this->SendDataToParent($SendData);
}
}
/**
* Erzeugt aus einen Datenframe ein JSON für den Datenaustausch mit dem IO.
*
* @param Websocket_Client $Client Der Client an welchen die Daten gesendet werden.
* @param string $Data Die Nutzdaten
* @param type $UseTLS Bei false wird TLS nicht benutzt, auch wenn der Client dies erwartet.
*
* @return bool|string Der JSON-String zum versand an den IO, im Fehlerfall false.
*/
private function MakeJSON(Websocket_Client $Client, string $Data, $UseTLS = true, int $Type = SocketType::Data)
{
if ($Type == SocketType::Data) {
if ($UseTLS && $Client->UseTLS) {
$this->SendDebug('Send TLS', $Data, 0);
try {
$this->lock($Client->ClientIP . $Client->ClientPort);
$TLS = $this->{'Multi_TLS_' . $Client->ClientIP . $Client->ClientPort};
$Send = $TLS->output($Data)->decode();
$this->{'Multi_TLS_' . $Client->ClientIP . $Client->ClientPort} = $TLS;
$this->unlock($Client->ClientIP . $Client->ClientPort);
} catch (Exception $exc) {
return false;
}
$Data = $Send;
}
$this->SendDebug('Send', $Data, 0);
} else {
$this->SendDebug('Send', SocketType::ToString($Type), 0);
}
$SendData['DataID'] = '{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}';
$SendData['Buffer'] = utf8_encode($Data);
$SendData['ClientIP'] = $Client->ClientIP;
$SendData['ClientPort'] = $Client->ClientPort;
$SendData['Type'] = $Type;
return json_encode($SendData);
}
/**
* Dekodiert die empfangenen Daten und sendet sie an die Childs.
*
* @param WebSocketFrame $Frame Ein Objekt welches einen kompletten Frame enthält.
* @param Websocket_Client $Client Der Client von welchem die Daten empfangen wurden.
*/
private function DecodeFrame(WebSocketFrame $Frame, Websocket_Client $Client)
{
$this->SendDebug('DECODE', $Frame, ($Frame->OpCode == WebSocketOPCode::continuation) ? $this->PayloadTyp - 1 : $Frame->OpCode - 1);
switch ($Frame->OpCode) {
case WebSocketOPCode::ping:
$this->SendPong($Client, $Frame->Payload);
return;
case WebSocketOPCode::close:
$this->SendDebug('Receive', 'Client send stream close !', 0);
$Client->State = WebSocketState::CloseReceived;
$this->SendDisconnect($Client);
$this->SendDataToChilds('', $Client); // RAW Childs
return;
case WebSocketOPCode::text:
case WebSocketOPCode::binary:
$this->{'OpCode' . $Client->ClientIP . $Client->ClientPort} = $Frame->OpCode;
$Data = $Frame->Payload;
break;
case WebSocketOPCode::continuation:
$Data = $this->{'Buffer' . $Client->ClientIP . $Client->ClientPort} . $Frame->Payload;
break;
case WebSocketOPCode::pong:
$this->{'Pong' . $Client->ClientIP . $Client->ClientPort} = $Frame->Payload;
$this->{'WaitForPong' . $Client->ClientIP . $Client->ClientPort} = true;
$JSON['DataID'] = '{8F1F6C32-B1AD-4B7F-8DFB-1244A96FCACF}';
$JSON['Buffer'] = utf8_encode($Frame->Payload);
$JSON['ClientIP'] = $Client->ClientIP;
$JSON['ClientPort'] = $Client->ClientPort;
$JSON['FrameTyp'] = WebSocketOPCode::pong;
$Data = json_encode($JSON);
$this->SendDataToChildren($Data);
return;
}
if ($Frame->Fin) {
$this->SendDataToChilds($Data, $Client); // RAW Childs
} else {
$this->{'Buffer' . $Client->ClientIP . $Client->ClientPort} = $Data;
}
}
/**
* Setzt den Intervall des Timer auf den nächsten Timeout eines Client.
*/
private function SetNextTimer()
{
$this->SetTimerInterval('KeepAlivePing', 0);
$Clients = $this->Multi_Clients;
$Client = $Clients->GetNextTimeout($this->PingInterval + 1);
$this->SendDebug('NextTimeout', $Client, 0);
if ($Client === false) {
$next = 0;
} else {
$next = $Client->Timestamp - time();
if ($next == 0) {
$next = 0.001;
}
if ($next < 0) {
$next = 0;
}
}
$this->SendDebug('TIMER NEXT', $next, 0);
$this->SetTimerInterval('KeepAlivePing', $next * 1000);
}
/**
* Sendet einen Pong an einen Client.
*
* @param Websocket_Client $Client Der Client an welchen das Pong versendet wird.
* @param string $Payload Der Payloaf des Pong.
*/
private function SendPong(Websocket_Client $Client, string $Payload = null)
{
$this->Send($Payload, WebSocketOPCode::pong, $Client);
}
/**
* Sendet ein Connection Close an alle Clients.
*/
private function DisconnectAllClients()
{
$this->SetTimerInterval('KeepAlivePing', 0);
$Clients = $this->Multi_Clients;
foreach ($Clients->GetClients() as $Client) {
$this->SendDisconnect($Client);
}
$this->Multi_Clients = new WebSocket_ClientList();
}
/**
* Sendet einen Close an einen Client und löscht alle Buffer dieses Clients.
*
* @param Websocket_Client $Client Der Client an welchen das Close gesendet wird.
*
* @return bool True bei Erfolg, sonst false.
*/
private function SendDisconnect(Websocket_Client $Client)
{
$ret = false;
if ($Client->State == WebSocketState::CloseReceived) {
$ret = true;
$this->SendDebug('Send', 'Answer Client stream close !', 0);
$this->Send('', WebSocketOPCode::close, $Client);
}
if ($Client->State == WebSocketState::Connected) {
$this->SendDebug('Send', 'Server send stream close !', 0);
$Clients = $this->Multi_Clients;
$Client->State = WebSocketState::CloseSend;
$Clients->Update($Client);
$this->Multi_Clients = $Clients;
$this->Send('', WebSocketOPCode::close, $Client);
$ret = $this->WaitForClose($Client);
$this->CloseConnection($Client);
}
$this->RemoveOneClient($Client);
return $ret;
}
private function RemoveOneClient(Websocket_Client $Client)
{
$this->ClearClientBuffer($Client);
$Clients = $this->Multi_Clients;
$Clients->Remove($Client);
$this->Multi_Clients = $Clients;
$this->SetNextTimer();
}
/**
* Leert die ClientListe und alle entsprechenden Buffer der Clients.
*/
private function RemoveAllClients()
{
$Clients = $this->Multi_Clients;
foreach ($Clients->GetClients() as $Client) {
$this->ClearClientBuffer($Client);
}
$this->Multi_Clients = new WebSocket_ClientList();
}
/**
* Leert die entsprechenden Buffer eines Clients.
*
* @param Websocket_Client $Client Der zu löschende Client.
*/
private function ClearClientBuffer(Websocket_Client $Client)
{
$this->SetBuffer('OpCode' . $Client->ClientIP . $Client->ClientPort, '');
$this->SetBuffer('Buffer' . $Client->ClientIP . $Client->ClientPort, '');
$this->SetBuffer('Pong' . $Client->ClientIP . $Client->ClientPort, '');
$this->SetBuffer('WaitForPong' . $Client->ClientIP . $Client->ClientPort, '');
$this->SetBuffer('WaitForClose' . $Client->ClientIP . $Client->ClientPort, '');
$this->{'Multi_TLS_' . $Client->ClientIP . $Client->ClientPort} = '';
$this->SetBuffer('BufferListe_Multi_TLS_' . $Client->ClientIP . $Client->ClientPort, '');
}
/**
* Wartet auf eine Handshake-Antwort.
*
* @param Websocket_Client $Client
*
* @return string|bool Der Payload des Pong, oder im Fehlerfall false.
*/
private function WaitForPong(Websocket_Client $Client)
{
for ($i = 0; $i < 500; $i++) {
if ($this->{'WaitForPong' . $Client->ClientIP . $Client->ClientPort} === true) {
$Payload = $this->{'Pong' . $Client->ClientIP . $Client->ClientPort};
$this->{'Pong' . $Client->ClientIP . $Client->ClientPort} = '';
$this->{'WaitForPong' . $Client->ClientIP . $Client->ClientPort} = false;
return $Payload;
}
IPS_Sleep(5);
}
return false;
}
/**
* Wartet auf eine Close-Antwort eines Clients.
*
* @param Websocket_Client $Client
*
* @return bool True bei Erfolg, sonst false.
*/
private function WaitForClose(Websocket_Client $Client)
{
for ($i = 0; $i < 500; $i++) {
if ($this->{'WaitForClose' . $Client->ClientIP . $Client->ClientPort} === true) {
$this->{'WaitForClose' . $Client->ClientIP . $Client->ClientPort} = false;
return true;
}
IPS_Sleep(5);
}
return false;
}
/**
* Versendet RawData mit OpCode an den IO.
*
* @param string $RawData Das zu sende Payload
* @param WebSocketOPCode $OPCode Der zu benutzende OPCode
* @param Websocket_Client $Client Der Client an welchen die Daten gesendet werden sollen.
* @param bool $Fin True wenn Ende von Payload erreicht.
*/
private function Send(string $RawData, int $OPCode, Websocket_Client $Client, $Fin = true)
{
$WSFrame = new WebSocketFrame($OPCode, $RawData);
$WSFrame->Fin = $Fin;
$Frame = $WSFrame->ToFrame();
$this->SendDebug('Send', $WSFrame, 0);
$SendData = $this->MakeJSON($Client, $Frame);
if ($SendData) {
$this->SendDataToParent($SendData);
}
}
/**
* Sendet die Rohdaten an die Childs.
*
* @param string $RawData Die Nutzdaten.
* @param Websocket_Client $Client Der Client von welchem die Daten empfangen wurden.
*/
private function SendDataToChilds(string $RawData, Websocket_Client $Client)
{
$JSON['DataID'] = '{7A1272A4-CBDB-46EF-BFC6-DCF4A53D2FC7}'; //ServerSocket Receive
$JSON['Buffer'] = utf8_encode($RawData);
$JSON['ClientIP'] = $Client->ClientIP;
$JSON['ClientPort'] = $Client->ClientPort;
$JSON['Type'] = 0; // Data
if ($Client->State == WebSocketState::Connected) {
$JSON['Type'] = 1; // Connected
}
if ($Client->State == WebSocketState::CloseReceived) {
$JSON['Type'] = 2; // Disconnected
}
$Data = json_encode($JSON);
$this->SendDataToChildren($Data);
$JSON['DataID'] = '{8F1F6C32-B1AD-4B7F-8DFB-1244A96FCACF}';
$JSON['FrameTyp'] = $this->{'OpCode' . $Client->ClientIP . $Client->ClientPort};
if ($Client->State == WebSocketState::HandshakeReceived) {
$JSON['FrameTyp'] = WebSocketOPCode::continuation; // Connected
}
if ($Client->State == WebSocketState::CloseReceived) {
$JSON['FrameTyp'] = WebSocketOPCode::close; // Disconnected
}
$Data = json_encode($JSON);
$this->SendDataToChildren($Data);
}
private function SplitTLSFrames(string &$Payload)
{
$Frames = [];
while (strlen($Payload) > 0) {
$len = unpack('n', substr($Payload, 3, 2))[1] + 5;
if (strlen($Payload) >= $len) {
$this->SendDebug('Receive TLS Frame', substr($Payload, 0, $len), 0);
$Frames[] = substr($Payload, 0, $len);
$Payload = substr($Payload, $len);
} else {
break;
}
}
return $Frames;
}
private function ProcessIncomingData(Websocket_Client &$Client, string $Payload)
{
$this->SendDebug('Receive ' . $Client->ClientIP . ':' . $Client->ClientPort, $Payload, 0);
if ($Client->State == WebSocketState::init) { //new
if ($this->UseTLS && ((ord($Payload[0]) >= 0x14) && (ord($Payload[0]) <= 0x18) && (ord($Payload[1]) == 0x03))) { //valid header wenn TLS is active