SHOW:
|
|
- or go back to the newest paste.
1 | public static class InstagramPrivateAPI | |
2 | { | |
3 | private class ReferenceString //потому что иначе строки передаются по значению | |
4 | { | |
5 | public string val = ""; | |
6 | } | |
7 | private static string proxyPass = ""; | |
8 | ||
9 | static InstagramPrivateAPI() | |
10 | { | |
11 | Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); | |
12 | - | using (var mydb = new RobogramEntities()) |
12 | + | |
13 | ||
14 | - | proxyPass = mydb.Settings.Single(t => t.Name == "proxy-n-vpn.com").Value; |
14 | + | |
15 | ||
16 | public static CURLcode OnSSLContext(SSLContext ctx, Object extraData) | |
17 | { | |
18 | // To do anything useful with the SSLContext object, you'll need | |
19 | // to call the OpenSSL native methods on your own. So for this | |
20 | // demo, we just return what cURL is expecting. | |
21 | return CURLcode.CURLE_OK; | |
22 | } | |
23 | ||
24 | public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, | |
25 | Object extraData) | |
26 | { | |
27 | //Console.Write(System.Text.Encoding.UTF8.GetString(buf)); | |
28 | ((ReferenceString)extraData).val += System.Text.Encoding.UTF8.GetString(buf); | |
29 | return size * nmemb; | |
30 | } | |
31 | ||
32 | public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb, | |
33 | Object extraData) | |
34 | { | |
35 | FileStream fs = (FileStream)extraData; | |
36 | return fs.Read(buf, 0, size * nmemb); | |
37 | } | |
38 | ||
39 | public static string SendRequest(string url, bool post, string post_data, string user_agent, bool cookies, string userid) | |
40 | { | |
41 | Easy.ReadFunction rf; | |
42 | FileStream fs; | |
43 | ReferenceString instagram_response = new ReferenceString(); | |
44 | int d = 0; | |
45 | bool success = true; | |
46 | //Наша задача - попробовать сначала выполнить действие без авторизации, а если не получилось - | |
47 | //то его же, но уже после авторизации | |
48 | do | |
49 | { | |
50 | Easy easy = new Easy(); | |
51 | Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); | |
52 | easy.SetOpt(CURLoption.CURLOPT_URL, "https://i.instagram.com/api/v1/" + url); | |
53 | easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); | |
54 | easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, instagram_response); | |
55 | easy.SetOpt(CURLoption.CURLOPT_USERAGENT, user_agent); | |
56 | easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true); | |
57 | ||
58 | Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext); | |
59 | easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf); | |
60 | easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); | |
61 | easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false); | |
62 | ||
63 | if (post) | |
64 | { | |
65 | easy.SetOpt(CURLoption.CURLOPT_POST, true); | |
66 | easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, post_data); | |
67 | } | |
68 | ||
69 | - | var prms = GetProxyName(userid).Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); |
69 | + | //Для каждого аккаунта Instagram храним куки в отдельно файле |
70 | - | easy.SetOpt(CURLoption.CURLOPT_PROXY, prms[0]); |
70 | + | |
71 | - | easy.SetOpt(CURLoption.CURLOPT_PROXYPORT, prms[1]); |
71 | + | |
72 | - | easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP); |
72 | + | |
73 | - | easy.SetOpt(CURLoption.CURLOPT_PROXYUSERPWD, proxyPass); |
73 | + | |
74 | else | |
75 | { | |
76 | easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, string.Format("cookies{0}.txt", userid)); | |
77 | } | |
78 | ||
79 | ||
80 | SeasideResearch.LibCurlNet.CURLcode response = easy.Perform(); | |
81 | easy.GetInfo(CURLINFO.CURLINFO_HTTP_CONNECTCODE, ref d); | |
82 | easy.Cleanup(); | |
83 | easy = null; | |
84 | // Curl.GlobalCleanup(); обойдемся без этого | |
85 | } while (!success); | |
86 | ||
87 | return instagram_response.val; | |
88 | } | |
89 | ||
90 | public static string GenerateGuid() | |
91 | { | |
92 | var rand = new System.Random(); | |
93 | return string.Format("{0}{1}-{2}-{3}-{4}-{5}{6}{7}", | |
94 | rand.Next(0, 65535).ToString("x4"), | |
95 | rand.Next(0, 65535).ToString("x4"), | |
96 | rand.Next(0, 65535).ToString("x4"), | |
97 | rand.Next(16384, 20479).ToString("x4"), | |
98 | rand.Next(32768, 49151).ToString("x4"), | |
99 | rand.Next(0, 65535).ToString("x4"), | |
100 | rand.Next(0, 65535).ToString("x4"), | |
101 | rand.Next(0, 65535).ToString("x4")); | |
102 | } | |
103 | ||
104 | public static string GenerateUserAgent() | |
105 | { | |
106 | var resolutions = new string[] { "720x1280", "320x480", "480x800", "1024x768", "1280x720", "768x1024", "480x320" }; | |
107 | var versions = new string[] { "GT-N7000", "SM-N9000", "GT-I9220", "GT-I9100" }; | |
108 | var dpis = new string[] { "120", "160", "320", "240" }; | |
109 | ||
110 | var ver = versions[new System.Random().Next(0, versions.Count() - 1)]; | |
111 | var dpi = dpis[new System.Random().Next(0, dpis.Count() - 1)]; | |
112 | var res = resolutions[new System.Random().Next(0, resolutions.Count() - 1)]; | |
113 | ||
114 | return "Instagram 6.24.0" + " Android (" + | |
115 | new System.Random().Next(10, 11).ToString() + "/" + | |
116 | new System.Random().Next(1, 3).ToString() + "." + | |
117 | new System.Random().Next(3, 5).ToString() + "." + new System.Random().Next(0, 5).ToString() + | |
118 | "; " + dpi + "; " + res + "; samsung; " + ver + "; " + ver + "; smdkc210; en_US)"; | |
119 | } | |
120 | public static string GenerateSignature(string data) | |
121 | { | |
122 | string secret = "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5"; | |
123 | var secretBytes = Encoding.UTF8.GetBytes(secret); | |
124 | var dataBytes = Encoding.UTF8.GetBytes(data); | |
125 | string signature = ""; | |
126 | ||
127 | using (var hmac = new bouncy.System.Security.Cryptography.HMACSHA256(secretBytes)) | |
128 | { | |
129 | var hash = hmac.ComputeHash(dataBytes); | |
130 | for (int i = 0; i < hash.Length; i++) | |
131 | { | |
132 | signature += hash[i].ToString("x2"); | |
133 | } | |
134 | // signature = Convert.ToBase64String(hash); | |
135 | } | |
136 | return signature; | |
137 | } | |
138 | public static string datetimeToUnixEpoch(DateTime datetime) | |
139 | { | |
140 | DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
141 | return ((int)(datetime - sTime).TotalSeconds).ToString(); | |
142 | } | |
143 | ||
144 | public class InstaResponse | |
145 | { | |
146 | public string status; | |
147 | public string media_id; | |
148 | } | |
149 | ||
150 | ||
151 | public static bool login(string userid, string username, string password) | |
152 | { | |
153 | // Define the GuID | |
154 | string guid = GenerateGuid(); | |
155 | - | public static string GetPostData(string filename) |
155 | + | |
156 | // Define the user agent | |
157 | - | if (!System.IO.File.Exists(filename)) |
157 | + | |
158 | ||
159 | - | Console.WriteLine(string.Format("The image {0} doesn't exist ", filename)); |
159 | + | |
160 | - | return null; |
160 | + | |
161 | ||
162 | /* LOG IN */ | |
163 | // You must be logged in to the account that you wish to post a photo too | |
164 | - | string post_data = "device_timestamp=" + datetimeToUnixEpoch(DateTime.Now) + |
164 | + | |
165 | - | "&" + "photo="; |
165 | + | |
166 | - | foreach (var byt in System.IO.File.ReadAllBytes(filename)) |
166 | + | |
167 | "x-www-form-urlencoded; charset=UTF-8\"}"; | |
168 | - | post_data += byt.ToString("x2"); |
168 | + | |
169 | InstaResponse obj = null; | |
170 | - | return post_data; |
170 | + | |
171 | string login = SendRequest("accounts/login/", true, data, agent, false, userid)[0]; | |
172 | ||
173 | ||
174 | if (login.StartsWith("Sorry, an error occurred while processing this request.")) | |
175 | { | |
176 | return false; | |
177 | } | |
178 | else | |
179 | { | |
180 | if (string.IsNullOrEmpty(login)) | |
181 | { | |
182 | return false; | |
183 | } | |
184 | else | |
185 | { | |
186 | // Decode the array that is returned | |
187 | try | |
188 | { | |
189 | obj = Newtonsoft.Json.JsonConvert.DeserializeObject<InstaResponse>(login); | |
190 | var status = obj.status; | |
191 | if (status == "ok") return true; | |
192 | } | |
193 | catch | |
194 | { | |
195 | } | |
196 | } | |
197 | } | |
198 | return false; | |
199 | } | |
200 | } |