-
Notifications
You must be signed in to change notification settings - Fork 30
/
Bans.cs
279 lines (238 loc) · 9.39 KB
/
Bans.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
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
/*
* Starrybound Server
* Copyright 2013, Avilance Ltd
* Created by Zidonuke ([email protected]) and Crashdoom ([email protected])
*
* This file is a part of Starrybound Server.
* Starrybound Server 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.
* Starrybound Server 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 Starrybound Server. If not, see http://www.gnu.org/licenses/.
*/
using com.avilance.Starrybound.Util;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace com.avilance.Starrybound
{
public class Ban
{
public int banID;
public int timeBanned;
public string admin;
public int expiry;
public string reason;
public string username;
public string uuid;
public string ipaddress;
public Ban(int banID, string username, string uuid, string ipaddress, int timeBanned, string admin, int expiry, string reason)
{
this.banID = banID;
this.username = username;
this.uuid = uuid;
this.ipaddress = ipaddress;
this.timeBanned = timeBanned;
this.admin = admin;
this.expiry = expiry;
this.reason = reason;
}
public string getReason() { return this.reason; }
public string getExpiry() { return this.expiry.ToString(); }
public bool hasExpired()
{
if (this.expiry == 0) return false;
else if (Utils.getTimestamp() > this.expiry) return true;
else return false;
}
public bool doesMatch(string[] needle)
{
if (username.Equals(needle[0]) && !String.IsNullOrWhiteSpace(username)) return true;
else if (uuid.Equals(needle[1]) && !String.IsNullOrWhiteSpace(uuid)) return true;
else if (ipaddress.Equals(needle[2]) && !String.IsNullOrWhiteSpace(ipaddress)) return true;
else return false;
}
public void remove()
{
Bans.allBans.Remove(banID);
Bans.Write(Path.Combine(StarryboundServer.SavePath, "bans.json"));
}
}
public static class Bans
{
public static Dictionary<int, Ban> allBans = new Dictionary<int, Ban>();
public static Dictionary<int, Ban> legacyBans = new Dictionary<int, Ban>();
static int nextBanID = 1;
static bool writeBans = true;
public static void ProcessBans ()
{
readLegacyBans();
List<Ban> bans = Read(Path.Combine(StarryboundServer.SavePath, "bans.json"));
foreach (Ban ban in bans)
{
allBans.Add(ban.banID, ban);
nextBanID = ban.banID + 1;
}
foreach (Ban lBan in legacyBans.Values)
{
if (!allBans.ContainsKey(lBan.banID))
{
allBans.Add(lBan.banID, lBan);
}
}
int removedCount = 0;
foreach (Ban ban in allBans.Values)
{
if (ban.hasExpired()) { ban.remove(); removedCount++; }
}
Write(Path.Combine(StarryboundServer.SavePath, "bans.json"));
StarryboundServer.logInfo(allBans.Count + " ban(s) have been loaded from file. " + removedCount + " ban(s) have expired and been removed.");
}
/// <summary>
/// Checks to see if the data matches the ban record
/// </summary>
/// <param name="args">Username, UUID, IP Address</param>
/// <returns>empty array if no match, or a string array with reason and expiry</returns>
public static string[] checkForBan(string[] args)
{
foreach (Ban banData in allBans.Values)
{
if (banData.hasExpired()) banData.remove();
if (banData.doesMatch(args)) return new string[] { banData.getReason(), banData.getExpiry() };
}
return new string[] { };
}
public static bool addNewBan(string username, string uuid, string ipaddress, int timeBanned, string admin, int expiry, string reason)
{
string[] args = new string[8];
args[0] = nextBanID.ToString();
args[1] = username;
args[2] = uuid;
args[3] = ipaddress;
args[4] = timeBanned.ToString();
args[5] = admin;
args[6] = expiry.ToString();
args[7] = reason;
try
{
Ban ban = new Ban(nextBanID, username, uuid, ipaddress, timeBanned, admin, expiry, reason);
allBans.Add(nextBanID, ban);
Write(Path.Combine(StarryboundServer.SavePath, "bans.json"));
nextBanID++;
}
catch (Exception e) {
StarryboundServer.logException("Unable to write ban to banned-players.txt: " + e.Message);
return false;
}
return true;
}
public static List<Ban> Read(string path)
{
if (!File.Exists(path))
{
return new List<Ban>();
}
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
List<Ban> file = Read(fs);
return file;
}
}
public static List<Ban> Read(Stream stream)
{
try
{
using (var sr = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<List<Ban>>(sr.ReadToEnd());
}
}
catch (Exception)
{
StarryboundServer.logException("Server ban file is not readable - Bans WILL NOT operate until this issue is fixed.");
writeBans = false;
return new List<Ban>();
}
}
public static void Write(string path)
{
if (!writeBans) return;
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
Write(fs);
}
}
public static void Write(Stream stream)
{
List<Ban> banList;
if (StarryboundServer.groups.Count > 0)
{
banList = new List<Ban>();
foreach (Ban ban in allBans.Values)
{
banList.Add(ban);
}
}
else banList = new List<Ban>();
var str = JsonConvert.SerializeObject(banList, Formatting.Indented);
using (var sw = new StreamWriter(stream))
{
sw.Write(str);
}
}
#region Legacy Bans
public static void readLegacyBans()
{
try
{
StreamReader reader = File.OpenText(Path.Combine(StarryboundServer.SavePath, "banned-players.txt"));
string line;
int banCount = 0;
while ((line = reader.ReadLine()) != null)
{
string[] args = line.Split('|');
/*
* Ban format:
* int ID
* string Username
* string UUID
* string IP Address
* timestamp UnixTimestamp (Time of Addition)
* string Admin
* timestamp Expiry
* string Reason
*
* Example:
* 2|User|133bfef193364620513ea1980ba39dc3|127.0.0.1|1387767903|Crashdoom|0|Griefing the spawn
*
*/
if (args.Length != 8) continue;
try
{
int banID = int.Parse(args[0]);
string username = args[1];
string uuid = args[2];
string ipaddress = args[3];
int timeBanned = int.Parse(args[4]);
string admin = args[5];
int expiry = int.Parse(args[6]);
string reason = args[7];
Ban ban = new Ban(banID, username, uuid, ipaddress, timeBanned, admin, expiry, reason);
legacyBans.Add(banID, ban);
nextBanID = banID + 1;
banCount++;
}
catch (Exception) { banCount--; StarryboundServer.logWarn("Invalid ban detected in banned-players.txt"); }
}
reader.Close();
}
catch (Exception e)
{
StarryboundServer.logWarn("Unable to read bans from legacy banned-players.txt: " + e.Message);
}
}
#endregion Legacy
}
}