aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/VirusScanner.cs
blob: a13299679cfdeab95763171f53e7fb1accf02bc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
using System.Threading;
using Newtonsoft.Json;

namespace ShiftOS.WinForms.Applications
{
    [RequiresUpgrade("virus_scanner")]
    [WinOpen("virus_scanner")]
    [Launcher("Virus Scanner", false, null, "Utilities")]
    [DefaultTitle("Virus Scanner")]
    public partial class VirusScanner : UserControl, IShiftOSWindow
    {
        private int grade = 1;

        public VirusScanner()
        {
            InitializeComponent();
        }

        public void OnLoad()
        {
            pnlintro.BringToFront();
        }

        private List<Objects.ViralInfection> FoundViruses = new List<Objects.ViralInfection>();
        
        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            return true;
        }

        public void OnUpgrade()
        {
            grade = 1;
            if (Shiftorium.UpgradeInstalled("virus_scanner_grade_2"))
                grade++;
            if (Shiftorium.UpgradeInstalled("virus_scanner_grade_3"))
                grade++;
            if (Shiftorium.UpgradeInstalled("virus_scanner_grade_4"))
                grade++;
            SetupStatus();
        }

        public void SetupStatus()
        {
            lbstatus.Text = $"Grade: {grade}";
        }

        private string _scanMessage = "We are currently scanning your system. Please wait.";
        private int _threatsFound = 0;

        public int ThreatsFound
        {
            get
            {
                return _threatsFound;
            }
            set
            {
                _threatsFound = value;
                lbscanstatus.Text = $@"{_scanMessage}

Threats found: {_threatsFound}";
            }
        }

        public string ScannerMessage
        {
            get
            {
                return _scanMessage;
            }
            set
            {
                _scanMessage = value;
                lbscanstatus.Text = $@"{_scanMessage}

Threats found: {_threatsFound}";
                
            }
        }

        private void btnscanmem_Click(object sender, EventArgs e)
        {
            ScannerMessage = "We are currently scanning the system memory. Please wait.";
            ThreatsFound = 0;
            ShowScannerUI();
            ScanMemory();
        }

        public void ScanMemory()
        {
            pgscannerprogress.Maximum = SaveSystem.CurrentSave.ViralInfections.Count;
            var t = new Thread(() =>
            {
                foreach(var virus in SaveSystem.CurrentSave.ViralInfections)
                {
                    Thread.Sleep(1000);
                    Desktop.InvokeOnWorkerThread(() =>
                    {
                        pgscannerprogress.Value++;
                        ThreatsFound++;
                        if(virus.ThreatLevel <= grade)
                        {
                            FoundViruses.Add(virus);
                        }
                    });
                }
                Desktop.InvokeOnWorkerThread(() =>
                {
                    SetupSummary();
                });
            });
            t.IsBackground = true;
            t.Start();
        }

        public void SetupSummary()
        {
            flviruses.Controls.Clear();
            if(FoundViruses.Count == 0)
            {
                var noViruses = new Label();
                noViruses.Text = "No threats found.";
                noViruses.Tag = "header3";
                Tools.ControlManager.SetupControls(noViruses);
                noViruses.AutoSize = true;
                flviruses.Controls.Add(noViruses);
                noViruses.Show();
            }
            foreach(var virus in FoundViruses)
            {
                var headerLabel = new Label();
                headerLabel.Text = $"virus.th{virus.ThreatLevel}.{virus.ID}";
                flviruses.Controls.Add(headerLabel);
                headerLabel.Tag = "header3";
                headerLabel.MaximumSize = new Size(((flviruses.Width) - flviruses.Padding.Right) - flviruses.Padding.Left, 0);
                headerLabel.AutoSize = true;
                Tools.ControlManager.SetupControl(headerLabel);
                headerLabel.Show();

                var descLabel = new Label();
                descLabel.MaximumSize = new Size(((flviruses.Width) - flviruses.Padding.Right) - flviruses.Padding.Left, 0);
                descLabel.AutoSize = true;
                var virusInfo = GetVirusInformation(virus.ID);
                descLabel.Text = $@"Real name: {virusInfo.Name}
Description: {virusInfo.Description}";
                if(virus is FileViralInfection)
                {
                    descLabel.Text += Environment.NewLine + "File path: " + (virus as FileViralInfection).FilePath;
                }
                flviruses.Controls.Add(descLabel);
                descLabel.Show();

                var cleanButton = new Button();
                cleanButton.Text = "Disinfect";
                cleanButton.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                cleanButton.AutoSize = true;
                cleanButton.Click += (o, a) =>
                {
                    Disinfect(virus);
                };
                flviruses.Controls.Add(cleanButton);
                Tools.ControlManager.SetupControl(cleanButton);
                cleanButton.Show();
            }
            pnlsummary.BringToFront();
        }

