Menu

[r88]: / phprpc_2.1 / java / PHPRPCServer.java  Maximize  Restore  History

Download this file

1 lines (1 with data), 16.8 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* PHPRPCServer.java
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 2.1
* LastModified: 2006-08-07
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=204
* Example usage:
*
* rpc.jsp
<%
PHPRPC.PHPRPCServer phprpc_server = new PHPRPC.PHPRPCServer(request, response, session);
phprpc_server.add("min", java.lang.Math.class);
phprpc_server.add(new String[] { "sin", "cos" }, java.lang.Math.class);
phprpc_server.start();
%>
*/
package PHPRPC;
import java.lang.*;
import java.lang.reflect.*;
import java.io.*;
import java.math.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
class RemoteFunction {
protected String name;
protected Object obj;
protected Class cls;
protected Method[] functions;
protected RemoteFunction(String name, Object obj, Class cls, Method[] functions) {
this.name = name;
this.obj = obj;
this.cls = cls;
this.functions = functions;
}
}
public class PHPRPCServer {
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;
private PrintWriter reswriter;
private ArrayList functions;
private RemoteFunction[] rfs;
private boolean debug;
private String charset;
private boolean encode;
private boolean byref;
private boolean encrypt;
private int encryptmode;
private byte[] key;
private String result;
private String arguments;
private String output;
private String callback;
private Integer errno;
private String errstr;
public PHPRPCServer(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
this.request = request;
this.response = response;
this.session = session;
this.functions = new ArrayList();
this.encode = true;
this.encrypt = false;
this.encryptmode = 0;
this.errno = 0;
this.errstr = "";
this.output = "";
this.callback = "";
this.byref = true;
this.key = null;
}
public void add(String function, Object obj) {
add(new String[] { function }, obj, obj.getClass());
}
public void add(String[] functions, Object obj) {
add(functions, obj, obj.getClass());
}
public void add(String function, Class cls) {
add(new String[] { function }, null, cls);
}
public void add(String[] functions, Class cls) {
add(functions, null, cls);
}
private void add(String[] functions, Object obj, Class cls) {
Method[] ms = cls.getMethods();
for (int i = 0, n = functions.length; i < n; i++) {
ArrayList fs = new ArrayList();
for (int j = 0, m = ms.length; j < m; j++) {
if (functions[i].toLowerCase().equals(
ms[j].getName().toLowerCase())
&& Modifier.isPublic(ms[j].getModifiers())) {
fs.add(ms[j]);
}
}
this.functions.add(
new RemoteFunction(functions[i], obj, cls,
(Method[]) fs.toArray(new Method[0])));
}
}
private String toHexString(int n) {
return ((n < 16) ? "0" : "") + Integer.toHexString(n);
}
private String addJsSlashes(String str) {
char[] s = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0, n = s.length; i < n; i++) {
if (s[i] <= 31 || s[i] == 34 || s[i] == 39 || s[i] == 92
|| s[i] == 127) {
sb.append("\\x");
sb.append(toHexString((int) s[i] & 0xff));
} else {
sb.append(s[i]);
}
}
return sb.toString();
}
private String addJsSlashes(byte[] s) {
StringBuffer sb = new StringBuffer();
for (int i = 0, n = s.length; i < n; i++) {
if (s[i] <= 31 || s[i] == 34 || s[i] == 39 || s[i] == 92
|| s[i] == 127) {
sb.append("\\x");
sb.append(toHexString((int) s[i] & 0xff));
} else {
sb.append((char) s[i]);
}
}
return sb.toString();
}
private RemoteFunction[] getFunction(String function) {
ArrayList rf = new ArrayList();
function = function.toLowerCase();
for (int i = 0, n = this.rfs.length; i < n; i++) {
if (function.equals(this.rfs[i].name.toLowerCase())) {
rf.add(this.rfs[i]);
}
}
return (RemoteFunction[]) (rf.toArray(new RemoteFunction[0]));
}
private void appendHeader() throws IOException {
this.response.setContentType("text/plain; charset=" + this.charset);
this.response.addHeader("X-Powered-By", "PHPRPC Server/2.1");
this.response.setDateHeader("Date", (new Date()).getTime());
this.response.setDateHeader("Last-Modified", (new Date()).getTime());
this.response.addHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
this.response.addHeader("Cache-Control",
"pre-check=0, post-check=0, max-age=0");
this.response.addHeader("Content-Encoding", "none");
this.reswriter = response.getWriter();
}
private void writeError() throws UnsupportedEncodingException {
reswriter.print("phprpc_errno=\"" + this.errno.toString() + "\";\r\n");
if (this.encode) {
reswriter.print(
"phprpc_errstr=\""
+ Base64.encode(this.errstr.getBytes(this.charset))
+ "\";\r\n");
} else {
reswriter.print(
"phprpc_errstr=\"" + this.addJsSlashes(this.errstr)
+ "\";\r\n");
}
reswriter.print("phprpc_output=\"" + this.output + "\";\r\n");
reswriter.print(this.callback);
}
private byte[] call(Method function, Object obj, Object[] args) throws UnSerializeException, UnsupportedEncodingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (request.getParameter("phprpc_args") != null) {
Class[] p = function.getParameterTypes();
for (int i = 0, n = Math.min(p.length, args.length); i < n; i++) {
if (args[i] != null) {
args[i] = PHPSerializer.cast(args[i], p[i]);
}
}
}
ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream defaultout = System.out;
PrintStream ps = new PrintStream(bs, true, this.charset);
System.setOut(ps);
byte[] result = PHPSerializer.serialize(function.invoke(obj, args),
this.charset);
System.setOut(defaultout);
ps.close();
this.output = new String(bs.toByteArray(), this.charset);
return result;
}
public void start() throws UnsupportedEncodingException, IOException {
start(false, "UTF-8");
}
public void start(String charset) throws UnsupportedEncodingException, IOException {
start(false, charset);
}
public void start(boolean debug) throws UnsupportedEncodingException, IOException {
start(debug, "UTF-8");
}
public void start(boolean debug, String charset) throws UnsupportedEncodingException, IOException {
this.debug = debug;
this.charset = charset;
this.rfs = (RemoteFunction[]) this.functions.toArray(
new RemoteFunction[0]);
if (this.request.getParameter("phprpc_encode") != null) {
this.encode = !this.request.getParameter("phprpc_encode").toLowerCase().equals(
"false");
}
if (this.request.getParameter("phprpc_callback") != null) {
this.callback = new String(
Base64.decode(this.request.getParameter("phprpc_callback")),
this.charset);
}
if (this.request.getParameter("phprpc_ref") != null) {
this.byref = !this.request.getParameter("phprpc_ref").toLowerCase().equals(
"false");
}
if (this.request.getParameter("phprpc_encrypt") != null) {
String encrypt = this.request.getParameter("phprpc_encrypt").toLowerCase();
if (encrypt.equals("true")) {
this.encrypt = true;
} else if (encrypt.equals("false")) {
this.encrypt = false;
} else if (encrypt.equals("0")) {
this.encryptmode = 0;
} else if (encrypt.equals("1")) {
this.encryptmode = 1;
} else if (encrypt.equals("2")) {
this.encryptmode = 2;
}
}
if (this.session.getAttribute("phprpc_encrypt") != null) {
HashMap kp = (HashMap) (this.session.getAttribute("phprpc_encrypt"));
if (kp.containsKey("k")) {
this.key = new byte[16];
byte[] key = new BigInteger((String) (kp.get("k"))).toByteArray();
for (int i = 1, n = Math.min(key.length, 16); i <= n; i++) {
this.key[16 - i] = key[key.length - i];
}
}
}
try {
this.appendHeader();
if (this.request.getParameter("phprpc_func") != null) {
RemoteFunction[] rf = this.getFunction(
this.request.getParameter("phprpc_func"));
if (rf.length > 0) {
byte[] arguments;
arguments = PHPSerializer.serialize(new Object[] {},
this.charset);
if (this.request.getParameter("phprpc_args") != null) {
arguments = Base64.decode(
this.request.getParameter("phprpc_args"));
if (this.encryptmode > 0) {
if (this.key != null) {
arguments = XXTEA.decrypt(arguments, this.key);
} else {
this.errno = 1;
this.errstr = "Can't find the key for decryption.";
this.writeError();
return;
}
}
}
Object[] args = null;
byte[] result = null;
for (int i = 0, n = rf.length; i < n; i++) {
for (int j = 0, m = rf[i].functions.length; j < m; j++) {
try {
args = (Object[]) PHPSerializer.unserialize(
arguments, (new Object[0]).getClass(),
this.charset);
result = call(rf[i].functions[j], rf[i].obj,
args);
break;
} catch (Exception e) {
if (i == n - 1 && j == m - 1) {
throw e;
}
}
}
}
if (this.byref && args != null) {
arguments = PHPSerializer.serialize(args, this.charset);
}
if (this.encryptmode > 0) {
if (this.key != null) {
if (this.encryptmode > 1) {
result = XXTEA.encrypt(result, this.key);
}
if (this.byref) {
arguments = XXTEA.encrypt(arguments, this.key);
}
} else {
this.errno = 1;
this.errstr = "Can't find the key for encryption.";
this.writeError();
return;
}
}
if (this.encode) {
this.result = Base64.encode(result);
if (this.byref) {
this.arguments = Base64.encode(arguments);
}
} else {
this.result = this.addJsSlashes(result);
if (this.byref) {
this.arguments = this.addJsSlashes(arguments);
}
}
} else {
this.errno = 1;
this.errstr = "Can't find this function "
+ this.request.getParameter("phprpc_func") + "().";
}
if (this.errno == 0) {
this.reswriter.print(
"phprpc_result=\"" + this.result + "\";\r\n");
if (this.byref) {
this.reswriter.print(
"phprpc_args=\"" + this.arguments + "\";\r\n");
}
}
this.writeError();
} else {
byte[] encrypt = null;
if (this.encrypt) {
HashMap kp = KeyPairGen.genRandomKeyPair();
BigInteger p = new BigInteger((String) kp.get("p"));
BigInteger g = new BigInteger((String) kp.get("g"));
BigInteger x = new BigInteger(127, new Random());
BigInteger y = g.modPow(x, p);
kp.put("y", y.toString());
encrypt = PHPSerializer.serialize(kp, this.charset);
kp.put("x", x.toString());
this.session.setAttribute("phprpc_encrypt", kp);
} else if (this.request.getParameter("phprpc_encrypt") != null
&& !this.request.getParameter("phprpc_encrypt").toLowerCase().equals(
"false")) {
HashMap kp = (HashMap) (this.session.getAttribute(
"phprpc_encrypt"));
kp.put("y", this.request.getParameter("phprpc_encrypt"));
BigInteger y = new BigInteger((String) (kp.get("y")));
BigInteger x = new BigInteger((String) (kp.get("x")));
BigInteger p = new BigInteger((String) (kp.get("p")));
BigInteger k = y.modPow(x, p);
kp.put("k", k.toString());
this.session.setAttribute("phprpc_encrypt", kp);
encrypt = PHPSerializer.serialize(true, this.charset);
}
if (encrypt != null) {
if (this.encode) {
this.reswriter.print(
"phprpc_encrypt=\"" + Base64.encode(encrypt)
+ "\";\r\n");
} else {
this.reswriter.print(
"phprpc_encrypt=\"" + this.addJsSlashes(encrypt)
+ "\";\r\n");
}
}
String[] fns = new String[this.rfs.length];
for (int i = 0, n = fns.length; i < n; i++) {
fns[i] = this.rfs[i].name;
}
if (this.encode) {
this.reswriter.print(
"phprpc_functions=\""
+ Base64.encode(
PHPSerializer.serialize(fns,
this.charset))
+ "\";\r\n");
} else {
this.reswriter.print(
"phprpc_functions=\""
+ this.addJsSlashes(
PHPSerializer.serialize(fns,
this.charset))
+ "\";\r\n");
}
this.reswriter.print(this.callback);
}
} catch (Exception e) {
this.errno = 1;
if (this.debug) {
this.errstr = e.toString();
} else {
this.errstr = e.getMessage();
}
this.writeError();
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.