forked from OMARATION/mcforge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeart.cs
332 lines (301 loc) · 12.4 KB
/
Heart.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
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
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCForge)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
namespace MCForge
{
public static class Heart
{
//static int _timeout = 60 * 1000;
private static int max_retries = 3;
static string hash = null;
public static string serverURL;
static string DefaultParameters;
//static string players = "";
//static string worlds = "";
//static BackgroundWorker worker;
static Random MCForgeBeatSeed = new Random(Process.GetCurrentProcess().Id);
static StreamWriter beatlogger;
static System.Timers.Timer MinecraftBeatTimer = new System.Timers.Timer(500);
static System.Timers.Timer MCForgeBeatTimer;
static object Lock = new object();
public static void Init()
{
if(Server.logbeat)
{
if(!File.Exists("heartbeat.log"))
{
File.Create("heartbeat.log").Close();
}
}
MCForgeBeatTimer = new System.Timers.Timer(1000 + MCForgeBeatSeed.Next(0, 2500));
DefaultParameters = "port=" + Server.port +
"&max=" + Server.players +
"&name=" + UrlEncode(Server.name) +
"&public=" + Server.pub +
"&version=" + Server.version;
Thread backupThread = new Thread(new ThreadStart(delegate
{
MinecraftBeatTimer.Elapsed += delegate
{
MinecraftBeatTimer.Interval = 50000;
try
{
Pump(new MinecraftBeat());
Pump(new WOMBeat());
}
catch (Exception e) { Server.ErrorLog(e); }
};
MinecraftBeatTimer.Start();
Thread.Sleep(5000);
MCForgeBeatTimer.Elapsed += delegate
{
MCForgeBeatTimer.Interval = 10*60*1000; // 10 minutes
try
{
Pump(new MCForgeBeat());
}
catch (Exception e)
{
Server.ErrorLog(e);
}
};
MCForgeBeatTimer.Start();
}));
backupThread.Start();
}
public static bool Pump(Beat beat)
{
//lock (Lock)
//{
String beattype = beat.GetType().Name;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(beat.URL));
beat.Parameters = DefaultParameters;
if (beat.Log)
{
beatlogger = new StreamWriter("heartbeat.log", true);
}
int totalTries = 0;
int totalTriesStream = 0;
retry: try
{
totalTries++;
totalTriesStream = 0;
beat.Prepare();
if (beat.GetType() == typeof(MinecraftBeat))
File.WriteAllText("text/heartbeaturl.txt", beat.URL + "?" + beat.Parameters, Encoding.UTF8);
// Set all the request settings
//Server.s.Log(beat.Parameters);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (compatible; Konqueror/3.5; Windows NT 6.0) KHTML/3.5.6 (like Gecko)";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
byte[] formData = Encoding.ASCII.GetBytes(beat.Parameters);
request.ContentLength = formData.Length;
request.Timeout = 15000; // 15 seconds
retryStream: try
{
totalTriesStream++;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(formData, 0, formData.Length);
if (Server.logbeat && beat.Log)
{
BeatLog(beat, beattype + " request sent at " + DateTime.Now.ToString());
}
requestStream.Flush();
requestStream.Close();
}
}
catch (WebException e)
{
//Server.ErrorLog(e);
if (e.Status == WebExceptionStatus.Timeout)
{
if (Server.logbeat && beat.Log)
{
#if DEBUG
Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
#endif
BeatLog(beat, beattype + " timeout detected at " + DateTime.Now.ToString());
}
if (totalTriesStream < max_retries)
{
goto retryStream;
}
else
{
if (Server.logbeat && beat.Log)
BeatLog(beat, beattype + " timed out " + max_retries + " times. Aborting this request. " + DateTime.Now.ToString());
Server.s.Log(beattype + " timed out " + max_retries + " times. Aborting this request.");
//throw new WebException("Failed during request.GetRequestStream()", e.InnerException, e.Status, e.Response);
beatlogger.Close();
return false;
}
}
else if (Server.logbeat && beat.Log)
{
#if DEBUG
Server.s.Log(beattype + " non-timeout exception detected: " + e.Message);
#endif
BeatLog(beat, beattype + " non-timeout exception detected: " + e.Message);
BeatLog(beat, "Stack Trace: " + e.StackTrace);
}
}
//if (hash == null)
//{
using (WebResponse response = request.GetResponse())
{
using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
{
if (Server.logbeat && beat.Log)
{
#if DEBUG
Server.s.Log(beattype + " response received at " + DateTime.Now.ToString());
#endif
BeatLog(beat, beattype + " response received at " + DateTime.Now.ToString());
}
if (String.IsNullOrEmpty(hash) && response.ContentLength > 0)
{
// Instead of getting a single line, get the whole damn thing and we'll strip stuff out
string line = responseReader.ReadToEnd().Trim();
if (Server.logbeat && beat.Log)
{
BeatLog(beat, "Received: " + line);
}
beat.OnPump(line);
}
else
{
beat.OnPump(String.Empty);
}
}
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.Timeout)
{
if (Server.logbeat && beat.Log)
{
#if DEBUG
Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
#endif
BeatLog(beat, "Timeout detected at " + DateTime.Now.ToString());
}
Pump(beat);
}
}
catch (Exception)
{
if (Server.logbeat && beat.Log)
{
BeatLog(beat, beattype + " failure #" + totalTries + " at " + DateTime.Now.ToString());
}
if (totalTries < max_retries) goto retry;
if (Server.logbeat && beat.Log)
{
#if DEBUG
Server.s.Log(beattype + " failed " + max_retries + " times. Stopping.");
#endif
BeatLog(beat, "Failed " + max_retries + " times. Stopping.");
beatlogger.Close();
}
return false;
}
finally
{
request.Abort();
}
if (beatlogger != null)
{
beatlogger.Close();
}
//}
return true;
}
public static string UrlEncode(string input)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if ((input[i] >= '0' && input[i] <= '9') ||
(input[i] >= 'a' && input[i] <= 'z') ||
(input[i] >= 'A' && input[i] <= 'Z') ||
input[i] == '-' || input[i] == '_' || input[i] == '.' || input[i] == '~')
{
output.Append(input[i]);
}
else if (Array.IndexOf<char>(reservedChars, input[i]) != -1)
{
output.Append('%').Append(((int)input[i]).ToString("X"));
}
}
return output.ToString();
}
private static void BeatLog(Beat beat, string text)
{
if (Server.logbeat && beat.Log && beatlogger != null)
{
try
{
beatlogger.WriteLine(text);
}
catch { }
}
}
/* public static Int32 MACToInt()
{
var nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
if (Server.mono)
{
foreach (var nic in nics)
{
if (nic.Name != "l0")
{
nics[0] = nic;
break;
}
}
}
string MAC = nics[0].GetPhysicalAddress().ToString();
Regex regex = new Regex(@"[^0-9a-f]");
MAC = regex.Replace(MAC, "");
Int32 seed = 0;
retry: try
{
seed = Convert.ToInt32(MAC);
}
catch (OverflowException)
{
MAC = MAC.Remove(MAC.Length - 1);
goto retry;
}
catch(FormatException)
{
seed = new Random().Next();
}
return seed;
} */
public static char[] reservedChars = { ' ', '!', '*', '\'', '(', ')', ';', ':', '@', '&',
'=', '+', '$', ',', '/', '?', '%', '#', '[', ']' };
}
public enum BeatType { Minecraft, MCForge }
}