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
|
#include "common.h"
#include "server_con.h"
#include "net.h"
#include "arc4.h"
#include "options.h"
#define SERVER_READ_BUFFER_SIZE (1024 * 32)
/* TODO: obtain IPs of network interfaces from user's machine, instead of
* hardcoding these values below (used in msim_compute_login_response).
* This is not immediately
* important because you can still connect and perform basic
* functions of the protocol. There is also a high chance that the addreses
* are RFC1918 private, so the servers couldn't do anything with them
* anyways except make note of that fact. Probably important for any
* kind of direct connection, or file transfer functionality.
*/
#define LOGIN_IP_LIST "\x00\x00\x00\x00\x05\x7f\x00\x00\x01\x00\x00\x00\x00\x0a\x00\x00\x40\xc0\xa8\x58\x01\xc0\xa8\x3c\x01"
#define LOGIN_IP_LIST_LEN 25
#define NONCE_SIZE 0x20
int status = ID_STATUS_OFFLINE;
bool server_running = false, server_stop = false;
HANDLE server_connection = 0;
int sesskey = 0, req_id = 1;
void SendMessage(ClientNetMessage &msg) {
char packet[4096];
int packet_size = msg.make_packet(packet, 4096);
if(server_connection) Netlib_Send(server_connection, packet, packet_size, MSG_DUMPASTEXT);
}
void CALLBACK sttMainThreadStatusCallback( ULONG dwParam ) {
if(status != dwParam) {
int previous_status = status;
status = dwParam;
ProtoBroadcastAck(MODULE,NULL,ACKTYPE_STATUS,ACKRESULT_SUCCESS, (HANDLE)previous_status, status);
}
}
int waitcallback(unsigned int *timeout) {
//PUShowMessage("waitcallback", SM_NOTIFY);
return server_stop ? 0 : 1;
}
int try_ports[9] = {1863, 6660,6661,6662,6665,6668,6669,80,0443};
void try_login(NetMessage &msg, HANDLE connection) {
char nonce[NONCE_SIZE * 2 + 2], *nc1 = nonce, *nc2 = nonce + NONCE_SIZE;
int size = NONCE_SIZE * 2 + 2;
if(msg.get_data("nc", nonce, &size) && size == NONCE_SIZE * 2) {
SHA1_INTERFACE sha1;
mir_sha1_ctx sha1_ctx;
mir_getSHA1I(&sha1);
ARC4_INTERFACE arc4;
mir_arc4_ctx arc4_ctx;
mir_getARC4I(&arc4);
char *ch_resp;
mir_sha1_byte_t pw_hash[20];
int ch_resp_size;
wchar_t wpw[256];
mir_sha1_byte_t key[20];
char email[256];
#ifdef _UNICODE
_tcscpy(wpw, options.pw);
WideCharToMultiByte(CP_UTF8, 0, options.email, -1, email, 256, 0, 0);
#else
strcpy(email, options.email);
MultiByteToWideChar(code_page, 0, options.pw, -1, wpw, 256);
#endif
sha1.sha1_hash((mir_sha1_byte_t*)wpw, wcslen(wpw) * sizeof(wchar_t), pw_hash);
sha1.sha1_init(&sha1_ctx);
sha1.sha1_append(&sha1_ctx, (mir_sha1_byte_t*)pw_hash, 20);
sha1.sha1_append(&sha1_ctx, (mir_sha1_byte_t*)nc2, NONCE_SIZE);
sha1.sha1_finish(&sha1_ctx, key);
arc4.arc4_init(&arc4_ctx, (char *)key, 0x10);
ch_resp_size = NONCE_SIZE + strlen(email) + LOGIN_IP_LIST_LEN;
ch_resp = new char[ch_resp_size];
memcpy(ch_resp, nc1, NONCE_SIZE);
memcpy(ch_resp + NONCE_SIZE, email, strlen(email));
memcpy(ch_resp + NONCE_SIZE + strlen(email), LOGIN_IP_LIST, LOGIN_IP_LIST_LEN);
arc4.arc4_crypt(&arc4_ctx, ch_resp, ch_resp, ch_resp_size);
ClientNetMessage reply;
reply.add_int("login2", 196610);
reply.add_string("username", email);
reply.add_data("response", ch_resp, ch_resp_size);
reply.add_int("clientver", 673);
reply.add_int("reconn", 0);
reply.add_int("status", 100);
reply.add_int("id", 1);
delete[] ch_resp;
SendMessage(reply);
} else {
PUShowMessage("Nonce format error", SM_NOTIFY);
}
}
void __cdecl ServerThreadFunc(void*) {
NETLIBOPENCONNECTION conn_data = {0};
conn_data.cbSize = sizeof(NETLIBOPENCONNECTION);
conn_data.flags = NLOCF_V2;
conn_data.szHost = "im.myspace.akadns.net";
conn_data.wPort = DBGetContactSettingDword(0, MODULE, "LastPort", try_ports[0]);
conn_data.timeout = 10;
conn_data.waitcallback = waitcallback;
int conn_stat = ID_STATUS_CONNECTING;
QueueUserAPC(sttMainThreadStatusCallback, mainThread, conn_stat);
server_running = true;
char *recv_buffer = new char[SERVER_READ_BUFFER_SIZE];
int bytes = 0;
char mt[256];
int tries = 0;
bool login = true;
HANDLE connection = 0;
while(!Miranda_Terminated() && !server_stop) {
if(login) {
if(connection) Netlib_CloseHandle(connection);
QueueUserAPC(sttMainThreadStatusCallback, mainThread, conn_stat++);
connection = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)hNetlibUser, (LPARAM)&conn_data);
}
bytes = Netlib_Recv(connection, (char *)recv_buffer, SERVER_READ_BUFFER_SIZE, MSG_DUMPASTEXT);
if(bytes == 0) {
PUShowMessage("Connection closed", SM_NOTIFY);
if(login && tries < 9) {
conn_data.wPort = try_ports[tries++];
} else
break;
} else if(bytes == SOCKET_ERROR) {
PUShowMessage("Socket ERROR", SM_NOTIFY);
if(login && tries < 9) {
conn_data.wPort = try_ports[tries++];
} else
break;
} else {
if(login) {
QueueUserAPC(sttMainThreadStatusCallback, mainThread, conn_stat++);
login = false;
server_connection = connection;
DBWriteContactSettingDword(0, MODULE, "LastPort", conn_data.wPort);
}
//mir_snprintf(mt, 256, "recvd %d bytes", bytes);
//PUShowMessage(mt, SM_NOTIFY);
NetMessage msg;
msg.parse(recv_buffer, bytes);
if(msg.exists(NMString("error"))) {
char errmsg[256];
if(msg.get_string("errmsg", errmsg, 256))
PUShowMessage(errmsg, SM_WARNING);
} else if(msg.get_int("lc") == 1) {
QueueUserAPC(sttMainThreadStatusCallback, mainThread, conn_stat++);
try_login(msg, server_connection);
} else if(msg.get_int("lc") == 2) {
sesskey = msg.get_int("sesskey");
DBWriteContactSettingDword(0, MODULE, "UID", msg.get_int("userid"));
char nick[256];
if(msg.get_string("uniquenick", nick, 256))
DBWriteContactSettingStringUtf(0, MODULE, "Nick", nick);
QueueUserAPC(sttMainThreadStatusCallback, mainThread, ID_STATUS_ONLINE);
} else if(msg.exists(NMString("persistr"))) {
int cmd, dsn, lid, req;
cmd = msg.get_int("cmd") & 255;
dsn = msg.get_int("dsn");
lid = msg.get_int("lid");
req = msg.get_int("rid");
mir_snprintf(mt, 256, "Peristr message: type is %d,%d,%d", cmd, dsn, lid);
PUShowMessage(mt, SM_NOTIFY);
if(cmd == 1 && dsn == 5 && lid == 7) {
Dictionary body = msg.get_dict("body");
char email[256], nick[256];
int uid = body.get_int("UserID");
if(uid != 0) {
MYPROTOSEARCHRESULT mpsr = {sizeof(mpsr)};
if(body.get_string("UserName", nick, 256))
mpsr.psr.nick = nick;
else if(body.get_string("DisplayName", nick, 256))
mpsr.psr.nick = nick;
if(body.get_string("Email", email, 256))
mpsr.psr.email = email;
mpsr.uid = uid;
ProtoBroadcastAck(MODULE, 0, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)req, (LPARAM)&mpsr);
} else
ProtoBroadcastAck(MODULE, 0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)req, 0);
} else
if(cmd == 1 && dsn == 4 && lid == 3) {
Dictionary body = msg.get_dict("body");
char errmsg[256];
if(body.get_string("ErrorMessage", errmsg, 256)) {
PUShowMessage(errmsg, SM_WARNING);
ProtoBroadcastAck(MODULE, 0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)req, 0);
} else {
char email[256], nick[256];
int uid = body.get_int("UserID");
if(uid != 0) {
MYPROTOSEARCHRESULT mpsr = {sizeof(mpsr)};
if(body.get_string("DisplayName", nick, 256))
mpsr.psr.nick = nick;
else if(body.get_string("UserName", nick, 256))
mpsr.psr.nick = nick;
if(body.get_string("Email", email, 256))
mpsr.psr.email = email;
mpsr.uid = uid;
ProtoBroadcastAck(MODULE, 0, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)req, (LPARAM)&mpsr);
} else
ProtoBroadcastAck(MODULE, 0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)req, 0);
}
} else
if(cmd == 2 && dsn == 0 && lid == 9) {
Dictionary body = msg.get_dict("body");
char errmsg[256];
if(body.get_string("ErrorMessage", errmsg, 256)) {
PUShowMessage(errmsg, SM_WARNING);
} else {
char nick[256];
int uid = body.get_int("ContactID");
if(uid != 0) {
if(body.get_string("NickName", nick, 256)) {
DBWriteContactSettingStringUtf(0, MODULE, "Nick", nick);
}
}
}
}
}
}
}
if(server_connection) {
Netlib_CloseHandle(server_connection);
server_connection = 0;
}
delete recv_buffer;
QueueUserAPC(sttMainThreadStatusCallback, mainThread, ID_STATUS_OFFLINE);
server_running = false;
}
void StartThread() {
if(!server_running) {
server_stop = false;
mir_forkthread(ServerThreadFunc, 0);
}
}
void StopThread() {
if(server_running) {
server_stop = true;
if(server_connection) {
Netlib_CloseHandle(server_connection);
server_connection = 0;
}
}
}
void InitServerConnection() {
StartThread();
}
void DeinitServerConnection() {
StopThread();
}
|