-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHelpers.java
More file actions
284 lines (262 loc) · 7.19 KB
/
Helpers.java
File metadata and controls
284 lines (262 loc) · 7.19 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
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
package net.dmcloud.cloudkey;
import java.io.*;
import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.zip.Deflater;
import org.apache.commons.codec.binary.Base64;
public class Helpers
{
static public String curl(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try
{
// Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes("UTF-8").length));
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(urlParameters.getBytes("UTF-8"));
wr.flush();
wr.close();
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null)
{
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
}
static public String normalize(Map data)
{
String normalize = "";
data = new TreeMap(data);
Iterator iter = data.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
normalize += entry.getKey();
normalize += normalizing(entry.getValue());
}
return normalize;
}
static public String normalize(ArrayList data)
{
String normalize = "";
for (int i=0; i<data.size(); i++)
{
normalize += normalizing(data.get(i));
}
return normalize;
}
static private String normalizing(Object o)
{
String normalize = "";
if (o instanceof Map)
{
normalize = normalize((Map)o);
}
else if (o instanceof ArrayList)
{
normalize = normalize((ArrayList)o);
}
else
{
normalize = o.toString();
}
return normalize;
}
static public String sign_url(String url, String secret) throws DCException
{
return sign_url(url, secret, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0);
}
static public String sign_url(String url, String secret, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
{
expires = (expires == 0) ? (int)((new Date().getTime() / 1000) + 7200) : expires;
// Compute digest
String[] tokens = url.split("\\?");
String query = "";
if (tokens.length > 1)
{
url = tokens[0];
query = tokens[1];
}
String secparams = "";
ArrayList<String> public_secparams = new ArrayList<String>();
if ((seclevel & CloudKey.SECLEVEL_DELEGATE) == 0)
{
if ((seclevel & CloudKey.SECLEVEL_ASNUM) > 0)
{
if (asnum.equals(""))
{
throw new DCException("IP security level required and no IP address provided.");
}
secparams += asnum;
}
if ((seclevel & CloudKey.SECLEVEL_IP) > 0)
{
if (asnum.equals(""))
{
throw new DCException("IP security level required and no IP address provided.");
}
secparams += ip;
}
if ((seclevel & CloudKey.SECLEVEL_USERAGENT) > 0)
{
if (asnum.equals(""))
{
throw new DCException("USERAGENT security level required and no user-agent provided.");
}
secparams += useragent;
}
if ((seclevel & CloudKey.SECLEVEL_COUNTRY) > 0)
{
if (countries == null || countries.length == 0)
{
throw new DCException("COUNTRY security level required and no country list provided.");
}
public_secparams.add("cc=" + implode(",", countries).toLowerCase());
}
if ((seclevel & CloudKey.SECLEVEL_REFERER) > 0 || (seclevel & CloudKey.SECLEVEL_REFERER_STRICT) > 0)
{
if (referers == null || referers.length == 0)
{
throw new DCException("REFERER security level required and no referer list provided.");
}
try
{
StringBuffer ref = new StringBuffer();
for (int i=0; i<referers.length; i++)
{
if (i > 0)
ref.append(" ");
ref.append(referers[i].replace(" ", "%20"));
}
public_secparams.add("rf=" + URLEncoder.encode(ref.toString(), "UTF-8"));
}
catch (Exception e)
{
throw new DCException(e.getMessage());
}
}
}
String public_secparams_encoded = "";
if (public_secparams.size() > 0)
{
try
{
public_secparams_encoded = Base64.encodeBase64URLSafeString(gzcompress(implode("&", public_secparams)));
}
catch (Exception e)
{
throw new DCException(e.getMessage());
}
}
String rand = "";
String letters = "abcdefghijklmnopqrstuvwxyz0123456789";
for (int i=0; i<8; i++)
{
int index = (int)Math.round(Math.random() * 35);
rand += letters.substring(index, index + 1);
}
String digest = md5(seclevel + url + expires + rand + secret + secparams + public_secparams_encoded);
return url + "?" + (!query.equals("") ? query + '&' : "") + "auth=" + expires + "-" + seclevel + "-" + rand + "-" + digest + (public_secparams_encoded != "" ? "-" + public_secparams_encoded : "");
}
private static String implode(String delim, String[] args)
{
StringBuffer sb = new StringBuffer();
for (int i=0; i<args.length; i++)
{
if (i > 0)
{
sb.append(delim);
}
sb.append(args[i]);
}
return sb.toString();
}
private static String implode(String delim, ArrayList<String> args)
{
StringBuffer sb = new StringBuffer();
for (int i=0; i<args.size(); i++)
{
if (i > 0)
{
sb.append(delim);
}
sb.append(args.get(i));
}
return sb.toString();
}
public static String md5(String password)
{
byte[] uniqueKey = password.getBytes();
byte[] hash = null;
try
{
hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
}
catch (NoSuchAlgorithmException e)
{
throw new Error("No MD5 support in this VM.");
}
StringBuilder hashString = new StringBuilder();
for (int i=0; i<hash.length; i++)
{
String hex = Integer.toHexString(hash[i]);
if (hex.length() == 1)
{
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
}
else
{
hashString.append(hex.substring(hex.length() - 2));
}
}
return hashString.toString();
}
private static byte[] gzcompress(String inputString) throws Exception
{
byte[] input = inputString.getBytes("UTF-8");
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
while (!compresser.finished())
{
int count = compresser.deflate(buf);
bos.write(buf, 0, count);
}
bos.close();
return bos.toByteArray();
}
}