/// <summary> /// Creates a new connection for processing data /// </summary> public Connection(Socket client, bool _IsServer, VaserOptions Mode, PortalCollection PColl, VaserKerberosServer KerberosS, VaserSSLServer SSLS, VaserKerberosClient KerberosC, VaserSSLClient SSLC, VaserServer srv = null) { IsServer = _IsServer; StreamIsConnected = true; _rms2 = new MemoryStream(); _rbr2 = new BinaryReader(_rms2); _Mode = Mode; _PCollection = PColl; _vSSLS = SSLS; _vKerberosS = KerberosS; _vSSLC = SSLC; _vKerberosC = KerberosC; _SocketTCPClient = client; _SocketTCPClient.LingerState = new LingerOption(true, 0); server = srv; IPv4Address = ((IPEndPoint)client.RemoteEndPoint).Address; link = new Link(PColl) { Connect = this }; if (Mode == VaserOptions.ModeNotEncrypted) { mySendNotEncryptedCallback = new AsyncCallback(SendNotEncryptedCallback); myReceiveNotEncryptedCallback = new AsyncCallback(ReceiveNotEncryptedCallback); } if (Mode == VaserOptions.ModeKerberos) { mySendKerberosCallback = new AsyncCallback(SendKerberosCallback); myReceiveKerberosCallback = new AsyncCallback(ReceiveKerberosCallback); } if (Mode == VaserOptions.ModeSSL) { mySendSSLCallback = new AsyncCallback(SendSSLCallback); myReceiveSSLCallback = new AsyncCallback(ReceiveSSLCallback); } if (_IsServer) { ThreadPool.QueueUserWorkItem(HandleClientComm); } else { HandleClientComm(null); } }
/// <summary> /// Creates a new connection for processing data /// </summary> public Connection(StreamSocket client, bool _IsServer, VaserOptions Mode, PortalCollection PColl, VaserKerberosServer KerberosS, VaserSSLServer SSLS, VaserKerberosClient KerberosC, VaserSSLClient SSLC, VaserServer srv = null) { IsServer = _IsServer; StreamIsConnected = true; _rms2 = new MemoryStream(); _rbr2 = new BinaryReader(_rms2); _Mode = Mode; _PCollection = PColl; _vSSLS = SSLS; _vKerberosS = KerberosS; _vSSLC = SSLC; _vKerberosC = KerberosC; _SocketTCPClient = client; server = srv; //Debug.WriteLine("Init Options Done"); IPv4Address = client.Information.RemoteAddress; link = new Link(PColl) { Connect = this }; //Debug.WriteLine("Create Link Done"); //Debug.WriteLine("Init Connection class DONE"); if (_IsServer) { //Debug.WriteLine("Send to HandleClientComm"); IAsyncAction asyncAction = ThreadPool.RunAsync(HandleClientComm); } else { HandleClientComm(null); } }
/// <summary> /// Creates a new connection for processing data /// </summary> public Connection(Socket client, bool _IsServer, VaserOptions Mode, PortalCollection PColl, VaserKerberosServer KerberosS, VaserSSLServer SSLS, VaserKerberosClient KerberosC, VaserSSLClient SSLC, VaserServer srv = null) { IsServer = _IsServer; StreamIsConnected = true; _rms2 = new MemoryStream(); _rbr2 = new BinaryReader(_rms2); _Mode = Mode; _PCollection = PColl; _vSSLS = SSLS; _vKerberosS = KerberosS; _vSSLC = SSLC; _vKerberosC = KerberosC; client.SendBufferSize = 65007; _SocketTCPClient = client; server = srv; IPv4Address = ((IPEndPoint)client.RemoteEndPoint).Address; link = new Link(PColl); link.Connect = this; if (_IsServer) { ThreadPool.QueueUserWorkItem(HandleClientComm); } else { HandleClientComm(null); } }
static void Main(string[] args) { bool online = true; //Client initalisieren Portal system = new Portal(100); PortalCollection PC = new PortalCollection(); PC.RegisterPortal(system); system.IncomingPacket += OnSystemPacket; TestRequest myRequest = new TestRequest(); PC.RegisterRequest(myRequest, system, 501); TestChannel myChannel = new TestChannel(); PC.RegisterChannel(myChannel, system, 502); // ########################################################### // Create a TestCert in CMD: makecert -sr LocalMachine -ss root -r -n "CN=localhost" -sky exchange -sk 123456 // Do not use in Production | do not use localhost -> use your machinename! // ########################################################### /*//Import Test Cert from local store * X509Certificate2Collection cCollection = new X509Certificate2Collection(); * * X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); * store.Open(OpenFlags.ReadOnly); * var certificates = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false); * store.Close(); * * if (certificates.Count == 0) * { * Console.WriteLine("Server certificate not found..."); * Console.ReadKey(); * return; * } * else * { * cCollection.Add(certificates[0]); * } * // Get the value. * string resultsTrue = cCollection[0].ToString(true); * // Display the value to the console. * Console.WriteLine(resultsTrue); * // Get the value. * string resultsFalse = cCollection[0].ToString(false); * // Display the value to the console. * Console.WriteLine(resultsFalse);*/ /*System.Timers.Timer _aTimer = new System.Timers.Timer(1); * _aTimer.Elapsed += DoPackets; * _aTimer.AutoReset = true; * _aTimer.Enabled = true;*/ Thread.Sleep(100); VaserSSLClient ssl = new VaserSSLClient("localhost"); VaserKerberosClient kerberos = new VaserKerberosClient(); Link lnk1 = VaserClient.ConnectClient("localhost", 3100, PC); lnk1.EmptyBuffer += OnEmptyBuffer; if (lnk1 != null) { Console.WriteLine("1: successfully established connection."); } //working if (lnk1.IsConnected) { Console.WriteLine("Test. Con OK"); } cStatus sts = myRequest.myRequestStarter("HELLO WORLD!", lnk1); myChannel.mySendStarter("This is my channel tester", lnk1); while (online) { if (sts != null) { if (sts.Done && !sts.Error) { Console.WriteLine("Request Done Result: " + (string)sts.ResultObject); sts = null; online = false; } if (sts != null && sts.Error) { Console.WriteLine("Error: " + sts.Message); sts = null; online = false; } } Thread.Sleep(1000); //entfernen //if (lnk1.IsConnected == false) online = false; } //Client1.CloseClient(); lnk1.Dispose(); Console.WriteLine("Test ended... press any key..."); Console.ReadKey(); }
/// <summary> /// Opens an kerberos encrypted connection to a server. /// </summary> /// <param name="IP">Hostname or IP-Address.</param> /// <param name="RemotePort">Target port of the remote server.</param> /// <param name="PColl">The Portal Collection.</param> /// <param name="Kerberos">The Kerberos connectionsettings.</param> /// <returns>Returns the link of the connection.</returns> /// <exception cref="System.Net.Sockets.SocketException">Thrown if vaser is unable to create a socket or a connection.</exception> public static Link ConnectClient(string IP, int RemotePort, PortalCollection PColl, VaserKerberosClient Kerberos) { //if (Mode == VaserOptions.ModeSSL) throw new Exception("Missing X509Certificate2"); if (PColl == null) { throw new Exception("PortalCollection is needed!"); } try { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Task t = client.ConnectAsync(IP, RemotePort); t.Wait(); if (client.Connected) { PColl._Active = true; Connection con = new Connection(client, false, VaserOptions.ModeKerberos, PColl, null, null, Kerberos, null); return(con.link); } else { return(null); } } catch (Exception e) { throw e; } }
public static void Run() { bool online = true; //Client initalisieren Portal system = new Portal(100); PortalCollection PC = new PortalCollection(); PC.RegisterPortal(system); system.IncomingPacket += OnSystemPacket; // ########################################################### // Create a TestCert in CMD: makecert -sr LocalMachine -ss root -r -n "CN=localhost" -sky exchange -sk 123456 // Do not use in Production | do not use localhost -> use your machinename! // ########################################################### /*//Import Test Cert from local store * X509Certificate2Collection cCollection = new X509Certificate2Collection(); * * X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); * store.Open(OpenFlags.ReadOnly); * var certificates = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false); * store.Close(); * * if (certificates.Count == 0) * { * Console.WriteLine("Server certificate not found..."); * Console.ReadKey(); * return; * } * else * { * cCollection.Add(certificates[0]); * } * // Get the value. * string resultsTrue = cCollection[0].ToString(true); * // Display the value to the console. * Console.WriteLine(resultsTrue); * // Get the value. * string resultsFalse = cCollection[0].ToString(false); * // Display the value to the console. * Console.WriteLine(resultsFalse);*/ /*System.Timers.Timer _aTimer = new System.Timers.Timer(1); * _aTimer.Elapsed += DoPackets; * _aTimer.AutoReset = true; * _aTimer.Enabled = true;*/ Thread.Sleep(100); VaserSSLClient ssl = new VaserSSLClient("localhost"); VaserKerberosClient kerberos = new VaserKerberosClient(); Link lnk1 = VaserClient.ConnectClient("localhost", 3100, PC); lnk1.EmptyBuffer += OnEmptyBuffer; if (lnk1 != null) { Console.WriteLine("1: successfully established connection."); } //working if (lnk1.IsConnected) { Console.WriteLine("Test. Con OK"); } while (online) { Thread.Sleep(100); //entfernen if (lnk1.IsConnected == false) { online = false; } } //Client1.CloseClient(); lnk1.Dispose(); Console.WriteLine("Test ended... press any key..."); Console.ReadKey(); }