        public void Disinfect(Objects.ViralInfection virus)
        {
            if (FoundViruses.Contains(virus))
            {
                FoundViruses.Remove(virus);
                VirusManager.Disinfect(virus.ID);
                if(virus is FileViralInfection)
                {
                    var fVirus = virus as FileViralInfection;
                    if (Objects.ShiftFS.Utils.FileExists(fVirus.FilePath))
                    {
                        var headerText = Objects.ShiftFS.Utils.GetHeaderText(fVirus.FilePath);
                        try
                        {
                            var list = JsonConvert.DeserializeObject<List<FileViralInfection>>(headerText);
                            var hVirus = list.FirstOrDefault(x => x.ID == virus.ID && x.ThreatLevel == virus.ThreatLevel);
                            list.Remove(hVirus);
                            Objects.ShiftFS.Utils.SetHeaderText(fVirus.FilePath, JsonConvert.SerializeObject(list));
                        }
                        catch
                        {

                        }
                    }
                }
            }
            SetupSummary();
        }

        public VirusAttribute GetVirusInformation(string id)
        {
            foreach(var type in ReflectMan.Types.Where(x => x.GetInterfaces().Contains(typeof(IVirus)) && Shiftorium.UpgradeAttributesUnlocked(x)))
            {
                var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is VirusAttribute) as VirusAttribute;
                if(attrib != null)
                {
                    if (attrib.ID == id)
                        return attrib;
                }
            }
            return null;
        }

        public void ShowScannerUI()
        {
            pgscannerprogress.Value = 0;
            pnlscanner.BringToFront();
        }

        private int filesScanned = 0;

        public void ScanFile(string path)
        {
            Thread.Sleep(25);
            var headerinfo = Objects.ShiftFS.Utils.GetHeaderText(path);
            filesScanned++;
            Desktop.InvokeOnWorkerThread(() =>
            {
                ScannerMessage = "Scanning file: " + path + Environment.NewLine + "Files scanned: " + filesScanned.ToString();
            });


            try
            {
                
                var list = JsonConvert.DeserializeObject<List<Objects.ViralInfection>>(headerinfo).Where(x => x.ThreatLevel <= grade);
                foreach (var virus in list)
                {
                    Thread.Sleep(50);
                    Desktop.InvokeOnWorkerThread(() =>
                    {
                        ThreatsFound++;
                    });
                    FoundViruses.Add(new FileViralInfection
                    {
                        ID = virus.ID,
                        ThreatLevel = virus.ThreatLevel,
                        FilePath = path
                    });
                    
                }

            }
            catch { }

        }

        public void ScanDirectory(string dir)
        {
            if (Objects.ShiftFS.Utils.DirectoryExists(dir))
            {
                foreach (var file in Objects.ShiftFS.Utils.GetFiles(dir))
                    ScanFile(file);
                foreach (var subdir in Objects.ShiftFS.Utils.GetDirectories(dir))
                    ScanDirectory(subdir);
            }
        }

        private void btnscanfile_Click(object sender, EventArgs e)
        {
            FileSkimmerBackend.GetFile(new[] { "" }, FileOpenerStyle.Open, (path) =>
             {
                 ScanFile(path);
                 SetupSummary();
             });
        }

        private void btnscanfs_Click(object sender, EventArgs e)
        {
            filesScanned = 0;
            ShowScannerUI();
            pgscannerprogress.Hide();
            ThreatsFound = 0;
            var t = new Thread(() =>
            {
                foreach(var mount in Objects.ShiftFS.Utils.Mounts)
                {
                    var index = Objects.ShiftFS.Utils.Mounts.IndexOf(mount);
                    ScanDirectory(index.ToString() + ":");
                }
                Desktop.InvokeOnWorkerThread(() =>
                {
                    SetupSummary();
                });
            });
            t.Start();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            AppearanceManager.Close(this);
        }
    }

    public class FileViralInfection : Objects.ViralInfection
    {
        public string FilePath { get; set; }
    }
}