forked from DeNA/PacketProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUIData.java
More file actions
587 lines (500 loc) · 18 KB
/
Copy pathGUIData.java
File metadata and controls
587 lines (500 loc) · 18 KB
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
/*
* Copyright 2019 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.gui;
import static packetproxy.util.Logging.errWithStackTrace;
import static packetproxy.util.Logging.log;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Supplier;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;
import javax.swing.border.LineBorder;
import packetproxy.controller.ResendController;
import packetproxy.controller.SinglePacketAttackController;
import packetproxy.http.Http;
import packetproxy.model.Diff;
import packetproxy.model.DiffBinary;
import packetproxy.model.DiffJson;
import packetproxy.model.Packet;
import packetproxy.model.Packets;
import packetproxy.util.CharSetUtility;
public class GUIData {
private JFrame owner;
private JPanel main_panel;
private TabSet tabs;
private JButton resend_button;
private JButton resend_multiple_button;
private JButton attack_button;
private JButton send_to_resender_button;
private JButton copy_url_body_button;
private JButton copy_url_button;
private JButton copy_body_button;
private JButton diff_orig_button;
private JButton diff_button;
private JButton stop_diff_button;
private CharSetUtility charSetUtility = CharSetUtility.getInstance();
boolean isDiff = false;
boolean isOrigColorExists = false;
int origIndex;
Color origColor;
private JComboBox charSetCombo = new JComboBox(charSetUtility.getAvailableCharSetList().toArray());
private Supplier<byte[]> dataProvider = null;
private Supplier<byte[]> bodyDataProvider = null;
private Supplier<byte[]> responseDataProvider = null;
public GUIData(JFrame owner) {
this.owner = owner;
}
public void setDataProvider(Supplier<byte[]> provider) {
this.dataProvider = provider;
}
public void setBodyDataProvider(Supplier<byte[]> provider) {
this.bodyDataProvider = provider;
}
public void setResponseDataProvider(Supplier<byte[]> provider) {
this.responseDataProvider = provider;
}
public JComponent createPanel() throws Exception {
createTabsPanel();
main_panel.add(createButtonPanel());
return main_panel;
}
public JComponent createTabsPanel() throws Exception {
main_panel = new JPanel();
main_panel.setLayout(new BoxLayout(main_panel, BoxLayout.Y_AXIS));
tabs = new TabSet(true, false);
main_panel.add(tabs.getTabPanel());
initButtons();
return main_panel;
}
public JComponent createButtonPanel() {
JPanel diff_panel = new JPanel();
diff_panel.add(diff_orig_button);
diff_panel.add(diff_button);
diff_panel.add(stop_diff_button);
diff_panel.setBorder(new LineBorder(Color.black, 1, true));
diff_panel.setLayout(new BoxLayout(diff_panel, BoxLayout.LINE_AXIS));
JPanel button_panel = new JPanel();
button_panel.add(charSetCombo);
button_panel.add(copy_url_body_button);
button_panel.add(copy_body_button);
button_panel.add(copy_url_button);
button_panel.add(resend_button);
button_panel.add(resend_multiple_button);
button_panel.add(attack_button);
button_panel.add(send_to_resender_button);
button_panel.add(new JLabel(" diff: "));
button_panel.add(diff_panel);
button_panel.setLayout(new BoxLayout(button_panel, BoxLayout.LINE_AXIS));
ScrollableCenteredPanel centered_panel = new ScrollableCenteredPanel();
centered_panel.add(button_panel);
return createButtonScrollPane(centered_panel);
}
/**
* ビューポートが十分に広い場合はボタンを中央寄せし、 狭い場合は横スクロールバーを表示するためのパネル。
* getScrollableTracksViewportWidth() でビューポート幅に追従するかを切り替える。
*/
private static class ScrollableCenteredPanel extends JPanel implements Scrollable {
ScrollableCenteredPanel() {
super(new FlowLayout(FlowLayout.CENTER));
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 20;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 100;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return getParent() != null && getParent().getWidth() >= getPreferredSize().width;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return true;
}
}
private byte[] getActiveData() {
if (dataProvider != null) {
return dataProvider.get();
}
int index = tabs.getSelectedIndex();
if (index == 0) {
return tabs.getRaw().getData();
} else if (index == 1) {
return tabs.getBinary().getData();
}
return null;
}
private void initButtons() throws Exception {
copy_url_body_button = new JButton("copy Method+URL+Body");
copy_url_body_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
byte[] data = getActiveData();
if (data == null || data.length == 0)
return;
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
Http http = Http.create(data);
String copyData = http.getMethod() + "\t" + http.getURL(packet.getServerPort(), packet.getUseSSL())
+ "\t" + new String(http.getBody(), "UTF-8");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(copyData);
clipboard.setContents(selection, selection);
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
copy_body_button = new JButton("copy Body");
copy_body_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = resolveDataForCopyBody();
if (data == null || data.length == 0)
return;
Http http = Http.create(data);
String body = new String(http.getBody(), "UTF-8");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(body);
clipboard.setContents(selection, selection);
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
copy_body_button.setAlignmentX(0.5f);
copy_url_button = new JButton("copy URL");
copy_url_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = getActiveData();
if (data == null || data.length == 0)
return;
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
Http http = Http.create(data);
String url = http.getURL(packet.getServerPort(), packet.getUseSSL());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(url);
clipboard.setContents(selection, selection);
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
copy_url_button.setAlignmentX(0.5f);
resend_button = new JButton("send");
resend_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = getActiveData();
if (data != null && data.length > 0) {
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
ResendController.getInstance().resend(packet.getOneShotPacket(data));
packet.setResend();
Packets.getInstance().update(packet);
GUIHistory.getInstance().updateRequestOne(id);
}
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
resend_button.setAlignmentX(0.5f);
resend_multiple_button = new JButton("send x 20");
resend_multiple_button.setAlignmentX(0.5f);
resend_multiple_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = getActiveData();
if (data != null && data.length > 0) {
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
ResendController.getInstance().resend(packet.getOneShotPacket(data), 20);
packet.setResend();
Packets.getInstance().update(packet);
GUIHistory.getInstance().updateRequestOne(id);
}
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
attack_button = new JButton("send x 20 (single-packet attack)");
attack_button.setAlignmentX(0.5f);
attack_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = getActiveData();
if (data != null && data.length > 0) {
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
new SinglePacketAttackController(packet.getOneShotPacket(data)).attack(20);
packet.setResend();
Packets.getInstance().update(packet);
GUIHistory.getInstance().updateRequestOne(id);
}
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
send_to_resender_button = new JButton("send to Resender");
send_to_resender_button.setAlignmentX(0.5f);
send_to_resender_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
byte[] data = getActiveData();
if (data != null && data.length > 0) {
int id = GUIHistory.getInstance().getSelectedPacketId();
Packet packet = Packets.getInstance().query(id);
packet.setResend();
Packets.getInstance().update(packet);
GUIResender.getInstance().addResends(packet.getOneShotPacket(data));
GUIHistory.getInstance().updateRequestOne(id);
}
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
stop_diff_button = new JButton("stop diff");
stop_diff_button.setAlignmentX(0.5f);
stop_diff_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (isDiff) {
Diff.getInstance().clearAsOriginal();
DiffBinary.getInstance().clearAsOriginal();
DiffJson.getInstance().clearAsOriginal();
if (isOrigColorExists) {
isOrigColorExists = false;
GUIHistory.getInstance().addCustomColoring(origIndex, new Color(0xb0, 0xb0, 0xb0)); // Gray
} else {
GUIHistory.getInstance().addCustomColoring(origIndex, Color.WHITE);
}
isDiff = false;
}
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
diff_button = new JButton("diff!!");
diff_button.setAlignmentX(0.5f);
diff_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = resolveDataForDiff();
if (data == null)
return;
Diff.getInstance().markAsTarget(data);
DiffBinary.getInstance().markAsTarget(data);
DiffJson.getInstance().markAsTarget(data);
GUIDiffDialogParent dlg = new GUIDiffDialogParent(owner);
dlg.showDialog();
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
diff_orig_button = new JButton("mark as orig");
diff_orig_button.setAlignmentX(0.5f);
diff_orig_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
byte[] data = resolveDataForDiff();
if (data == null)
return;
if (isDiff) {
Diff.getInstance().clearAsOriginal();
DiffBinary.getInstance().clearAsOriginal();
DiffJson.getInstance().clearAsOriginal();
if (isOrigColorExists) {
isOrigColorExists = false;
GUIHistory.getInstance().addCustomColoring(origIndex, origColor);
} else {
GUIHistory.getInstance().addCustomColoring(origIndex, Color.WHITE);
}
}
isDiff = true;
Diff.getInstance().markAsOriginal(data);
DiffBinary.getInstance().markAsOriginal(data);
DiffJson.getInstance().markAsOriginal(data);
if (GUIHistory.getInstance().containsColor()) {
origColor = GUIHistory.getInstance().getColor();
isOrigColorExists = true;
}
origIndex = GUIHistory.getInstance().getSelectedIndex();
GUIHistory.getInstance().addCustomColoringToCursorPos(new Color(0xb0, 0xb0, 0xb0)); // Gray
log("Diff: original text was saved!");
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
charSetCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
charSetUtility.setCharSet((String) charSetCombo.getSelectedItem());
try {
GUIPacket.getInstance().update();
} catch (Exception e2) {
errWithStackTrace(e2);
}
}
});
charSetCombo.setMaximumSize(new Dimension(150, charSetCombo.getMaximumSize().height));
charSetCombo.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
updateCharSetCombo();
}
});
charSetCombo.setSelectedItem(charSetUtility.getInstance().getCharSetForGUIComponent());
}
public void updateCharSetCombo() {
charSetCombo.removeAllItems();
for (String charSetName : charSetUtility.getAvailableCharSetList()) {
charSetCombo.addItem(charSetName);
}
String charSetName = CharSetUtility.getInstance().getCharSetForGUIComponent();
if (charSetUtility.getAvailableCharSetList().contains(charSetName)) {
charSetCombo.setSelectedItem(charSetName);
} else {
charSetCombo.setSelectedIndex(0);
}
}
static JScrollPane createButtonScrollPane(JPanel buttonPanel) {
JScrollPane scrollPane = new JScrollPane(buttonPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
JScrollBar hBar = getHorizontalScrollBar();
// スクロールバーが表示されている場合のみ高さを加算することで、
// 非表示時の余分なスペースを排除しつつ、表示時はレイアウトを押し下げて領域を確保する
if (hBar != null && hBar.isVisible()) {
d.height += hBar.getPreferredSize().height;
}
return d;
}
};
scrollPane.setBorder(null);
scrollPane.getViewport().addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
scrollPane.revalidate();
scrollPane.repaint();
}
});
return scrollPane;
}
private void update() {
}
public void setData(byte[] data) {
tabs.setData(data);
update();
}
public byte[] getData() {
if (tabs.getData() == null) {
return new byte[]{};
}
try {
return tabs.getData();
} catch (Exception e) {
errWithStackTrace(e);
}
return new byte[]{};
}
/**
* マージ行(Request+Response両方ある行)の場合はどちらのBodyをコピーするか
* ユーザに選択させる。単一パケット行の場合はRequestデータをそのまま返す。 ダイアログでキャンセルされた場合は null を返す。
*/
private byte[] resolveDataForCopyBody() throws Exception {
if (!GUIHistory.getInstance().isSelectedRowMerged()) {
return bodyDataProvider != null ? bodyDataProvider.get() : getActiveData();
}
// macOS の JOptionPane はボタンを右から左に描画するため、
// 視覚的に左から「Request | Response」の順にするには逆順で定義する。
String[] options = {"Response", "Request"};
int choice = JOptionPane.showOptionDialog(owner, "Which body do you want to copy?", "Select Copy Target",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (choice == JOptionPane.CLOSED_OPTION)
return null;
if (choice == 0)
return responseDataProvider != null ? responseDataProvider.get() : null;
return bodyDataProvider != null ? bodyDataProvider.get() : getActiveData();
}
/**
* マージ行(Request+Response両方ある行)の場合はどちらのデータをDiffに使うか
* ユーザに選択させる。単一パケット行の場合はRequestデータをそのまま返す。 ダイアログでキャンセルされた場合は null を返す。
*/
private byte[] resolveDataForDiff() throws Exception {
if (!GUIHistory.getInstance().isSelectedRowMerged()) {
return getActiveData();
}
// macOS の JOptionPane はボタンを右から左に描画するため、
// 視覚的に左から「Request | Response」の順にするには逆順で定義する。
String[] options = {"Response", "Request"};
int choice = JOptionPane.showOptionDialog(owner, "Which data do you want to use for Diff?",
"Select Diff Target", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (choice == JOptionPane.CLOSED_OPTION)
return null;
if (choice == 0)
return responseDataProvider != null ? responseDataProvider.get() : null;
return getActiveData();
}
}