1 package com.jsql.view.swing.panel.preferences;
2
3 import com.jsql.view.swing.action.ActionCheckIp;
4 import com.jsql.view.swing.panel.PanelPreferences;
5 import com.jsql.view.swing.text.JPopupTextField;
6 import com.jsql.view.swing.text.listener.DocumentListenerEditing;
7 import com.jsql.view.swing.util.MediatorHelper;
8
9 import javax.swing.*;
10 import javax.swing.event.DocumentListener;
11 import java.awt.*;
12 import java.util.stream.Stream;
13
14 public class PanelProxy extends JPanel {
15
16 private final JCheckBox checkboxIsUsingProxy = new JCheckBox("<html>Enable proxy for <b>HTTP</b>:</html>", MediatorHelper.model().getMediatorUtils().getProxyUtil().isUsingProxyHttp());
17 private final JCheckBox checkboxIsUsingProxyHttps = new JCheckBox("<html>Enable proxy for <b>HTTPS</b>:</html>", MediatorHelper.model().getMediatorUtils().getProxyUtil().isUsingProxyHttps());
18
19 private final JTextField textProxyAddress = new JPopupTextField("e.g. Tor address: 127.0.0.1", MediatorHelper.model().getMediatorUtils().getProxyUtil().getProxyAddressHttp()).getProxy();
20 private final JTextField textProxyPort = new JPopupTextField("e.g. Tor port: 8118", MediatorHelper.model().getMediatorUtils().getProxyUtil().getProxyPortHttp()).getProxy();
21 private final JTextField textProxyAddressHttps = new JPopupTextField("e.g. Tor address: 127.0.0.1", MediatorHelper.model().getMediatorUtils().getProxyUtil().getProxyAddressHttps()).getProxy();
22 private final JTextField textProxyPortHttps = new JPopupTextField("e.g. Tor port: 8118", MediatorHelper.model().getMediatorUtils().getProxyUtil().getProxyPortHttps()).getProxy();
23
24 public PanelProxy(PanelPreferences panelPreferences) {
25 var panelHttpIpAddress = new JPanel();
26 panelHttpIpAddress.setLayout(new BoxLayout(panelHttpIpAddress, BoxLayout.LINE_AXIS));
27 panelHttpIpAddress.add(new JLabel("IP "));
28 panelHttpIpAddress.add(this.textProxyAddress);
29 panelHttpIpAddress.setMaximumSize(new Dimension(325, this.textProxyAddress.getPreferredSize().height));
30
31 var panelHttpPort = new JPanel();
32 panelHttpPort.setLayout(new BoxLayout(panelHttpPort, BoxLayout.LINE_AXIS));
33 panelHttpPort.add(new JLabel("Port "));
34 panelHttpPort.add(this.textProxyPort);
35 panelHttpPort.setMaximumSize(new Dimension(325, this.textProxyPort.getPreferredSize().height));
36 panelHttpPort.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
37
38 var panelHttpsIpAddress = new JPanel();
39 panelHttpsIpAddress.setLayout(new BoxLayout(panelHttpsIpAddress, BoxLayout.LINE_AXIS));
40 panelHttpsIpAddress.add(new JLabel("IP "));
41 panelHttpsIpAddress.add(this.textProxyAddressHttps);
42 panelHttpsIpAddress.setMaximumSize(new Dimension(325, this.textProxyAddressHttps.getPreferredSize().height));
43
44 var panelHttpsPort = new JPanel();
45 panelHttpsPort.setLayout(new BoxLayout(panelHttpsPort, BoxLayout.LINE_AXIS));
46 panelHttpsPort.add(new JLabel("Port "));
47 panelHttpsPort.add(this.textProxyPortHttps);
48 panelHttpsPort.setMaximumSize(new Dimension(325, this.textProxyPortHttps.getPreferredSize().height));
49 panelHttpsPort.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
50
51 this.checkboxIsUsingProxy.setToolTipText("Enable proxy for HTTP protocol");
52 this.checkboxIsUsingProxyHttps.setToolTipText("Enable proxy for HTTPS protocol");
53
54 Stream.of(
55 this.checkboxIsUsingProxy,
56 this.checkboxIsUsingProxyHttps
57 )
58 .forEach(button -> button.addActionListener(panelPreferences.getActionListenerSave()));
59
60 DocumentListener documentListenerSave = new DocumentListenerEditing() {
61 @Override
62 public void process() {
63 panelPreferences.getActionListenerSave().actionPerformed(null);
64 }
65 };
66
67 Stream.of(
68 this.textProxyAddress,
69 this.textProxyPort,
70 this.textProxyAddressHttps,
71 this.textProxyPortHttps
72 )
73 .forEach(textField -> textField.getDocument().addDocumentListener(documentListenerSave));
74
75 final var buttonCheckIp = new JButton("Check your IP address");
76 buttonCheckIp.addActionListener(new ActionCheckIp());
77 buttonCheckIp.setToolTipText(
78 "<html><b>Show your public IP address</b><br>"
79 + "Your internal IP is displayed if you don't set a proxy. If you set a proxy<br>"
80 + "like TOR then another IP is used instead of your internal IP.</html>"
81 );
82
83 var labelOrigin = new JLabel("<html><b>Proxy settings (e.g Burp, Tor and Privoxy)</b></html>");
84 labelOrigin.setBorder(PanelGeneral.MARGIN);
85
86 var groupLayout = new GroupLayout(this);
87 this.setLayout(groupLayout);
88
89 groupLayout.setHorizontalGroup(
90 groupLayout
91 .createSequentialGroup()
92 .addGroup(
93 groupLayout
94 .createParallelGroup(GroupLayout.Alignment.LEADING, false)
95 .addComponent(labelOrigin)
96 .addComponent(this.checkboxIsUsingProxy)
97 .addComponent(panelHttpIpAddress)
98 .addComponent(panelHttpPort)
99 .addComponent(this.checkboxIsUsingProxyHttps)
100 .addComponent(panelHttpsIpAddress)
101 .addComponent(panelHttpsPort)
102 .addComponent(buttonCheckIp)
103 )
104 );
105
106 groupLayout.setVerticalGroup(
107 groupLayout
108 .createSequentialGroup()
109 .addGroup(
110 groupLayout
111 .createParallelGroup(GroupLayout.Alignment.BASELINE)
112 .addComponent(labelOrigin)
113 )
114 .addGroup(
115 groupLayout
116 .createParallelGroup(GroupLayout.Alignment.BASELINE)
117 .addComponent(this.checkboxIsUsingProxy)
118 )
119 .addGroup(
120 groupLayout
121 .createParallelGroup(GroupLayout.Alignment.BASELINE)
122 .addComponent(panelHttpIpAddress)
123 )
124 .addGroup(
125 groupLayout
126 .createParallelGroup(GroupLayout.Alignment.BASELINE)
127 .addComponent(panelHttpPort)
128 )
129 .addGroup(
130 groupLayout
131 .createParallelGroup(GroupLayout.Alignment.BASELINE)
132 .addComponent(this.checkboxIsUsingProxyHttps)
133 )
134 .addGroup(
135 groupLayout
136 .createParallelGroup(GroupLayout.Alignment.BASELINE)
137 .addComponent(panelHttpsIpAddress)
138 )
139 .addGroup(
140 groupLayout
141 .createParallelGroup(GroupLayout.Alignment.BASELINE)
142 .addComponent(panelHttpsPort)
143 )
144 .addGroup(
145 groupLayout
146 .createParallelGroup(GroupLayout.Alignment.BASELINE)
147 .addComponent(buttonCheckIp)
148 )
149 );
150 }
151
152
153
154
155 public JCheckBox getCheckboxIsUsingProxy() {
156 return this.checkboxIsUsingProxy;
157 }
158
159 public JTextField getTextProxyAddress() {
160 return this.textProxyAddress;
161 }
162
163 public JTextField getTextProxyPort() {
164 return this.textProxyPort;
165 }
166
167 public JCheckBox getCheckboxIsUsingProxyHttps() {
168 return this.checkboxIsUsingProxyHttps;
169 }
170
171 public JTextField getTextProxyAddressHttps() {
172 return this.textProxyAddressHttps;
173 }
174
175 public JTextField getTextProxyPortHttps() {
176 return this.textProxyPortHttps;
177 }
178 }