Exemplo n.º 1
0
        /// <summary> Creates an instance of Client. </summary>
        /// <remarks> All parameters are optional. If null is passed, the object's
        /// configuration is read from the app.config file. </remarks>
        /// <param name="logger">An optional Logger to log messages. If null is passed,
        /// an attempt is made to read the log4net configuration from the app.config
        /// file. If the configuration is not present a <see cref="US.OpenServer.Log4NetLogger"/>
        /// is created otherwise a <see cref="US.OpenServer.ConsoleLogger"/> is
        /// created.</param>
        /// <param name="serverConfiguration">An optional ServerConfiguration that contains the
        /// properties necessary to connect to the server. If null is passed, the
        /// configuration is read from the app.config's 'server' XML section
        /// node.</param>
        /// <param name="protocolConfigurations">An optional Dictionary of
        /// ProtocolConfiguration objects keyed with each protocol's unique identifier.
        /// If null is passed, the configuration is read from the app.config's
        /// 'protocols' XML section node.</param>
        /// <param name="userData">An Object the caller can pass through to each protocol.</param>
        public Client(
            ServerConfiguration serverConfiguration = null,
            Dictionary<ushort, ProtocolConfiguration> protocolConfigurations = null,
            Logger logger = null,
            object userData = null)
        {
            if (logger == null)
            {
                object log4NetConfiguration = ConfigurationManager.GetSection("log4net");
                if (log4NetConfiguration != null)
                    logger = new Log4NetLogger("DotNetOpenServerClient");
                else
                    logger = new ConsoleLogger();
            }

            Logger = logger;
            Logger.Log(Level.Info, string.Format("Execution Mode: {0}", Debugger.IsAttached ? "Debug" : "Release"));

            if (serverConfiguration == null)
                serverConfiguration = (ServerConfiguration)ConfigurationManager.GetSection("server");
            if (serverConfiguration == null)
            {
                serverConfiguration = new ServerConfiguration();
                serverConfiguration.Host = ServerConfiguration.DEFAULT_HOST;
            }
            ServerConfiguration = serverConfiguration;

            if (protocolConfigurations == null)
                protocolConfigurations = (Dictionary<ushort, ProtocolConfiguration>)ConfigurationManager.GetSection("protocols");
            if (protocolConfigurations == null)
                protocolConfigurations = new Dictionary<ushort, ProtocolConfiguration>();
            ProtocolConfigurations = protocolConfigurations;

            UserData = userData;
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Client client = null;
            try
            {
                ServerConfiguration cfg = new ServerConfiguration();
                //cfg.Host = "yourserver.com";
                //cfg.TlsConfiguration.Enabled = true;

                Dictionary<ushort, ProtocolConfiguration> protocolConfigurations =
                    new Dictionary<ushort, ProtocolConfiguration>();

                protocolConfigurations.Add(KeepAliveProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(KeepAliveProtocol.PROTOCOL_IDENTIFIER, typeof(KeepAliveProtocol)));

                protocolConfigurations.Add(WinAuthProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(WinAuthProtocol.PROTOCOL_IDENTIFIER, typeof(WinAuthProtocolClient)));

                protocolConfigurations.Add(HelloProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(HelloProtocol.PROTOCOL_IDENTIFIER, typeof(HelloProtocolClient)));

                client = new Client(cfg, protocolConfigurations);
                client.Connect();

                ushort[] protocolIds = client.GetServerSupportedProtocolIds();
                foreach (int protocolId in protocolIds)
                    Console.WriteLine("Server Supports Protocol ID: " + protocolId);

                string userName = "******";
                WinAuthProtocolClient wap = client.Initialize(WinAuthProtocol.PROTOCOL_IDENTIFIER) as WinAuthProtocolClient;
                if (!wap.Authenticate(userName, "T3stus3r", null))
                    throw new Exception("Access denied.");

                client.Initialize(KeepAliveProtocol.PROTOCOL_IDENTIFIER);

                HelloProtocolClient hpc = (HelloProtocolClient)client.Initialize(HelloProtocol.PROTOCOL_IDENTIFIER);
                string serverResponse = hpc.Hello(userName);
                Console.WriteLine(serverResponse);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadKey();
                if (client != null)
                    client.Close();
            }
        }
