Example #1
0
        private static List <ApiKey> getKeyRing()
        {
            List <ApiKey> keyRing = new List <ApiKey>();

            log.Debug("Getting keyring.");
            string html = "";

            try {
                html = new Extensions.OgcsWebClient().DownloadString(keyringURL);
            } catch (System.Exception ex) {
                log.Error("Failed to retrieve keyring data: " + ex.Message);
            }
            if (!string.IsNullOrEmpty(html))
            {
                html = html.Replace("\n", "");
                MatchCollection keyRecords = findText(html, @"<table><thead>.*?<tbody>(<tr>.*?</tr>)</tbody></table>");
                if (keyRecords.Count == 0)
                {
                    log.Warn("Could not find table of keys.");
                    return(keyRing);
                }
                foreach (String record in keyRecords[0].Captures[0].Value.Split(new string[] { "<tr>" }, StringSplitOptions.None).Skip(2))
                {
                    MatchCollection keyAttributes = findText(record, @"<td.*?>(.*?)</td>");
                    if (keyAttributes.Count > 0)
                    {
                        try {
                            keyRing.Add(new ApiKey(keyAttributes));
                        } catch { }
                    }
                }
            }
            log.Debug("There are " + keyRing.Count + " keys.");
            return(keyRing);
        }
Example #2
0
        public static void Initialise()
        {
            if (Program.StartedWithSquirrelArgs && !(Environment.GetCommandLineArgs()[1].ToLower().Equals("--squirrel-firstrun")))
            {
                return;
            }
            if (Program.InDeveloperMode)
            {
                return;
            }

            //Note, logging isn't actually initialised yet, so log4net won't log any lines within this function

            String cloudCredsURL = "https://raw.githubusercontent.com/phw198/OutlookGoogleCalendarSync/master/docs/keyring.md";
            String html          = null;
            String line          = null;
            String placeHolder   = "###";
            String cloudID       = null;
            String cloudKey      = null;

            log.Debug("Getting credential attributes");
            try {
                try {
                    html = new Extensions.OgcsWebClient().DownloadString(cloudCredsURL);
                    html = html.Replace("\n", "");
                } catch (System.Exception ex) {
                    log.Error("Failed to retrieve data: " + ex.Message);
                }

                if (string.IsNullOrEmpty(html))
                {
                    throw new ApplicationException("Not able to retrieve error reporting credentials.");
                }

                Regex           rgx        = new Regex(@"### Error Reporting.*\|ID\|(.*)\|\|Key\|(.*?)\|", RegexOptions.IgnoreCase);
                MatchCollection keyRecords = rgx.Matches(html);
                if (keyRecords.Count == 1)
                {
                    cloudID  = keyRecords[0].Groups[1].ToString();
                    cloudKey = keyRecords[0].Groups[2].ToString();
                }
                else
                {
                    throw new ApplicationException("Unexpected parse of error reporting credentials.");
                }

                List <String> newLines = new List <string>();
                StreamReader  sr       = new StreamReader(templateCredFile);
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.IndexOf(placeHolder) > 0)
                    {
                        if (line.IndexOf("private_key_id") > 0)
                        {
                            line = line.Replace(placeHolder, cloudID);
                        }
                        else if (line.IndexOf("private_key") > 0)
                        {
                            line = line.Replace(placeHolder, cloudKey);
                        }
                    }
                    newLines.Add(line);
                }
                try {
                    File.WriteAllLines(credFile, newLines.ToArray());
                } catch (System.IO.IOException ex) {
                    if (OGCSexception.GetErrorCode(ex) == "0x80070020")
                    {
                        log.Warn("ErrorReporting.json is being used by another process (perhaps multiple instances of OGCS are being started on system startup?)");
                    }
                    else
                    {
                        throw;
                    }
                }
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credFile);
            } catch (ApplicationException ex) {
                log.Warn(ex.Message);
                Initialised = false;

                //} catch (System.Exception ex) {
                //Logging isn't initialised yet, so don't catch this error - let it crash out so user is aware and hopefully reports it!
                //System.Windows.Forms.OgcsMessageBox.Show(ex.Message);
                //log.Debug("Failed to initialise error reporting.");
                //OGCSexception.Analyse(ex);
            }
        }