-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGUIOptionHubServer.java
More file actions
161 lines (125 loc) · 4.08 KB
/
GUIOptionHubServer.java
File metadata and controls
161 lines (125 loc) · 4.08 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
package packetproxy.gui;
import static packetproxy.model.PropertyChangeEventType.CONFIGS;
import static packetproxy.util.Logging.errWithStackTrace;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import org.apache.commons.lang3.RandomStringUtils;
import packetproxy.common.ConfigHttpServer;
import packetproxy.common.ConfigSettingsWriter;
import packetproxy.common.I18nString;
import packetproxy.model.ConfigBoolean;
import packetproxy.model.ConfigString;
import packetproxy.model.Configs;
public class GUIOptionHubServer implements PropertyChangeListener {
private JFrame frame;
private JPanel panel;
private JCheckBox checkBox;
private JTextField accessTokenField;
private JButton reGenerateButton;
private ConfigHttpServer server;
public GUIOptionHubServer(JFrame frame) throws Exception {
this.frame = frame;
this.panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(createCheckBox());
panel.add(createAccessTokenPanel());
Configs.getInstance().addPropertyChangeListener(this);
}
private JCheckBox createCheckBox() {
checkBox = new JCheckBox(I18nString.get("Enabled"));
checkBox.addActionListener(event -> {
try {
if (checkBox.isSelected()) {
new ConfigBoolean("SharingConfigs").setState(true);
} else {
new ConfigBoolean("SharingConfigs").setState(false);
}
} catch (Exception e) {
errWithStackTrace(e);
}
});
checkBox.setMinimumSize(new Dimension(Short.MAX_VALUE, checkBox.getMaximumSize().height));
return checkBox;
}
private JComponent createAccessTokenPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JLabel("AccessToken:"));
accessTokenField = new JTextField();
accessTokenField.setEditable(false);
accessTokenField.setMaximumSize(new Dimension(Short.MAX_VALUE, accessTokenField.getMinimumSize().height));
panel.add(accessTokenField);
reGenerateButton = new JButton(I18nString.get("Regenerate"));
reGenerateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
generateAccessToken();
} catch (Exception e1) {
errWithStackTrace(e1);
}
}
});
panel.add(reGenerateButton);
return panel;
}
private boolean isRunningServer() {
return this.server != null && this.server.isAlive();
}
private void stopServer() {
if (this.server == null)
return;
this.server.stop();
this.server = null;
}
private void startServer() throws Exception {
String accessToken = new ConfigString("SharingConfigsAccessToken").getString();
this.server = new ConfigHttpServer("localhost", 32349, accessToken, new SwingConfigHttpUiActions(),
new ConfigSettingsWriter());
this.server.start();
}
private void refresh() {
try {
String accessToken = new ConfigString("SharingConfigsAccessToken").getString();
accessTokenField.setText(accessToken);
checkBox.setSelected(new ConfigBoolean("SharingConfigs").getState());
if (checkBox.isSelected()) {
this.accessTokenField.setEnabled(true);
this.reGenerateButton.setEnabled(true);
if (!isRunningServer())
startServer();
} else {
if (isRunningServer())
stopServer();
this.accessTokenField.setEnabled(false);
this.reGenerateButton.setEnabled(false);
}
} catch (Exception e) {
errWithStackTrace(e);
}
}
private void generateAccessToken() throws Exception {
String accessToken = RandomStringUtils.randomAlphabetic(20);
new ConfigString("SharingConfigsAccessToken").setString(accessToken);
}
public JComponent createPanel() throws Exception {
String accessToken = new ConfigString("SharingConfigsAccessToken").getString();
if (accessToken.isEmpty()) {
generateAccessToken();
}
refresh();
return panel;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (CONFIGS.matches(evt)) {
refresh();
}
}
}