1818import static packetproxy .http2 .frames .FrameUtils .PREFACE ;
1919import static packetproxy .http2 .frames .FrameUtils .SETTINGS ;
2020import static packetproxy .http2 .frames .FrameUtils .WINDOW_UPDATE ;
21- import static packetproxy .util .Logging .err ;
2221
2322import java .io .ByteArrayOutputStream ;
2423import java .util .ArrayList ;
@@ -67,36 +66,14 @@ public SinglePacketAttackController(final OneShotPacket oneshot, final int sleep
6766 }
6867
6968 public void attack (final int count ) throws Exception {
69+ if (count <= 0 ) {
70+ return ;
71+ }
72+
7073 sendConnectionPreface ();
7174 launchAttack (count );
7275 }
7376
74- private void sendConnectionPreface () throws Exception {
75- final var preface = new ByteArrayOutputStream ();
76- preface .write (PREFACE );
77- preface .write (SETTINGS );
78- preface .write (WINDOW_UPDATE );
79-
80- attackConnection .execFastSend (preface .toByteArray ());
81- }
82-
83- private void launchAttack (final int count ) throws Exception {
84- var currentStreamId = 1 ;
85-
86- for (var i = 0 ; i < count ; i ++) {
87- try {
88- final var request = new SingleAttackRequest (currentStreamId , baseAttackFrames , attackConnection ,
89- sleepTimeMs );
90-
91- request .execute ();
92- } catch (Exception e ) {
93- err ("Stream %d : Single Packet Attack failed with exception: %s" , currentStreamId , e .getMessage ());
94- }
95-
96- currentStreamId += 2 ;
97- }
98- }
99-
10077 private static boolean isHttp2 (final OneShotPacket oneshot ) {
10178 var alpn = oneshot .getAlpn ();
10279 return alpn != null && (alpn .equals ("h2" ) || alpn .equals ("grpc" ) || alpn .equals ("grpc-exp" ));
@@ -124,6 +101,53 @@ private static boolean isGetMethod(final OneShotPacket oneshot) {
124101 return method .equals ("GET" );
125102 }
126103
104+ private void sendConnectionPreface () throws Exception {
105+ final var preface = new ByteArrayOutputStream ();
106+ preface .write (PREFACE );
107+ preface .write (SETTINGS );
108+ preface .write (WINDOW_UPDATE );
109+
110+ attackConnection .execFastSend (preface .toByteArray ());
111+ }
112+
113+ private void launchAttack (final int count ) throws Exception {
114+ final var requests = createRequests (count );
115+
116+ for (final var request : requests ) {
117+ request .sendFirstFrames ();
118+ }
119+
120+ Thread .sleep (sleepTimeMs );
121+ sendPing ();
122+
123+ for (final var request : requests ) {
124+ request .sendLastFrames ();
125+ }
126+
127+ for (var i = 0 ; i < requests .size (); i ++) {
128+ attackConnection .receive ();
129+ }
130+ }
131+
132+ private ArrayList <SingleAttackRequest > createRequests (final int count ) throws Exception {
133+ final var requests = new ArrayList <SingleAttackRequest >();
134+
135+ for (var i = 0 ; i < count ; i ++) {
136+ final var streamId = i * 2 + 1 ;
137+ final var request = new SingleAttackRequest (streamId , baseAttackFrames , attackConnection );
138+ requests .add (request );
139+ }
140+
141+ return requests ;
142+ }
143+
144+ private void sendPing () throws Exception {
145+ final var pingPayload = new byte [8 ];
146+ final var pingFrame = new Frame (Frame .Type .PING , 0 , 0 , pingPayload );
147+ final var pingData = pingFrame .toByteArray ();
148+ attackConnection .execFastSend (pingData );
149+ }
150+
127151 private static AttackFrames generateAttackFrames (final OneShotPacket packet ) throws Exception {
128152 final var originalFrames = convertPacketToFrames (packet );
129153
@@ -317,23 +341,13 @@ private static class SingleAttackRequest {
317341 private final AttackFrames originalAttackFrames ;
318342 private final DuplexSync connection ;
319343 private final AttackFrames streamAttackFrames ;
320- private final int sleepTimeMs ;
321344
322345 public SingleAttackRequest (final int streamId , final AttackFrames originalAttackFrames ,
323- final DuplexSync connection , final int sleepTimeMs ) throws Exception {
346+ final DuplexSync connection ) throws Exception {
324347 this .streamId = streamId ;
325348 this .originalAttackFrames = originalAttackFrames ;
326349 this .connection = connection ;
327350 this .streamAttackFrames = createStreamAttackFrames ();
328- this .sleepTimeMs = sleepTimeMs ;
329- }
330-
331- public void execute () throws Exception {
332- sendFirstFrames ();
333- Thread .sleep (sleepTimeMs );
334- sendPing ();
335- sendLastFrames ();
336- connection .receive ();
337351 }
338352
339353 private AttackFrames createStreamAttackFrames () throws Exception {
@@ -357,15 +371,8 @@ private List<Frame> updateFrameStreamIds(final List<Frame> originalFrames, final
357371
358372 private void sendFirstFrames () throws Exception {
359373 final var firstFramesData = FrameUtils .toByteArray (streamAttackFrames .firstFrames );
360- connection .execFastSend (firstFramesData );
361- }
362-
363- private void sendPing () throws Exception {
364- final var pingPayload = new byte [8 ];
365- final var pingFrame = new Frame (Frame .Type .PING , 0 , 0 , pingPayload );
366- final var pingData = pingFrame .toByteArray ();
367374
368- connection .execFastSend (pingData );
375+ connection .execFastSend (firstFramesData );
369376 }
370377
371378 private void sendLastFrames () throws Exception {
0 commit comments