Search in sources :

Example 1 with IServerMessenger

use of games.strategy.net.IServerMessenger in project triplea by triplea-game.

the class HeadlessGameServer method remoteBanPlayer.

public String remoteBanPlayer(final String playerName, final int hours, final String hashedPassword, final String salt) {
    final String password = System.getProperty(LOBBY_GAME_SUPPORT_PASSWORD, "");
    if (password.equals(GameRunner.NO_REMOTE_REQUESTS_ALLOWED)) {
        return "Host not accepting remote requests!";
    }
    final String localPassword = System.getProperty(LOBBY_GAME_SUPPORT_PASSWORD, "");
    // milliseconds (30 days max)
    final Instant expire = Instant.now().plus(Duration.ofHours(Math.min(24 * 30, hours)));
    if (hashPassword(localPassword, salt).equals(hashedPassword)) {
        new Thread(() -> {
            if (getServerModel() == null) {
                return;
            }
            final IServerMessenger messenger = getServerModel().getMessenger();
            if (messenger == null) {
                return;
            }
            final Set<INode> nodes = messenger.getNodes();
            if (nodes == null) {
                return;
            }
            try {
                for (final INode node : nodes) {
                    final String realName = node.getName().split(" ")[0];
                    final String ip = node.getAddress().getHostAddress();
                    final String mac = messenger.getPlayerMac(node.getName());
                    if (realName.equals(playerName)) {
                        System.out.println("Remote Ban of Player: " + playerName);
                        try {
                            messenger.notifyUsernameMiniBanningOfPlayer(realName, expire);
                        } catch (final Exception e) {
                            logger.log(Level.SEVERE, "Failed to notify username ban of player", e);
                        }
                        try {
                            messenger.notifyIpMiniBanningOfPlayer(ip, expire);
                        } catch (final Exception e) {
                            logger.log(Level.SEVERE, "Failed to notify IP ban of player", e);
                        }
                        try {
                            messenger.notifyMacMiniBanningOfPlayer(mac, expire);
                        } catch (final Exception e) {
                            logger.log(Level.SEVERE, "Failed to notify MAC ban of player", e);
                        }
                        messenger.removeConnection(node);
                    }
                }
            } catch (final Exception e) {
                logger.log(Level.SEVERE, "Failed to notify ban of player", e);
            }
        }).start();
        return null;
    }
    System.out.println("Attempted remote ban player with invalid password.");
    return "Invalid password!";
}
Also used : INode(games.strategy.net.INode) Set(java.util.Set) HashSet(java.util.HashSet) Instant(java.time.Instant) IServerMessenger(games.strategy.net.IServerMessenger)

Example 2 with IServerMessenger

use of games.strategy.net.IServerMessenger in project triplea by triplea-game.

the class VaultTest method testLocal.

@Test
public void testLocal() throws Exception {
    final IServerMessenger messenger = mock(IServerMessenger.class);
    when(messenger.getLocalNode()).thenReturn(new Node("dummy", InetAddress.getLocalHost(), 0));
    final UnifiedMessenger unifiedMessenger = new UnifiedMessenger(messenger);
    final ChannelMessenger channelMessenger = new ChannelMessenger(unifiedMessenger);
    // RemoteMessenger remoteMessenger = new RemoteMessenger(unifiedMessenger);
    final Vault vault = new Vault(channelMessenger);
    final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
    final VaultID id = vault.lock(data);
    vault.unlock(id);
    assertArrayEquals(data, vault.get(id));
    vault.release(id);
}
Also used : UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) Node(games.strategy.net.Node) IServerMessenger(games.strategy.net.IServerMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) Test(org.junit.jupiter.api.Test)

Example 3 with IServerMessenger

use of games.strategy.net.IServerMessenger in project triplea by triplea-game.

the class InGameLobbyWatcher method newInGameLobbyWatcher.

/**
 * Reads SystemProperties to see if we should connect to a lobby server
 *
 * <p>
 * After creation, those properties are cleared, since we should watch the first start game.
 * </p>
 *
 * @return null if no watcher should be created
 */
