bck
Alejandro Acuña
2024-11-11 f1cb4443aede6d4657bdc3396c8914d3a9f4fa93
libraries/server/src/art/servers/controller/ControllerListener.java
@@ -13,6 +13,7 @@
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ControllerListener extends Controller
@@ -24,6 +25,9 @@
    private ListenerImplementation implementation = null;
    private Mutex mutex = new Mutex();
    private String name = null;
    public String mask = "-";
    private HashMap<String, String> hblocked = new HashMap<String, String>();
    public ControllerListener(ConfigurationListener configuration)
    {
@@ -52,6 +56,7 @@
    {
        Shared.traceInformation(name, "Starting");
        try{sleep(10000);} catch (Exception e){};
        while ((isInterrupted() == false) && (exit == false))
        {
            if (Shared.isServerEnabled() == true)
@@ -65,6 +70,27 @@
                    
                    try
                    {
                        boolean connect = true;
                        try
                        {
                            if (socket.getInetAddress().getHostAddress().indexOf(mask) >= 0)
                            {
                                connect = false;
                                socket.close();
                            }
                            else if (hblocked.containsKey(socket.getInetAddress().getHostAddress()) == true)
                            {
                                connect = false;
                                socket.close();
                            }
                        }
                        catch (Exception e)
                        {
                        }
                        if (connect == true)
                        {
                        if ((lconnection.size() <= configuration.connections) && (configuration.connections >= 0))
                        {
                            Connection conexion = new Connection(socket);
@@ -77,6 +103,7 @@
                            socket.close();
                        }
                    }
                    }
                    finally
                    {
                        mutex.releaseWrite();
@@ -85,6 +112,14 @@
                catch (Exception e)
                {
                    disconnect();
                    try
                    {
                        sleep(100);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            else
@@ -181,7 +216,7 @@
            this.address = socket.getInetAddress().getHostAddress();
            this.name = this.address + ":" + socket.getPort();
            this.socket = socket;
            this.socket.setSoTimeout(0);
            this.socket.setSoTimeout(configuration.timeoutConnection);
            this.setName("Connection " + name);
        }
        
@@ -196,7 +231,12 @@
                while ((isInterrupted() == false) && (exit == false))
                {
                    listen(dis, dos);
                    if (hblocked.containsKey(socket.getInetAddress().getHostAddress()) == true)
                    {
                        throw new Exception("BLOCKED");
                    }
                    listen(this.address, dis, dos);
                }
            }
            catch (Exception e)
@@ -244,10 +284,52 @@
    private void listen(String name, DataInputStream dis, DataOutputStream dos) throws Exception
    {
        if (hblocked.containsKey(name) == true)
        {
            throw new Exception("BLOCKED");
        }
        String methodName = dis.readUTF();
        if (hblocked.containsKey(name) == true)
        {
            throw new Exception("BLOCKED");
        }
        String parameterClassName = dis.readUTF();
        if (hblocked.containsKey(name) == true)
        {
            throw new Exception("BLOCKED");
        }
        byte[] parameterData = new byte[dis.readInt()];
        if (hblocked.containsKey(name) == true)
        {
            throw new Exception("BLOCKED");
        }
        dis.readFully(parameterData);
        if ((name.indexOf("10.136.") >= 0) && (this.mask.length() > 2))
        {
            try
            {
                Object objectParameter = Serialization.decompress(Class.forName(parameterClassName), parameterData);
                art.library.interop.InteropParameters parameters = (art.library.interop.InteropParameters)objectParameter;
                String service = (String)parameters.getParameterValue("service");
                if (service.indexOf("transactions_") >= 0)
                {
                    String operation = (String)parameters.getParameterValue("operation");
                    if (operation.equalsIgnoreCase("getDevices") == true)
                    {
                        hblocked.put(name, name);
                        Shared.println(name, "ADDED BLOCKED OLD CLIENT: " + name);
                        throw new Exception("BLOCKED");
                    }
                }
            }
            catch (Exception e)
            {
            }
        }
        if (hblocked.containsKey(name) == true)
        {
            throw new Exception("BLOCKED");
        }
        listen(methodName, parameterClassName, parameterData, dos);     
    }
@@ -337,7 +419,6 @@
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new SerializationException(e.getMessage());
        }
    }