-
Notifications
You must be signed in to change notification settings - Fork 214
/
AMQP.ps1
2010 lines (1797 loc) · 72.7 KB
/
AMQP.ps1
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
# This file contains functions for AMQP and relay messaging
# http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-transport-v1.0-os.html
# Parses Bus message from the given byte array
function Parse-BusMessage
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes
)
Process
{
# Check the message type
if($Bytes[0] -eq 0x41 -and $Bytes[1] -eq 0x4D -and $Bytes[2] -eq 0x51 -and $Bytes[3] -eq 0x50)
{
# This is version negotiation message
# Construct the message object
$message = New-Object PSObject
switch($Bytes[4])
{
0 { $type = "AMQP"}
1 { $type = "AMQP"}
2 { $type = "TLS"}
3 { $type = "SASL"}
}
$message | Add-Member -NotePropertyName "Type" -NotePropertyValue "Protocol $type"
$message | Add-Member -NotePropertyName "Protocol" -NotePropertyValue $Bytes[4]
$message | Add-Member -NotePropertyName "Major" -NotePropertyValue $Bytes[5]
$message | Add-Member -NotePropertyName "Minor" -NotePropertyValue $Bytes[6]
$message | Add-Member -NotePropertyName "Revision" -NotePropertyValue $Bytes[7]
}
elseif($Bytes[0] -eq 0x00 -and $Bytes[1] -eq 0x53 -and $Bytes[2] -eq 0x75 -and $Bytes[3] -eq 0xb0)
{
# This is a OnewaySend message
$message = Parse-RelayMessage -Bytes $Bytes
}
else
{
# This is an AMQP frame
$message = Parse-AMQPFrame -Bytes $Bytes
}
Write-Verbose "IN: $message"
return $message
}
}
# Parses AMQP Frame error
# Mar 12th 2020
function Parse-AMQPError
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[PSObject]$Error
)
Process
{
$retVal = $Error
# If the error is not $null, let's try to get the actual error message
if($Error -ne $null)
{
$enum = $Error.getEnumerator()
if($enum.MoveNext())
{
$retVal = $enum.Value[1]
}
}
return $retVal
}
}
# Parses AMQP Frame from the given byte array
# Mar 10th 2020
function Parse-AMQPFrame
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes
)
Process
{
# Parse the header
$Size = ([BitConverter]::ToUInt32($Bytes[3..0],0))
$DOff = $Bytes[4]
$ExtendedHeader = $Bytes[8..$($DOff * 4)]
# Construct the message
$message = New-Object PSObject
$message | Add-Member -NotePropertyName "Size" -NotePropertyValue $Size
$message | Add-Member -NotePropertyName "DOFF" -NotePropertyValue $DOff
$message | Add-Member -NotePropertyName "Extended Header" -NotePropertyValue $ExtendedHeader
# Data position
$pos = $DOff * 4 + 2
if($Bytes[5] -eq 0x00) # Parse AQMP Frame
{
$message | Add-Member -NotePropertyName "Type" -NotePropertyValue "AQMP"
# Channel
$channel = ([BitConverter]::ToUInt16($Bytes[7..6],0))
$message | Add-Member -NotePropertyName "Channel" -NotePropertyValue $channel
switch($Bytes[$pos++])
{
0x10 {
$message.Type = "AQMP Open"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "ContainerId" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "HostName" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "MaxFrameSize" -NotePropertyValue $content[2]
$message | Add-Member -NotePropertyName "ChannelMax" -NotePropertyValue $content[3]
$message | Add-Member -NotePropertyName "IdleTimeOut" -NotePropertyValue $content[4]
$message | Add-Member -NotePropertyName "OutgoingLocales" -NotePropertyValue $content[5]
$message | Add-Member -NotePropertyName "IncomingLocales" -NotePropertyValue $content[6]
$message | Add-Member -NotePropertyName "OfferedCapabilities" -NotePropertyValue $content[7]
$message | Add-Member -NotePropertyName "DesiredCapabilities" -NotePropertyValue $content[8]
$message | Add-Member -NotePropertyName "Properties" -NotePropertyValue $content[9]
}
0x11 {
$message.Type = "AQMP Begin"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "RemoteChannel" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "NextOutgoingId" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "IncomingWindow" -NotePropertyValue $content[2]
$message | Add-Member -NotePropertyName "OutgoingWindow" -NotePropertyValue $content[3]
$message | Add-Member -NotePropertyName "HandleMax" -NotePropertyValue $content[4]
$message | Add-Member -NotePropertyName "OfferedCapabilities" -NotePropertyValue $content[5]
$message | Add-Member -NotePropertyName "DesiredCapabilities" -NotePropertyValue $content[6]
$message | Add-Member -NotePropertyName "Properties" -NotePropertyValue $content[7]
}
0x12 {
$message.Type = "AQMP Attach"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "Name" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "Handle" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "Direction" -NotePropertyValue "out"
if($content[2] -eq "True"){ $message.Direction = "in"; $targetPos=-1}
# Target
$enum=$content[(6+$targetPos)].Values.GetEnumerator()
if($enum.MoveNext())
{
$message | Add-Member -NotePropertyName "Target" -NotePropertyValue ($enum.Value[0])
}
# Tracking id
$enum=$content[13].Values.GetEnumerator()
if($enum.MoveNext())
{
$message | Add-Member -NotePropertyName "TrackingId" -NotePropertyValue ($enum.Value)
}
}
0x13 {
$message.Type = "AQMP Flow"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "NextIncomingId" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "IncomingWindow" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "NextOutgoingId" -NotePropertyValue $content[2]
$message | Add-Member -NotePropertyName "OutgoingWindow" -NotePropertyValue $content[3]
$message | Add-Member -NotePropertyName "Handle" -NotePropertyValue $content[4]
$message | Add-Member -NotePropertyName "DeliveryCount" -NotePropertyValue $content[5]
$message | Add-Member -NotePropertyName "LinkCredit" -NotePropertyValue $content[6]
$message | Add-Member -NotePropertyName "Available" -NotePropertyValue $content[7]
$message | Add-Member -NotePropertyName "Drain" -NotePropertyValue $content[8]
$message | Add-Member -NotePropertyName "Echo" -NotePropertyValue $content[9]
$message | Add-Member -NotePropertyName "Properties" -NotePropertyValue $content[10]
}
0x14 {
$message.Type = "AQMP Transfer"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "Handle" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "DeliveryId" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "DeliveryTag" -NotePropertyValue $content[2]
$message | Add-Member -NotePropertyName "MessageFormat" -NotePropertyValue $content[3]
$message | Add-Member -NotePropertyName "Settled" -NotePropertyValue $content[4]
$message | Add-Member -NotePropertyName "More" -NotePropertyValue $content[5]
$message | Add-Member -NotePropertyName "RcvSettleMode" -NotePropertyValue $content[6]
$message | Add-Member -NotePropertyName "State" -NotePropertyValue $content[7]
$message | Add-Member -NotePropertyName "Resume" -NotePropertyValue $content[8]
$message | Add-Member -NotePropertyName "Aborted" -NotePropertyValue $content[9]
$message | Add-Member -NotePropertyName "Batchable" -NotePropertyValue $content[10]
}
0x16 {
$message.Type = "AQMP Detach"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "Handle" -NotePropertyValue $content[0]
$message | Add-Member -NotePropertyName "Closed" -NotePropertyValue $content[1]
$message | Add-Member -NotePropertyName "Error" -NotePropertyValue (Parse-AMQPError -Error $content[2])
}
0x17 {
$message.Type = "AQMP End"
$content = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$pos)
if($content -ne $null)
{
$message | Add-Member -NotePropertyName "Error" -NotePropertyValue (Parse-AMQPError -Error $content[0])
}
else
{
$message | Add-Member -NotePropertyName "Error" -NotePropertyValue $null
}
}
0x18 {
$message.Type = "AQMP Close"
if($content -ne $null)
{
$message | Add-Member -NotePropertyName "Error" -NotePropertyValue (Parse-AMQPError -Error $content[0])
}
}
}
}
else # Parse SASL Frame
{
switch($Bytes[$pos++])
{
0x40 { # sasl-server-mechanisms = list
$message | Add-Member -NotePropertyName "Type" -NotePropertyValue "SASL Mechanisms"
$content = Parse-AMQPList -Bytes $Bytes -Pos ([ref]$pos)
$message | Add-Member -NotePropertyName "Content" -NotePropertyValue $content
}
0x44 { # sasl-outcome = list
$message | Add-Member -NotePropertyName "Type" -NotePropertyValue "SASL Outcome"
$content = Parse-AMQPList -Bytes $Bytes -Pos ([ref]$pos)
# Status
$statusCodes = @("ok","auth","sys","sys-perm","sys-temp")
$status = $statusCodes[$content[0]]
# Message
$text = [text.encoding]::ASCII.GetString( [convert]::FromBase64String($content[1]))
$message | Add-Member -NotePropertyName "Status" -NotePropertyValue $status
$message | Add-Member -NotePropertyName "Message" -NotePropertyValue $text
}
}
}
return $message
}
}
# Parses an AMQP item from the given byte array
# Mar 10th 2020
function Parse-AMQPItem
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes,
[Parameter(Mandatory=$True)]
[ref]$Pos
)
Process
{
$p=$Pos.Value
$retVal = $null
# Check the item type
switch($Bytes[$p++])
{
# descriptor constructor
0x00 {
$descriptor = Parse-AMQPItem -Bytes $Bytes -Pos([ref]$p)
$value = Parse-AMQPItem -Bytes $Bytes -Pos([ref]$p)
$retVal = @{ $descriptor = $value }
}
# null
0x40 {
$pos.Value = $p
return $null
}
# true
0x41 { $retVal = $True }
# false
0x42 { $retVal = $False }
# uint 0
0x43 { $retVal = 0 }
# ulong 0
0x44 { $retVal = 0 }
# empty list
0x45 { $retVal = @() }
# boolean
0x56 {
$boolean = $Bytes[$p++]
$retVal = $boolean -eq 0x01 # 0x01 = true
}
# ubyte
0x50 {
$retVal = [byte]$Bytes[$p++]
}
# byte
0x51 {
$retVal = [byte]$Bytes[$p++]
}
# smalluint
0x52 {
$retVal = [byte]$Bytes[$p++]
}
# smallulong
0x53 {
$retVal = [byte]$Bytes[$p++]
}
# smallint
0x54 {
$retVal = [int]$Bytes[$p++]
}
# smalllong
0x55 {
$retVal = [long]$Bytes[$p++]
}
# ushort
0x60 {
$retVal = [BitConverter]::ToUInt16($Bytes[$($p+1)..$($p)],0)
$p+=2
}
# short
0x61 {
$retVal = [BitConverter]::ToInt16($Bytes[$($p+1)..$($p)],0)
$p+=2
}
# uint
0x70 {
$retVal = [BitConverter]::ToUInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
}
# int
0x71 {
$retVal = [BitConverter]::ToUInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
}
# float
0x72 {
$retVal = [float][BitConverter]::ToInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
}
# char
0x73 {
$retVal = [text.encoding]::UTF32.GetChars($Bytes[$($p+3)..$($p)])
$p+=4
}
# decimal32
0x74 {
# Do nothing
$p+=4
}
# ulong
0x80 {
$retVal = [BitConverter]::ToUInt64($Bytes[$($p+7)..$($p)],0)
$p+=8
}
# long
0x81 {
$retVal = [BitConverter]::ToInt64($Bytes[$($p+7)..$($p)],0)
$p+=8
}
# double
0x82 {
$retVal = [BitConverter]::ToDouble($Bytes[$($p+7)..$($p)],0)
$p+=8
}
# timestamp
0x83 {
$timeStamp = [BitConverter]::ToUint($Bytes[$($p+7)..$($p)],0)
$retVal = $epoch.AddSeconds($timeStamp)
$p+=8
}
# decimal64
0x84 {
# Do nothing
$p+=8
}
# decimal128
0x94 {
# Do nothing
$p+=16
}
# UUID
0x98 {
$retVal = [guid][BitConverter]::ToUint($Bytes[$($p+15)..$($p)],0)
$p+=16
}
# Binary
0xa0 {
$size = $Bytes[$p++]
$retVal = [convert]::ToBase64String($Bytes[$p..$($p+$size)])
$p += $size
}
# String
0xa1 {
$size = $Bytes[$p++]
$retVal = [text.encoding]::UTF8.GetString($Bytes[$p..$($p+$size-1)])
$p += $size
}
# symbol
0xa3 {
$size = $Bytes[$p++]
$retVal = [text.encoding]::ASCII.GetString($Bytes[$p..$($p+$size-1)])
$p += $size
}
# Binary
0xb0 {
$size = [BitConverter]::ToUInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
$retVal = [convert]::ToBase64String($Bytes[$p..$($p+$size)])
$p += $size
}
# String
0xb1 {
$size = [BitConverter]::ToUInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
$retVal = [text.encoding]::UTF8.GetString($Bytes[$p..$($p+$size)])
$p += $size
}
# Symbol
0xb3 {
$size = [BitConverter]::ToUInt32($Bytes[$($p+3)..$($p)],0)
$p+=4
$retVal = [text.encoding]::ASCII.GetString($Bytes[$p..$($p+$size)])
$p += $size
}
# List
0xC0 {
#$p--
$retVal = Parse-AMQPList -Bytes $Bytes -Pos ([ref]$p) }
# List
0xD0 {
#$p--
$retVal = Parse-AMQPList -Bytes $Bytes -Pos ([ref]$p) }
# Map
0xC1 {
#$p--
$retVal = Parse-AMQPMap -Bytes $Bytes -Pos ([ref]$p) }
# Map
0xD1 {
#$p--
$retVal = Parse-AMQPMap -Bytes $Bytes -Pos ([ref]$p) }
# Array
0xE0 {
$retVal = Parse-AMQPArray -Bytes $Bytes -Pos ([ref]$p) }
# Array
0xF0 {
$retVal = Parse-AMQPArray -Bytes $Bytes -Pos ([ref]$p) }
}
$Pos.Value = $p
#if($retVal -ne $null)
#{
return $retVal
#}
}
}
# Parses a AMQP list from the given byte array
# Mar 10th 2020
function Parse-AMQPList
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes,
[Parameter(Mandatory=$True)]
[ref]$Pos
)
Process
{
$p=$Pos.Value
$p--
# Check the list type
switch($Bytes[$p++])
{
0x45 { # The empty list
$size = 0
}
0xC0 {
$size = $Bytes[$p++]
$intSize = 1
}
0xD0 {
$size = [BitConverter]::ToUInt16($bytes[$($p+3)..$($p)],0)
$p += 4
$intSize = 4
}
}
$max = $p + $size
# Next int indicates the number of the items so increase position by the size of the int
$p += $intSize
$retVal = @()
# Loop through the items
while($p -lt $max)
{
$retVal += Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$p)
}
$Pos.Value=$p
return $retVal
}
}
# Parses a AMQP list from the given byte array
# Mar 10th 2020
function Parse-AMQPArray
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes,
[Parameter(Mandatory=$True)]
[ref]$Pos
)
Process
{
$p=$Pos.Value
$retVal = @()
# Size
$size = $Bytes[$p++]
# Number of elements
$elements = $Bytes[$p++]
# Type
$type = $Bytes[$p++]
for($a = 0 ; $a -lt $elements ; $a++)
{
# Array elements does not have type (except for the first one)
$p--
$Bytes[$p]=$type
$retVal += Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$p)
}
$Pos.Value = $p
return $retVal
}
}
# Parses a AMQP list from the given byte array
# Mar 12th 2020
function Parse-AMQPMap
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte[]]$Bytes,
[Parameter(Mandatory=$True)]
[ref]$Pos
)
Process
{
$p=$Pos.Value
$p--
# Check the list type
switch($Bytes[$p++])
{
0xC1 {
$size = $Bytes[$p++]
$intSize = 1
}
0xD1 {
$size = [BitConverter]::ToUInt16($bytes[$($p+3)..$($p)],0)
$p += 4
$intSize = 4
}
}
$max = $p + $size
# Next int indicates the number of the items so increase position by the size of the int
$p += $intSize
$retVal = @()
# Loop through the items
while($p -lt $max)
{
$key = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$p)
$value = Parse-AMQPItem -Bytes $Bytes -Pos ([ref]$p)
$retVal = @{ $key = $value }
}
$Pos.Value=$p
return $retVal
}
}
# Returns a SASL Init message
# Mar 10th 2020
function New-SASLInit
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet('EXTERNAL','MSSBCBS','PLAIN','ANONYMOUS')]
[String]$Mechanics="EXTERNAL"
)
Process
{
# Get the ascii bytes of the selected mechanics
$mechBytes = [text.encoding]::ASCII.getBytes($Mechanics)
$array = @(
0xC0, # Array
[byte]($mechBytes.length + 5), # Length of the Array
0x03, # Number of elements
0xA3, # Symbol
[byte]$mechBytes.length) # Length of the mechanics string
$array += $mechBytes # The mechanics string
$array += @(
0x40, # The initial response ($null)
0x40) # The hostname ($null)
# Construct the message
$message = @(
# The length of the whole message
[BitConverter]::GetBytes([Uint32]($mechBytes.length+18))[3..0])
$message += @(
0x02, # DOFF = 2
0x01, # Message type = SASL
0x00, #
0x00, #
0x00, #
0x53, # SmallULong
0x41) # SASL Init
$message += $array # The array
return $message
}
}
# Returns an AMQP Open message
# Mar 10th 2020
function New-AMQPOpen
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$ContainerId,
[Parameter(Mandatory=$True)]
[String]$HostName
)
Process
{
# Get the ascii bytes of the strings
$idByt = [text.encoding]::ASCII.getBytes($ContainerId)
$idS=$idByt.Length
$hostByt = [text.encoding]::ASCII.getBytes($HostName)
$hostS=$hostByt.Length
$array = @(
0xC0, # Array
[byte]($ids+$hostS + 19), # Length of the Array
0x0A, # Number of elements
0xA1, # UTF-8
[byte]$ids) # Length of the ContainerId string
$array += $idByt # The ContainerId string
$array += @(
0xA1, # UTF-8
[byte]$hostS) # Length of the ContainerId string
$array += $hostByt # The ContainerId string
$array += @(
0x70, # UINT 32 bit
0x00,0x01,0x00, 0x00, # Max Frame Size = 65536
0x60, # UShort 16 bit
0x1F, 0xFF, # Channel Max = 8191
0x40, # Idle timeout in millis. ($null)
0x40, # Outgoing locales ($null)
0x40, # Incoming locales ($null)
0x40, # Offered capabilities ($null)
0x40, # Desired capabilities ($null)
0x40) # Properties ($null)
# Construct the message
$message = @(
# The length of the whole message
[BitConverter]::GetBytes([Uint32]($array[1]+13))[3..0])
$message += @(
0x02, # DOFF = 2
0x00, # Message type = AMQP
0x00, #
0x00, #
0x00, #
0x53, # SmallULong
0x10) # AMQP Open
$message += $array # The array
return $message
}
}
# Returns an AMQP Open message
# Mar 10th 2020
function New-AMQPBegin
{
[cmdletbinding()]
Param()
Process
{
# Construct the message
$message = [byte[]]@(
0x00, 0x00, 0x00, 0x23, # Length of the message
0x02, # DOFF = 2
0x00, # Message type = AMQP
0x00, #
0x00, #
0x00, #
0x53, # SmallULong
0x11, # AMQP Begin
0xC0, # Array
0x16, # Array length
0x08, # Array items
0x40, # Remote Channel ($null)
0x52, # Small Uint
0x01, # Next outgoing Id
0x70, # UInt 32
0x00, 0x00, 0x13, 0x88, # Incoming Window = 5000
0x70, # UInt 32
0x00, 0x00, 0x13, 0x88, # Outgoing Window = 5000
0x70, # UInt 32
0x00, 0x03, 0xFF, 0xFF, # Handle max = 262143
0x40, # Offered capabilities ($null)
0x40, # Desired capabilities ($null)
0x40) # Properties ($null)
return $message
}
}
# Returns an AMQP Attach message
# Mar 11th 2020
function New-AMQPAttach
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[byte]$Handle,
[Parameter(Mandatory=$True)]
[PSObject]$BootStrap,
[Parameter(Mandatory=$True)]
[String]$RelayLinkGuid,
[Parameter(Mandatory=$True)]
[ValidateSet('in','out')]
[String]$Direction,
[Parameter(Mandatory=$True)]
[String]$TrackingID
)
Process
{
# Define variables
$name = "RelayLink_$($RelayLinkGuid):$($Direction)"
$url = "://$($BootStrap.Namespace).servicebus.windows.net/$($BootStrap.ServicePath)/"
$target = "sb$url"
# Calculate SAS Token
$sasUrl = "http$url"
$SASToken = Get-SASToken -Url $sasUrl -Key $BootStrap.SharedAccessKey -KeyName $BootStrap.SharedAccessKeyName
# Get the bytes of the strings
$bName= [text.encoding]::UTF8.GetBytes($name)
$bTarget= [text.encoding]::UTF8.GetBytes($target)
$bSwt= [text.encoding]::ASCII.GetBytes("com.microsoft:swt")
$bSAS= [text.encoding]::UTF8.GetBytes($SASToken)
$bClientAgent= [text.encoding]::ASCII.GetBytes("com.microsoft:client-agent")
$bClientAgentString= [text.encoding]::UTF8.GetBytes("ServiceBus/3.0.51093.14;")
$bDynamicRelay= [text.encoding]::ASCII.GetBytes("com.microsoft:dynamic-relay")
$bListenerType= [text.encoding]::ASCII.GetBytes("com.microsoft:listener-type")
$bRelayedConnection= [text.encoding]::UTF8.GetBytes("RelayedConnection")
$bTrackingId= [text.encoding]::ASCII.GetBytes("com.microsoft:tracking-id")
$bTrackingIdString = [text.encoding]::UTF8.GetBytes($TrackingId.ToString())
# Calculate the combined length
$strLen = $bName.length + $bTarget.length + $bSwt.length + $bSAS.length + $bClientAgent.length + `
$bClientAgentString.length + $bDynamicRelay.length + $bListenerType.length + `
$bRelayedConnection.length + $bTrackingId.length + $bTrackingIdString.length
# Set the handle
if($Handle -gt 0)
{
$bHandle=(0x52, # smallUint
$Handle) # handle value
}
else
{
$bHandle+=@(0x43) # Handle = 0, UInt 8
}
# Set the role
if($Direction -eq "in")
{
$bRole=0x41
}
else
{
$bRole=0x42
}
# Construct the message
$message = [byte[]]@(([BitConverter]::GetBytes([uint32]($strLen + 91 +($bHandle.Length - 1))))[3..0]) # Length of the frame
$message+=@(0x02, # DOFF = 2
0x00, # Message type = AMQP
0x00, #
0x00, #
0x00, #
0x53, # SmallULong
0x12, # AMQP Attach
0xD0) # List
$message+=@(([BitConverter]::GetBytes([uint32]($strLen + 75 +($bHandle.Length - 1))))[3..0]
0x00, 0x00, 0x00, 0x0E, # Number of elements
0xA1, # String
$bName.length) # String length
$message+=@($bName) # Name
$message+=@($bHandle) # Handle
$message+=@($bRole, # Role (False) = Sender
0x40, # snd-settle-mode ($null)
0x40, # rcv-settle-mode ($null)
0x00, # Source (0x00)
0x53, # SmallULong
0x28) # (0x28 = 40)
if($Direction -eq "in")
{
$message+=@(0xC0, # List
($bTarget.length + 13), # Array Length
0x0B, # ?
0xA1, # String
$bTarget.length) # Length
$message+=@($bTarget) # Target
$message+=@(0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40) # ? (belongs to the array)
$message+=@(0x00, # ?
0x53, # SmallULong
0x29, # (0x29 = 41)
0xC0, # List
0x08, # List Length
0x07, # List items
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40) # ?
}
else
{
$message+=@(0xC0, # List
0x0C, # List Length (0x0c = 12)
0x0b, # List elements
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x00, #
0x53, # SmallULong
0x29, # (0x29 = 41)
0xC0, # Array
($bTarget.length + 9), # Array Length
0x07, # ?
0xA1, # String
$bTarget.length) # Length
$message+=@($bTarget) # Target
$message+=@(0x40, 0x40, 0x40, 0x40, 0x40, 0x40) # ? (belongs to the array)
}
$message+=@(0x40, 0x40, 0x40, 0x40, 0x40, 0x40, # ?
0xD1) # Map (unsettled)
# Length of the map:
$message+=@(([BitConverter]::GetBytes([uint32]($strLen - $bName.length - $bTarget.length + 23)))[3..0])
$message+=@(0x00, 0x00, 0x00, 0x0A # ?
0xA3, # Symbol
$bSwt.length) # Length
$message+=@($bSwt) # com.microsoft:swt
$message+=@(0xA1, # String
$bSAS.length) # Length
$message+=@($bSAS) # SASToken
$message+=@(0xA3, # Symbol
$bClientAgent.length) # Length
$message+=@($bClientAgent) # com.microsoft:client-agent
$message+=@(0xA1, # String
$bClientAgentString.length) # Length
$message+=@($bClientAgentString) # ServiceBus/3.0.51093.14
$message+=@(0xA3, # Symbol
$bDynamicRelay.length) # Length
$message+=@($bDynamicRelay) # com.microsoft:dynamic-relay
$message+=@(0x42) # False
$message+=@(0xA3, # Symbol
$bListenerType.length) # Length
$message+=@($bListenerType) # com.microsoft:client-agent
$message+=@(0xA1, # String
$bRelayedConnection.length) # Length
$message+=@($bRelayedConnection) # RelayedConnection
$message+=@(0xA3, # Symbol
$bTrackingId.length) # Length
$message+=@($bTrackingId) # com.microsoft:tracking-id
$message+=@(0xA1, # String
$bTrackingIdString.length) # Length
$message+=@($bTrackingIdString) # GUID
return [byte[]]$message
}
}
# Returns an AMQP Attach message
# May 28th 2021
function New-AMQPAttachADFS
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[byte]$Handle=0,
[Parameter(Mandatory=$True)]
[ValidateSet('in','out')]
[String]$Direction,
[Parameter(Mandatory=$True)]
[String]$Name,
[Parameter(Mandatory=$True)]
[String]$Target,
[Parameter(Mandatory=$False)]
[String]$Source
)
Process
{
# Get the bytes of the strings
$bName= [text.encoding]::UTF8.GetBytes($Name)
$bTarget= [text.encoding]::UTF8.GetBytes($Target)
$bSource= [text.encoding]::UTF8.GetBytes($Source)
# Calculate the combined length
$strLen = $bName.length + $bTarget.length +$bSource.length
#if($bSource.length -gt 0)
#{
# $strLen+=1 # 0xA1
#}
# Set the handle
if($Handle -gt 0)
{
$bHandle=(0x52, # smallUint
$Handle) # handle value
}
else
{
$bHandle+=@(0x43) # Handle = 0, UInt 8
}
# Set the role
if($Direction -eq "in")
{
$bRole=0x41 # true
}
else
{
$bRole=0x42 # false
}
# Construct the message
$message = [byte[]]@(([BitConverter]::GetBytes([uint32]($strLen + 90 +($bHandle.Length - 1))))[3..0]) # Length of the frame
$message+=@(0x02, # DOFF = 2
0x00, # Message type = AMQP
0x00, #
0x00, #
0x00, #
0x53, # SmallULong
0x12) # AMQP Attach
$message+=@(0xC0, # List
($strLen+78+($bHandle.Length - 1)), # Length
0x0E, # Number of elements
0xA1, # String
$bName.length) # String length
$message+=@($bName) # 1. Name
$message+=@($bHandle) # 2. Handle
$message+=@($bRole) # 3. Role (False) = Sender
if($Direction -eq "in")