1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.net.Inet4Address;
|
---|
6 | import java.net.Inet6Address;
|
---|
7 | import java.net.InetAddress;
|
---|
8 | import java.net.UnknownHostException;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
11 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
|
---|
12 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
13 | import org.openstreetmap.josm.tools.Logging;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Manager class for remote control operations.
|
---|
17 | *
|
---|
18 | * IMPORTANT! increment the minor version on compatible API extensions
|
---|
19 | * and increment the major version and set minor to 0 on incompatible changes.
|
---|
20 | */
|
---|
21 | public class RemoteControl {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * If the remote control feature is enabled or disabled. If disabled,
|
---|
25 | * it should not start the server.
|
---|
26 | */
|
---|
27 | public static final BooleanProperty PROP_REMOTECONTROL_ENABLED = new BooleanProperty("remotecontrol.enabled", false);
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * If the remote control feature is enabled or disabled for HTTPS. If disabled,
|
---|
31 | * only HTTP access will be available.
|
---|
32 | * @since 7335
|
---|
33 | */
|
---|
34 | public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty(
|
---|
35 | "remotecontrol.https.enabled", false);
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * RemoteControl HTTP protocol version. Change minor number for compatible
|
---|
39 | * interface extensions. Change major number in case of incompatible
|
---|
40 | * changes.
|
---|
41 | */
|
---|
42 | static final int protocolMajorVersion = 1;
|
---|
43 | static final int protocolMinorVersion = 7;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Starts the remote control server
|
---|
47 | */
|
---|
48 | public static void start() {
|
---|
49 | RemoteControlHttpServer.restartRemoteControlHttpServer();
|
---|
50 | if (supportsHttps()) {
|
---|
51 | RemoteControlHttpsServer.restartRemoteControlHttpsServer();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Stops the remote control server
|
---|
57 | * @since 5861
|
---|
58 | */
|
---|
59 | public static void stop() {
|
---|
60 | RemoteControlHttpServer.stopRemoteControlHttpServer();
|
---|
61 | if (supportsHttps()) {
|
---|
62 | RemoteControlHttpsServer.stopRemoteControlHttpsServer();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Determines if the current JVM support HTTPS remote control.
|
---|
68 | * @return {@code true} if the JVM provides {@code sun.security.x509} classes
|
---|
69 | * @since 12703
|
---|
70 | */
|
---|
71 | public static boolean supportsHttps() {
|
---|
72 | try {
|
---|
73 | return Class.forName("sun.security.x509.GeneralName") != null;
|
---|
74 | } catch (ClassNotFoundException | SecurityException e) {
|
---|
75 | Logging.trace(e);
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Adds external request handler.
|
---|
82 | * Can be used by plugins that want to use remote control.
|
---|
83 | *
|
---|
84 | * @param command The command name.
|
---|
85 | * @param handlerClass The additional request handler.
|
---|
86 | */
|
---|
87 | public void addRequestHandler(String command, Class<? extends RequestHandler> handlerClass) {
|
---|
88 | RequestProcessor.addRequestHandlerClass(command, handlerClass);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Returns the remote control directory.
|
---|
93 | * @return The remote control directory
|
---|
94 | * @since 7335
|
---|
95 | */
|
---|
96 | public static String getRemoteControlDir() {
|
---|
97 | return new File(Config.getDirs().getUserDataDirectory(true), "remotecontrol").getAbsolutePath();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the IPv6 address used for remote control.
|
---|
102 | * @return the IPv6 address used for remote control
|
---|
103 | * @throws UnknownHostException if the local host name could not be resolved into an address.
|
---|
104 | * @since 8337
|
---|
105 | */
|
---|
106 | public static InetAddress getInet6Address() throws UnknownHostException {
|
---|
107 | for (InetAddress a : InetAddress.getAllByName(Config.getPref().get("remote.control.host.ipv6", "::1"))) {
|
---|
108 | if (a instanceof Inet6Address) {
|
---|
109 | return a;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | throw new UnknownHostException();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Returns the IPv4 address used for remote control.
|
---|
117 | * @return the IPv4 address used for remote control
|
---|
118 | * @throws UnknownHostException if the local host name could not be resolved into an address.
|
---|
119 | * @since 8337
|
---|
120 | */
|
---|
121 | public static InetAddress getInet4Address() throws UnknownHostException {
|
---|
122 | // Return an address to the loopback interface by default
|
---|
123 | for (InetAddress a : InetAddress.getAllByName(Config.getPref().get("remote.control.host.ipv4", "127.0.0.1"))) {
|
---|
124 | if (a instanceof Inet4Address) {
|
---|
125 | return a;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | throw new UnknownHostException();
|
---|
129 | }
|
---|
130 | }
|
---|