/// <summary>
 /// Gets the context of the function
 /// </summary>
 /// <param name="context">Context.</param>
 public AppletFunctionBridge(frmDisconnectedClient context)
 {
     this.m_context = context;
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // Output main header
            var parser = new ParameterParser <ConsoleParameters>();

            Parameters = parser.Parse(args);

            // Output copyright info
            var entryAsm = Assembly.GetEntryAssembly();

            AppDomain.CurrentDomain.AssemblyResolve += (o, e) =>
            {
                string pAsmName = e.Name;
                if (pAsmName.Contains(","))
                {
                    pAsmName = pAsmName.Substring(0, pAsmName.IndexOf(","));
                }

                var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => e.Name == a.FullName) ??
                          AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => pAsmName == a.GetName().Name);
                return(asm);
            };


            try
            {
                // Security Application Information
                var applicationIdentity = new SecurityApplication()
                {
                    Key = Guid.Parse("feeca9f3-805e-4be9-a5c7-30e6e495939b"),
                    ApplicationSecret = Parameters.ApplicationSecret ?? "FE78825ADB56401380DBB406411221FD",
                    Name = Parameters.ApplicationName ?? "org.santedb.disconnected_client.win32"
                };

                // Setup basic parameters
                String[] directory =
                {
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),      "SanteDB", "dc-win32"),
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SanteDB", "dc-win32")
                };

                foreach (var dir in directory)
                {
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                }

                ServicePointManager.DefaultConnectionLimit             = 2;
                ServicePointManager.MaxServicePointIdleTime            = 100;
                TokenValidationManager.SymmetricKeyValidationCallback += (o, k, i) =>
                {
                    return(MessageBox.Show(String.Format("Trust issuer {0} with symmetric key?", i), "Token Validation Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes);
                };
                ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, error) =>
                {
                    if (certificate == null || chain == null)
                    {
                        return(false);
                    }
                    else
                    {
                        var valid = s_trustedCerts.Contains(certificate.Subject);
                        if (!valid && (chain.ChainStatus.Length > 0 || error != SslPolicyErrors.None))
                        {
                            if (MessageBox.Show(String.Format("The remote certificate is not trusted. The error was {0}. The certificate is: \r\n{1}\r\nWould you like to temporarily trust this certificate?", error, certificate.Subject), "Certificate Error", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                            {
                                return(false);
                            }
                            else
                            {
                                s_trustedCerts.Add(certificate.Subject);
                            }
                        }

                        return(true);
                        //isValid &= chain.ChainStatus.Length == 0;
                    }
                };


                if (Parameters.ShowHelp)
                {
                    parser.WriteHelp(Console.Out);
                }
                else if (Parameters.Reset && MessageBox.Show("Are you sure you want to wipe all your data and configuration for the Disconnected Client?", "Confirm Reset", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SanteDB", "dc-win32");
                    var cData   = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SanteDB", "dc-win32");
                    if (Directory.Exists(appData))
                    {
                        Directory.Delete(cData, true);
                    }
                    if (Directory.Exists(appData))
                    {
                        Directory.Delete(appData, true);
                    }
                    MessageBox.Show("Environment Reset Successful");
                    return;
                }
                else // RUN THE SERVICE
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    var settings = new CefSettings()
                    {
                        UserAgent                = $"SanteDB-DC",
                        ExternalMessagePump      = false,
                        MultiThreadedMessageLoop = true,
                        LogFile       = Path.Combine(directory[1], "cef-sdb.log"),
                        CachePath     = Path.Combine(directory[1], ".cache"),
                        RootCachePath = Path.Combine(directory[1], ".root-cache")
                    };
                    CefSharpSettings.WcfEnabled = true;
                    CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
                    CefSharpSettings.ShutdownOnExit = true;
                    Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
                    Cef.EnableHighDPISupport();

                    frmDisconnectedClient frmMain = null;
                    frmSplash             splash  = new frmSplash();
                    splash.Show();

                    if (!DcApplicationContext.StartContext(new WinFormsDialogProvider(), "dc-win32", applicationIdentity, Core.SanteDBHostType.Client))
                    {
                        if (!DcApplicationContext.StartTemporary(new WinFormsDialogProvider(), "dc-win32", applicationIdentity, Core.SanteDBHostType.Client))
                        {
                            MessageBox.Show("There was an error starting up the Disconnected Client. Please see logs in %localappdata%\\log for more information");
                            Cef.Shutdown();
                            Application.Exit();
                            Environment.Exit(666);
                            return;
                        }
                        else
                        {
                            while (!DcApplicationContext.Current.IsRunning)
                            {
                                Application.DoEvents();
                            }

                            frmMain = new frmDisconnectedClient("http://127.0.0.1:9200/#!/config/initialSettings");
                        }
                    }
                    else
                    {
                        while (!DcApplicationContext.Current.IsRunning)
                        {
                            Application.DoEvents();
                        }

                        frmMain = new frmDisconnectedClient("http://127.0.0.1:9200/");
                    }
                    splash.Close();
                    ApplicationContext.Current.GetService <IConfigurationManager>().SetAppSetting("http.bypassMagic", "");

                    Application.Run(frmMain);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("FATAL ERROR ON STARTUP: {0}", e.ToString()), "Error");
                Cef.Shutdown();
                Application.Exit();
                Environment.Exit(996);
            }
            finally
            {
                Cef.Shutdown();
            }
        }