コード例 #1
0
        //Listen incoming connections.
        private void listenThread()
        {
            Socket serverSock = null;

            try {
                int        recvNum;
                byte[]     data    = new byte[10240];
                IPEndPoint localIp = null;
                foreach (IPAddress ipa in IpEntry.AddressList)
                {
                    if (ipa.AddressFamily == AddressFamily.InterNetwork)
                    {
                        localIp = new IPEndPoint(ipa, bindPort);
                        break;
                    }
                }
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);

                if (MainWindow.activicePort.Contains(bindPort))
                {
                    MessageBox.Show("Port has been used.");
                    throw new Exception();
                }

                serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                serverSock.Bind(localIp);
                Success = true;
                MainWindow.activicePort.Add(bindPort);
                sockets.Add(serverSock);
                EndPoint server = serverEndPoint;
                EndPoint remote = new IPEndPoint(IPAddress.Any, 0);

                int port = 23000;

                while (true)
                {
                    ArrayList denies = dt.read();
                    if (port > 23500)
                    {
                        port = 23000;
                    }
                    recvNum = serverSock.ReceiveFrom(data, ref remote);
                    Lt.write("********************************");
                    Lt.write("Time: " + DateTime.Now.ToString());
                    Lt.write("Connect IP: " + (remote as IPEndPoint).Address.ToString());
                    Lt.write("Connect port: " + (remote as IPEndPoint).Port.ToString());
                    Lt.write("Destination Port: " + bindPort);
                    Lt.write("Protocol: UDP");
                    if (denies.Contains((remote as IPEndPoint).Address.ToString()))
                    {
                        Lt.write("Operation: Reject");
                        Lt.write("********************************");
                        Console.WriteLine("Message received from " + remote.ToString() + "\t reject.");
                        continue;
                    }
                    Lt.write("Operation: Accept");
                    Lt.write("********************************");
                    if (!remote.ToString().Equals(server.ToString()))
                    {
                        while (proxyTable.ContainsKey(port))
                        {
                            port++;
                        }
                        IPEndPoint IPRemote = remote as IPEndPoint;
                        string     ip       = IPRemote.Address.ToString();
                        int        tarPort  = IPRemote.Port;
                        proxyTable.Add(port, IPRemote);
                        Thread   proxy = new Thread(proxyThread);
                        object[] param = new object[5] {
                            port, data, server, tarPort, recvNum
                        };
                        port++;
                        proxy.Start(param);
                    }
                }
            } catch (Exception ex) {
            } finally {
                if (serverSock != null)
                {
                    serverSock.Close();
                }
                Success = false;
                MainWindow.activicePort.Remove(bindPort);
                stop();
            }
        }
コード例 #2
0
ファイル: TCPFirewall.cs プロジェクト: UncleYi2016/Firewall
        //Listen incoming connections.
        private void listenThread()
        {
            Socket clientSocket  = null;
            string clientAddrStr = null;

            try {
                if (MainWindow.activicePort.Contains(bindingPort))
                {
                    MessageBox.Show("Port has been used.");
                    throw new Exception();
                }
                Socket hostSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                foreach (IPAddress ipa in IpEntry.AddressList)
                {
                    if (ipa.AddressFamily == AddressFamily.InterNetwork)
                    {
                        IPEndPoint ipe = new IPEndPoint(ipa, bindingPort);
                        hostSocket.Bind(ipe);
                        break;
                    }
                }
                hostSocket.Listen(10);
                sockets.Add(hostSocket);
                Success = true;
                MainWindow.activicePort.Add(bindingPort);

                while (true)
                {
                    clientSocket = hostSocket.Accept();
                    ArrayList  denies    = Dt.read();
                    IPEndPoint clientEnd = clientSocket.RemoteEndPoint as IPEndPoint;
                    Lt.write("********************************");
                    Lt.write("Time: " + DateTime.Now.ToString());
                    Lt.write("Connect IP: " + clientEnd.Address.ToString());
                    Lt.write("Connect port: " + clientEnd.Port.ToString());
                    Lt.write("Destination Port: " + bindingPort);
                    Lt.write("Protocol: TCP");
                    if (denies.Contains(clientEnd.Address.ToString()))
                    {
                        clientSocket.Close();
                        Lt.write("Operation: Reject");
                        Lt.write("********************************");
                        continue;
                    }
                    Lt.write("Operation: Accept");
                    Lt.write("********************************");
                    sockets.Add(clientSocket);
                    Count++;
                    IPAddress ip           = IPAddress.Parse(ServerIP);
                    Socket    serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    serverSocket.Connect(new IPEndPoint(ip, ServerPort)); //配置服务器IP与端口
                    sockets.Add(serverSocket);
                    clientSocket.SendTimeout    = 5000;
                    serverSocket.ReceiveTimeout = 5000;
                    Socket[] socketPair = { clientSocket, serverSocket };
                    clientAddrStr = clientEnd.Address + ": " + clientEnd.Port;
                    try {
                        if (!MainWindow.connectStatuses.ContainsKey(clientAddrStr))
                        {
                            MainWindow.connectStatuses.Add(clientAddrStr, "0");
                        }
                    } catch (IndexOutOfRangeException indexEx) {
                    }
                    Thread receiveThread = new Thread(receiveMessage);
                    Thread sendThread    = new Thread(sendMessage);
                    receiveThread.Start(socketPair);
                    receiveThread.Name = "receive:" + clientEnd.Port;
                    sendThread.Start(socketPair);
                    sendThread.Name = "send:" + clientEnd.Port;
                }
            } catch (Exception ex) {
                //MessageBox.Show("Proxy falied!");
                Count--;
                if (MainWindow.connectStatuses.ContainsKey(clientAddrStr))
                {
                    MainWindow.connectStatuses.Remove(clientAddrStr);
                }
                if (clientSocket != null && clientSocket.Connected)
                {
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            } finally {
                Success = false;
                MainWindow.activicePort.Remove(bindingPort);
                stop();
            }
        }