コード例 #1
0
        public ErrorTrackerSvc()
        {
            Settings.data.Load();

            Logger.CatchAll((sender, e) =>
            {
                Emailer.SendError(null, sender, e);
            });

            Settings.data.SaveIfNoExist();

            if (Settings.data.CountUsers() == 0)
            {
                User defaultAdmin = new User("admin", "admin", null, true)
                {
                    Permanent = true
                };
                Settings.data.TryAddUser(defaultAdmin);
                defaultAdmin.InitializeUserId();
                Settings.data.Save();
            }
            if (string.IsNullOrWhiteSpace(Settings.data.systemName))
            {
                Settings.data.systemName = "Error Tracker";
                Settings.data.Save();
            }
            if (string.IsNullOrWhiteSpace(Settings.data.privateSigningKey))
            {
                Settings.data.privateSigningKey = new SignatureFactory().ExportPrivateKey();
                Settings.data.Save();
            }
            if (string.IsNullOrWhiteSpace(Settings.data.vapidPrivateKey) ||
                string.IsNullOrWhiteSpace(Settings.data.vapidPublicKey))
            {
                WebPush.VapidDetails vapidKeys = WebPush.VapidHelper.GenerateVapidKeys();
                Settings.data.vapidPrivateKey = vapidKeys.PrivateKey;
                Settings.data.vapidPublicKey  = vapidKeys.PublicKey;
                foreach (User u in Settings.data.GetAllUsers())
                {
                    u.ClearAllPushNotificationSubscriptions();
                }
                Settings.data.Save();
            }
            BPUtil.PasswordReset.StatelessPasswordResetBase.Initialize(Settings.data.privateSigningKey);

            // Initialize User IDs.
            bool setAny = false;

            foreach (User user in Settings.data.GetAllUsers())
            {
                if (user.InitializeUserId())
                {
                    setAny = true;
                }
            }
            if (setAny)
            {
                Settings.data.Save();
            }

            InitializeComponent();

            SvcName = this.ServiceName;

            ICertificateSelector certSelector = null;

            if (!string.IsNullOrWhiteSpace(Settings.data.certificatePath) && File.Exists(Settings.data.certificatePath))
            {
                X509Certificate2 cert;
                if (!string.IsNullOrWhiteSpace(Settings.data.certificatePassword))
                {
                    cert = new X509Certificate2(Settings.data.certificatePath, Settings.data.certificatePassword);
                }
                else
                {
                    cert = new X509Certificate2(Settings.data.certificatePath);
                }
                certSelector = SimpleCertificateSelector.FromCertificate(cert);
            }
            SimpleHttpLogger.RegisterLogger(Logger.httpLogger);
            srv = new WebServer(Settings.data.port_http, Settings.data.port_https, certSelector, IPAddress.Any);

            thrMaintainProjects              = new Thread(maintainProjects);
            thrMaintainProjects.Name         = "Maintain Projects";
            thrMaintainProjects.IsBackground = false;
        }
コード例 #2
0
 private void MvcErrorHandler(RequestContext context, Exception ex)
 {
     Emailer.SendError(context, "An unhandled exception was thrown while processing an MVC request.", ex);
 }
コード例 #3
0
ファイル: PushManager.cs プロジェクト: bp2008/ErrorTracker
        private static void doPushBackgroundWork()
        {
            try
            {
                WebPush.WebPushClient client = new WebPush.WebPushClient();
                while (true)
                {
                    try
                    {
                        if (!newEvents.IsEmpty)
                        {
                            // Accumulate a list of events we want to notify each subscription about.
                            // Map subscriptionkey > event list
                            Dictionary <string, List <EventToNotifyAbout> > accumulatedEvents = new Dictionary <string, List <EventToNotifyAbout> >();
                            List <User> users = Settings.data.GetAllUsers();
                            while (newEvents.TryDequeue(out EventToNotifyAbout en))
                            {
                                foreach (User u in users)
                                {
                                    string[] subscriptionKeys = u.GetPushNotificationSubscriptions(en.projectName, en.ev.FolderId);
                                    foreach (string key in subscriptionKeys)
                                    {
                                        List <EventToNotifyAbout> events;
                                        if (!accumulatedEvents.TryGetValue(key, out events))
                                        {
                                            accumulatedEvents[key] = events = new List <EventToNotifyAbout>();
                                        }
                                        events.Add(en);
                                    }
                                }
                            }

                            if (accumulatedEvents.Count > 0)
                            {
                                WebPush.VapidDetails vapidDetails = new WebPush.VapidDetails(GetVapidSubject(), Settings.data.vapidPublicKey, Settings.data.vapidPrivateKey);
                                // Build and send one notification to each affected subscription.
                                foreach (KeyValuePair <string, List <EventToNotifyAbout> > kvp in accumulatedEvents)
                                {
                                    string subscriptionKey           = kvp.Key;
                                    List <EventToNotifyAbout> events = kvp.Value;

                                    WebPush.PushSubscription subscription = null;
                                    try
                                    {
                                        dynamic dyn = JsonConvert.DeserializeObject(subscriptionKey);
                                        subscription          = new WebPush.PushSubscription();
                                        subscription.Endpoint = dyn.endpoint;
                                        subscription.P256DH   = dyn.keys.p256dh;
                                        subscription.Auth     = dyn.keys.auth;
                                    }
                                    catch { }

                                    // For now, we'll just ignore any unparseable subscription keys.
                                    // I know this is asking for trouble later on, and I'm sorry for that.
                                    if (subscription != null)
                                    {
                                        StringBuilder sb = new StringBuilder();

                                        PushMessage message = new PushMessage(Settings.data.systemName, events.Count + " new events:");
                                        if (events.Count == 1)
                                        {
                                            EventToNotifyAbout en = events[0];
                                            message.message = en.ev.EventType.ToString() + ": " + en.ev.SubType;
                                            message.eventid = en.ev.EventId;
                                        }

                                        // If all events are in the same project, set message.project so that clicking the notification can open the correct project.
                                        string projectName = events[0].projectName;
                                        if (events.All(en => en.projectName == projectName))
                                        {
                                            message.project = projectName;

                                            // If all events are in the same folder, set message.folderid so that clicking the notification can open the correct folder.
                                            int folderId = events[0].ev.FolderId;
                                            if (events.All(en => en.ev.FolderId == folderId))
                                            {
                                                message.folderid = folderId;
                                            }
                                        }

                                        try
                                        {
                                            client.SendNotification(subscription, message.ToString(), vapidDetails);
                                        }
                                        catch (Exception ex)
                                        {
                                            Logger.Debug(ex, "Failed to send push notification.");
                                        }
                                    }
                                }
                            }
                        }
                        Thread.Sleep(1000);
                    }
                    catch (ThreadAbortException) { }
                    catch (Exception ex)
                    {
                        Emailer.SendError(null, "Error in PushManager", ex);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
        }