-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cs
352 lines (337 loc) · 14.8 KB
/
Functions.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FacebookMessageSender
{
public class AuthFaceBook
{
private bool islogin;
public bool IsLogin
{
get { return islogin; }
set { islogin = value; }
}
private string username;
public string UserName
{
get { return username; }
set { username = value; }
}
private string pass;
public string Password
{
get { return pass; }
set { pass = value; }
}
private CookieContainer cookies;
public CookieContainer Cookies
{
get { return cookies; }
set { cookies = value; }
}
private string useragent = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+";//"Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0";
public AuthFaceBook(string username, string password)
{
if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "usersFile.dat")))
{
File.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "usersFile.dat"));
}
string[] usersFile = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "usersFile.dat"));
if (usersFile.Any(s=>s.Contains(username)))
{
string cookefile = usersFile.ToList().Where(s => s.Contains(username)).First().Split('|')[1];
if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile)))
{
var Cookie = ReadCookiesFromDisk(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile));
if (Cookie != null)
{
this.username = username;
this.pass = password;
this.cookies = Cookie;
this.islogin = true;
}
else
{
IsLogin = false;
File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile));
}
}
else
{
AuthFaceBookNow(username, password);
if (IsLogin)
{
FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"usersFile.dat"), FileMode.OpenOrCreate, FileAccess.Write);
fs.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(fs);
bool res = WriteCookiesToDisk(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile), this.Cookies);
if (res)
{
sw.WriteLine(username + "|" + Guid.NewGuid().ToString() + ".dat");
sw.Flush();
sw.Close();
fs.Close();
}
else
{
IsLogin = false;
File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile));
}
}
}
}
else
{
AuthFaceBookNow(username, password);
if (IsLogin)
{
FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"usersFile.dat"), FileMode.OpenOrCreate, FileAccess.Write);
fs.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(fs);
string cookefile = Guid.NewGuid().ToString() + ".dat";
bool res = WriteCookiesToDisk(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile), this.Cookies);
if (res)
{
sw.WriteLine(username + "|" + Guid.NewGuid().ToString() + ".dat");
sw.Flush();
sw.Close();
fs.Close();
}
else
{
IsLogin = false;
File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cookefile));
}
}
}
}
public AuthFaceBook()
{
// TODO: Complete member initialization
}
public void AuthFaceBookNow(string username, string password)
{
UserName = username;
Password = password;
cookies = new CookieContainer();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/");
request.CookieContainer = new CookieContainer();
request.CookieContainer = cookies;
request.UserAgent = useragent;
request.KeepAlive = false;
request.Timeout = 45000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies.Add(response.Cookies);
StreamReader sr = new StreamReader(response.GetResponseStream());
string html = sr.ReadToEnd();
html = html.Substring(html.IndexOf("login_form"));
html = html.Remove(html.IndexOf("/form"));
Regex reg = new Regex(@"name=""[^""]+"" value=""[^""]+""");
MatchCollection mc = reg.Matches(html);
List<string> values = new List<string>();
for (int k = 0; k < mc.Count; k++)
{
if (k != 0)
postData += "&";
if (k == 1)
{
postData += "email=" + this.username + "&pass=" + this.pass + "&";
}
string m = mc[k].Value.Replace("\"", "");
m = m.Replace("name=", "");
m = m.Replace(" value=", "=");
postData += m;
}
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer = cookies; //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = useragent;
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = false;
getRequest.ContentType = "application/x-www-form-urlencoded";
getRequest.Referer = "https://www.facebook.com";
getRequest.KeepAlive = false;
getRequest.Timeout = 45000;
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
cookies.Add(getResponse.Cookies);
if (getResponse.Cookies.Count > 6)
islogin = true;
else
islogin = false;
}
catch { }
}
public string postData { get; set; }
public void ReloadAuth()
{
AuthFaceBookNow(username, pass);
}
public string UserAgent
{
get { return useragent; }
set { useragent = value; }
}
public CookieContainer ReadCookiesFromDisk(string file)
{
try
{
using (Stream stream = File.Open(file, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
return (CookieContainer)formatter.Deserialize(stream);
}
}
catch (Exception)
{
return null;
}
}
public bool WriteCookiesToDisk(string file, CookieContainer cookieJar)
{
using (Stream stream = File.Create(file))
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, cookieJar);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
public class FacebookHttpRequest
{
private string output_message;
public string OutputMessage
{
get { return output_message; }
set { output_message = value; }
}
public string SendMessage(AuthFaceBook client, string TextMessage, OptionMessage option)
{
if (client.IsLogin)
{
string Data = string.Format("fb_dtsg={0}&body={1}&send=Send&wwwupp=V3&ids%5B{2}%5D={3}&referrer=&ctype=&cver=legacy", option.DTSG, Uri.EscapeDataString(TextMessage), option.UserId, option.UserId);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://m.facebook.com/messages/send/?icm=1");
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer = client.Cookies; //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = client.UserAgent;
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = false;
getRequest.ContentType = "application/x-www-form-urlencoded";
getRequest.Referer = "https://m.facebook.com";
getRequest.KeepAlive = false;
getRequest.Timeout = 45000;
byte[] byteArray1 = Encoding.ASCII.GetBytes(Data);
getRequest.ContentLength = byteArray1.Length;
Stream newStream2 = getRequest.GetRequestStream(); //open connection
newStream2.Write(byteArray1, 0, byteArray1.Length); // Send the data.
newStream2.Close();
output_message = "Message Sent";
return output_message;
}
else
{
output_message = "Facebook Auth Error";
return output_message;
}
}
}
public class OptionMessage
{
private string tids;
public string error = "";
public string TIDS
{
get { return tids; }
set { tids = value; }
}
private string dtsg;
public string DTSG
{
get { return dtsg; }
set { dtsg = value; }
}
private string userId;
public string UserId
{
get { return userId; }
set { userId = value; }
}
private string linkReq = "";
public OptionMessage(string uid, AuthFaceBook client)
{
try
{
userId = uid;
linkReq = "https://m.facebook.com/messages/thread/" + userId;
HttpWebRequest Arequest = (HttpWebRequest)WebRequest.Create(linkReq);
Arequest.CookieContainer = new CookieContainer();
Arequest.CookieContainer = client.Cookies;
Arequest.UserAgent = client.UserAgent;
Arequest.KeepAlive = false;
Arequest.Timeout = 45000;
HttpWebResponse Aresponse = (HttpWebResponse)Arequest.GetResponse();
StreamReader Asr = new StreamReader(Aresponse.GetResponseStream());
error += "Step 1 ";
string Ahtml = Asr.ReadToEnd();
var splitted_fb_dtsg = Ahtml.Split(new string[] { "type=\"hidden\" name=\"fb_dtsg" }, StringSplitOptions.RemoveEmptyEntries);
string fb_dtsg_container = splitted_fb_dtsg[1];
var splitted_tids = Ahtml.Split(new string[] { "tids" }, StringSplitOptions.RemoveEmptyEntries);
string tids_container = "";
if (splitted_tids.Length > 2)
{
tids_container = splitted_tids[1];
}
else
{
tids_container = splitted_tids[0];
}
error += "Step 2 ";
var splitted1_fb_dtsg = fb_dtsg_container.Split('"');
string fb_dtsg_rubsh = splitted1_fb_dtsg[2];
for (int u = 0; u < fb_dtsg_rubsh.Length; u++)
{
if (fb_dtsg_rubsh[u] != '"')
dtsg += fb_dtsg_rubsh[u];
}
error += "Step 3 ";
var splitted1_tids = tids_container.Split(new string[] { "\"" }, StringSplitOptions.RemoveEmptyEntries);
string tids_rubsh = splitted1_tids[1];
for (int u = 0; u < tids_rubsh.Length; u++)
{
if (tids_rubsh[u] != '"')
tids += tids_rubsh[u];
}
}
catch (Exception ex)
{
error = ex.Message;
}
}
}
}