Skip to content

Commit

Permalink
Merge pull request #2725 from sixlettervariables/issue-2722-hide-ip-a…
Browse files Browse the repository at this point in the history
…ddresses

Issue #2722: Do not send IP addresses in chat by default
  • Loading branch information
sixlettervariables committed Mar 18, 2021
2 parents a7e2f6f + bced6f6 commit 939ee95
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
9 changes: 9 additions & 0 deletions megamek/src/megamek/common/preference/ClientPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ClientPreferences extends PreferenceStoreProxy implements
store.setDefault(MAP_HEIGHT, 1);
store.setDefault(DEBUG_OUTPUT_ON,false);
store.setDefault(MEMORY_DUMP_ON,false);
store.setDefault(IP_ADDRESSES_IN_CHAT, false);
setLocale(store.getString(LOCALE));
setMekHitLocLog();
}
Expand Down Expand Up @@ -255,6 +256,14 @@ public void setGUIName(String guiName) {
store.setValue(GUI_NAME, guiName);
}

public boolean getShowIPAddressesInChat() {
return store.getBoolean(IP_ADDRESSES_IN_CHAT);
}

public void setShowIPAddressesInChat(boolean value) {
store.setValue(IP_ADDRESSES_IN_CHAT, value);
}

protected Locale locale = null;

public void setLocale(String l) {
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/common/preference/IClientPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public interface IClientPreferences extends IPreferenceStore {
public static final String BOARD_HEIGHT = "BoardHeight";
public static final String MAP_WIDTH = "MapWidth";
public static final String MAP_HEIGHT = "MapHeight";
public static final String IP_ADDRESSES_IN_CHAT = "IPAddressesInChat";

boolean getPrintEntityChange();

Expand Down Expand Up @@ -163,4 +164,6 @@ public interface IClientPreferences extends IPreferenceStore {

int getMapHeight();

boolean getShowIPAddressesInChat();

}
26 changes: 18 additions & 8 deletions megamek/src/megamek/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -902,28 +902,38 @@ private void receivePlayerName(Packet packet, int connId) {
// send current game info
sendCurrentInfo(connId);

final boolean showIPAddressesInChat = PreferenceManager.getClientPreferences().getShowIPAddressesInChat();

try {
InetAddress[] addresses = InetAddress.getAllByName(InetAddress
.getLocalHost().getHostName());
for (InetAddress addresse : addresses) {
sendServerChat(connId,
"Machine IP is " + addresse.getHostAddress());
for (InetAddress address : addresses) {
MegaMek.getLogger().info("s: machine IP " + address.getHostAddress());
if (showIPAddressesInChat) {
sendServerChat(connId,
"Machine IP is " + address.getHostAddress());
}
}
} catch (UnknownHostException e) {
// oh well.
}

// Send the port we're listening on. Only useful for the player
// on the server machine to check.
sendServerChat(connId,
"Listening on port " + serverSocket.getLocalPort());
MegaMek.getLogger().info("s: listening on port " + serverSocket.getLocalPort());
if (showIPAddressesInChat) {
// Send the port we're listening on. Only useful for the player
// on the server machine to check.
sendServerChat(connId,
"Listening on port " + serverSocket.getLocalPort());
}

// Get the player *again*, because they may have disconnected.
player = getPlayer(connId);
if (null != player) {
String who = player.getName() + " connected from " + getClient(connId).getInetAddress();
MegaMek.getLogger().info("s: player #" + connId + ", " + who);
sendServerChat(who);
if (showIPAddressesInChat) {
sendServerChat(who);
}

} // Found the player

Expand Down
38 changes: 18 additions & 20 deletions megamek/src/megamek/server/commands/WhoCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* MegaMek - Copyright (C) 2000-2002 Ben Mazur (bmazur@sev.org)
* - Copyright (C) 2021 The MegaMek Team
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
Expand All @@ -12,24 +13,16 @@
* for more details.
*/

/*
* WhoCommand.java
*
* Created on March 30, 2002, 7:35 PM
*/

package megamek.server.commands;

import java.util.Enumeration;

import megamek.common.net.IConnection;
import megamek.common.preference.PreferenceManager;
import megamek.server.Server;

/**
* Lists all the players connected and some info about them.
*
* @author Ben
* @version
*/
public class WhoCommand extends ServerCommand {

Expand All @@ -42,21 +35,26 @@ public WhoCommand(Server server) {
@Override
public void run(int connId, String[] args) {
server.sendServerChat(connId, "Listing all connections...");
server
.sendServerChat(connId,
"[id#] : [name], [address], [pending], [bytes sent], [bytes received]");
server.sendServerChat(
connId, "[id#] : [name], [address], [pending], [bytes sent], [bytes received]");

final boolean includeIPAddress = PreferenceManager.getClientPreferences().getShowIPAddressesInChat();
for (Enumeration<IConnection> i = server.getConnections(); i.hasMoreElements();) {
IConnection conn = i.nextElement();
StringBuffer cb = new StringBuffer();
cb.append(conn.getId()).append(" : ");
cb.append(server.getPlayer(conn.getId()).getName()).append(", ");
cb.append(conn.getInetAddress());
cb.append(", ").append(conn.hasPending()).append(", ");
cb.append(conn.bytesSent());
cb.append(", ").append(conn.bytesReceived());
server.sendServerChat(connId, cb.toString());
server.sendServerChat(connId, getConnectionDescription(conn, includeIPAddress));
}

server.sendServerChat(connId, "end list");
}

private String getConnectionDescription(IConnection conn, boolean includeIPAddress) {
StringBuilder cb = new StringBuilder();
cb.append(conn.getId()).append(" : ");
cb.append(server.getPlayer(conn.getId()).getName()).append(", ");
cb.append(includeIPAddress ? conn.getInetAddress() : "<hidden>");
cb.append(", ").append(conn.hasPending()).append(", ");
cb.append(conn.bytesSent());
cb.append(", ").append(conn.bytesReceived());
return cb.toString();
}
}

0 comments on commit 939ee95

Please sign in to comment.