Socket Whitelist IP accepted connection C #

0

I'm trying to make a Socket (Server -> Client), however I want to make a whitelist of ip that can be accepted in the client connection to the server, however I do not know how, Server check if the ip that is in the "whitelist" that will be an array, if ip have the connection the client will be accepted if you do not have the ip the will not connect!.

Server:

        public Client(Socket ClientSocket)
    {
        //Client = this;
        this.ClientSocket = ClientSocket;
        ClientStream = new NetworkStream(ClientSocket);

        EndPoint = (IPEndPoint)ClientSocket.RemoteEndPoint;

        ClientThread = new Thread(ClientCallback);
        ClientThread.Start();

        LogFactory.GetLog(this).LogInfo("Client <{0}> connected to the server!", EndPoint);
    }
    
asked by anonymous 01.11.2017 / 15:42

1 answer

0

Only create a list with the allowed ips, and when the connection arrives check if the list contains the ip that you connected:

    private void teste(Socket ClientSocket)
    {
        List<string> whiteList = new List<string>
        {
            "192.168.0.1",
            "192.168.0.2",
            "192.168.0.3",
            "192.168.0.4",
            "192.168.0.5",
            "192.168.0.6"                
        };

        string _ip = ((IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString();

        if (whiteList.Contains(_ip))
        {
            //permite
        }
        else
        {
            ClientSocket.Disconnect(false);
        }

    }
    
01.11.2017 / 16:46