-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPotatoWallClient.cs
210 lines (173 loc) · 8.75 KB
/
PotatoWallClient.cs
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
/*
* This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/).
* Copyright (c) 2023 POQDavid
* Copyright (c) contributors
*
* PotatoWall is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PotatoWall is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PotatoWall. If not, see <https://www.gnu.org/licenses/>.
*/
namespace PotatoWall;
/// <copyright file="PotatoWallClient.cs" company="POQDavid">
/// Copyright (c) POQDavid. All rights reserved.
/// </copyright>
/// <author>POQDavid</author>
/// <summary>This is the PotatoWallClient class.</summary>
public partial class PotatoWallClient
{
public static string AppDataPath { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PotatoWall");
public static string DataPath { get; set; } = Path.Combine(AppDataPath, @"data");
public static string LogsPath { get; set; } = Path.Combine(AppDataPath, @"logs");
public static string ListsPath { get; set; } = Path.Combine(AppDataPath, @"lists");
public static string ColorDataPath { get; set; } = Path.Combine(AppDataPath, "Colors.data");
///<summary>
/// Gets or sets the dataDir property.
///</summary>
///<value>Directory for the plugin's data and settings.</value>
public static string SettingPath { get; set; } = Path.Combine(AppDataPath, "Default.json");
///<summary>
/// Gets or sets the iSettings property.
///</summary>
///<value>Plugin Settings.</value>
public static Setting.Settings ISettings { get; set; } = new();
private static PotatoWallUI iPotatoWallUI;
private static Logger logger;
private static TextWriter potatoWriter;
public static Uri CityDBIPurl { get; set; } = new Uri("https://" + $"download.db-ip.com/free/dbip-city-lite-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.mmdb.gz");
public static string CityDBIPSavePath { get; set; } = Path.Combine(AppDataPath, $@"data\dbip-city-lite-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.mmdb.gz");
public static Uri ASNDBIPurl { get; set; } = new Uri("https://" + $"download.db-ip.com/free/dbip-asn-lite-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.mmdb.gz");
public static string ASNDBIPSavePath { get; set; } = Path.Combine(AppDataPath, $@"data\dbip-asn-lite-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.mmdb.gz");
public static Uri CityMMurl { get; set; }
public static Uri CityMMsha256url { get; set; }
public static string CityMMSavePath { get; set; } = Path.Combine(AppDataPath, $@"data\mm-city-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.tar.gz");
public static Uri AsnMMurl { get; set; }
public static Uri AsnMMsha256url { get; set; }
public static string AsnMMSavePath { get; set; } = Path.Combine(AppDataPath, $@"data\mm-asn-{DateTime.Now.Year}-{DateTime.Now.Month:d2}.tar.gz");
public static string CityDBIPPath { get; set; } = Path.Combine(AppDataPath, @"data\dbip-city.mmdb");
public static string ASNDBIPPath { get; set; } = Path.Combine(AppDataPath, @"data\dbip-asn.mmdb");
public static string CityMMPath { get; set; } = Path.Combine(AppDataPath, @"data\mm-city.mmdb");
public static string AsnMMPath { get; set; } = Path.Combine(AppDataPath, @"data\mm-asn.mmdb");
public static Reader CityMMDBReader { get; set; }
public static Reader ASNMMDBReader { get; set; }
public static Logger Logger { get => logger; set => logger = value; }
public static TextWriter PotatoWriter { get => potatoWriter; set => potatoWriter = value; }
public static string WhiteListPath { get; set; } = Path.Combine(ListsPath, "WhiteList.json");
public static string BlackListPath { get; set; } = Path.Combine(ListsPath, "BlackList.json");
public static void InitializeComponent()
{
Thread.CurrentThread.Name = "MainThread";
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
.Build();
logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
logger.Information("Initializing resources for Client");
ISettings.LoadSettings();
logger.Information("Settings loaded");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
CityMMurl = new Uri($"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key={ISettings.GeoIP.LicenseKEY}&suffix=tar.gz");
CityMMsha256url = new Uri($"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key={ISettings.GeoIP.LicenseKEY}&suffix=tar.gz.sha256");
AsnMMurl = new Uri($"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key={ISettings.GeoIP.LicenseKEY}&suffix=tar.gz");
AsnMMsha256url = new Uri($"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key={ISettings.GeoIP.LicenseKEY}&suffix=tar.gz.sha256");
if (Directory.Exists(AppDataPath))
{
logger.Information("AppData Path: {AppDataPath}", AppDataPath);
}
else
{
logger.Information("Creating AppData folder {AppDataPath}", AppDataPath); Directory.CreateDirectory(AppDataPath);
}
if (Directory.Exists(DataPath))
{
logger.Information("Data Path: {DataPath}", DataPath);
}
else
{
logger.Information("Creating Data folder {DataPath}", DataPath); Directory.CreateDirectory(DataPath);
}
if (Directory.Exists(LogsPath))
{
logger.Information("Logs Path: {LogsPath}", LogsPath);
}
else
{
Directory.CreateDirectory(LogsPath); logger.Information("Creating Logs folder {LogsPath}", LogsPath);
}
if (Directory.Exists(ListsPath))
{
logger.Information("Lists Path: {ListsPath}", ListsPath);
}
else
{
Directory.CreateDirectory(ListsPath); logger.Information("Creating Lists folder {ListsPath}", ListsPath);
}
LoadGeoIPDBs();
iPotatoWallUI = new PotatoWallUI();
}
public static void LoadGeoIPDBs()
{
if (ISettings.GeoIP.GeoIPDBProvider == "DBIP")
{
if (File.Exists(ASNDBIPPath))
{
logger.Information("Loading {ASNDBPath} database.", ASNDBIPPath); ASNMMDBReader = new(ASNDBIPPath);
}
else
{
logger.Information("{ASNDBPath} database does not Exist.", ASNDBIPPath);
}
if (File.Exists(CityDBIPPath))
{
logger.Information("Loading {CityDBPath} database.", CityDBIPPath); CityMMDBReader = new(CityDBIPPath);
}
else
{
logger.Information("{CityDBPath} database does not Exist.", CityDBIPPath);
}
}
else
{
if (File.Exists(AsnMMPath))
{
logger.Information("Loading {AsnMMPath} database.", AsnMMPath); ASNMMDBReader = new(AsnMMPath);
}
else
{
logger.Information("{AsnMMPath} database does not Exist.", AsnMMPath);
}
if (File.Exists(CityMMPath))
{
logger.Information("Loading {CityDBPath} database.", CityMMPath); CityMMDBReader = new(CityMMPath);
}
else
{
logger.Information("{CityDBPath} database does not Exist.", CityMMPath);
}
}
}
public static void Run()
{
logger.Information("Initialized resources for Client");
iPotatoWallUI.Show();
}
}
[Flags]
public enum LogLevel
{
Info,
Error,
Trace
}