static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

            IUserContentManagementServiceClient ucmService = TypeSafeHttpBuilder <IUserContentManagementServiceClient> .Create()
                                                             .RegisterDefaultSerializers()
                                                             .RegisterDotNetHttpClient("https://localhost.:5002/")
                                                             .RegisterJsonNetSerializer()
                                                             .Build();

            Console.WriteLine("Sending request");

            RequestedUrlResponseModel responseModel = ucmService.GetNewWorldUploadUrl(AuthToken)
                                                      .Result;

            Console.WriteLine($"URL: {responseModel.UploadUrl}");

            IWorldServiceClient worldService = TypeSafeHttpBuilder <IWorldServiceClient> .Create()
                                               .RegisterDefaultSerializers()
                                               .RegisterDotNetHttpClient("https://localhost.:5003/")
                                               .RegisterJsonNetSerializer()
                                               .Build();

            WorldDownloadURLResponse downloadResponse = worldService.GetWorldDownloadUrl(new WorldDownloadURLRequest(64), AuthToken).Result;

            Console.WriteLine($"Download URL Response: {downloadResponse.ResultCode} {downloadResponse.DownloadURL}");

            Console.ReadKey();
        }
 //TODO: Handle HTTPS cert issue
 public override void Register(IServiceRegister register)
 {
     register.RegisterAsImplementedInterfaces(TypeSafeHttpBuilder <IGameServerSessionService> .Create()
                                              .RegisterDefaultSerializers()
                                              .RegisterJsonNetSerializer()
                                              .RegisterDotNetHttpClient(@"http://localhost:5003")
                                              .Build()); //TODO: How should an instance server find its mediator?
 }
        //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
        private static bool MyRemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
        {
            TypeSafeHttpBuilder <IUserContentManagementServiceClient> .Create()
            .RegisterDefaultSerializers()
            .RegisterDotNetHttpClient("https://localhost.:5002/")
            .RegisterJsonNetSerializer()
            .Build();

            return(true);
        }
Exemplo n.º 4
0
        public static IWorldServiceClient Create(string baseUrl)
        {
            if (string.IsNullOrWhiteSpace(baseUrl))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(baseUrl));
            }

            return(TypeSafeHttpBuilder <IWorldServiceClient> .Create()
                   .RegisterDefaultSerializers()
                   .RegisterDotNetHttpClient(baseUrl)
                   .RegisterJsonNetSerializer()
                   .Build());
        }
        /// <inheritdoc />
        public override void Register(IServiceRegister register)
        {
            //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

            IAuthenticationService authService = TypeSafeHttpBuilder <IAuthenticationService> .Create()
                                                 .RegisterDefaultSerializers()
                                                 .RegisterJsonNetSerializer()
                                                 .RegisterDotNetHttpClient(GetDeclaredServiceUrl("AuthenticationService")) //TODO: Central the naming of these services
                                                 .Build();

            //TODO: Adjust registeration
            register.RegisterInstance <IAuthenticationService, IAuthenticationService>(authService);
        }
        public ConsulDotNetHttpClient(string endpoint)
        {
            if (string.IsNullOrWhiteSpace(endpoint))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(endpoint));
            }

            Service = TypeSafeHttpBuilder <TConsulServiceType>
                      .Create()
                      .RegisterDefaultSerializers()
                      .RegisterDotNetHttpClient(endpoint)
                      .RegisterJsonNetSerializer()
                      .Build();

            if (Service == null)
            {
                throw new InvalidOperationException($"Failed to build {nameof(Service)} for Type: {typeof(TConsulServiceType).Name}");
            }
        }
        public void Test()
        {
            //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

            //TODO: Service discovery
            IAuthenticationService authService = TypeSafeHttpBuilder <IAuthenticationService> .Create()
                                                 .RegisterDefaultSerializers()
                                                 .RegisterDotNetHttpClient("https://localhost:5001/")
                                                 .RegisterJsonNetSerializer()
                                                 .Build();

            authService.TryAuthenticate(new AuthenticationRequestModel(UserName, Password))
            .UnityAsyncContinueWith(this, ar => WorldServiceClientFactory.Create().GetWorldDownloadUrl(new WorldDownloadURLRequest(int.Parse(WorldId)), $"Bearer {ar.AccessToken}"))
            .UnityAsyncContinueWith(this, r =>
            {
                Debug.Log($"Result: {r.ResultCode} Download URL: {r.DownloadURL}");
                StartCoroutine(AssetBundleRequest(r.DownloadURL));
            });
        }
Exemplo n.º 8
0
        public static async Task AsyncMain()
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

            ITestInterface apiInterface = TypeSafeHttpBuilder <ITestInterface> .Create()
                                          .RegisterRestSharpClient(@"http://localhost.fiddler:5000")
                                          .RegisterDefaultSerializers()
                                          .RegisterJsonNetSerializer()
                                          .Build();

            Console.WriteLine("About to call intercepted method.");

            Task <TestReturnModel> result = apiInterface.TestMethod("test", "testing dynamic header value");

            Console.WriteLine("API method temporarily yielded.");

            await result;

            Console.WriteLine($"Finished call intercepted method. Result: {result.Result.ReturnValue}");
        }
        private bool Authenticate()
        {
            //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

            //TODO: Service discovery
            IAuthenticationService authService = TypeSafeHttpBuilder <IAuthenticationService> .Create()
                                                 .RegisterDefaultSerializers()
                                                 .RegisterDotNetHttpClient("https://localhost:5001/")
                                                 .RegisterJsonNetSerializer()
                                                 .Build();

            //Authentication using provided credentials
            JWTModel result = authService.TryAuthenticate(new AuthenticationRequestModel(AccountName, Password)).Result;

            Debug.Log($"Auth Result: {result.isTokenValid} Token: {result.AccessToken} Error: {result.Error} ErrorDescription: {result.ErrorDescription}.");

            AuthToken = $"Bearer {result.AccessToken}";

            return(result.isTokenValid);
        }
