Example #1
0
 public void freeNode(NodeStatus node)
 {
     lock (nodeLock)
     {
         node.InUse = false;
     }
 }
Example #2
0
        protected virtual void OnNodeUpdated(NodeStatus nodeStatus)
        {
            NodeStatusEventArgs e = new NodeStatusEventArgs(nodeStatus);
            var handler           = NodeStatusChanged;

            if (handler != null)
            {
                NodeStatusChanged(this, e);
            }
        }
Example #3
0
        private void Run()
        {
            Int32 port = Convert.ToInt32(Properties.Settings.Default.port);

            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(IPAddress.Any, port));
            server.Listen(4);
            while (true)
            {
                OnStatusMessageUpdated("Waiting for connection.", StatusMessageEventArgs.MessageType.LogInfo);
                try
                {
                    Socket socket = server.Accept();
                    Console.WriteLine("After accept.");
                    NodeStatus node = getNextNode();
                    string     ip   = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString();
                    // HACK
                    if (ip == "202.39.236.116")
                    {
                        // This IP has been bad. Blacklist it until proper filtering is added.
                        OnStatusMessageUpdated("Attempt from blacklisted IP.", StatusMessageEventArgs.MessageType.LogInfo);
                        Thread.Sleep(1000);
                        node = null;
                    }
                    OnStatusMessageUpdated("Connection from " + ip, StatusMessageEventArgs.MessageType.Connect);
                    if (node != null)
                    {
                        node.RemoteAddress = ip;
                        OnStatusMessageUpdated("Launching Node #" + node.Node, StatusMessageEventArgs.MessageType.LogInfo);
                        Thread instanceThread = new Thread(() => LaunchInstance(node, socket));
                        instanceThread.Name = "Instance #" + node.Node;
                        instanceThread.Start();
                        OnNodeUpdated(node);
                    }
                    else
                    {
                        // Send BUSY signal.
                        OnStatusMessageUpdated("Sending Busy Signal.", StatusMessageEventArgs.MessageType.Status);
                        byte[] busy = System.Text.Encoding.ASCII.GetBytes("BUSY");
                        try
                        {
                            socket.Send(busy);
                        }
                        finally
                        {
                            socket.Close();
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
Example #4
0
        private void LaunchInstance(NodeStatus node, Socket socket)
        {
            try
            {
                var executable = (node.NodeType == NodeType.BBS)
          ? Properties.Settings.Default.executable : Properties.Settings.Default.binkpExecutable;

                var homeDirectory = Properties.Settings.Default.homeDirectory;

                Launcher launcher     = new Launcher(executable, homeDirectory, argumentsTemplate, DebugLog);
                var      socketHandle = socket.Handle.ToInt32();
                using (Process p = launcher.launchSocketNode(node.Node, socketHandle))
                {
                    if (p != null)
                    {
                        p.WaitForExit();
                    }
                }
            }
            catch (Exception e)
            {
                DebugLog(e.ToString());
            }
            finally
            {
                try
                {
                    // Let's try to shutdown in a try..catch incase it's already closed.
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
                catch (SocketException e)
                {
                    Debug.WriteLine(e.ToString());
                }
                finally
                {
                    nodeManager.freeNode(node);
                    OnNodeUpdated(node);

                    // Decrement the # active sessions.
                    DecrementConnection(node.RemoteAddress);
                }
            }
        }
Example #5
0
 public NodeStatusEventArgs(NodeStatus status)
 {
   this.node = status.Node;
   this.status = status.Status;
   this.remoteAddress = status.RemoteAddress;
 }
Example #6
0
 protected virtual void OnNodeUpdated(NodeStatus nodeStatus)
 {
   NodeStatusEventArgs e = new NodeStatusEventArgs(nodeStatus);
   var handler = NodeStatusChanged;
   if (handler != null)
   {
     NodeStatusChanged(this, e);
   }
 }
Example #7
0
    private void LaunchInstance(NodeStatus node, Socket socket)
    {
      try
      {
        var executable = (node.NodeType == NodeType.BBS)
          ? Properties.Settings.Default.executable : Properties.Settings.Default.binkpExecutable;

        var homeDirectory = Properties.Settings.Default.homeDirectory;

        Launcher launcher = new Launcher(executable, homeDirectory, argumentsTemplate, DebugLog);
        var socketHandle = socket.Handle.ToInt32();
        using (Process p = launcher.launchSocketNode(node.Node, socketHandle))
        {
          if (p != null)
          {
            p.WaitForExit();
          }
        }
      }
      catch (Exception e)
      {
        DebugLog(e.ToString());
      }
      finally
      {
        try
        {
          // Let's try to shutdown in a try..catch incase it's already closed.
          socket.Shutdown(SocketShutdown.Both);
          socket.Close();
        }
        catch (SocketException e)
        {
          Debug.WriteLine(e.ToString());
        }
        finally
        {
          nodeManager.freeNode(node);
          OnNodeUpdated(node);
        }
      }
    }
 public NodeStatusEventArgs(NodeStatus status)
 {
     this.node          = status.Node;
     this.status        = status.Status;
     this.remoteAddress = status.RemoteAddress;
 }
Example #9
0
        private void LaunchInstance(NodeStatus node, Socket socket)
        {
            try
            {
                var executable = Properties.Settings.Default.executable;
                var homeDirectory = Properties.Settings.Default.homeDirectory;

                Launcher launcher = new Launcher(executable, homeDirectory, argumentsTemplate, DebugLog);
                var socketHandle = socket.Handle.ToInt32();
                Process p = launcher.launchTelnetNode(node.Node, socketHandle);
                p.WaitForExit();
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                nodeManager.freeNode(node);
                OnNodeUpdated(node);
            }
        }
Example #10
0
        private void Run()
        {
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(IPAddress.Any, port));
            server.Listen(4);

            while (true)
            {
                Debug.WriteLine("Waiting for connection.");
                string ip = "";
                try
                {
                    Socket socket = server.Accept();
                    ip = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString();
                    Debug.WriteLine("After accept from IP: " + ip);
                    OnStatusMessageUpdated(this.name + " from " + ip, StatusMessageEventArgs.MessageType.Connect);

                    if (IncrementConcurrentSession(ip))
                    {
                        SendBusyAndCloseSocket(socket);
                        continue;
                    }

                    if (!CanConnect(socket))
                    {
                        DecrementConnection(ip);
                        continue;
                    }

                    // Grab a node # after we've cleared everything else.
                    NodeStatus node = nodeManager.getNextNode(this.nodeType);
                    if (node == null)
                    {
                        // NO node available.
                        Log("No node available.");
                        SendBusyAndCloseSocket(socket);
                        DecrementConnection(ip);
                        continue;
                    }

                    node.RemoteAddress = ip;
                    Thread        instanceThread = new Thread(() => LaunchInstance(node, socket));
                    StringBuilder n = new StringBuilder();
                    if (node.NodeType == NodeType.BBS)
                    {
                        n.Append("Instance #").Append(node.Node);
                    }
                    else if (node.NodeType == NodeType.BINKP)
                    {
                        n.Append("BinkP");
                    }
                    instanceThread.Name = n.ToString();
                    instanceThread.Start();
                    OnNodeUpdated(node);
                }
                catch (SocketException e)
                {
                    DecrementConnection(ip);
                    DebugLog("Exception" + e.ToString());
                }
                catch (ThreadAbortException)
                {
                    DecrementConnection(ip);
                    Debug.WriteLine("Server Exiting normally...");
                    return;
                }
                catch (Exception e)
                {
                    DecrementConnection(ip);
                    DebugLog("Exception" + e.ToString());
                }
            }
        }
Example #11
0
        private void LaunchInstance(NodeStatus node, Socket socket)
        {
            try
            {
                var executable = Properties.Settings.Default.executable;
                // Detect Port 22 SSH or Use Telnet
                string bbsProperties;
                if (isSSH != "1")
                {
                    bbsProperties = Properties.Settings.Default.parameters;
                }
                else
                {
                    bbsProperties = Properties.Settings.Default.parameters2;
                }
                var argumentsTemplate = bbsProperties;
                var homeDirectory = Properties.Settings.Default.homeDirectory;

                Launcher launcher = new Launcher(executable, homeDirectory, argumentsTemplate, DebugLog);
                var socketHandle = socket.Handle.ToInt32();
                Process p = launcher.launchTelnetNode(node.Node, socketHandle);
                p.WaitForExit();
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                lock (nodeLock)
                {
                    node.InUse = false;
                }
                OnNodeUpdated(node);
            }
        }