Esempio n. 1
0
        public void DemoRpcOverLrpc()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   //allow GSS_NEGOTIATE
                   .AddAuthNegotiate()
                   //LRPC named 'lrpctest'
                   .AddProtocol("ncalrpc", "lrpctest")
                   //Begin responding
                   .StartListening())
            {
                //Create the rpc client connection and give it to the new SearchService
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncalrpc", null, "lrpctest").Authenticate(
                                RpcAuthenticationType.Self)))
                {
                    //party on!
                    SearchResponse results =
                        client.Search(SearchRequest.CreateBuilder().AddCriteria("Test Criteria").Build());
                    Assert.AreEqual(1, results.ResultsCount);
                    Assert.AreEqual("Test Criteria", results.ResultsList[0].Name);
                    Assert.AreEqual("http://whatever.com", results.ResultsList[0].Url);
                }
            }
        }
Esempio n. 2
0
 public void MultiPartMessageTest()
 {
     //Notice that both client and server must enable multi-part messages...
     using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
            .AddAuthNegotiate()
            .AddProtocol("ncacn_ip_tcp", "12345")
            .EnableMultiPart()
            .StartListening())
     {
         using (
             SearchService client = new SearchService(RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "::1", "12345")
                                                      .Authenticate(RpcAuthenticationType.Self).
                                                      EnableMultiPart(1000000)))
         {
             // Non-LRPC channels have limitations on message sizes, we use multiple calls to forward large messages
             // and store state on the server in the RpcSession associated with this client.  This is all transparent
             // to the caller, but this 7 meg message will produce several rpc-calls.
             SearchRequest.Builder criteria = SearchRequest.CreateBuilder();
             byte[] bytes = new byte[2500];
             Random r     = new Random();
             for (int i = 0; i < 2500; i++)
             {
                 r.NextBytes(bytes);
                 criteria.AddCriteria(Convert.ToBase64String(bytes));
             }
             SearchResponse results = client.Search(criteria.Build());
             Assert.AreEqual(2500, results.ResultsCount);
         }
     }
 }
Esempio n. 3
0
        public void DemoClientProxyChain()
        {
            Guid iid1 = Guid.NewGuid();
            Guid iid2 = Guid.NewGuid();

            //forward reuests from iid1 to service iid2:
            using (
                RpcServer.CreateRpc(iid1,
                                    new SearchService.ServerStub(
                                        RpcClient.ConnectRpc(iid2, "ncalrpc", null, @"lrpctest").Authenticate(
                                            RpcAuthenticationType.Self)))
                .AddProtocol("ncalrpc", @"lrpctest")
                .AddAuthNegotiate()
                .StartListening())
                //iid calls the implementation
                using (RpcServer.CreateRpc(iid2, new SearchService.ServerStub(new AuthenticatedSearch()))
                       .AddProtocol("ncalrpc", @"lrpctest")
                       .AddAuthNegotiate()
                       .StartListening())
                {
                    using (
                        SearchService client =
                            new SearchService(
                                RpcClient.ConnectRpc(iid1, "ncalrpc", null, @"lrpctest").Authenticate(
                                    RpcAuthenticationType.Self)))
                        Assert.AreEqual(1,
                                        client.Search(SearchRequest.CreateBuilder().AddCriteria(String.Empty).Build()).
                                        ResultsCount);
                }
        }
Esempio n. 4
0
        public void DemoExampleConnection()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   //allow GSS_NEGOTIATE
                   .AddAuthNegotiate()
                   //LRPC named 'lrpctest'
                   .AddProtocol("ncalrpc", "lrpctest")
                   .AddProtocol("ncacn_ip_tcp", "12345")
                   .AddProtocol("ncacn_np", @"\pipe\p1")
                   //Begin responding
                   .StartListening())
            {
                // Demonstrate a typical client-implemented wrapper that can parse URIs, retry connections, etc.
                using (var client = new SearchService(new ExampleRpcConnection(iid, "lrpc://localhost/lrpctest")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test1").Build()).ResultsCount);

                using (var client = new SearchService(new ExampleRpcConnection(iid, "rpc://self@localhost:12345")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test2").Build()).ResultsCount);

                using (var client = new SearchService(new ExampleRpcConnection(iid, "np://self@localhost/pipe/p1")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test3").Build()).ResultsCount);
            }
        }
        public void TestClientServerWithProtoFormat()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());
            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            ISearchService client = new SearchService(new ExampleClient(wire, "application/x-protobuf"));
            //now the client has a real, typed, interface to work with:
            SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());

            Assert.AreEqual(1, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            //The test part of this, call the only other method
            result =
                client.RefineSearch(
                    RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
            Assert.AreEqual(2, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            Assert.AreEqual("Refine", result.ResultsList[1].Name);
            Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
        }
Esempio n. 6
0
 public void AuthWinNTTest()
 {
     using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
            .AddAuthWinNT()
            .AddProtocol("ncacn_ip_tcp", "12345")
            .StartListening())
     {
         using (
             SearchService client =
                 new SearchService(
                     RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "127.0.0.1", "12345").Authenticate(
                         RpcAuthenticationType.Self)))
         {
             SearchResponse results =
                 client.Search(SearchRequest.CreateBuilder().AddCriteria("Test Criteria").Build());
             Assert.AreEqual(1, results.ResultsCount);
         }
     }
 }
Esempio n. 7
0
        public void DemoCustomAuthorization()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));
            //create the implementation and wrap our autthorization around it
            IRpcDispatch impl = new AuthorizeFilter(new SearchService.Dispatch(new AuthenticatedSearch()));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(impl))
                   .AddProtocol("ncalrpc", @"lrpctest")
                   .StartListening())
            {
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncalrpc", null, @"lrpctest").Authenticate(
                                RpcAuthenticationType.Self)))
                    client.Search(SearchRequest.CreateBuilder().AddCriteria(String.Empty).Build());
            }
        }
