public static boolean isAllowed(Connection connection) { if (!allowedIPs.isEmpty()) { // The server is using a whitelist so check that the IP address of the client // is authorized to connect to the server boolean forbidAccess = false; try { if (!allowedIPs.containsKey(connection.getHostAddress())) { byte[] address = connection.getAddress(); String range1 = (address[0] & 0xff) + "." + (address[1] & 0xff) + "." + (address[2] & 0xff) + ".*"; String range2 = (address[0] & 0xff) + "." + (address[1] & 0xff) + ".*.*"; String range3 = (address[0] & 0xff) + ".*.*.*"; if (!allowedIPs.containsKey(range1) && !allowedIPs.containsKey(range2) && !allowedIPs.containsKey(range3)) { forbidAccess = true; } } } catch (UnknownHostException e) { forbidAccess = true; } return !forbidAccess; } return true; }
/** * Creates a new client session that was established to the specified connection manager. The new * session will not be findable through its stream ID. * * @param connectionManagerDomain the connection manager that is handling the connection of the * session. * @param streamID the stream ID created by the connection manager for the new session. * @param hostName the address's hostname of the client or null if using old connection manager. * @param hostAddress the textual representation of the address of the client or null if using old * CM. * @return true if a session was created or false if the client should disconnect. */ public boolean createClientSession( String connectionManagerDomain, String streamID, String hostName, String hostAddress) { Connection connection = new ClientSessionConnection(connectionManagerDomain, hostName, hostAddress); // Check if client is allowed to connect from the specified IP address. Ignore the checking if // connection // manager is old version and is not passing client's address byte[] address = null; try { address = connection.getAddress(); } catch (UnknownHostException e) { // Ignore } if (address == null || LocalClientSession.isAllowed(connection)) { LocalClientSession session = SessionManager.getInstance().createClientSession(connection, new BasicStreamID(streamID)); // Register that this streamID belongs to the specified connection manager streamIDs.put(streamID, connectionManagerDomain); // Register which sessions are being hosted by the speicifed connection manager Map<String, LocalClientSession> sessions = sessionsByManager.get(connectionManagerDomain); if (sessions == null) { synchronized (connectionManagerDomain.intern()) { sessions = sessionsByManager.get(connectionManagerDomain); if (sessions == null) { sessions = new ConcurrentHashMap<>(); sessionsByManager.put(connectionManagerDomain, sessions); } } } sessions.put(streamID, session); return true; } return false; }