Exemplo n.º 10
0
        public static async Task AsyncMain()
        {
            ITestInterface apiInterface = TypeSafeHttpBuilder <ITestInterface> .Create()
                                          .RegisterDotNetHttpClient(@"https://localhost:5001", new HttpClientHandler()
            {
                SslProtocols = SslProtocols.Tls, ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
            })
                                          .RegisterDefaultSerializers()
                                          .RegisterJsonNetSerializer()
                                          .Build();

            Console.WriteLine("About to call intercepted method.");

            Task <TestReturnModel> result = apiInterface.TestMethod(new TestModel(12456, "Two"), "test");

            Console.WriteLine("API method temporarily yielded.");

            await result;

            Console.WriteLine($"Finished call intercepted method. Result: {result.Result.ReturnValue}");
        }
        protected async Task <string> GetDeclaredServiceUrl(string serviceType)
        {
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serviceType));
            }

            //We can't do this async. It's a required dependency before things can move forward
            if (ServiceUrlToDiscoveryServiceMap.ContainsKey(_ServiceDiscoveryEndpoint))
            {
                return(await QueryServiceForEndpoint(serviceType)
                       .ConfigureAwait(false));
            }
            else
            {
                ServiceUrlToDiscoveryServiceMap[_ServiceDiscoveryEndpoint] = TypeSafeHttpBuilder <IServiceDiscoveryService> .Create()
                                                                             .RegisterJsonNetSerializer()
                                                                             .RegisterDotNetHttpClient(_ServiceDiscoveryEndpoint)
                                                                             .Build();

                return(await GetDeclaredServiceUrl(serviceType)
                       .ConfigureAwait(false));
            }
        }
        //TODO: Enable this once we use actual seperate gameservers
        //PlayerPrefs.HasKey(PlayerPreferences.GameServerIp.ToString()) && PlayerPrefs.HasKey(PlayerPreferences.GameServerPort.ToString());

        /// <inheritdoc />
        public override void Register(IServiceRegister register)
        {
            if (hasStoredGameServer)
            {
                //We need to know where the server is first. We can't ask the discovery service.
                register.RegisterInstance <IGameServerMediatorService, IGameServerMediatorService>(TypeSafeHttpBuilder <IGameServerMediatorService> .Create()
                                                                                                   .RegisterDefaultSerializers()
                                                                                                   .RegisterJsonNetSerializer()
                                                                                                   .RegisterDotNetHttpClient(GenerateGameServerMediatorUrl())
                                                                                                   .Build());
            }
            else
            {
                throw new InvalidOperationException($"Failed to load gameserver from {nameof(PlayerPrefs)}");
            }
        }
        void OnGUI()
        {
            AccountName = EditorGUILayout.TextField("Account", AccountName);
            Password    = EditorGUILayout.PasswordField("Password", Password);

            //TODO: Validate scene file
            SceneObject = EditorGUILayout.ObjectField("Scene", SceneObject, typeof(SceneAsset), false);

            if (GUILayout.Button("Build World AssetBundle"))
            {
                if (!Authenticate())
                {
                    Debug.LogError($"Failed to authenticate User: {AccountName}");
                    return;
                }

                //Once authenticated we need to try to build the bundle.
                ProjectVindictiveAssetbundleBuilder builder = new ProjectVindictiveAssetbundleBuilder(SceneObject);

                //TODO: Handle uploading build
                AssetBundleManifest manifest = builder.BuildBundle();

                //TODO: Refactor all this crap
                AssetBundlePath = manifest.GetAllAssetBundles().First();

                return;
            }

            if (GUILayout.Button("Upload Assetbundle"))
            {
                //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
                ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

                IUserContentManagementServiceClient ucmService = TypeSafeHttpBuilder <IUserContentManagementServiceClient> .Create()
                                                                 .RegisterDefaultSerializers()
                                                                 .RegisterDotNetHttpClient("https://localhost:5002/")
                                                                 .RegisterJsonNetSerializer()
                                                                 .Build();

                Thread uploadThread = new Thread(new ThreadStart(async() =>
                {
                    Debug.Log("Recieved URL Response.");
                    //HttpWebRequest httpRequest = WebRequest.Create(ucmService.GetNewWorldUploadUrl(AuthToken).Result.UploadUrl) as HttpWebRequest;
                    HttpWebRequest httpRequest = WebRequest.Create((await ucmService.GetNewWorldUploadUrl(AuthToken)).UploadUrl) as HttpWebRequest;
                    Debug.Log("Built http request with URL");

                    httpRequest.Method = "PUT";
                    using (Stream dataStream = httpRequest.GetRequestStream())
                    {
                        byte[] buffer = new byte[8000];
                        using (FileStream fileStream = new FileStream(Path.Combine("AssetBundles/temp/", AssetBundlePath), FileMode.Open, FileAccess.Read))
                        {
                            int bytesRead = 0;
                            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                dataStream.Write(buffer, 0, bytesRead);
                            }
                        }
                    }

                    HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
                }));

                uploadThread.Start();
            }
        }