public static InGameLobbyWatcher newInGameLobbyWatcher(final IServerMessenger gameMessenger, final JComponent parent, final InGameLobbyWatcher oldWatcher) {
    final String host = System.getProperty(LOBBY_HOST);
    final String port = System.getProperty(LOBBY_PORT);
    final String hostedBy = System.getProperty(LOBBY_GAME_HOSTED_BY);
    if (host == null || port == null) {
        return null;
    }
    // clear the properties
    System.clearProperty(LOBBY_HOST);
    System.clearProperty(LOBBY_PORT);
    System.clearProperty(LOBBY_GAME_HOSTED_BY);
    // add them as temporary properties (in case we load an old savegame and need them again)
    System.setProperty(LOBBY_HOST + GameRunner.OLD_EXTENSION, host);
    System.setProperty(LOBBY_PORT + GameRunner.OLD_EXTENSION, port);
    System.setProperty(LOBBY_GAME_HOSTED_BY + GameRunner.OLD_EXTENSION, hostedBy);
    final IConnectionLogin login = challengeProperties -> {
        final Map<String, String> properties = new HashMap<>();
        properties.put(LobbyLoginValidator.ANONYMOUS_LOGIN, Boolean.TRUE.toString());
        properties.put(LobbyLoginValidator.LOBBY_VERSION, LobbyServer.LOBBY_VERSION.toString());
        properties.put(LobbyLoginValidator.LOBBY_WATCHER_LOGIN, Boolean.TRUE.toString());
        return properties;
    };
    try {
        System.out.println("host:" + host + " port:" + port);
        final String mac = MacFinder.getHashedMacAddress();
        final ClientMessenger messenger = new ClientMessenger(host, Integer.parseInt(port), getRealName(hostedBy) + "_" + LOBBY_WATCHER_NAME, mac, login);
        final UnifiedMessenger um = new UnifiedMessenger(messenger);
        final RemoteMessenger rm = new RemoteMessenger(um);
        final RemoteHostUtils rhu = new RemoteHostUtils(messenger.getServerNode(), gameMessenger);
        rm.registerRemote(rhu, RemoteHostUtils.getRemoteHostUtilsName(um.getLocalNode()));
        return new InGameLobbyWatcher(messenger, rm, gameMessenger, parent, oldWatcher);
    } catch (final Exception e) {
        ClientLogger.logQuietly("Failed to create in-game lobby watcher", e);
        return null;
    }
}
Also used : Observer(java.util.Observer) ClientMessenger(games.strategy.net.ClientMessenger) LOBBY_GAME_COMMENTS(games.strategy.engine.framework.CliProperties.LOBBY_GAME_COMMENTS) GUID(games.strategy.net.GUID) SERVER_PASSWORD(games.strategy.engine.framework.CliProperties.SERVER_PASSWORD) HashMap(java.util.HashMap) HeadlessGameServer(games.strategy.engine.framework.headlessGameServer.HeadlessGameServer) IConnectionLogin(games.strategy.net.IConnectionLogin) LobbyServer(games.strategy.engine.lobby.server.LobbyServer) MacFinder(games.strategy.net.MacFinder) IMessengerErrorListener(games.strategy.net.IMessengerErrorListener) SwingUtilities(javax.swing.SwingUtilities) Map(java.util.Map) IMessenger(games.strategy.net.IMessenger) GameDescription(games.strategy.engine.lobby.server.GameDescription) IConnectionChangeListener(games.strategy.net.IConnectionChangeListener) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ClientContext(games.strategy.engine.ClientContext) JComponent(javax.swing.JComponent) LobbyLoginValidator(games.strategy.engine.lobby.server.login.LobbyLoginValidator) INode(games.strategy.net.INode) Frame(java.awt.Frame) ILobbyGameController(games.strategy.engine.lobby.server.ILobbyGameController) LOBBY_HOST(games.strategy.engine.framework.CliProperties.LOBBY_HOST) GameStepListener(games.strategy.engine.data.events.GameStepListener) GameStatus(games.strategy.engine.lobby.server.GameDescription.GameStatus) Instant(java.time.Instant) JOptionPane(javax.swing.JOptionPane) GameRunner(games.strategy.engine.framework.GameRunner) RemoteHostUtils(games.strategy.engine.lobby.server.RemoteHostUtils) TRIPLEA_PORT(games.strategy.engine.framework.CliProperties.TRIPLEA_PORT) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) ClientLogger(games.strategy.debug.ClientLogger) GameSelectorModel(games.strategy.engine.framework.startup.mc.GameSelectorModel) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) LOBBY_PORT(games.strategy.engine.framework.CliProperties.LOBBY_PORT) OpenFileUtility(games.strategy.net.OpenFileUtility) IServerMessenger(games.strategy.net.IServerMessenger) UrlConstants(games.strategy.triplea.UrlConstants) IGame(games.strategy.engine.framework.IGame) LOBBY_GAME_HOSTED_BY(games.strategy.engine.framework.CliProperties.LOBBY_GAME_HOSTED_BY) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) RemoteHostUtils(games.strategy.engine.lobby.server.RemoteHostUtils) HashMap(java.util.HashMap) Map(java.util.Map) IConnectionLogin(games.strategy.net.IConnectionLogin) ClientMessenger(games.strategy.net.ClientMessenger)