Esempio n. 8
0
        public void DemoRpcOverAnonymousNamedPipe()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AnonymousSearch()))
                   //pipes start with '\pipe\'
                   .AddProtocol("ncacn_np", @"\pipe\p1")
                   //Begin responding
                   .StartListening())
            {
                //using the anonId interface and AnonymousSearch implementation we can call without authentication
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncacn_np", @"\\localhost", @"\pipe\p1").Authenticate(
                                RpcAuthenticationType.Anonymous)))
                    client.Search(SearchRequest.CreateBuilder().Build());
            }
        }
Esempio n. 9
0
        public void InprocPerformanceTest()
        {
            using (
                SearchService client =
                    new SearchService(new InprocRpcClient(new SearchService.ServerStub(new AuthenticatedSearch()))))
            {
                SearchResponse previous = client.Search(SearchRequest.CreateBuilder()
                                                        .AddCriteria("one").AddCriteria("two").AddCriteria("three").
                                                        Build());
                RefineSearchRequest req = RefineSearchRequest.CreateBuilder()
                                          .AddCriteria("four").SetPreviousResults(previous).Build();

                Stopwatch w = new Stopwatch();
                w.Start();
                for (int i = 0; i < 1000; i++)
                {
                    client.RefineSearch(req);
                }
                w.Stop();
                Trace.TraceInformation("Inproc Performance = {0}", w.ElapsedMilliseconds);
            }
        }
Esempio n. 10
0
        public void MultiPartMessageFailsWithoutHandler()
        {
            //Notice that both client and server must enable multi-part messages...
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   .AddAuthNegotiate()
                   .AddProtocol("ncacn_ip_tcp", "12345")
//Omit server response:        .EnableMultiPart()
                   .StartListening())
            {
                using (
                    SearchService client = new SearchService(RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "::1", "12345")
                                                             .Authenticate(RpcAuthenticationType.Self).
                                                             EnableMultiPart(1000000)))
                {
                    SearchRequest.Builder criteria = SearchRequest.CreateBuilder();
                    byte[] bytes = new byte[1000000];
                    new Random().NextBytes(bytes);
                    SearchResponse results = client.Search(criteria.AddCriteria(Convert.ToBase64String(bytes)).Build());
                    Assert.AreEqual(2500, results.ResultsCount);
                }
            }
        }
