1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.remotecontrol;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.security.GeneralSecurityException;
|
---|
13 | import java.security.KeyStore;
|
---|
14 | import java.security.KeyStoreException;
|
---|
15 | import java.security.NoSuchAlgorithmException;
|
---|
16 | import java.security.cert.CertificateException;
|
---|
17 | import java.util.LinkedHashMap;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 |
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.Box;
|
---|
23 | import javax.swing.JButton;
|
---|
24 | import javax.swing.JCheckBox;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JSeparator;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
35 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
36 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
37 | import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
|
---|
38 | import org.openstreetmap.josm.io.remotecontrol.RemoteControlHttpsServer;
|
---|
39 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
|
---|
40 | import org.openstreetmap.josm.tools.GBC;
|
---|
41 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Preference settings for Remote Control.
|
---|
45 | *
|
---|
46 | * @author Frederik Ramm
|
---|
47 | */
|
---|
48 | public final class RemoteControlPreference extends DefaultTabPreferenceSetting {
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Factory used to build a new instance of this preference setting
|
---|
52 | */
|
---|
53 | public static class Factory implements PreferenceSettingFactory {
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public PreferenceSetting createPreferenceSetting() {
|
---|
57 | return new RemoteControlPreference();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private RemoteControlPreference() {
|
---|
62 | super("remotecontrol", tr("Remote Control"), tr("Settings for the remote control feature."));
|
---|
63 | for (PermissionPrefWithDefault p : PermissionPrefWithDefault.getPermissionPrefs()) {
|
---|
64 | JCheckBox cb = new JCheckBox(p.preferenceText);
|
---|
65 | cb.setSelected(p.isAllowed());
|
---|
66 | prefs.put(p, cb);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | private final Map<PermissionPrefWithDefault, JCheckBox> prefs = new LinkedHashMap<>();
|
---|
70 | private JCheckBox enableRemoteControl;
|
---|
71 | private JCheckBox enableHttpsSupport;
|
---|
72 |
|
---|
73 | private JButton installCertificate;
|
---|
74 | private JButton uninstallCertificate;
|
---|
75 |
|
---|
76 | private final JCheckBox loadInNewLayer = new JCheckBox(tr("Download objects to new layer"));
|
---|
77 | private final JCheckBox alwaysAskUserConfirm = new JCheckBox(tr("Confirm all Remote Control actions manually"));
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
81 |
|
---|
82 | JPanel remote = new JPanel(new GridBagLayout());
|
---|
83 |
|
---|
84 | final JLabel descLabel = new JLabel("<html>"
|
---|
85 | + tr("Allows JOSM to be controlled from other applications, e.g. from a web browser.")
|
---|
86 | + "</html>");
|
---|
87 | descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
|
---|
88 | remote.add(descLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
---|
89 |
|
---|
90 | final JLabel portLabel = new JLabel("<html>" + tr("JOSM will always listen at <b>port {0}</b> (http) and <b>port {1}</b> (https) on localhost."
|
---|
91 | + "<br>These ports are not configurable because they are referenced by external applications talking to JOSM.",
|
---|
92 | Main.pref.get("remote.control.port", "8111"),
|
---|
93 | Main.pref.get("remote.control.https.port", "8112")) + "</html>");
|
---|
94 | portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
|
---|
95 | remote.add(portLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
---|
96 |
|
---|
97 | enableRemoteControl = new JCheckBox(tr("Enable remote control"), RemoteControl.PROP_REMOTECONTROL_ENABLED.get());
|
---|
98 | remote.add(enableRemoteControl, GBC.eol());
|
---|
99 |
|
---|
100 | final JPanel wrapper = new JPanel();
|
---|
101 | wrapper.setLayout(new GridBagLayout());
|
---|
102 | wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));
|
---|
103 |
|
---|
104 | remote.add(wrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 5));
|
---|
105 |
|
---|
106 | boolean https = RemoteControl.PROP_REMOTECONTROL_HTTPS_ENABLED.get();
|
---|
107 |
|
---|
108 | enableHttpsSupport = new JCheckBox(tr("Enable HTTPS support"), https);
|
---|
109 | wrapper.add(enableHttpsSupport, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
110 |
|
---|
111 | // Certificate installation only available on Windows for now, see #10033
|
---|
112 | if (Main.isPlatformWindows()) {
|
---|
113 | installCertificate = new JButton(tr("Install..."));
|
---|
114 | uninstallCertificate = new JButton(tr("Uninstall..."));
|
---|
115 | installCertificate.setToolTipText(tr("Install JOSM localhost certificate to system/browser root keystores"));
|
---|
116 | uninstallCertificate.setToolTipText(tr("Uninstall JOSM localhost certificate from system/browser root keystores"));
|
---|
117 | wrapper.add(new JLabel(tr("Certificate:")), GBC.std().insets(15, 5, 0, 0));
|
---|
118 | wrapper.add(installCertificate, GBC.std().insets(5, 5, 0, 0));
|
---|
119 | wrapper.add(uninstallCertificate, GBC.eol().insets(5, 5, 0, 0));
|
---|
120 | enableHttpsSupport.addActionListener(new ActionListener() {
|
---|
121 | @Override
|
---|
122 | public void actionPerformed(ActionEvent e) {
|
---|
123 | installCertificate.setEnabled(enableHttpsSupport.isSelected());
|
---|
124 | }
|
---|
125 | });
|
---|
126 | installCertificate.addActionListener(new ActionListener() {
|
---|
127 | @Override
|
---|
128 | public void actionPerformed(ActionEvent e) {
|
---|
129 | try {
|
---|
130 | boolean changed = RemoteControlHttpsServer.setupPlatform(
|
---|
131 | RemoteControlHttpsServer.loadJosmKeystore());
|
---|
132 | String msg = changed ?
|
---|
133 | tr("Certificate has been successfully installed.") :
|
---|
134 | tr("Certificate is already installed. Nothing to do.");
|
---|
135 | Main.info(msg);
|
---|
136 | JOptionPane.showMessageDialog(wrapper, msg);
|
---|
137 | } catch (IOException | GeneralSecurityException ex) {
|
---|
138 | Main.error(ex);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | });
|
---|
142 | uninstallCertificate.addActionListener(new ActionListener() {
|
---|
143 | @Override
|
---|
144 | public void actionPerformed(ActionEvent e) {
|
---|
145 | try {
|
---|
146 | String msg;
|
---|
147 | KeyStore ks = PlatformHookWindows.getRootKeystore();
|
---|
148 | if (ks.containsAlias(RemoteControlHttpsServer.ENTRY_ALIAS)) {
|
---|
149 | Main.info(tr("Removing certificate {0} from root keystore.", RemoteControlHttpsServer.ENTRY_ALIAS));
|
---|
150 | ks.deleteEntry(RemoteControlHttpsServer.ENTRY_ALIAS);
|
---|
151 | msg = tr("Certificate has been successfully uninstalled.");
|
---|
152 | } else {
|
---|
153 | msg = tr("Certificate is not installed. Nothing to do.");
|
---|
154 | }
|
---|
155 | Main.info(msg);
|
---|
156 | JOptionPane.showMessageDialog(wrapper, msg);
|
---|
157 | } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException ex) {
|
---|
158 | Main.error(ex);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | });
|
---|
162 | installCertificate.setEnabled(https);
|
---|
163 | }
|
---|
164 |
|
---|
165 | wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
|
---|
166 |
|
---|
167 | wrapper.add(new JLabel(tr("Permitted actions:")), GBC.eol().insets(5, 0, 0, 0));
|
---|
168 | for (JCheckBox p : prefs.values()) {
|
---|
169 | wrapper.add(p, GBC.eol().insets(15, 5, 0, 0).fill(GBC.HORIZONTAL));
|
---|
170 | }
|
---|
171 |
|
---|
172 | wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
|
---|
173 | wrapper.add(loadInNewLayer, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
174 | wrapper.add(alwaysAskUserConfirm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
175 |
|
---|
176 | remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
|
---|
177 |
|
---|
178 | loadInNewLayer.setSelected(Main.pref.getBoolean(RequestHandler.loadInNewLayerKey, RequestHandler.loadInNewLayerDefault));
|
---|
179 | alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault));
|
---|
180 |
|
---|
181 | ActionListener remoteControlEnabled = new ActionListener() {
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public void actionPerformed(ActionEvent e) {
|
---|
185 | GuiHelper.setEnabledRec(wrapper, enableRemoteControl.isSelected());
|
---|
186 | // 'setEnabled(false)' does not work for JLabel with html text, so do it manually
|
---|
187 | // FIXME: use QuadStateCheckBox to make checkboxes unset when disabled
|
---|
188 | if (installCertificate != null && uninstallCertificate != null) {
|
---|
189 | // Install certificate button is enabled if HTTPS is also enabled
|
---|
190 | installCertificate.setEnabled(enableRemoteControl.isSelected() && enableHttpsSupport.isSelected());
|
---|
191 | // Uninstall certificate button is always enabled
|
---|
192 | uninstallCertificate.setEnabled(true);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | };
|
---|
196 | enableRemoteControl.addActionListener(remoteControlEnabled);
|
---|
197 | remoteControlEnabled.actionPerformed(null);
|
---|
198 | createPreferenceTabWithScrollPane(gui, remote);
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | public boolean ok() {
|
---|
203 | boolean enabled = enableRemoteControl.isSelected();
|
---|
204 | boolean httpsEnabled = enableHttpsSupport.isSelected();
|
---|
205 | boolean changed = RemoteControl.PROP_REMOTECONTROL_ENABLED.put(enabled);
|
---|
206 | boolean httpsChanged = RemoteControl.PROP_REMOTECONTROL_HTTPS_ENABLED.put(httpsEnabled);
|
---|
207 | if (enabled) {
|
---|
208 | for (Entry<PermissionPrefWithDefault, JCheckBox> p : prefs.entrySet()) {
|
---|
209 | Main.pref.put(p.getKey().pref, p.getValue().isSelected());
|
---|
210 | }
|
---|
211 | Main.pref.put(RequestHandler.loadInNewLayerKey, loadInNewLayer.isSelected());
|
---|
212 | Main.pref.put(RequestHandler.globalConfirmationKey, alwaysAskUserConfirm.isSelected());
|
---|
213 | }
|
---|
214 | if (changed) {
|
---|
215 | if (enabled) {
|
---|
216 | RemoteControl.start();
|
---|
217 | } else {
|
---|
218 | RemoteControl.stop();
|
---|
219 | }
|
---|
220 | } else if (httpsChanged) {
|
---|
221 | if (httpsEnabled) {
|
---|
222 | RemoteControlHttpsServer.restartRemoteControlHttpsServer();
|
---|
223 | } else {
|
---|
224 | RemoteControlHttpsServer.stopRemoteControlHttpsServer();
|
---|
225 | }
|
---|
226 | }
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 | }
|
---|