Changeset 7800 in josm for trunk


Ignore:
Timestamp:
2014-12-12T20:08:22+01:00 (10 years ago)
Author:
Don-vip
Message:

fix #10833 - robustness to localhost name resolution for remote control

Location:
trunk/src/org/openstreetmap/josm/io/remotecontrol
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java

    r7702 r7800  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io.remotecontrol;
     3
     4import java.net.InetAddress;
     5import java.net.UnknownHostException;
    36
    47import org.openstreetmap.josm.Main;
     
    3437    static final int protocolMajorVersion = 1;
    3538    static final int protocolMinorVersion = 7;
     39
     40    private static final String LOCALHOST = "localhost";
    3641
    3742    /**
     
    7176        return Main.pref.getPreferencesDir() + "remotecontrol/";
    7277    }
     78
     79    /**
     80     * Returns the inet address used for remote control.
     81     * @return the inet address used for remote control
     82     * @throws UnknownHostException if the local host name could not be resolved into an address.
     83     * @since 7800
     84     */
     85    public static InetAddress getInetAddress() throws UnknownHostException {
     86        String hostname = Main.pref.get("remote.control.host", LOCALHOST);
     87        InetAddress result = InetAddress.getByName(hostname);
     88        // Sometimes localhost resolution does not work as expected, see #10833
     89        if (LOCALHOST.equalsIgnoreCase(hostname) && !LOCALHOST.equalsIgnoreCase(result.getHostName())) {
     90            InetAddress localhostAddr = InetAddress.getLocalHost();
     91            // Use this result if it's better. Not sure if it's a Java bug or not
     92            if (LOCALHOST.equalsIgnoreCase(localhostAddr.getHostName())) {
     93                result = localhostAddr;
     94            }
     95        }
     96        return result;
     97    }
    7398}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java

    r7034 r7800  
    66import java.io.IOException;
    77import java.net.BindException;
    8 import java.net.InetAddress;
    98import java.net.ServerSocket;
    109import java.net.Socket;
     
    6867        this.setDaemon(true);
    6968        // Start the server socket with only 1 connection.
    70         // Also make sure we only listen
    71         // on the local interface so nobody from the outside can connect!
     69        // Also make sure we only listen on the local interface so nobody from the outside can connect!
    7270        // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
    73         this.server = new ServerSocket(port, 1,
    74             InetAddress.getByName(Main.pref.get("remote.control.host", "localhost")));
     71        this.server = new ServerSocket(port, 1, RemoteControl.getInetAddress());
    7572    }
    7673
     
    8077    @Override
    8178    public void run() {
    82         Main.info(marktr("RemoteControl::Accepting connections on port {0}"),
    83              Integer.toString(server.getLocalPort()));
     79        Main.info(marktr("RemoteControl::Accepting connections on {0}:{1}"),
     80                server.getInetAddress(), Integer.toString(server.getLocalPort()));
    8481        while (true) {
    8582            try {
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpsServer.java

    r7402 r7800  
    99import java.math.BigInteger;
    1010import java.net.BindException;
    11 import java.net.InetAddress;
    1211import java.net.ServerSocket;
    1312import java.net.Socket;
     
    380379
    381380        // Start the server socket with only 1 connection.
    382         // Also make sure we only listen
    383         // on the local interface so nobody from the outside can connect!
     381        // Also make sure we only listen on the local interface so nobody from the outside can connect!
    384382        // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
    385         this.server = factory.createServerSocket(port, 1,
    386             InetAddress.getByName(Main.pref.get("remote.control.host", "localhost")));
     383        this.server = factory.createServerSocket(port, 1, RemoteControl.getInetAddress());
    387384
    388385        if (Main.isTraceEnabled() && server instanceof SSLServerSocket) {
     
    402399    @Override
    403400    public void run() {
    404         Main.info(marktr("RemoteControl::Accepting secure connections on port {0}"),
    405              Integer.toString(server.getLocalPort()));
     401        Main.info(marktr("RemoteControl::Accepting secure connections on {0}:{1}"),
     402                server.getInetAddress(), Integer.toString(server.getLocalPort()));
    406403        while (true) {
    407404            try {
Note: See TracChangeset for help on using the changeset viewer.