-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
226 lines (204 loc) · 8.61 KB
/
Utils.java
File metadata and controls
226 lines (204 loc) · 8.61 KB
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
package com.etiaro.facebook;
import android.text.TextUtils;
import android.util.Log;
import android.util.MalformedJsonException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by jakub on 15.03.18.
*/
public class Utils {
public static String encrypt(String from, String key){
String generatedString = "";
try {
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(from.getBytes());
generatedString = new String(encrypted);
} catch (Exception e) {
Log.e("Encrypt", e.toString());
}
return generatedString;
}
public static String decrypt(String from, String key){
String generatedString = "";
try {
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] encrypted = from.getBytes();
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
generatedString = new String(cipher.doFinal(encrypted));
} catch (Exception e) {
Log.e("Encrypt", e.toString());
}
return generatedString;
}
public static String generateThreadingID(String clientID){
return "<"+Calendar.getInstance().getTimeInMillis()+":"+Math.abs(new Random().nextLong())%4294967295l+"-"+clientID+"@mail.projektitan.com>";
}
public static String generateOfflineThreadingID(){
float ret = Calendar.getInstance().getTimeInMillis();
float value = (float)Math.floor(new Random().nextFloat()*4294967295f);
String str = ("0000000000000000000000" + Integer.toBinaryString(Float.floatToIntBits(value)));
str = str.substring(str.length()-23);
String msgs = Integer.toBinaryString(Float.floatToIntBits(ret)) + str;
return String.valueOf(Long.parseLong(msgs, 2));
}
public static String readBuffer(InputStream is) throws IOException {
String ret = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
ret += out;
reader.close();
return ret;
}
public static String cutString(String str, String start, String end){
if (str.split(start).length <= 1)
return "null";
str = str.split(start)[1];
str = str.split(end)[0];
return str;
}
public static String generatePresence(String ID) {
long time = Calendar.getInstance().getTimeInMillis();
try {
return "E" + URLEncoder.encode("{\"v\": 3, \"time\":" + Math.round(time / 1000) +
",\"user\"" + ID + ",\"state\":{" +
"\"ut\":0" + ",\"t2\":[]" + ",\"lm2\":null" + ",\"uct2\":" + time + ",\"tr\":null" +
",\"tw\":" + ((int) (new Random().nextFloat() * 4294967295f) + 1) +
",\"at\":" + time +
"},\"ch\":{" +
"[\"p_\"" + ID + "]: 0" +
"}" + "}", "UTF-8").toLowerCase().replace("+", "%20");
} catch (UnsupportedEncodingException e) {
return "null";
}
}
public static String generateAccessibilityCookie() {
long time = Calendar.getInstance().getTimeInMillis();
try {
return URLEncoder.encode(
"{'sr': 0,'sr-ts':"+ time+
",'jk': 0,'jk-ts':"+ time+
",'kb': 0,'kb-ts':"+ time+
",'hcm': 0"+
",'hcm-ts':"+ time+"}","UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
return "null";
}
}
public static String formatGetData(HashMap<String, String>values){
String s = "";
for(Map.Entry<String, String> e : values.entrySet())
s+= "&"+e.getKey()+"="+e.getValue();
return s;
}
public static String checkAndFormatResponse(String response){
if(response.indexOf("{") >0)
response = response.substring(response.indexOf("{"));
try{
String er = new JSONObject(response).getString("error");
if(er.equals("1357001"))
return "NotLoggedIn";
if(er != null)
return null;
}catch (Exception e){
return response;
}
return response;
}
//NEVER call from main thread!
public static class SiteLoader {
private String data;
private CookieManager cookiesManager = new CookieManager();
private HttpURLConnection connection;
public SiteLoader(String URL) throws IOException {
java.net.URL url = new URL(URL);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
}
public void followRedirects(boolean b){
connection.setInstanceFollowRedirects( b );
}
public void addCookies(CookieManager cookieManager){
cookiesManager = cookieManager;
if (cookieManager.getCookieStore().getCookies().size() > 0) {
connection.setRequestProperty("Cookie",
TextUtils.join(";", cookieManager.getCookieStore().getCookies()));
}
}
public void post(String params) throws IOException {
connection.setDoOutput(true);
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty( "charset", "utf-8");
connection.setRequestProperty( "Referer", "https://www.facebook.com/");
connection.setRequestProperty( "Origin", "https://www.facebook.com");
connection.setRequestProperty( "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18");
connection.setRequestProperty( "Connection", "keep-alive");
connection.setUseCaches( false );
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
byte[] postData = params.getBytes( StandardCharsets.UTF_8 );
out.write(postData);
out.flush();
out.close();
}
public void load() throws IOException {
int status = getResponseCode();
InputStream in;
if (status == HttpURLConnection.HTTP_OK) {
in = new BufferedInputStream(connection.getInputStream());
data = readBuffer(in); //connected loads site content
} else {
//in = new BufferedInputStream(connection.getErrorStream());
data = "";//readBuffer(in); //connected loads site content
//in.close();
}
connection.disconnect();
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
cookiesManager.getCookieStore().add(URI.create(connection.getURL().toString()), HttpCookie.parse(cookie).get(0));
}
}
}
public int getResponseCode() throws IOException { return connection.getResponseCode(); }
public String getData(){ return data; }
public CookieManager getCookiesManager(){ return cookiesManager; }
public String getHeaderField(String name){ return connection.getHeaderField(name); }
}
}