Пример #1
0
        public void WithLibzmqServer()
        {
            using (var ctx = new ZContext())
                using (var client = new DealerSocket())
                    using (var server = ZSocket.Create(ctx, ZSocketType.DEALER))
                    {
                        var serverCert = new ZCert();
                        server.CurveServer    = true;
                        server.CurveSecretKey = serverCert.SecretKey;
                        server.Bind($"tcp://127.0.0.1:55367");

                        var clientKeyPair = new NetMQCertificate();
                        client.Options.CurveCertificate = clientKeyPair;
                        client.Options.CurveServerKey   = serverCert.PublicKey;
                        client.Connect("tcp://127.0.0.1:55367");

                        server.SendBytes(Encoding.ASCII.GetBytes("Hello"), 0, 5);
                        var hello = client.ReceiveFrameString();
                        Assert.Equal("Hello", hello);

                        client.SendFrame("Hello");
                        var frame = server.ReceiveFrame();
                        Assert.Equal("Hello", frame.ReadString());
                    }
        }
Пример #2
0
        public static void IronhouseServer(string[] args)
        {
            //
            // Hello World server with ironhouse security
            //
            // Author: hawkans
            //

            if (args == null || args.Length < 1)
            {
                Console.WriteLine();
                Console.WriteLine("Usage: ./{0} Ironhouse HWServer [Name]", AppDomain.CurrentDomain.FriendlyName);
                Console.WriteLine();
                Console.WriteLine("    Name   Your name. Default: World");
                Console.WriteLine();
                args = new string[] { "World" };
            }

            string name = args[0];
            // Create or load certificates
            ZCert clientCert = GetOrCreateCert("clienttest");
            ZCert serverCert = GetOrCreateCert("servertest");

            using (var responder = new ZSocket(ZSocketType.REP))
                using (var actor = new ZActor(ZAuth.Action0, null))
                {
                    actor.Start();
                    // send CURVE settings to ZAuth
                    actor.Frontend.Send(new ZFrame("VERBOSE"));
                    actor.Frontend.Send(new ZMessage(new List <ZFrame>()
                    {
                        new ZFrame("ALLOW"), new ZFrame("127.0.0.1")
                    }));
                    actor.Frontend.Send(new ZMessage(new List <ZFrame>()
                    {
                        new ZFrame("CURVE"), new ZFrame(".curve")
                    }));

                    responder.CurvePublicKey = serverCert.PublicKey;
                    responder.CurveSecretKey = serverCert.SecretKey;
                    responder.CurveServer    = true;
                    // Bind
                    responder.Bind("tcp://*:5555");

                    while (true)
                    {
                        // Receive
                        using (ZFrame request = responder.ReceiveFrame())
                        {
                            Console.WriteLine("Received {0}", request.ReadString());

                            // Do some work
                            Thread.Sleep(1);

                            // Send
                            responder.Send(new ZFrame(name));
                        }
                    }
                }
        }
Пример #3
0
        public static void IronhouseClient(string[] args)
        {
            //
            // Hello World client with ironhouse security
            //
            // Author: hawkans
            //

            if (args == null || args.Length < 1)
            {
                Console.WriteLine();
                Console.WriteLine("Usage: ./{0} Ironhouse HWClient [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
                Console.WriteLine();
                Console.WriteLine("    Endpoint  Where HWClient should connect to.");
                Console.WriteLine("              Default is tcp://127.0.0.1:5555");
                Console.WriteLine();
                args = new string[] { "tcp://127.0.0.1:5555" };
            }

            string endpoint = args[0];

            // Create

            using (var context = new ZContext())
                using (var requester = new ZSocket(context, ZSocketType.REQ))
                {
                    ZCert clientCert = GetOrCreateCert("clienttest", ".curve");
                    ZCert serverCert = GetOrCreateCert("servertest");
                    requester.CurvePublicKey = clientCert.PublicKey;
                    requester.CurveSecretKey = clientCert.SecretKey;
                    requester.CurveServer    = true;
                    requester.CurveServerKey = serverCert.PublicKey;
                    // Connect
                    requester.Connect(endpoint);

                    for (int n = 0; n < 10; ++n)
                    {
                        string requestText = "Hello";
                        Console.Write("Sending {0}...", requestText);

                        // Send
                        requester.Send(new ZFrame(requestText));

                        // Receive
                        using (ZFrame reply = requester.ReceiveFrame())
                        {
                            Console.WriteLine(" Received: {0} {1}!", requestText, reply.ReadString());
                        }
                    }
                }
        }
Пример #4
0
        private static ZCert GetOrCreateCert(string name, string curvpath = ".curve")
        {
            ZCert  cert;
            string keyfile = Path.Combine(curvpath, name + ".pub");

            if (!File.Exists(keyfile))
            {
                cert = new ZCert();
                Directory.CreateDirectory(curvpath);
                cert.SetMeta("name", name);
                cert.Save(keyfile);
            }
            else
            {
                cert = ZCert.Load(keyfile);
            }
            return(cert);
        }