Exemplo n.º 3
0
        /// <summary> Creates an instance of Client. </summary>        
        /// <param name="serverConfiguration">Contains the properties necessary to
        /// connect to the server.</param>
        /// <param name="protocolConfigurations">A Dictionary of ProtocolConfiguration
        /// objects keyed with each protocol's unique identifier.</param>
        /// <param name="logger">An optional Logger to log messages. If null is passed,
        /// a <see cref="US.OpenServer.Logger"/> object is created.</param>      
        /// <param name="userData">An optional Object the caller can pass through to
        /// each protocol.</param>
        public Client(
            ServerConfiguration serverConfiguration,
            Dictionary<ushort, ProtocolConfiguration> protocolConfigurations,
            Logger logger = null,
            object userData = null)
        {
            if (logger == null)
                logger = new Logger();
            Logger = logger;
            Logger.Log(Level.Info, string.Format("Execution Mode: {0}", Debugger.IsAttached ? "Debug" : "Release"));

            ServerConfiguration = serverConfiguration;
            ProtocolConfigurations = protocolConfigurations;
            UserData = userData;
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Server server = null;
            try
            {
            #if USE_APP_CONFIG
                server = new Server();
            #else
                ServerConfiguration cfg = new ServerConfiguration();
                cfg.Host = "0.0.0.0";//bind address
                cfg.TlsConfiguration.Enabled = false;

                Dictionary<ushort, ProtocolConfiguration> protocolConfigurations =
                    new Dictionary<ushort, ProtocolConfiguration>();

                protocolConfigurations.Add(KeepAliveProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(KeepAliveProtocol.PROTOCOL_IDENTIFIER, typeof(KeepAliveProtocol)));

                WinAuthProtocolConfigurationServer winAuthCfg = new WinAuthProtocolConfigurationServer();
                winAuthCfg.AddRole("Administrators");
                winAuthCfg.AddUser("TestUser");
                protocolConfigurations.Add(WinAuthProtocol.PROTOCOL_IDENTIFIER, winAuthCfg);

                protocolConfigurations.Add(HelloProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(HelloProtocol.PROTOCOL_IDENTIFIER, typeof(HelloProtocolServer)));

                server = new Server(cfg, protocolConfigurations);
                server.Logger.LogPackets = true;
                server.Logger.LogToDebuggerOutputView = true;
                server.Logger.LogDebug = true;
            #endif
                server.Logger.Log(Level.Info, "Press any key to quit.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadKey();
                if (server != null)
                    server.Close();
            }
        }
Exemplo n.º 5
0
        private void Connect()
        {
            ServerConfiguration cfg = new ServerConfiguration();
            cfg.Host = txtHost.Text;
            //cfg.TlsConfiguration.Enabled = true;            

            Dictionary<ushort, ProtocolConfiguration> protocolConfigurations =
                new Dictionary<ushort, ProtocolConfiguration>();

            protocolConfigurations.Add(KeepAliveProtocol.PROTOCOL_IDENTIFIER,
                new ProtocolConfiguration(KeepAliveProtocol.PROTOCOL_IDENTIFIER, typeof(KeepAliveProtocol)));

            protocolConfigurations.Add(WinAuthProtocol.PROTOCOL_IDENTIFIER,
                new ProtocolConfiguration(WinAuthProtocol.PROTOCOL_IDENTIFIER, typeof(WinAuthProtocolClient)));

            protocolConfigurations.Add(HelloProtocol.PROTOCOL_IDENTIFIER,
                new ProtocolConfiguration(HelloProtocol.PROTOCOL_IDENTIFIER, typeof(HelloProtocolClient)));

            client = new Client(cfg, protocolConfigurations);
            client.Logger.OnLogMessage += logger_OnLogMessage;
            client.OnConnectionLost += client_OnConnectionLost;
            client.Connect();

            try
            {
                WinAuthProtocolClient wap = (WinAuthProtocolClient)client.Initialize(WinAuthProtocol.PROTOCOL_IDENTIFIER);
                if (!wap.Authenticate(txtUserName.Text, txtPassword.Password))
                    throw new Exception("Access denied.");

                client.Initialize(KeepAliveProtocol.PROTOCOL_IDENTIFIER);

                HelloProtocolClient hpc = (HelloProtocolClient)client.Initialize(HelloProtocol.PROTOCOL_IDENTIFIER);
                hpc.OnHelloComplete += hpc_OnHelloComplete;
                hpc.Hello(txtUserName.Text);
            }
            catch (Exception)
            {
                client.Close();
                throw;
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Server server = null;
            try
            {
                ServerConfiguration cfg = new ServerConfiguration();
                //cfg.TlsConfiguration.Enabled = true;
                //cfg.TlsConfiguration.Certificate = "yourserver.com";

                Dictionary<ushort, ProtocolConfiguration> protocolConfigurations =
                    new Dictionary<ushort, ProtocolConfiguration>();

                protocolConfigurations.Add(KeepAliveProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(KeepAliveProtocol.PROTOCOL_IDENTIFIER, typeof(KeepAliveProtocol)));

                WinAuthProtocolConfigurationServer winAuthCfg = new WinAuthProtocolConfigurationServer();
                winAuthCfg.AddRole("Administrators");
                winAuthCfg.AddUser("TestUser");
                protocolConfigurations.Add(WinAuthProtocol.PROTOCOL_IDENTIFIER, winAuthCfg);

                protocolConfigurations.Add(HelloProtocol.PROTOCOL_IDENTIFIER,
                    new ProtocolConfiguration(HelloProtocol.PROTOCOL_IDENTIFIER, typeof(HelloProtocolServer)));

                server = new Server(cfg, protocolConfigurations);
                server.Logger.Log(Level.Info, "Press any key to quit.");
                Console.ReadKey();

                server.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadKey();
                if (server != null)
                    server.Close();
            }
        }
Exemplo n.º 7
0
        /// <summary> Creates an instance of Server. </summary>
        /// <remarks> All parameters are optional. If null is passed, the object's
        /// configuration is read from the app.config file. If SSL/TLS is enabled, gets
        /// the server-side certificate from the local certificate store.</remarks>
        /// <param name="logger">An optional Logger to log messages. If null is passed,
        /// an attempt is made to read the log4net configuration from the app.config's 
        /// file. If the configuration is present, a <see cref="US.OpenServer.Log4NetLogger"/>
        /// is created otherwise a <see cref="US.OpenServer.ConsoleLogger"/> is
        /// created.</param>
        /// <param name="serverConfiguration">An optional ServerConfiguration that contains the
        /// properties necessary to create the server. If null is passed, the
        /// configuration is read from the app.config's 'server' XML section
        /// node.</param>
        /// <param name="protocolConfigurations">An optional Dictionary of
        /// ProtocolConfiguration objects keyed with each protocol's unique identifier.
        /// If null is passed, the configuration is read from the app.config's
        /// 'protocols' XML section node.</param>
        /// <param name="userData">An Object the caller can pass through to each protocol.</param>
        public Server(
            ServerConfiguration serverConfiguration = null,
            Dictionary<ushort, ProtocolConfiguration> protocolConfigurations = null,
            Logger logger = null,
            object userData = null)
        {
            if (logger == null)
            {
                object log4NetConfiguration = ConfigurationManager.GetSection("log4net");
                if (log4NetConfiguration != null)
                    logger = new Log4NetLogger("DotNetOpenServer");
                else
                    logger = new ConsoleLogger();
            }

            Logger = logger;
            Logger.Log(Level.Info, string.Format("Execution Mode: {0}", Debugger.IsAttached ? "Debug" : "Release"));

            if (serverConfiguration == null)
                serverConfiguration = (ServerConfiguration)ConfigurationManager.GetSection("server");
            if (serverConfiguration == null)
                serverConfiguration = new ServerConfiguration();
            ServerConfiguration = serverConfiguration;

            if (serverConfiguration.TlsConfiguration.Enabled)
            {
                certificate = Session.GetCertificateFromStore(
                string.Format("CN={0}", ServerConfiguration.TlsConfiguration.Certificate));
                if (certificate == null)
                {
                    throw new Exception(string.Format(
                        "SSL Certificate '{0}' not found.",
                        ServerConfiguration.TlsConfiguration.Certificate));
                }
            }

            if (protocolConfigurations == null)
                protocolConfigurations = (Dictionary<ushort, ProtocolConfiguration>)ConfigurationManager.GetSection("protocols");
            if (protocolConfigurations == null)
                protocolConfigurations = new Dictionary<ushort, ProtocolConfiguration>();
            ProtocolConfigurations = protocolConfigurations;

            UserData = userData;

            t = new Thread(new ThreadStart(Run));
            t.Start();
        }
        /// <summary>
        /// Reads the TCP socket server configuration properties from the app.config
        /// file.
        /// </summary>
        /// <param name="parent">The parent object. This parameter is not used.</param>
        /// <param name="configContext">The Configuration context object. This parameter
        /// is not used.</param>        
        /// <param name="section">The XML section node.</param>
        /// <returns>A ServerConfiguration that contains the configuration properties.</returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            ServerConfiguration cfg = new ServerConfiguration();

            XmlNode node = section.SelectSingleNode(HOST);
            if (node != null)
                cfg.Host = node.Attributes[VALUE].Value;

            node = section.SelectSingleNode(PORT);
            if (node != null)
                cfg.Port = ushort.Parse(node.Attributes[VALUE].Value);

            node = section.SelectSingleNode(IDLETIMEOUT);
            if (node != null)
                cfg.IdleTimeout = int.Parse(node.Attributes[VALUE].Value);

            node = section.SelectSingleNode(RECEIVETIMEOUT);
            if (node != null)
                cfg.ReceiveTimeout = int.Parse(node.Attributes[VALUE].Value);

            node = section.SelectSingleNode(SENDTIMEOUT);
            if (node != null)
                cfg.SendTimeout = int.Parse(node.Attributes[VALUE].Value);

            node = section.SelectSingleNode(TLS);
            if (node != null)
            {
                cfg.TlsConfiguration.Enabled = bool.Parse(node.Attributes[VALUE].Value);
                if (cfg.TlsConfiguration.Enabled)
                {
                    cfg.TlsConfiguration.Certificate = node.Attributes[CERTIFICATE].Value;
                    cfg.TlsConfiguration.RequireRemoteCertificate = bool.Parse(node.Attributes[REQUIREREMOTECERTIFICATE].Value);
                    cfg.TlsConfiguration.AllowSelfSignedCertificate = bool.Parse(node.Attributes[ALLOWSELFSIGNEDCERTIFICATE].Value);
                    cfg.TlsConfiguration.CheckCertificateRevocation = bool.Parse(node.Attributes[CHECKCERTIFICATEREVOCATION].Value);
                    cfg.TlsConfiguration.AllowCertificateChainErrors = bool.Parse(node.Attributes[ALLOWCERTIFICATECHAINERRORS].Value);
                }
            }

            return cfg;
        }