Exemple #1
0
        static void Main(string[] args)
        {
            HttpToCtpLogger.logger      = Console.Out;
            HttpToCtpLogger.errorLogger = Console.Out;

            //
            // Options
            //
            Int32 backlog = 32;

            Int32     listenPort = 8080;
            IPAddress listenIP   = null;

            //HttpToCtp.proxySelector = new SingleConnectorSelector(
            //    new GatewayProxy(new DnsEndPoint("proxy.houston.hp.com", 8080, true)));

            IPEndPoint listenEndPoint = (listenIP == null) ?
                                        new IPEndPoint(IPAddress.Any, listenPort) :
                                        new IPEndPoint(listenIP, listenPort);

            SelectServer selectServer = new SelectServer(false, new Buf(4096, 4096));

            {
                Socket listenSock = new Socket(listenEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                listenSock.Bind(listenEndPoint);
                listenSock.Listen(backlog);

                HttpToCtpSelectServerHandler server = new HttpToCtpSelectServerHandler();
                selectServer.control.AddReceiveSocket(listenSock, server.AcceptCallback);
            }

            selectServer.Run();
        }
Exemple #2
0
        static void AddRpcServer(SelectServer selectServer, EndPoint endPoint, RpcServerHandler handler, int backlog)
        {
            Socket tcpAcceptSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            tcpAcceptSocket.Bind(endPoint);
            tcpAcceptSocket.Listen(backlog);
            selectServer.control.AddListenSocket(tcpAcceptSocket, handler.AcceptCallback);

            Socket udpSocket = new Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

            udpSocket.Bind(endPoint);
            selectServer.control.AddReceiveSocket(udpSocket, handler.DatagramRecvHandler);
        }
        public static void Main(String[] args)
        {
            WebServerOptions optionsParser = new WebServerOptions();
            List <String>    nonOptionArgs = optionsParser.Parse(args);

            if (nonOptionArgs.Count == 0)
            {
                Console.WriteLine("Error: missing root-path");
                return;
            }

            if (nonOptionArgs.Count > 1)
            {
                Console.WriteLine("Error: too many arguments");
                return;
            }

            RootDirectory = nonOptionArgs[0];
            if (!Directory.Exists(RootDirectory))
            {
                Console.WriteLine("Error: root directory '{0}' does not exist", RootDirectory);
                return;
            }

            //fileHandler = new DefaultFileResourceHandler(
            //    UrlTranslator, optionsParser.defaultIndexFile.ArgValue);

            /*
             * ExtensionFilteredResourceHandler extensionHandler =
             *  new ExtensionFilteredResourceHandler(defaultFileHandler);
             *
             * extensionHandler.AddExtensionHandler("bat", new BatchResourceHandler(
             *  urlTranslator, TimeSpan.FromSeconds(20)));
             */

            SelectServer server = new SelectServer(false, new Buf(1024, 1024));

            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listenSocket.Bind(new IPEndPoint(IPAddress.Any, optionsParser.port.ArgValue));
            listenSocket.Listen(optionsParser.socketBackLog.ArgValue);
            server.control.AddListenSocket(listenSocket, AcceptCallback);

            server.Run();
        }
        static void Main(string[] args)
        {
            AppLayerProxy.Logger      = Console.Out;
            AppLayerProxy.ErrorLogger = Console.Out;

            var options       = new AppLayerProxyOptions();
            var nonOptionArgs = options.Parse(args);

            if (nonOptionArgs.Count == 0 || options.help.set)
            {
                options.PrintUsage();
                return;
            }
            if (nonOptionArgs.Count > 1)
            {
                options.ErrorAndUsage("Expected 1 argument but got {0}", args.Length);
                return;
            }

            if (options.forwardProxy.set)
            {
                AppLayerProxy.ForwardProxy = Proxy.ParseProxy(options.forwardProxy.ArgValue,
                                                              DnsPriority.IPv4ThenIPv6, null);
            }

            ListenPort = UInt16.Parse(nonOptionArgs[0]);
            var listenIP = options.listenIP.ArgValue;

            SelectServer selectServer = new SelectServer(false, new Buf(8192));

            Socket listenSocket = new Socket(listenIP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            listenSocket.Bind(new IPEndPoint(listenIP, ListenPort));
            listenSocket.Listen(options.backlog.ArgValue);

            SelectControl selectControl = new SelectControl(true);

            selectControl.AddListenSocket(listenSocket, AcceptProxyClient);

            selectServer.Run();
        }
Exemple #5
0
        public static Int32 Run(Options options, List <String> nonOptionArgs)
        {
            log = Console.Out;

            //
            // Options
            //
            if (nonOptionArgs.Count < 2)
            {
                return(options.ErrorAndUsage("Not enough arguments"));
            }

            String clientSideConnector = nonOptionArgs[0];
            String listenPortsString   = nonOptionArgs[1];

            {
                Proxy  proxy;
                String ipOrHostAndPort = Proxy.StripAndParseProxies(clientSideConnector, DnsPriority.IPv4ThenIPv6, out proxy);
                UInt16 port;
                String ipOrHost = EndPoints.SplitIPOrHostAndPort(ipOrHostAndPort, out port);
                server = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
            }
            SortedNumberSet listenPortSet = PortSet.ParsePortSet(listenPortsString);

            SelectServer selectServer = new SelectServer(false, new Buf(options.readBufferSize.ArgValue));
            IPAddress    listenIP     = IPAddress.Any;

            foreach (var port in listenPortSet)
            {
                IPEndPoint listenEndPoint = new IPEndPoint(listenIP, port);
                Socket     socket         = new Socket(listenEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(listenEndPoint);
                socket.Listen(options.socketBackLog.ArgValue);
                selectServer.control.AddListenSocket(socket, AcceptCallback);
            }

            selectServer.Run();
            return(0);
        }
Exemple #6
0
        public void Run(TextWriter selectServerEventLog, IPEndPoint debugServerEndPoint, IPEndPoint npcServerEndPoint,
                        IPAddress listenIPAddress, Int32 backlog, SharedFileSystem sharedFileSystem,
                        Int32 portmapPort, Int32 mountPort, Int32 nfsPort, UInt32 readSizeMax, UInt32 suggestedReadSizeMultiple)
        {
            Buf sendBuffer = new Buf(4096);
            Buf recvBuffer = new Buf(4096);

            //
            // Create Mappings List
            //
            NamedMapping[] namedMappings = new NamedMapping[] {
                new NamedMapping(PortMap.Name, new Mapping(PortMap.ProgramNumber, PortMap2.ProgramVersion, PortMap.IPProtocolTcp, (UInt32)portmapPort)),
                new NamedMapping(PortMap.Name, new Mapping(PortMap.ProgramNumber, PortMap2.ProgramVersion, PortMap.IPProtocolUdp, (UInt32)portmapPort)),

                new NamedMapping(Mount.Name, new Mapping(Mount.ProgramNumber, Mount1.ProgramVersion, PortMap.IPProtocolTcp, (UInt32)mountPort)),
                new NamedMapping(Mount.Name, new Mapping(Mount.ProgramNumber, Mount1.ProgramVersion, PortMap.IPProtocolUdp, (UInt32)mountPort)),

                new NamedMapping(Mount.Name, new Mapping(Mount.ProgramNumber, Mount3.ProgramVersion, PortMap.IPProtocolTcp, (UInt32)mountPort)),
                new NamedMapping(Mount.Name, new Mapping(Mount.ProgramNumber, Mount3.ProgramVersion, PortMap.IPProtocolUdp, (UInt32)mountPort)),

                new NamedMapping(Nfs.Name, new Mapping(Nfs.ProgramNumber, Nfs3.ProgramVersion, PortMap.IPProtocolTcp, (UInt32)nfsPort)),
                new NamedMapping(Nfs.Name, new Mapping(Nfs.ProgramNumber, Nfs3.ProgramVersion, PortMap.IPProtocolUdp, (UInt32)nfsPort)),
            };

            PortMap2Server   portMapServer = new PortMap2Server(this, namedMappings, sendBuffer);
            Mount1And3Server mountServer   = new Mount1And3Server(this, sharedFileSystem, sendBuffer);
            Nfs3Server       nfsServer     = new Nfs3Server(this, sharedFileSystem, sendBuffer, readSizeMax, suggestedReadSizeMultiple);


            //
            // Create Endpoints
            //
            if (listenIPAddress == null)
            {
                listenIPAddress = IPAddress.Any;
            }
            IPEndPoint portMapEndPoint = new IPEndPoint(listenIPAddress, portmapPort);
            IPEndPoint mountEndPoint   = new IPEndPoint(listenIPAddress, mountPort);
            IPEndPoint nfsEndPoint     = new IPEndPoint(listenIPAddress, nfsPort);

            selectServer = new SelectServer(false, recvBuffer);

            AddRpcServer(selectServer, portMapEndPoint, portMapServer, backlog);
            AddRpcServer(selectServer, mountEndPoint, mountServer, backlog);
            AddRpcServer(selectServer, nfsEndPoint, nfsServer, backlog);

            if (debugServerEndPoint != null)
            {
                Socket tcpAcceptSocket = new Socket(debugServerEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                tcpAcceptSocket.Bind(debugServerEndPoint);
                tcpAcceptSocket.Listen(4);
                selectServer.control.AddListenSocket(tcpAcceptSocket, new ControlServer().AcceptCallback);
            }

            if (npcServerEndPoint != null)
            {
#if !WindowsCE
                NpcSelectServerHandler npcServerHandler;
                {
                    Nfs3Server.NfsServerManager nfsServerManager = new Nfs3Server.NfsServerManager(nfsServer);
                    NpcReflector reflector = new NpcReflector(
                        new NpcExecutionObject(nfsServerManager, "Nfs3ServerManager", null, null),
                        new NpcExecutionObject(nfsServer, "Nfs3Server", null, null),
                        new NpcExecutionObject(portMapServer, "Portmap2Server", null, null),
                        new NpcExecutionObject(mountServer, "Mount1And3Server", null, null)
                        );
                    npcServerHandler = new NpcSelectServerHandler(NpcCallback.Instance, reflector, new DefaultNpcHtmlGenerator("NfsServer", reflector));
                }

                Socket tcpAcceptSocket = new Socket(npcServerEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                tcpAcceptSocket.Bind(npcServerEndPoint);
                tcpAcceptSocket.Listen(4);

                selectServer.control.AddListenSocket(tcpAcceptSocket, npcServerHandler.AcceptCallback);
#endif
            }

            this.serverStartTimeStopwatchTicks = Stopwatch.GetTimestamp();

            /*
             * selectServer.Run(selectServerEventLog, new byte[1024], tcpListeners.ToArray(),
             *  new UdpSelectListener[]{
             *      new UdpSelectListener(portMapEndPoint, portMapServer),
             *      new UdpSelectListener(mountEndPoint  , mountServer),
             *      new UdpSelectListener(nfsEndPoint    , nfsServer),
             *  }
             * );
             */
            selectServer.Run();
        }
Exemple #7
0
        static Int32 Main(string[] args)
        {
            TunnelOptions optionsParser = new TunnelOptions();
            List <String> nonOptionArgs = optionsParser.Parse(args);

            if (nonOptionArgs.Count <= 0)
            {
                Console.WriteLine("Must give at least one listener");
                optionsParser.PrintUsage();
                return(-1);
            }

            SelectControl selectControl = new SelectControl(false);

            //
            // Parse listeners
            //
            int arg = 0;

            do
            {
                //
                // Get Command Line Arguments
                //
                String connector = nonOptionArgs[arg];
                arg++;
                if (arg >= nonOptionArgs.Count)
                {
                    Console.WriteLine("EndPoint '{0}' is missing a protocol 'tcp|udp' and a listen port set", connector);
                    optionsParser.PrintUsage();
                    return(-1);
                }
                //
                // Parse End Point
                //
                HostWithOptionalProxy forwardHost = ConnectorParser.ParseConnectorWithPortAndOptionalProxy(connector);

                //
                // Parse Protocol and Port Set
                //
                String protocolAndPortSet = nonOptionArgs[arg++];

                Boolean isTcp;
                if (protocolAndPortSet.StartsWith("tcp", StringComparison.CurrentCultureIgnoreCase))
                {
                    isTcp = true;
                }
                else if (protocolAndPortSet.StartsWith("udp", StringComparison.CurrentCultureIgnoreCase))
                {
                    isTcp = false;
                }
                else
                {
                    throw new InvalidOperationException(String.Format("Unknown protocol '{0}', expected 'tcp' or 'udp'",
                                                                      (protocolAndPortSet.Length < 3) ? protocolAndPortSet : protocolAndPortSet.Remove(3)));
                }

                if (protocolAndPortSet.Length < 4)
                {
                    Console.WriteLine("ProtocolAndPortSet '{0}' Protocol '{1}' is missing a listen port set", connector, protocolAndPortSet);
                    optionsParser.PrintUsage();
                    return(-1);
                }

                String portSetString = protocolAndPortSet.Substring(3);
                if (String.IsNullOrEmpty(portSetString))
                {
                    Console.WriteLine("EndPoint '{0}' Protocol '{1}' is missing a listen port set", connector, protocolAndPortSet);
                    optionsParser.PrintUsage();
                    return(-1);
                }

                PortSet portSet = ParseUtilities.ParsePortSet(portSetString);

                if (isTcp)
                {
                    TcpCallback tcpCallback = new TcpCallback(forwardHost);

                    for (int i = 0; i < portSet.Length; i++)
                    {
                        Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        listenSocket.Bind(new IPEndPoint(IPAddress.Any, portSet[i]));
                        listenSocket.Listen(optionsParser.socketBackLog.ArgValue);
                        selectControl.AddListenSocket(listenSocket, tcpCallback.HandleAccept);
                    }
                }
                else
                {
                    if (forwardHost.proxy == null)
                    {
                        IPEndPoint        serverEndPoint = forwardHost.directEndPoint.GetOrResolveToIPEndPoint();
                        DirectUdpCallback udpCallback    = new DirectUdpCallback(serverEndPoint, 30 * 60); // 30 minutes
                        for (int i = 0; i < portSet.Length; i++)
                        {
                            Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            udpSocket.Bind(new IPEndPoint(IPAddress.Any, portSet[i]));
                            selectControl.AddReceiveSocket(udpSocket, udpCallback.ReceiveHandler);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();

                        /*
                         * UdpThroughProxyCallback udpCallback = new UdpThroughProxyCallback(serverEndPoint, proxyConnector,
                         *  1000 * 60 * 4, 1000 * 60 * 10);
                         * for (int i = 0; i < portSet.Length; i++)
                         * {
                         *  udpListenerList.Add(new UdpSelectListener(new IPEndPoint(IPAddress.Any, portSet[i]), udpCallback));
                         * }
                         */
                    }
                }
            } while (arg < nonOptionArgs.Count);

            SelectServer selectServer = new SelectServer(selectControl, new Buf(8192));

            selectServer.Run();

            return(-1);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            Options       options       = new Options();
            List <String> nonOptionArgs = options.Parse(args);

            if (nonOptionArgs.Count != 2)
            {
                options.ErrorAndUsage("Expected 2 non option argument but got none");
                return;
            }

            String serverName = nonOptionArgs[0];
            String accessorConnectorString = nonOptionArgs[1];

            TlsSettings tlsSettings;

            if (!options.authenticationFile.set)
            {
                tlsSettings = new TlsSettings(false);
            }
            else
            {
                throw new NotImplementedException("Authentication file not yet implemented");
            }

            /*
             * Run(tlsSettings, serverName, accessorConnector,
             *  options.heartbeatSeconds.ArgValue,
             *  options.reconnectWaitSeconds.ArgValue, options.receiveBufferLength.ArgValue);
             * }
             * public static void Run(TlsSettings tlsSettings, String serverName, String accessorConnectorString,
             * UInt16 heartbeatSeconds, UInt16 reconnectWaitSeconds, UInt32 receiveBufferLength)
             * {*/

            ServerInfo serverInfo = new ServerInfo(
                Encoding.ASCII.GetBytes(serverName),
                options.heartbeatSeconds.ArgValue,
                options.reconnectWaitSeconds.ArgValue);

            //SelectTunnelsThread tunnelsThread = new SelectTunnelsThread(receiveBufferLength);
            AccessorConnection accessorConnection = new AccessorConnection(accessorConnectorString);

            //TunnelControlServer tunnelServer = new TunnelControlServer(tunnelsThread, accessor);
            //NpcReflector npcReflector = new NpcReflector(
            //    new NpcExecutionObject(tunnelServer, "TunnelServer", null, null)
            //    );

            Int32 heartbeatMillis     = serverInfo.SecondsPerHeartbeat * 1000;
            Int32 reconnectWaitMillis = serverInfo.SecondsPerReconnect * 1000;

            Buf          safeBuffer   = new Buf(options.receiveBufferLength.ArgValue);
            SelectServer selectServer = new SelectServer(true, safeBuffer);

            accessorConnection.StartConnect(ref selectServer.control, safeBuffer);
            selectServer.Run();


            /*
             * //
             * // TmpHiddenServer Loop
             * //
             * Byte[] receiveBuffer = new Byte[receiveBufferLength];
             * Buf sendBuffer = new Buf(256, 256);
             * Int64 lastHearbeatTime = 0;
             * SingleObjectList singleAccessorSocket = new SingleObjectList();
             *
             * while (true)
             * {
             *  //
             *  // While the accessor is not connected
             *  //
             *  while (!accessorConnection.Connected)
             *  {
             *      Console.WriteLine("{0} [{1}] Attempting reconnect...", DateTime.Now, accessorConnection.accessorHostString);
             *      if (accessorConnection.TryConnectAndInitialize(tlsSettings, sendBuffer, serverInfo))
             *      {
             *          Console.WriteLine("{0} [{1}] Connected", DateTime.Now, accessorConnection.accessorHostString);
             *          lastHearbeatTime = Stopwatch.GetTimestamp();
             *      }
             *      else
             *      {
             *          Console.WriteLine("{0} [{1}] Failed to connect, waiting {2} seconds for reconnect",
             *              DateTime.Now, accessorConnection.accessorHostString, reconnectWaitMillis / 1000);
             *          Thread.Sleep(reconnectWaitMillis);
             *      }
             *  }
             *
             *  //
             *  // While the accessor is connected
             *  //
             *  while (accessorConnection.Connected)
             *  {
             *      //
             *      // Calculate the next heartbeat timeout
             *      //
             *      Int64 now = Stopwatch.GetTimestamp();
             *      Int32 nextHeartbeatTimeoutMillis = heartbeatMillis - StopwatchExtensions.StopwatchTicksAsInt32Milliseconds(now - lastHearbeatTime);
             *      if (nextHeartbeatTimeoutMillis <= 0)
             *      {
             *          Console.WriteLine("{0} [{1}] Sending hearbeat...", DateTime.Now, accessorConnection.accessorHostString);
             *
             *          accessorConnection.SendHeartbeat();
             *          if (!accessorConnection.Connected) break;
             *
             *          lastHearbeatTime = now;
             *          nextHeartbeatTimeoutMillis = heartbeatMillis;
             *      }
             *
             *      accessorConnection.ReceiveWithTimeout(receiveBuffer, nextHeartbeatTimeoutMillis);
             *  }
             * }
             */
        }