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