Example #1
0
        /// <summary>
        /// Encrypt an image message and send it to the given recipient.
        /// </summary>
        /// <param name="threemaId">threemaId target Threema ID</param>
        /// <param name="imageFilePath">path to read image data from</param>
        /// <returns>generated message ID</returns>
        public string SendImageMessage(string threemaId, string imageFilePath)
        {
            //fetch public key
            byte[] publicKey = this.apiConnector.LookupKey(threemaId);

            if (publicKey == null)
            {
                throw new InvalidKeyException("invalid threema id");
            }

            //check capability of a key
            CapabilityResult capabilityResult = this.apiConnector.LookupKeyCapability(threemaId);

            if (capabilityResult == null || !capabilityResult.CanImage)
            {
                throw new NotAllowedException();
            }

            byte[] fileData = File.ReadAllBytes(imageFilePath);
            if (fileData == null)
            {
                throw new IOException("invalid file");
            }

            //encrypt the image
            EncryptResult encryptResult = CryptTool.Encrypt(fileData, this.privateKey, publicKey);

            //upload the image
            UploadResult uploadResult = apiConnector.UploadFile(encryptResult);

            if (!uploadResult.IsSuccess)
            {
                throw new IOException("could not upload file (upload response " + uploadResult.ResponseCode + ")");
            }

            //send it
            EncryptResult imageMessage = CryptTool.EncryptImageMessage(encryptResult, uploadResult, this.privateKey, publicKey);

            return(apiConnector.SendE2EMessage(
                       threemaId,
                       imageMessage.Nonce,
                       imageMessage.Result));
        }
Example #2
0
        public MainWindow(Dictionary <string, string> args)
        {
            InitializeComponent();

            //Set Client ID
            string rawID = $"{Environment.MachineName}-{Environment.UserName}-Miner Tracker-Client";

            CLIENT_ID = CryptTool.Encrypt(rawID);

            //Set SystemTray Icon
            systemTrayIcon         = new NotifyIcon();
            systemTrayIcon.Icon    = new Icon(SYSTEM_TRAY_ICON);
            systemTrayIcon.Visible = true;
            systemTrayIcon.Text    = ToolTipsText;
            this.Hide();
            systemTrayIcon.DoubleClick +=
                delegate(object callBack, EventArgs mouseEvent)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
                this.Activate();
            };
            systemTrayIcon.BalloonTipClosed += (sender, e) => { var thisIcon = (NotifyIcon)sender; thisIcon.Visible = false; thisIcon.Dispose(); };
            System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
            PCIDContextMenu         = contextMenu.MenuItems.Add("Checking PCID...");
            PCIDContextMenu.Enabled = false;
            GPUContextMenu          = contextMenu.MenuItems.Add("GPU List");
            GPUContextMenu.Enabled  = false;
            contextMenu.MenuItems.Add("View Log", (s, e) =>
            {
                try
                {
                    Process.Start("notepad.exe", Path.Combine(ServiceConfig.LogPath, EventLogger.GetLogFileName()));
                }
                catch (Exception ex)
                {
                    ShowErrorDialog("Unable to open log", ex.ToString());
                    EventLogger.WriteLog("OPEN LOG ERROR", ex.ToString());
                }
            });
            contextMenu.MenuItems.Add("Change PC ID", (s, e) =>
            {
                OpenSettingContextMenu();
            });

            contextMenu.MenuItems.Add("Restore", (s, e) =>
            {
                this.Show();
                this.WindowState = WindowState.Normal;
                this.Activate();
            });
            contextMenu.MenuItems.Add("Exit", (s, e) =>
            {
                System.Windows.Application.Current.Shutdown();
            });

            systemTrayIcon.ContextMenu = contextMenu;
            //Get version
            var currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            FooterVersionTextBlock.Text = $"Version: {currentVersion}";
            var createdTime = AssemblyInformationManager.GetCreatedTime(@"C:\Program Files\Miner Tracker\Miner Tracker Desktop.exe");

            AboutVersionTextBlock.Text = $"Version {currentVersion} -Released {createdTime.Month}/{createdTime.Year}";

            //Get config
            var serviceConfigReader = new ConfigurationReader();

            ServiceConfig = serviceConfigReader.GetServiceConfig();
            //If can not get service config, then show error and shutdown
            if (!ClassUtils.CheckNull(ServiceConfig))
            {
                EventLogger.WriteLog("CLIENT READ SERVICE CONFIG ERROR", "Null retrieved config");
                ShowErrorDialog("File system error", "Unable to retrieve service configuration");
                System.Windows.Application.Current.Shutdown();
            }

            var userConfigReader = new ConfigurationReader(isService: false, userConfigPath: ServiceConfig.UserConfigPath, userConfigFile: ServiceConfig.UserConfigFileName);

            UserConfig = userConfigReader.GetUserConfig();
            var CurrentUserConfig = UserConfig;

            //Service config is okay but can not retrieve the user.config

            if (!ClassUtils.CheckNull(UserConfig))
            {
                ShowErrorDialog("File system error", "Unable to retrieve client configuration. Options have been set to default.");
                UserConfig = new UserConfiguration
                {
                    ID                 = CurrentUserConfig == null || CurrentUserConfig.ID == 0 ? ServiceConfig.UserID : CurrentUserConfig.ID,
                    LogPath            = CurrentUserConfig == null || CurrentUserConfig.LogPath == null ? ServiceConfig.UserLogPath : CurrentUserConfig.LogPath,
                    OpenOnStartup      = CurrentUserConfig == null || CurrentUserConfig.OpenOnStartup == null ? ServiceConfig.UserOpenOnStartup : CurrentUserConfig.OpenOnStartup,
                    CheckInterval      = CurrentUserConfig == null || CurrentUserConfig.CheckInterval == null ? ServiceConfig.UserCheckInterval : CurrentUserConfig.CheckInterval,
                    MinimizedWhenClose = CurrentUserConfig == null || CurrentUserConfig.MinimizedWhenClose == null ? ServiceConfig.UserMinimizedWhenClose : CurrentUserConfig.MinimizedWhenClose,
                    PCID               = CurrentUserConfig == null || CurrentUserConfig.PCID == null ? ServiceConfig.UserPCID : CurrentUserConfig.PCID,
                    PreviousPCID       = CurrentUserConfig == null || CurrentUserConfig.PreviousPCID == null ? ServiceConfig.UserPCID : CurrentUserConfig.PreviousPCID
                };
                SaveUserConfig();
            }
            // Since here, can use UserConfig
            SetupComputerSection();
            SetupConfigSection();
            SetupOptionSection();
            try
            {
                CheckTimer          = new System.Timers.Timer();
                CheckTimer.Elapsed += new ElapsedEventHandler(OnTimer);
                CheckTimer.Interval = 10;
                CheckTimer.Start();
            }
            catch (Exception ex)
            {
                EventLogger.WriteLog("TIMER ERROR", ex.ToString(), UserConfig.LogPath);
                EventLogger.WriteLog("", "Exiting....", UserConfig.LogPath);
                return;
            }

            if (args.TryGetValue("--minimized", out string value))
            {
                if (value.Equals("true"))
                {
                    //this.Hide();
                }
            }
        }