Esempio n. 11
0
        public void LrpcPerformanceTest()
        {
            Win32RpcServer.VerboseLogging = false;
            try
            {
                using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                       .AddAuthNegotiate()
                       .AddProtocol("ncalrpc", "lrpctest")
                       .StartListening())
                {
                    using (
                        SearchService client =
                            new SearchService(
                                RpcClient.ConnectRpc(iid, "ncalrpc", null, "lrpctest").Authenticate(
                                    RpcAuthenticationType.Self)))
                    {
                        SearchResponse previous = client.Search(SearchRequest.CreateBuilder()
                                                                .AddCriteria("one").AddCriteria("two").AddCriteria(
                                                                    "three").Build());
                        RefineSearchRequest req = RefineSearchRequest.CreateBuilder()
                                                  .AddCriteria("four").SetPreviousResults(previous).Build();

                        Stopwatch w = new Stopwatch();
                        w.Start();
                        for (int i = 0; i < 1000; i++)
                        {
                            client.RefineSearch(req);
                        }
                        w.Stop();
                        Trace.TraceInformation("Lrpc Performance = {0}", w.ElapsedMilliseconds);
                    }
                }
            }
            finally
            {
                Win32RpcServer.VerboseLogging = true;
            }
        }
        public void TestClientServerWithCustomFormat()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());

            //Setup our custom mime-type format as the only format supported:
            server.Options.MimeInputTypes.Clear();
            server.Options.MimeInputTypes.Add("foo/bar", CodedInputStream.CreateInstance);
            server.Options.MimeOutputTypes.Clear();
            server.Options.MimeOutputTypes.Add("foo/bar", CodedOutputStream.CreateInstance);

            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            ExampleClient exclient = new ExampleClient(wire, "foo/bar");

            //Add our custom mime-type format
            exclient.Options.MimeInputTypes.Add("foo/bar", CodedInputStream.CreateInstance);
            exclient.Options.MimeOutputTypes.Add("foo/bar", CodedOutputStream.CreateInstance);
            ISearchService client = new SearchService(exclient);

            //now the client has a real, typed, interface to work with:
            SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());

            Assert.AreEqual(1, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            //The test part of this, call the only other method
            result =
                client.RefineSearch(
                    RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
            Assert.AreEqual(2, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            Assert.AreEqual("Refine", result.ResultsList[1].Name);
            Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
        }
Esempio n. 13
0
    static int Run(string[] args)
    {
        ParseCommandLine(args);

        // Locate path to certificates folder in distribution.
        string rootDir  = DllPathResolver.FindDistributionRootDir();
        string certsDir = Path.Combine(rootDir, "certs");

        // Initialize RCFProto.
        RCFProto.Init();

        // Build a search request.
        SearchRequest request = SearchRequest.CreateBuilder()
                                .SetQuery("something to search for")
                                .SetResultPerPage(10)
                                .SetPageNumber(0)
                                .Build();

        // Call the server on each of its supported endpoints.
        List <Endpoint> endpoints = new List <Endpoint>();

        endpoints.Add(new TcpEndpoint(50001));
        endpoints.Add(new HttpEndpoint(50002));
        endpoints.Add(new HttpsEndpoint(50003));
        endpoints.Add(new Win32NamedPipeEndpoint("DemoServerPipe"));

        foreach (Endpoint endpoint in endpoints)
        {
            // Create channel.
            RcfProtoChannel    channel       = new RcfProtoChannel(endpoint);
            RcfProtoController rcfController = new RcfProtoController();
            SearchService.Stub searchService = new SearchService.Stub(channel);

            // Set certificate validation callback, for SSL.
            channel.SetCertificateValidationCallback(OnValidateCertificate);

            {
                // Synchronous remote call.
                searchService.Search(null, request, null);
                SearchResponse response = (SearchResponse)channel.GetResponse();
            }

            {
                // Asynchronous remote call.
                channel.SetAsynchronousRpcMode(true);

                Action <SearchResponse> done = delegate(SearchResponse response)
                {
                    OnRpcDone(searchService, rcfController, request, response);
                };
                searchService.Search(rcfController, request, done);

                while (gCallCompleted == false)
                {
                    System.Threading.Thread.Sleep(500);
                }
            }
        }

        {
            // Create TCP channel.
            RcfProtoChannel    channel       = new RcfProtoChannel(endpoints[0]);
            RcfProtoController rcfController = new RcfProtoController();
            SearchService.Stub searchService = new SearchService.Stub(channel);

            // Enable compression (requires zlib to be installed).
            //channel.SetEnableCompression(true);

            // Custom connection timeout (10s).
            channel.SetConnectTimeoutMs(10 * 1000);

            // Custom remote call timeout (60s).
            channel.SetRemoteCallTimeoutMs(60 * 1000);

            // Enable NTLM authentication and encryption.
            channel.SetTransportProtocol(TransportProtocol.Ntlm);

            // The channel will pick up the credentials of the user running the
            // program. Alternatively, we can set the credentials explicitly:

            //channel.setUsername("MyDomain\\MyUsername");
            //channel.setPassword("MyPassword");

            searchService.Search(null, request, null);
            SearchResponse response = (SearchResponse)channel.GetResponse();

            // Enable SSL, with custom certificate validation callback.
            channel.SetTransportProtocol(TransportProtocol.Ssl);
            channel.SetCertificateValidationCallback(OnValidateCertificate);

            searchService.Search(null, request, null);
            response = (SearchResponse)channel.GetResponse();

            // Enable SSL, with validation against Windows certificate root store. Requires Schannel.
            string certPath = Path.Combine(certsDir, "caCertA.p12");

            // Open CA certificate.
            PfxCertificate caCert = new PfxCertificate(certPath, "", "RCF CA A");

            // Add it to the root store of the local machine.
            caCert.AddToStore(Win32CertificateLocation.LocalMachine, Win32CertificateStore.Root);

            channel.SetEnableSchannelCertificateValidation("localhost");
            channel.SetSslImplementation(SslImplementation.Schannel);

            // Disconnect so SSL handshake takes place again.
            channel.Disconnect();

            searchService.Search(null, request, null);
            response = (SearchResponse)channel.GetResponse();
        }

        return(0);
    }