-
Notifications
You must be signed in to change notification settings - Fork 4
/
GreeHandler.cs
131 lines (114 loc) · 5.61 KB
/
GreeHandler.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
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DummyServer
{
public class GreeHandlerResponse
{
public string text = "";
public bool keepAlive = false;
}
public class GreeHandler
{
public static GreeHandlerResponse process(string input)
{
input = input.Split("\n")[0];
JObject resObj = new JObject();
JObject resPackObj = new JObject();
GreeHandlerResponse response = new GreeHandlerResponse();
JObject req = JObject.Parse(input);
string cmd = (string)req["t"];
Console.WriteLine("################################################## "+DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine(input);
switch (cmd)
{
case "dis": // Discover
Console.WriteLine("Request: Discover");
string mac = (string)req["mac"];
resPackObj["datHost"] = Program.m_DomainName;
resPackObj["datHostPort"] = 5000;
resPackObj["host"] = Program.m_ExternalIp;
resPackObj["ip"] = Program.m_ExternalIp;
resPackObj["ip2"] = Program.m_ExternalIp;
resPackObj["protocol"] = "TCP";
resPackObj["t"] = "svr";
resPackObj["tcpPort"] = 5000;
resPackObj["udpPort"] = 5000;
resObj["cid"] = "";
resObj["i"] = 1;
resObj["pack"] = Crypter.Encrypt(resPackObj.ToString(), "");
resObj["t"] = "pack";
resObj["tcid"] = mac;
resObj["uid"] = 0;
//Console.WriteLine("response pack: "+resPackObj.ToString());
response.text = resObj.ToString();
response.keepAlive = false;
break;
case "pack": // look in Pack Object
JObject pack = JObject.Parse(Crypter.Decrypt((string)req["pack"], ""));
switch ((string)pack["t"])
{
case "devLogin":
Console.WriteLine("Request: devLogin ");
string obscuredMac = (string) pack["mac"];
char[] obscuredArr = obscuredMac.ToCharArray();
char[] normalizedArr = new char[12];
normalizedArr[0] = obscuredArr[8];
normalizedArr[1] = obscuredArr[9];
normalizedArr[2] = obscuredArr[14];
normalizedArr[3] = obscuredArr[15];
normalizedArr[4] = obscuredArr[2];
normalizedArr[5] = obscuredArr[3];
normalizedArr[6] = obscuredArr[10];
normalizedArr[7] = obscuredArr[11];
normalizedArr[8] = obscuredArr[4];
normalizedArr[9] = obscuredArr[5];
normalizedArr[10] = obscuredArr[0];
normalizedArr[11] = obscuredArr[1];
string normalizedMac = new string(normalizedArr);
resPackObj["t"] = "loginRes";
resPackObj["r"] = 200;
resPackObj["cid"] = normalizedMac;
resPackObj["uid"] = 0;
resObj["t"] = "pack";
resObj["i"] = 1;
resObj["tcid"] = "";
resObj["cid"] = "";
resObj["uid"] = 0;
resObj["pack"] = Crypter.Encrypt(resPackObj.ToString(), "");
//Console.WriteLine("response pack: "+resPackObj.ToString());
response.text = resObj.ToString();
response.keepAlive = true;
break;
default:
Console.WriteLine("Request Pack unknown: "+(string)pack["t"]);
response.text = "";
response.keepAlive = false;
break;
}
break;
case "tm": // get Time
Console.WriteLine("Request: Time");
resObj["t"] = "tm";
resObj["time"] = DateTime.Now.ToLocalTime().ToString("yyyy-MM-ddHH:mm:ss");
response.text = resObj.ToString();
response.keepAlive = true;
break;
case "hb": // Heartbeat
Console.WriteLine("Request: Heartbeat");
resObj["t"] = "hbok";
response.text = resObj.ToString();
response.keepAlive = true;
break;
default:
Console.WriteLine("Request: unknown: "+cmd);
response.text = "";
response.keepAlive = false;
break;
}
response.text = response.text.Trim().Replace("\r", "").Replace("\n", "").Replace(" ", "")+"\n";
Console.WriteLine("Response: "+response.text.Trim('\n'));
return response;
}
}
}