Example 4 with IServerMessenger

use of games.strategy.net.IServerMessenger in project triplea by triplea-game.

the class ClientLoginIntegrationTest method newServerMessenger.

private static IServerMessenger newServerMessenger() throws Exception {
    final ServerMessenger serverMessenger = new ServerMessenger("server", 0);
    serverMessenger.setAcceptNewConnections(true);
    serverMessenger.setLoginValidator(newLoginValidator(serverMessenger));
    return serverMessenger;
}
Also used : ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger)

Example 5 with IServerMessenger

use of games.strategy.net.IServerMessenger in project triplea by triplea-game.

the class StatusTest method testStatus.

@Test
public void testStatus() throws Exception {
    final IServerMessenger messenger = mock(IServerMessenger.class);
    final INode dummyNode = new Node("dummy", InetAddress.getLocalHost(), 0);
    when(messenger.getLocalNode()).thenReturn(dummyNode);
    when(messenger.getServerNode()).thenReturn(dummyNode);
    when(messenger.isConnected()).thenReturn(true);
    when(messenger.isServer()).thenReturn(true);
    final Messengers messengers = new Messengers(messenger);
    final StatusManager manager = new StatusManager(messengers);
    assertNull(manager.getStatus(messenger.getLocalNode()));
    manager.setStatus("test");
    Interruptibles.sleep(200);
    assertEquals("test", manager.getStatus(messenger.getLocalNode()));
    assertEquals("test", new StatusManager(messengers).getStatus(messenger.getLocalNode()));
}
Also used : INode(games.strategy.net.INode) Messengers(games.strategy.net.Messengers) INode(games.strategy.net.INode) Node(games.strategy.net.Node) IServerMessenger(games.strategy.net.IServerMessenger) Test(org.junit.jupiter.api.Test)

Aggregations

IServerMessenger (games.strategy.net.IServerMessenger)8 INode (games.strategy.net.INode)5 Instant (java.time.Instant)3 UnifiedMessenger (games.strategy.engine.message.unifiedmessenger.UnifiedMessenger)2 Node (games.strategy.net.Node)2 HashSet (java.util.HashSet)2 ClientLogger (games.strategy.debug.ClientLogger)1 ClientContext (games.strategy.engine.ClientContext)1 GameStepListener (games.strategy.engine.data.events.GameStepListener)1 LOBBY_GAME_COMMENTS (games.strategy.engine.framework.CliProperties.LOBBY_GAME_COMMENTS)1 LOBBY_GAME_HOSTED_BY (games.strategy.engine.framework.CliProperties.LOBBY_GAME_HOSTED_BY)1 LOBBY_HOST (games.strategy.engine.framework.CliProperties.LOBBY_HOST)1 LOBBY_PORT (games.strategy.engine.framework.CliProperties.LOBBY_PORT)1 SERVER_PASSWORD (games.strategy.engine.framework.CliProperties.SERVER_PASSWORD)1 TRIPLEA_PORT (games.strategy.engine.framework.CliProperties.TRIPLEA_PORT)1 GameRunner (games.strategy.engine.framework.GameRunner)1 IGame (games.strategy.engine.framework.IGame)1 HeadlessGameServer (games.strategy.engine.framework.headlessGameServer.HeadlessGameServer)1 GameSelectorModel (games.strategy.engine.framework.startup.mc.GameSelectorModel)1 GameDescription (games.strategy.engine.lobby.server.GameDescription)1