-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.cs
251 lines (247 loc) · 11.8 KB
/
Web.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace main
{
public class Web
{
public class WebResult<T>
{
T _result = default;
Exception _exception = null;
HttpWebRequest _request = null;
HttpWebResponse _response = null;
public void SetError(Exception exception) => _exception = exception;
public Exception GetError() => _exception;
public HttpWebRequest GetRequest() => _request;
public HttpWebResponse GetResponse() => _response;
public HttpStatusCode GetStatusCode() => _response != null ? _response.StatusCode : new HttpStatusCode();
public string GetDescription() => _response != null ? _response.StatusDescription : "";
public WebResult<T> SetResult(T result, HttpWebRequest request = null, HttpWebResponse response = null)
{
_result = result;
_request = request;
_response = response;
return this;
}
public T GetResult()
{
if (_result == null)
if (_exception != null) throw _exception;
else return default;
return _result;
}
public bool IsSuccess { get => _result != null && _exception == null; }
public WebResult(Exception exception) => SetError(exception);
public WebResult(T result, HttpWebRequest request = null, HttpWebResponse response = null) =>
SetResult(result, request, response);
}
public static CookieContainer Cookies = new CookieContainer();
public static HttpWebRequest GenXRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("x-requested-with", "XMLHttpRequest");
request.Headers.Add("x-instagram-ajax", InstagramApi.Config.rollout_hash);
request.Headers.Add("x-csrftoken", InstagramApi.Config.Csrf_token);
return request;
}
public static class Navigate
{
static Navigate()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
}
public static class Limited
{
static bool isLocked = false;
const double targ_limited_time = 0;
const double get_limited_time = 0.625; // 625 ms
public static async Task<string> Get(HttpWebRequest request,
string ContentType = "application/x-www-form-urlencoded")
{
var rand_wait = new Random().Next(66) + 1;
while (isLocked)
Thread.Sleep(rand_wait);
isLocked = true;
var z = Dev.GetUnixTimestamp();
var y = z - targ_limited_time;
if (y < get_limited_time)
Thread.Sleep((int)Math.Round((get_limited_time - y) * 1000));
var result = await (new StreamReader((await Task.Run(async () =>
{
try
{
request.Method = "GET";
request.CookieContainer = Cookies;
if (ContentType != null) request.ContentType = ContentType;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
current_action_web_list.Add(new Act(request.RequestUri.ToString()));
}
catch (WebException ex)
{
await Log.Write(new List<string>() { string.Format("[WebException]: {0}", ex.Message) }, Log.Type.error);
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.error));
}
catch (Exception ex)
{
await Log.Write(new List<string>() { string.Format("[Exception]: {0}", ex.Message) }, Log.Type.error);
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.error));
throw ex;
}
return response;
}
catch (Exception ex)
{
throw ex;
}
})).GetResponseStream())).ReadToEndAsync();
isLocked = false;
return result;
}
}
public static async Task<WebResult<string>> Post(HttpWebRequest request, string Data)
{
Data = Uri.EscapeUriString(Data);
request.Method = "POST";
request.CookieContainer = Cookies;
if (Data.Length > 0)
request.ContentType = "application/x-www-form-urlencoded";
byte[] byteArr = Encoding.UTF8.GetBytes(Data);
request.ContentLength = byteArr.Length;
using (Stream s = request.GetRequestStream())
s.Write(byteArr, 0, byteArr.Length);
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
using (var reader = new StreamReader(response.GetResponseStream()))
{
current_action_web_list.Add(new Act(request.RequestUri.ToString(), Data, Act.Type.post));
return new WebResult<string>(reader.ReadToEnd());
}
}
throw new Exception("No result");
}
catch (WebException ex)
{
current_action_web_list.Add(new Act(request.RequestUri.ToString(), Data, Act.Type.error));
if (ex.Response != null)
{
using (var response = (HttpWebResponse)ex.Response)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return new WebResult<string>(reader.ReadToEnd(), request, response);
}
}
}
else
{
await Log.Write(new List<string>() { string.Format("[WebException]: {0}", ex.Message) }, Log.Type.error);
throw ex;
}
}
catch (Exception ex)
{
await Log.Write(new List<string>() { string.Format("[Exception]: {0}", ex.Message) }, Log.Type.error);
current_action_web_list.Add(new Act(request.RequestUri.ToString(), Data, Act.Type.error));
throw ex;
}
}
public static async Task<string> GetRedirectLocation(string url,
string Method = "GET",
string ContentType = "application/x-www-form-urlencoded") => await Task.Run(async () =>
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.Method = Method;
request.CookieContainer = Cookies;
if (ContentType != null) request.ContentType = ContentType;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.get));
if (response.StatusCode == HttpStatusCode.Redirect)
return response.Headers["location"];
}
catch (Exception ex)
{
await Log.Write(new List<string>() { string.Format("[Exception]: {0}", ex.Message) }, Log.Type.error);
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.error));
throw ex;
}
return null;
});
public static async Task<WebResult<string>> Get(HttpWebRequest request)
{
request.Method = "GET";
request.CookieContainer = Cookies;
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
using (var reader = new StreamReader(response.GetResponseStream()))
{
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.get));
return new WebResult<string>(reader.ReadToEnd());
}
}
throw new Exception("No result");
}
catch (WebException ex)
{
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.error));
if (ex.Response != null)
{
using (var response = (HttpWebResponse)ex.Response)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return new WebResult<string>(ex).SetResult(reader.ReadToEnd(), request, response);
}
}
}
else
{
await Log.Write(new List<string>() { string.Format("[WebException]: {0}", ex.Message) }, Log.Type.error);
throw ex;
}
}
catch (Exception ex)
{
await Log.Write(new List<string>() { string.Format("[Exception]: {0}", ex.Message) }, Log.Type.error);
current_action_web_list.Add(new Act(request.RequestUri.ToString(), "", Act.Type.error));
throw ex;
}
}
}
public class Act
{
public enum Type { unknown = 0, get = 1, post = 2, error = 3 };
public Type type;
public string url;
public string data;
public double timestamp = Dev.GetUnixTimestamp();
public Act(string url, string data = "", Type type = Type.get)
{
this.url = url;
this.data = data;
this.type = type;
}
public override string ToString() => string.Format("[{0}] has been ?", type.ToString().ToUpper());
}
public static List<Act> current_action_web_list = new List<Act>();
}
}