001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io.remotecontrol; 003 004import java.io.File; 005import java.net.Inet4Address; 006import java.net.Inet6Address; 007import java.net.InetAddress; 008import java.net.UnknownHostException; 009 010import org.openstreetmap.josm.Main; 011import org.openstreetmap.josm.data.preferences.BooleanProperty; 012import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler; 013 014/** 015 * Manager class for remote control operations. 016 * 017 * IMPORTANT! increment the minor version on compatible API extensions 018 * and increment the major version and set minor to 0 on incompatible changes. 019 */ 020public class RemoteControl { 021 022 /** 023 * If the remote control feature is enabled or disabled. If disabled, 024 * it should not start the server. 025 */ 026 public static final BooleanProperty PROP_REMOTECONTROL_ENABLED = new BooleanProperty("remotecontrol.enabled", false); 027 028 /** 029 * If the remote control feature is enabled or disabled for HTTPS. If disabled, 030 * only HTTP access will be available. 031 * @since 7335 032 */ 033 public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty( 034 "remotecontrol.https.enabled", false); 035 036 /** 037 * RemoteControl HTTP protocol version. Change minor number for compatible 038 * interface extensions. Change major number in case of incompatible 039 * changes. 040 */ 041 static final int protocolMajorVersion = 1; 042 static final int protocolMinorVersion = 7; 043 044 /** 045 * Starts the remote control server 046 */ 047 public static void start() { 048 RemoteControlHttpServer.restartRemoteControlHttpServer(); 049 RemoteControlHttpsServer.restartRemoteControlHttpsServer(); 050 } 051 052 /** 053 * Stops the remote control server 054 * @since 5861 055 */ 056 public static void stop() { 057 RemoteControlHttpServer.stopRemoteControlHttpServer(); 058 RemoteControlHttpsServer.stopRemoteControlHttpsServer(); 059 } 060 061 /** 062 * Adds external request handler. 063 * Can be used by plugins that want to use remote control. 064 * 065 * @param command The command name. 066 * @param handlerClass The additional request handler. 067 */ 068 public void addRequestHandler(String command, Class<? extends RequestHandler> handlerClass) { 069 RequestProcessor.addRequestHandlerClass(command, handlerClass); 070 } 071 072 /** 073 * Returns the remote control directory. 074 * @return The remote control directory 075 * @since 7335 076 */ 077 public static String getRemoteControlDir() { 078 return new File(Main.pref.getUserDataDirectory(), "remotecontrol").getAbsolutePath(); 079 } 080 081 /** 082 * Returns the IPv6 address used for remote control. 083 * @return the IPv6 address used for remote control 084 * @throws UnknownHostException if the local host name could not be resolved into an address. 085 * @since 8337 086 */ 087 public static InetAddress getInet6Address() throws UnknownHostException { 088 for (InetAddress a : InetAddress.getAllByName(Main.pref.get("remote.control.host.ipv6", "::1"))) { 089 if (a instanceof Inet6Address) { 090 return a; 091 } 092 } 093 throw new UnknownHostException(); 094 } 095 096 /** 097 * Returns the IPv4 address used for remote control. 098 * @return the IPv4 address used for remote control 099 * @throws UnknownHostException if the local host name could not be resolved into an address. 100 * @since 8337 101 */ 102 public static InetAddress getInet4Address() throws UnknownHostException { 103 // Return an address to the loopback interface by default 104 for (InetAddress a : InetAddress.getAllByName(Main.pref.get("remote.control.host.ipv4", "127.0.0.1"))) { 105 if (a instanceof Inet4Address) { 106 return a; 107 } 108 } 109 throw new UnknownHostException(); 110 } 111}